Closes #1. May need some fine tuning.

This commit is contained in:
Amine 2018-10-14 15:38:06 +02:00
parent 8788e920ce
commit 89d34a1a71
10 changed files with 130 additions and 30 deletions

View File

@ -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<String> = 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<List<Item>> {
override fun onFailure(call: Call<List<Item>>, t: Throwable) {
}
override fun onResponse(
call: Call<List<Item>>,
response: Response<List<Item>>
) {
thread {
if (response.body() != null) {
val apiItems = (response.body() as ArrayList<Item>).filter {
maybeTagFilter != null || filter(it.tags)
} as ArrayList<Item>
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<List<Item>>
) {
fun handleItemsResponse(response: Response<List<Item>>) {
val shouldUpdate = (response.body() != items)
val shouldUpdate = (response.body()?.toSet() != items.toSet())
if (response.body() != null) {
if (shouldUpdate) {
getAndStoreAllItems()
items = response.body() as ArrayList<Item>
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<Item>) {
private fun updateItems(adapterItems: ArrayList<Item>) {
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()
}
}
}

View File

@ -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();

Binary file not shown.

After

Width:  |  Height:  |  Size: 523 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 660 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 982 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -148,4 +148,8 @@
<string name="acra_login">Enable logging</string>
<string name="drawer_item_hidden_tags">Hidden Tags</string>
<string name="unmark">Mark item as unread</string>
<string name="pref_header_offline">Offline and cache</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">Save items for offline use</string>
</resources>

View File

@ -11,6 +11,12 @@
android:icon="@drawable/ic_chrome_reader_mode_black_24"
android:title="@string/pref_header_viewer"/>
<header
android:fragment="apps.amine.bou.readerforselfoss.settings.SettingsActivity$OfflinePreferenceFragment"
android:icon="@drawable/ic_signal_wifi_statusbar_not_connected"
android:title="@string/pref_header_offline"/>
<header
android:fragment="apps.amine.bou.readerforselfoss.settings.SettingsActivity$DebugPreferenceFragment"
android:icon="@drawable/ic_bug_report"

View File

@ -0,0 +1,8 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="false"
android:key="items_caching"
android:summaryOff="@string/pref_switch_items_caching_off"
android:summaryOn="@string/pref_switch_items_caching_on"
android:title="@string/pref_switch_items_caching" />
</PreferenceScreen>