Offline shortcut.

This commit is contained in:
Amine 2018-11-07 21:05:23 +01:00
parent 4efd76fcbc
commit 42e8ecee78
2 changed files with 23 additions and 14 deletions

View File

@ -146,6 +146,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
private var badgeFavs: Int = -1 private var badgeFavs: Int = -1
private var fromTabShortcut: Boolean = false private var fromTabShortcut: Boolean = false
private var offlineShortcut: Boolean = false
private lateinit var tagsBadge: Map<Long, Int> private lateinit var tagsBadge: Map<Long, Int>
@ -164,8 +165,11 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
fromTabShortcut = intent.getIntExtra("shortcutTab", -1) != -1 fromTabShortcut = intent.getIntExtra("shortcutTab", -1) != -1
offlineShortcut = intent.getBooleanExtra("startOffline", false)
elementsShown = intent.getIntExtra("shortcutTab", UNREAD_SHOWN) if (fromTabShortcut) {
elementsShown = intent.getIntExtra("shortcutTab", UNREAD_SHOWN)
}
setContentView(R.layout.activity_home) setContentView(R.layout.activity_home)
@ -209,6 +213,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
R.color.refresh_progress_3 R.color.refresh_progress_3
) )
swipeRefreshLayout.setOnRefreshListener { swipeRefreshLayout.setOnRefreshListener {
offlineShortcut = false
allItems = ArrayList() allItems = ArrayList()
lastFetchDone = false lastFetchDone = false
handleDrawerItems() handleDrawerItems()
@ -731,7 +736,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
var sources: List<Source>? var sources: List<Source>?
fun sourcesApiCall() { fun sourcesApiCall() {
if (this@HomeActivity.isNetworkAccessible(null)) { if (this@HomeActivity.isNetworkAccessible(null, offlineShortcut)) {
api.sources.enqueue(object : Callback<List<Source>> { api.sources.enqueue(object : Callback<List<Source>> {
override fun onResponse( override fun onResponse(
call: Call<List<Source>>?, call: Call<List<Source>>?,
@ -754,7 +759,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
} }
} }
if (this@HomeActivity.isNetworkAccessible(null)) { if (this@HomeActivity.isNetworkAccessible(null, offlineShortcut)) {
api.tags.enqueue(object : Callback<List<Tag>> { api.tags.enqueue(object : Callback<List<Tag>> {
override fun onResponse( override fun onResponse(
call: Call<List<Tag>>, call: Call<List<Tag>>,
@ -885,7 +890,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
else -> Unit else -> Unit
} }
} else { } else {
if (this@HomeActivity.isNetworkAccessible(this@HomeActivity.findViewById(R.id.coordLayout))) { if (this@HomeActivity.isNetworkAccessible(this@HomeActivity.findViewById(R.id.coordLayout), offlineShortcut)) {
when (position) { when (position) {
0 -> getUnRead() 0 -> getUnRead()
1 -> getRead() 1 -> getRead()
@ -982,7 +987,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
handleListResult() handleListResult()
doGetAccordingToTab() doGetAccordingToTab()
} else { } else {
if (this@HomeActivity.isNetworkAccessible(this@HomeActivity.findViewById(R.id.coordLayout))) { if (this@HomeActivity.isNetworkAccessible(this@HomeActivity.findViewById(R.id.coordLayout), offlineShortcut)) {
doGetAccordingToTab() doGetAccordingToTab()
getAndStoreAllItems() getAndStoreAllItems()
} }
@ -1041,7 +1046,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
swipeRefreshLayout.post { swipeRefreshLayout.isRefreshing = true } swipeRefreshLayout.post { swipeRefreshLayout.isRefreshing = true }
} }
if (this@HomeActivity.isNetworkAccessible(this@HomeActivity.findViewById(R.id.coordLayout))) { if (this@HomeActivity.isNetworkAccessible(this@HomeActivity.findViewById(R.id.coordLayout), offlineShortcut)) {
call(maybeTagFilter?.tag, maybeSourceFilter?.id?.toLong(), maybeSearchFilter) call(maybeTagFilter?.tag, maybeSourceFilter?.id?.toLong(), maybeSearchFilter)
.enqueue(object : Callback<List<Item>> { .enqueue(object : Callback<List<Item>> {
override fun onResponse( override fun onResponse(
@ -1171,7 +1176,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
} }
private fun reloadBadges() { private fun reloadBadges() {
if (this@HomeActivity.isNetworkAccessible(null) && (displayUnreadCount || displayAllCount)) { if (this@HomeActivity.isNetworkAccessible(null, offlineShortcut) && (displayUnreadCount || displayAllCount)) {
api.stats.enqueue(object : Callback<Stats> { api.stats.enqueue(object : Callback<Stats> {
override fun onResponse(call: Call<Stats>, response: Response<Stats>) { override fun onResponse(call: Call<Stats>, response: Response<Stats>) {
if (response.body() != null) { if (response.body() != null) {
@ -1277,7 +1282,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.refresh -> { R.id.refresh -> {
if (this@HomeActivity.isNetworkAccessible(null)) { if (this@HomeActivity.isNetworkAccessible(null, offlineShortcut)) {
needsConfirmation(R.string.menu_home_refresh, R.string.refresh_dialog_message) { needsConfirmation(R.string.menu_home_refresh, R.string.refresh_dialog_message) {
api.update().enqueue(object : Callback<String> { api.update().enqueue(object : Callback<String> {
override fun onResponse( override fun onResponse(
@ -1321,7 +1326,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
ACRA.getErrorReporter().maybeHandleSilentException(e, this@HomeActivity) ACRA.getErrorReporter().maybeHandleSilentException(e, this@HomeActivity)
} }
if (ids.isNotEmpty() && this@HomeActivity.isNetworkAccessible(null)) { if (ids.isNotEmpty() && this@HomeActivity.isNetworkAccessible(null, offlineShortcut)) {
api.readAll(ids).enqueue(object : Callback<SuccessResponse> { api.readAll(ids).enqueue(object : Callback<SuccessResponse> {
override fun onResponse( override fun onResponse(
call: Call<SuccessResponse>, call: Call<SuccessResponse>,
@ -1465,7 +1470,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
}) })
} }
if (this@HomeActivity.isNetworkAccessible(null)) { if (this@HomeActivity.isNetworkAccessible(null, offlineShortcut)) {
thread { thread {
val actions = db.actionsDao().actions() val actions = db.actionsDao().actions()

View File

@ -11,15 +11,16 @@ import com.google.android.material.snackbar.Snackbar
var snackBarShown = false var snackBarShown = false
var view: View? = null var view: View? = null
lateinit var s: Snackbar
fun Context.isNetworkAccessible(v: View?): Boolean { fun Context.isNetworkAccessible(v: View?, overrideOffline: Boolean = false): Boolean {
val cm = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val cm = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork: NetworkInfo? = cm.activeNetworkInfo val activeNetwork: NetworkInfo? = cm.activeNetworkInfo
val networkIsAccessible = activeNetwork != null && activeNetwork.isConnectedOrConnecting val networkIsAccessible = activeNetwork != null && activeNetwork.isConnectedOrConnecting
if (v != null && !networkIsAccessible && (!snackBarShown || v != view)) { if (v != null && (!networkIsAccessible || overrideOffline) && (!snackBarShown || v != view)) {
view = v view = v
val s = Snackbar s = Snackbar
.make( .make(
v, v,
R.string.no_network_connectivity, R.string.no_network_connectivity,
@ -37,5 +38,8 @@ fun Context.isNetworkAccessible(v: View?): Boolean {
s.show() s.show()
snackBarShown = true snackBarShown = true
} }
return networkIsAccessible if (snackBarShown && networkIsAccessible && !overrideOffline) {
s.dismiss()
}
return if(overrideOffline) overrideOffline else networkIsAccessible
} }