Network status on articles loading.
This commit is contained in:
parent
ab2d0c4036
commit
0bb2195bff
@ -3,6 +3,7 @@
|
||||
package="apps.amine.bou.readerforselfoss"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
|
@ -47,6 +47,7 @@ import apps.amine.bou.readerforselfoss.utils.drawer.CustomUrlPrimaryDrawerItem
|
||||
import apps.amine.bou.readerforselfoss.utils.flattenTags
|
||||
import apps.amine.bou.readerforselfoss.utils.longHash
|
||||
import apps.amine.bou.readerforselfoss.utils.maybeHandleSilentException
|
||||
import apps.amine.bou.readerforselfoss.utils.network.isNetworkAccessible
|
||||
import apps.amine.bou.readerforselfoss.utils.persistence.toEntity
|
||||
import apps.amine.bou.readerforselfoss.utils.persistence.toView
|
||||
import co.zsmb.materialdrawerkt.builders.accountHeader
|
||||
@ -333,7 +334,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
|
||||
getElementsAccordingToTab()
|
||||
|
||||
|
||||
handleGDPRDialog(sharedPref.getBoolean("GDPR_shown", false))
|
||||
}
|
||||
|
||||
@ -817,11 +817,51 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
override fun onTabSelected(position: Int) {
|
||||
offset = 0
|
||||
lastFetchDone = false
|
||||
when (position) {
|
||||
0 -> getUnRead()
|
||||
1 -> getRead()
|
||||
2 -> getStarred()
|
||||
else -> Unit
|
||||
|
||||
if (itemsCaching) {
|
||||
|
||||
if (!swipeRefreshLayout.isRefreshing) {
|
||||
swipeRefreshLayout.post { swipeRefreshLayout.isRefreshing = true }
|
||||
}
|
||||
|
||||
thread {
|
||||
val dbItems = db.itemsDao().items().map { it.toView() }
|
||||
runOnUiThread {
|
||||
if (dbItems.isNotEmpty()) {
|
||||
items = when (position) {
|
||||
0 -> ArrayList(dbItems.filter { it.unread })
|
||||
1 -> ArrayList(dbItems.filter { !it.unread })
|
||||
2 -> ArrayList(dbItems.filter { it.starred })
|
||||
else -> ArrayList(dbItems.filter { it.unread })
|
||||
}
|
||||
handleListResult()
|
||||
when (position) {
|
||||
0 -> getUnRead()
|
||||
1 -> getRead()
|
||||
2 -> getStarred()
|
||||
else -> Unit
|
||||
}
|
||||
} else {
|
||||
if (this@HomeActivity.isNetworkAccessible(this@HomeActivity.findViewById(R.id.coordLayout))) {
|
||||
when (position) {
|
||||
0 -> getUnRead()
|
||||
1 -> getRead()
|
||||
2 -> getStarred()
|
||||
else -> Unit
|
||||
}
|
||||
getAndStoreAllItems()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
when (position) {
|
||||
0 -> getUnRead()
|
||||
1 -> getRead()
|
||||
2 -> getStarred()
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -899,8 +939,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
handleListResult()
|
||||
doGetAccordingToTab()
|
||||
} else {
|
||||
doGetAccordingToTab()
|
||||
getAndStoreAllItems()
|
||||
if (this@HomeActivity.isNetworkAccessible(this@HomeActivity.findViewById(R.id.coordLayout))) {
|
||||
doGetAccordingToTab()
|
||||
getAndStoreAllItems()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -956,24 +998,28 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
swipeRefreshLayout.post { swipeRefreshLayout.isRefreshing = true }
|
||||
}
|
||||
|
||||
call(maybeTagFilter?.tag, maybeSourceFilter?.id?.toLong(), maybeSearchFilter)
|
||||
.enqueue(object : Callback<List<Item>> {
|
||||
override fun onResponse(
|
||||
call: Call<List<Item>>,
|
||||
response: Response<List<Item>>
|
||||
) {
|
||||
handleItemsResponse(response)
|
||||
}
|
||||
if (this@HomeActivity.isNetworkAccessible(this@HomeActivity.findViewById(R.id.coordLayout))) {
|
||||
call(maybeTagFilter?.tag, maybeSourceFilter?.id?.toLong(), maybeSearchFilter)
|
||||
.enqueue(object : Callback<List<Item>> {
|
||||
override fun onResponse(
|
||||
call: Call<List<Item>>,
|
||||
response: Response<List<Item>>
|
||||
) {
|
||||
handleItemsResponse(response)
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
||||
swipeRefreshLayout.isRefreshing = false
|
||||
Toast.makeText(
|
||||
this@HomeActivity,
|
||||
toastMessage,
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
})
|
||||
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
||||
swipeRefreshLayout.isRefreshing = false
|
||||
Toast.makeText(
|
||||
this@HomeActivity,
|
||||
toastMessage,
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
})
|
||||
} else {
|
||||
swipeRefreshLayout.post { swipeRefreshLayout.isRefreshing = false }
|
||||
}
|
||||
}
|
||||
|
||||
private fun getUnRead(appendResults: Boolean = false) {
|
||||
|
@ -0,0 +1,39 @@
|
||||
package apps.amine.bou.readerforselfoss.utils.network
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Color
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkInfo
|
||||
import android.view.View
|
||||
import android.widget.TextView
|
||||
import apps.amine.bou.readerforselfoss.R
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
|
||||
var snackBarShown = false
|
||||
|
||||
fun Context.isNetworkAccessible(v: View?): Boolean {
|
||||
val cm = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
val activeNetwork: NetworkInfo? = cm.activeNetworkInfo
|
||||
val networkIsAccessible = activeNetwork != null && activeNetwork.isConnectedOrConnecting
|
||||
|
||||
if (v != null && !networkIsAccessible && !snackBarShown) {
|
||||
val s = Snackbar
|
||||
.make(
|
||||
v,
|
||||
R.string.no_network_connectivity,
|
||||
Snackbar.LENGTH_INDEFINITE
|
||||
)
|
||||
|
||||
s.setAction(android.R.string.ok) {
|
||||
snackBarShown = false
|
||||
s.dismiss()
|
||||
}
|
||||
|
||||
val view = s.view
|
||||
val tv: TextView = view.findViewById(com.google.android.material.R.id.snackbar_text)
|
||||
tv.setTextColor(Color.WHITE)
|
||||
s.show()
|
||||
snackBarShown = true
|
||||
}
|
||||
return networkIsAccessible
|
||||
}
|
@ -152,4 +152,5 @@
|
||||
<string name="pref_switch_items_caching_off">Articles won\'t be saved to the device memory, and the app won\'t be usable offline.</string>
|
||||
<string name="pref_switch_items_caching_on">Articles will be saved to the device memory and will be used for offline use.</string>
|
||||
<string name="pref_switch_items_caching">Save items for offline use</string>
|
||||
<string name="no_network_connectivity">Not connected !</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user