From 231fe9153e0cb5d928497aff72cddf36b604b5ae Mon Sep 17 00:00:00 2001 From: Levin Li Date: Sun, 17 Apr 2022 12:14:29 +0800 Subject: [PATCH] Avoid repeated refetches --- .../celestia/mobilecelestia/MainActivity.kt | 2 +- .../resource/CommonWebFragment.kt | 3 ++- .../resource/ResourceFragment.kt | 4 +++- .../resource/ResourceItemFragment.kt | 19 +++++++++++-------- .../main/res/layout/fragment_common_web.xml | 4 ++++ 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt b/app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt index 0f422aa1..e30230cb 100644 --- a/app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt +++ b/app/src/main/java/space/celestia/mobilecelestia/MainActivity.kt @@ -693,7 +693,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main), lifecycleScope.launch { try { val result = resourceAPI.item(lang, addon).commonHandler(ResourceItem::class.java, ResourceAPI.gson) - showBottomSheetFragment(ResourceItemFragment.newInstance(result, lang)) + showBottomSheetFragment(ResourceItemFragment.newInstance(result, lang, Date())) } catch (ignored: Throwable) {} } cleanup() diff --git a/app/src/main/java/space/celestia/mobilecelestia/resource/CommonWebFragment.kt b/app/src/main/java/space/celestia/mobilecelestia/resource/CommonWebFragment.kt index 53789e75..b3c3b2bd 100644 --- a/app/src/main/java/space/celestia/mobilecelestia/resource/CommonWebFragment.kt +++ b/app/src/main/java/space/celestia/mobilecelestia/resource/CommonWebFragment.kt @@ -34,6 +34,7 @@ import space.celestia.mobilecelestia.utils.CelestiaString import space.celestia.mobilecelestia.utils.commonHandler import java.io.File import java.lang.ref.WeakReference +import java.util.* import javax.inject.Inject @AndroidEntryPoint @@ -249,7 +250,7 @@ class CommonWebFragment: NavigationFragment.SubFragment(), CelestiaJavascriptInt val result = resourceAPI.item(lang, id).commonHandler(ResourceItem::class.java, ResourceAPI.gson) val frag = parentFragment if (frag is NavigationFragment) { - frag.pushFragment(ResourceItemFragment.newInstance(result, lang)) + frag.pushFragment(ResourceItemFragment.newInstance(result, lang, Date())) } } catch (ignored: Throwable) {} } diff --git a/app/src/main/java/space/celestia/mobilecelestia/resource/ResourceFragment.kt b/app/src/main/java/space/celestia/mobilecelestia/resource/ResourceFragment.kt index 849c38a0..12779e81 100644 --- a/app/src/main/java/space/celestia/mobilecelestia/resource/ResourceFragment.kt +++ b/app/src/main/java/space/celestia/mobilecelestia/resource/ResourceFragment.kt @@ -15,6 +15,7 @@ import android.os.Bundle import androidx.appcompat.widget.Toolbar import space.celestia.mobilecelestia.common.NavigationFragment import space.celestia.mobilecelestia.resource.model.ResourceItem +import java.util.* class ResourceFragment : NavigationFragment(), Toolbar.OnMenuItemClickListener { private lateinit var language: String @@ -30,7 +31,8 @@ class ResourceFragment : NavigationFragment(), Toolbar.OnMenuItemClickListener { } fun pushItem(item: ResourceItem) { - val frag = ResourceItemFragment.newInstance(item, language) + // Installed item, update time is unknown so set to epoch time here + val frag = ResourceItemFragment.newInstance(item, language, Date(0)) pushFragment(frag) } diff --git a/app/src/main/java/space/celestia/mobilecelestia/resource/ResourceItemFragment.kt b/app/src/main/java/space/celestia/mobilecelestia/resource/ResourceItemFragment.kt index fab7f996..a60985e1 100644 --- a/app/src/main/java/space/celestia/mobilecelestia/resource/ResourceItemFragment.kt +++ b/app/src/main/java/space/celestia/mobilecelestia/resource/ResourceItemFragment.kt @@ -33,6 +33,7 @@ import space.celestia.mobilecelestia.utils.CelestiaString import space.celestia.mobilecelestia.utils.commonHandler import space.celestia.mobilecelestia.utils.showAlert import java.io.File +import java.util.* import javax.inject.Inject @AndroidEntryPoint @@ -45,7 +46,7 @@ class ResourceItemFragment : NavigationFragment.SubFragment(), ResourceManager.L private lateinit var language: String private lateinit var item: ResourceItem - private var hasFetchedLatestData = false + private lateinit var lastUpdateDate: Date private lateinit var statusButton: Button private lateinit var goToButton: Button private lateinit var progressIndicator: LinearProgressIndicator @@ -69,10 +70,10 @@ class ResourceItemFragment : NavigationFragment.SubFragment(), ResourceManager.L resourceManager.addListener(this) if (savedInstanceState != null) { item = savedInstanceState.getSerializable(ARG_ITEM) as ResourceItem - hasFetchedLatestData = savedInstanceState.getBoolean(ARG_LATEST, false) + lastUpdateDate = savedInstanceState.getSerializable(ARG_UPDATED_DATE) as Date } else { item = requireArguments().getSerializable(ARG_ITEM) as ResourceItem - hasFetchedLatestData = false + lastUpdateDate = requireArguments().getSerializable(ARG_UPDATED_DATE) as Date } language = requireArguments().getString(ARG_LANG, "en") } @@ -113,13 +114,14 @@ class ResourceItemFragment : NavigationFragment.SubFragment(), ResourceManager.L replace(CommonWebFragment.newInstance(uri, listOf("item"), resourceManager.contextDirectory(item.id)), R.id.webview_container) } - if (!hasFetchedLatestData) { + if (Date().time - lastUpdateDate.time > UPDATE_INTERVAL_MILLISECONDS) { // Fetch the latest item, this is needed as user might come // here from Installed where the URL might be incorrect lifecycleScope.launch { try { val result = resourceAPI.item(language, item.id).commonHandler(ResourceItem::class.java, ResourceAPI.gson) item = result + lastUpdateDate = Date() updateUI() } catch (ignored: Throwable) {} } @@ -128,7 +130,7 @@ class ResourceItemFragment : NavigationFragment.SubFragment(), ResourceManager.L override fun onSaveInstanceState(outState: Bundle) { outState.putSerializable(ARG_ITEM, item) - outState.putBoolean(ARG_LATEST, hasFetchedLatestData) + outState.putSerializable(ARG_UPDATED_DATE, lastUpdateDate) super.onSaveInstanceState(outState) } @@ -179,7 +181,6 @@ class ResourceItemFragment : NavigationFragment.SubFragment(), ResourceManager.L activity.showAlert(CelestiaString("Do you want to cancel this task?", "")) { dm.cancel(item.id) currentState = ResourceItemState.None - hasFetchedLatestData = true updateUI() } return @@ -253,14 +254,16 @@ class ResourceItemFragment : NavigationFragment.SubFragment(), ResourceManager.L companion object { private const val ARG_ITEM = "item" private const val ARG_LANG = "lang" - private const val ARG_LATEST = "latest" + private const val ARG_UPDATED_DATE = "date" + private const val UPDATE_INTERVAL_MILLISECONDS = 1800000L @JvmStatic - fun newInstance(item: ResourceItem, language: String) = + fun newInstance(item: ResourceItem, language: String, lastUpdateDate: Date) = ResourceItemFragment().apply { arguments = Bundle().apply { putSerializable(ARG_ITEM, item) putString(ARG_LANG, language) + putSerializable(ARG_UPDATED_DATE, lastUpdateDate) } } } diff --git a/app/src/main/res/layout/fragment_common_web.xml b/app/src/main/res/layout/fragment_common_web.xml index d892d6a6..243ed749 100644 --- a/app/src/main/res/layout/fragment_common_web.xml +++ b/app/src/main/res/layout/fragment_common_web.xml @@ -49,6 +49,10 @@ android:layout_height="wrap_content" app:icon="@drawable/ic_action_back_tint" /> + +