diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt index 716324f..7973ce9 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -107,6 +107,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { private var displayAccountHeader: Boolean = false private var infiniteScroll: Boolean = false private var lastFetchDone: Boolean = false + private var itemsCaching: Boolean = false private var hiddenTags: List = emptyList() private lateinit var tabNewBadge: TextBadgeItem @@ -180,24 +181,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { handleSwipeRefreshLayout() } - private fun handleGDPRDialog(GDPRShown: Boolean) { - val sharedEditor = sharedPref.edit() - if (!GDPRShown) { - val alertDialog = AlertDialog.Builder(this).create() - alertDialog.setTitle(getString(R.string.gdpr_dialog_title)) - alertDialog.setMessage(getString(R.string.gdpr_dialog_message)) - alertDialog.setButton( - AlertDialog.BUTTON_NEUTRAL, - "OK" - ) { dialog, _ -> - sharedEditor.putBoolean("GDPR_shown", true) - sharedEditor.commit() - dialog.dismiss() - } - alertDialog.show() - } - } - private fun handleSwipeRefreshLayout() { swipeRefreshLayout.setColorSchemeResources( R.color.refresh_progress_1, @@ -350,9 +333,33 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { getElementsAccordingToTab() + handleGDPRDialog(sharedPref.getBoolean("GDPR_shown", false)) } + private fun getAndStoreAllItems() { + api.allItems().enqueue(object : Callback> { + override fun onFailure(call: Call>, t: Throwable) { + } + + override fun onResponse( + call: Call>, + response: Response> + ) { + thread { + if (response.body() != null) { + val apiItems = (response.body() as ArrayList).filter { + maybeTagFilter != null || filter(it.tags) + } as ArrayList + db.itemsDao().deleteAllItems() + db.itemsDao() + .insertAllItems(*(apiItems.map { it.toEntity() }).toTypedArray()) + } + } + } + }) + } + override fun onStop() { super.onStop() customTabActivityHelper.unbindCustomTabsService(this) @@ -371,6 +378,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { userIdentifier = sharedPref.getString("unique_id", "") displayAccountHeader = sharedPref.getBoolean("account_header_displaying", false) infiniteScroll = sharedPref.getBoolean("infinite_loading", false) + itemsCaching = sharedPref.getBoolean("items_caching", false) hiddenTags = if (sharedPref.getString("hidden_tags", "").isNotEmpty()) { sharedPref.getString("hidden_tags", "").replace("\\s".toRegex(), "").split(",") } else { @@ -729,8 +737,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { thread { var drawerData = DrawerData(db.drawerDataDao().tags().map { it.toView() }, db.drawerDataDao().sources().map { it.toView() }) - handleDrawerData(drawerData, loadedFromCache = true) - drawerApiCalls(drawerData) + runOnUiThread { + handleDrawerData(drawerData, loadedFromCache = true) + drawerApiCalls(drawerData) + } } } @@ -854,6 +864,15 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { appendResults: Boolean = false, offsetOverride: Int? = null ) { + fun doGetAccordingToTab() { + when (elementsShown) { + UNREAD_SHOWN -> getUnRead(appendResults) + READ_SHOWN -> getRead(appendResults) + FAV_SHOWN -> getStarred(appendResults) + else -> getUnRead(appendResults) + } + } + offset = if (appendResults && offsetOverride === null) { (offset + itemsNumber) } else { @@ -861,12 +880,35 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { } firstVisible = if (appendResults) firstVisible else 0 - when (elementsShown) { - UNREAD_SHOWN -> getUnRead(appendResults) - READ_SHOWN -> getRead(appendResults) - FAV_SHOWN -> getStarred(appendResults) - else -> getUnRead(appendResults) + 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 (elementsShown) { + UNREAD_SHOWN -> ArrayList(dbItems.filter { it.unread }) + READ_SHOWN -> ArrayList(dbItems.filter { !it.unread }) + FAV_SHOWN -> ArrayList(dbItems.filter { it.starred }) + else -> ArrayList(dbItems.filter { it.unread }) + } + handleListResult() + doGetAccordingToTab() + } else { + doGetAccordingToTab() + getAndStoreAllItems() + } + } + } + + } else { + doGetAccordingToTab() } + } private fun filter(tags: String): Boolean { @@ -880,9 +922,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { call: (String?, Long?, String?) -> Call> ) { fun handleItemsResponse(response: Response>) { - val shouldUpdate = (response.body() != items) + val shouldUpdate = (response.body()?.toSet() != items.toSet()) if (response.body() != null) { if (shouldUpdate) { + getAndStoreAllItems() items = response.body() as ArrayList items = items.filter { maybeTagFilter != null || filter(it.tags) @@ -902,9 +945,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { allItems = ArrayList() } } - if (shouldUpdate) { - handleListResult(appendResults) - } + + handleListResult(appendResults) if (!appendResults) mayBeEmpty() swipeRefreshLayout.isRefreshing = false @@ -1269,8 +1311,26 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { else -> badgeNew // if !elementsShown then unread are fetched. } - fun updateItems(adapterItems: ArrayList) { + private fun updateItems(adapterItems: ArrayList) { items = adapterItems } + + private fun handleGDPRDialog(GDPRShown: Boolean) { + val sharedEditor = sharedPref.edit() + if (!GDPRShown) { + val alertDialog = AlertDialog.Builder(this).create() + alertDialog.setTitle(getString(R.string.gdpr_dialog_title)) + alertDialog.setMessage(getString(R.string.gdpr_dialog_message)) + alertDialog.setButton( + AlertDialog.BUTTON_NEUTRAL, + "OK" + ) { dialog, _ -> + sharedEditor.putBoolean("GDPR_shown", true) + sharedEditor.commit() + dialog.dismiss() + } + alertDialog.show() + } + } } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/settings/SettingsActivity.java b/app/src/main/java/apps/amine/bou/readerforselfoss/settings/SettingsActivity.java index ff1b573..69e8f48 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/settings/SettingsActivity.java +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/settings/SettingsActivity.java @@ -135,6 +135,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { return PreferenceFragment.class.getName().equals(fragmentName) || GeneralPreferenceFragment.class.getName().equals(fragmentName) || ArticleViewerPreferenceFragment.class.getName().equals(fragmentName) + || OfflinePreferenceFragment.class.getName().equals(fragmentName) || DebugPreferenceFragment.class.getName().equals(fragmentName) || LinksPreferenceFragment.class.getName().equals(fragmentName) || ThemePreferenceFragment.class.getName().equals(fragmentName); @@ -363,6 +364,27 @@ public class SettingsActivity extends AppCompatPreferenceActivity { } } + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public static class OfflinePreferenceFragment extends PreferenceFragment { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.pref_offline); + setHasOptionsMenu(true); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + int id = item.getItemId(); + if (id == android.R.id.home) { + getActivity().finish(); + return true; + } + return super.onOptionsItemSelected(item); + } + } + + @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); diff --git a/app/src/main/res/drawable-hdpi/ic_signal_wifi_statusbar_not_connected.png b/app/src/main/res/drawable-hdpi/ic_signal_wifi_statusbar_not_connected.png new file mode 100644 index 0000000..f45bb36 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_signal_wifi_statusbar_not_connected.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_signal_wifi_statusbar_not_connected.png b/app/src/main/res/drawable-mdpi/ic_signal_wifi_statusbar_not_connected.png new file mode 100644 index 0000000..3f95269 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_signal_wifi_statusbar_not_connected.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_signal_wifi_statusbar_not_connected.png b/app/src/main/res/drawable-xhdpi/ic_signal_wifi_statusbar_not_connected.png new file mode 100644 index 0000000..7b965c1 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_signal_wifi_statusbar_not_connected.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_signal_wifi_statusbar_not_connected.png b/app/src/main/res/drawable-xxhdpi/ic_signal_wifi_statusbar_not_connected.png new file mode 100644 index 0000000..ea06e23 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_signal_wifi_statusbar_not_connected.png differ diff --git a/app/src/main/res/drawable-xxxhdpi/ic_signal_wifi_statusbar_not_connected.png b/app/src/main/res/drawable-xxxhdpi/ic_signal_wifi_statusbar_not_connected.png new file mode 100644 index 0000000..b20c6d3 Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/ic_signal_wifi_statusbar_not_connected.png differ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8e56321..1ee6ad1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -148,4 +148,8 @@ Enable logging Hidden Tags Mark item as unread + Offline and cache + 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 diff --git a/app/src/main/res/xml/pref_headers.xml b/app/src/main/res/xml/pref_headers.xml index 79c18aa..62fa491 100644 --- a/app/src/main/res/xml/pref_headers.xml +++ b/app/src/main/res/xml/pref_headers.xml @@ -11,6 +11,12 @@ android:icon="@drawable/ic_chrome_reader_mode_black_24" android:title="@string/pref_header_viewer"/> + +
+
+ +