Network status on articles loading.
This commit is contained in:
parent
ab2d0c4036
commit
0bb2195bff
@ -3,6 +3,7 @@
|
|||||||
package="apps.amine.bou.readerforselfoss"
|
package="apps.amine.bou.readerforselfoss"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
<application
|
<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.flattenTags
|
||||||
import apps.amine.bou.readerforselfoss.utils.longHash
|
import apps.amine.bou.readerforselfoss.utils.longHash
|
||||||
import apps.amine.bou.readerforselfoss.utils.maybeHandleSilentException
|
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.toEntity
|
||||||
import apps.amine.bou.readerforselfoss.utils.persistence.toView
|
import apps.amine.bou.readerforselfoss.utils.persistence.toView
|
||||||
import co.zsmb.materialdrawerkt.builders.accountHeader
|
import co.zsmb.materialdrawerkt.builders.accountHeader
|
||||||
@ -333,7 +334,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
|||||||
|
|
||||||
getElementsAccordingToTab()
|
getElementsAccordingToTab()
|
||||||
|
|
||||||
|
|
||||||
handleGDPRDialog(sharedPref.getBoolean("GDPR_shown", false))
|
handleGDPRDialog(sharedPref.getBoolean("GDPR_shown", false))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -817,11 +817,51 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
|||||||
override fun onTabSelected(position: Int) {
|
override fun onTabSelected(position: Int) {
|
||||||
offset = 0
|
offset = 0
|
||||||
lastFetchDone = false
|
lastFetchDone = false
|
||||||
when (position) {
|
|
||||||
0 -> getUnRead()
|
if (itemsCaching) {
|
||||||
1 -> getRead()
|
|
||||||
2 -> getStarred()
|
if (!swipeRefreshLayout.isRefreshing) {
|
||||||
else -> Unit
|
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()
|
handleListResult()
|
||||||
doGetAccordingToTab()
|
doGetAccordingToTab()
|
||||||
} else {
|
} else {
|
||||||
doGetAccordingToTab()
|
if (this@HomeActivity.isNetworkAccessible(this@HomeActivity.findViewById(R.id.coordLayout))) {
|
||||||
getAndStoreAllItems()
|
doGetAccordingToTab()
|
||||||
|
getAndStoreAllItems()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -956,24 +998,28 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
|||||||
swipeRefreshLayout.post { swipeRefreshLayout.isRefreshing = true }
|
swipeRefreshLayout.post { swipeRefreshLayout.isRefreshing = true }
|
||||||
}
|
}
|
||||||
|
|
||||||
call(maybeTagFilter?.tag, maybeSourceFilter?.id?.toLong(), maybeSearchFilter)
|
if (this@HomeActivity.isNetworkAccessible(this@HomeActivity.findViewById(R.id.coordLayout))) {
|
||||||
.enqueue(object : Callback<List<Item>> {
|
call(maybeTagFilter?.tag, maybeSourceFilter?.id?.toLong(), maybeSearchFilter)
|
||||||
override fun onResponse(
|
.enqueue(object : Callback<List<Item>> {
|
||||||
call: Call<List<Item>>,
|
override fun onResponse(
|
||||||
response: Response<List<Item>>
|
call: Call<List<Item>>,
|
||||||
) {
|
response: Response<List<Item>>
|
||||||
handleItemsResponse(response)
|
) {
|
||||||
}
|
handleItemsResponse(response)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
|
||||||
swipeRefreshLayout.isRefreshing = false
|
swipeRefreshLayout.isRefreshing = false
|
||||||
Toast.makeText(
|
Toast.makeText(
|
||||||
this@HomeActivity,
|
this@HomeActivity,
|
||||||
toastMessage,
|
toastMessage,
|
||||||
Toast.LENGTH_SHORT
|
Toast.LENGTH_SHORT
|
||||||
).show()
|
).show()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
swipeRefreshLayout.post { swipeRefreshLayout.isRefreshing = false }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getUnRead(appendResults: Boolean = 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_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_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="pref_switch_items_caching">Save items for offline use</string>
|
||||||
|
<string name="no_network_connectivity">Not connected !</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user