Skip to content

Commit

Permalink
Avoid repeated refetches
Browse files Browse the repository at this point in the history
  • Loading branch information
levinli303 committed Apr 17, 2022
1 parent b845943 commit 231fe91
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")
}
Expand Down Expand Up @@ -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) {}
}
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/layout/fragment_common_web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
android:layout_height="wrap_content"
app:icon="@drawable/ic_action_back_tint" />

<Space
android:layout_width="16dp"
android:layout_height="wrap_content"/>

<com.google.android.material.button.MaterialButton
style="@style/Widget.Material3.Button.IconButton"
android:id="@+id/forward_button"
Expand Down

0 comments on commit 231fe91

Please sign in to comment.