diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ea477bc..6d58b93 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="apps.amine.bou.readerforselfoss" xmlns:tools="http://schemas.android.com/tools"> + 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> { - override fun onResponse( - call: Call>, - response: Response> - ) { - handleItemsResponse(response) - } + if (this@HomeActivity.isNetworkAccessible(this@HomeActivity.findViewById(R.id.coordLayout))) { + call(maybeTagFilter?.tag, maybeSourceFilter?.id?.toLong(), maybeSearchFilter) + .enqueue(object : Callback> { + override fun onResponse( + call: Call>, + response: Response> + ) { + handleItemsResponse(response) + } - override fun onFailure(call: Call>, t: Throwable) { - swipeRefreshLayout.isRefreshing = false - Toast.makeText( - this@HomeActivity, - toastMessage, - Toast.LENGTH_SHORT - ).show() - } - }) + override fun onFailure(call: Call>, 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) { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/network/NetworkUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/network/NetworkUtils.kt new file mode 100644 index 0000000..1e51f11 --- /dev/null +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/network/NetworkUtils.kt @@ -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 +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1ee6ad1..a69accd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -152,4 +152,5 @@ Articles won\'t be saved to the device memory, and the app won\'t be usable offline. Articles will be saved to the device memory and will be used for offline use. Save items for offline use + Not connected !