Call getImageLimits() in SettingsActivity's onCreate()
This commit is contained in:
1 parent
283e8f20b3
commit
e51c297a7d
5 files changed
+53
-52
No files matched your search
@@ -8,10 +8,7 @@ import android.widget.Button
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.client.EhEngine
|
||||
import com.hippo.ehviewer.client.parser.HomeParser
|
||||
import com.hippo.ehviewer.ui.SettingsActivity
|
||||
import com.hippo.ehviewer.ui.scene.BaseScene
|
||||
import com.hippo.preference.DialogPreference
|
||||
import eu.kanade.tachiyomi.util.lang.launchIO
|
||||
import eu.kanade.tachiyomi.util.lang.withUIContext
|
||||
@@ -20,18 +17,11 @@ class ImageLimitsPreference(context: Context, attrs: AttributeSet) :
|
||||
DialogPreference(context, attrs), View.OnClickListener {
|
||||
private val mActivity = context as SettingsActivity
|
||||
private val placeholder = context.getString(R.string.please_wait)
|
||||
private val coroutineScope = mActivity.lifecycleScope
|
||||
private lateinit var resetButton: Button
|
||||
private lateinit var mDialog: AlertDialog
|
||||
private lateinit var mLimits: HomeParser.Limits
|
||||
private lateinit var mFunds: HomeParser.Funds
|
||||
|
||||
init {
|
||||
coroutineScope.launchIO {
|
||||
getImageLimits {
|
||||
summary = "${mLimits.current} / ${mLimits.maximum}"
|
||||
}
|
||||
}
|
||||
updateSummary()
|
||||
}
|
||||
|
||||
override fun onPrepareDialogBuilder(builder: AlertDialog.Builder) {
|
||||
@@ -45,46 +35,40 @@ class ImageLimitsPreference(context: Context, attrs: AttributeSet) :
|
||||
resetButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE)
|
||||
resetButton.setOnClickListener(this)
|
||||
resetButton.isEnabled = false
|
||||
if (this::mLimits.isInitialized) {
|
||||
bind()
|
||||
} else {
|
||||
coroutineScope.launchIO {
|
||||
getImageLimits(true) {
|
||||
bind()
|
||||
}
|
||||
}
|
||||
launchIOCatching {
|
||||
mActivity.getImageLimits()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDialogClosed(positiveResult: Boolean) {
|
||||
super.onDialogClosed(positiveResult)
|
||||
if (this::mLimits.isInitialized) {
|
||||
summary = "${mLimits.current} / ${mLimits.maximum}"
|
||||
}
|
||||
updateSummary()
|
||||
}
|
||||
|
||||
private suspend fun getImageLimits(showError: Boolean = false, onSuccess: () -> Unit) {
|
||||
private fun updateSummary() {
|
||||
summary = mActivity.mLimits?.run {
|
||||
mActivity.getString(R.string.image_limits_summary, current, maximum)
|
||||
} ?: mActivity.getString(R.string.image_limits_summary, 0, 0)
|
||||
}
|
||||
|
||||
private fun launchIOCatching(block: suspend () -> Unit) = mActivity.lifecycleScope.launchIO {
|
||||
runCatching {
|
||||
EhEngine.getImageLimits()
|
||||
block()
|
||||
}.onFailure {
|
||||
it.printStackTrace()
|
||||
if (showError) {
|
||||
withUIContext {
|
||||
mDialog.setMessage(it.message)
|
||||
}
|
||||
withUIContext {
|
||||
mDialog.setMessage(it.message)
|
||||
}
|
||||
}.onSuccess {
|
||||
mLimits = it.limits
|
||||
mFunds = it.funds
|
||||
withUIContext {
|
||||
onSuccess()
|
||||
bind()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun bind() {
|
||||
val (current, maximum, resetCost) = mLimits
|
||||
val (fundsGP, fundsC) = mFunds
|
||||
val (current, maximum, resetCost) = mActivity.mLimits!!
|
||||
val (fundsGP, fundsC) = mActivity.mFunds!!
|
||||
val cost = if (fundsGP >= resetCost) "$resetCost GP" else "$resetCost Credits"
|
||||
val message = mActivity.getString(R.string.current_limits, "$current / $maximum", cost) +
|
||||
"\n" + mActivity.getString(R.string.current_funds, "$fundsGP+", fundsC)
|
||||
@@ -95,22 +79,8 @@ class ImageLimitsPreference(context: Context, attrs: AttributeSet) :
|
||||
override fun onClick(v: View) {
|
||||
resetButton.isEnabled = false
|
||||
mDialog.setMessage(placeholder)
|
||||
coroutineScope.launchIO {
|
||||
runCatching {
|
||||
EhEngine.resetImageLimits()
|
||||
}.onFailure {
|
||||
it.printStackTrace()
|
||||
withUIContext {
|
||||
mDialog.setMessage(it.message)
|
||||
}
|
||||
}.onSuccess {
|
||||
withUIContext {
|
||||
mLimits = it ?: HomeParser.Limits(maximum = mLimits.maximum).also {
|
||||
mActivity.showTip(R.string.reset_limits_succeed, BaseScene.LENGTH_SHORT)
|
||||
}
|
||||
bind()
|
||||
}
|
||||
}
|
||||
launchIOCatching {
|
||||
mActivity.resetImageLimits()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,20 +20,38 @@ import android.view.MenuItem
|
||||
import android.widget.TextView
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.fragment.app.FragmentTransaction
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.ActivityNavigator
|
||||
import com.google.android.material.appbar.AppBarLayout
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.client.EhCookieStore
|
||||
import com.hippo.ehviewer.client.EhEngine
|
||||
import com.hippo.ehviewer.client.parser.HomeParser
|
||||
import com.hippo.ehviewer.ui.fragment.SettingsFragment
|
||||
import com.hippo.ehviewer.ui.scene.BaseScene
|
||||
import eu.kanade.tachiyomi.util.lang.launchIO
|
||||
import eu.kanade.tachiyomi.util.lang.withUIContext
|
||||
|
||||
class SettingsActivity : EhActivity() {
|
||||
private var mFab: FloatingActionButton? = null
|
||||
private var mContentText: TextView? = null
|
||||
private var mAppbarLayout: AppBarLayout? = null
|
||||
var mLimits: HomeParser.Limits? = null
|
||||
private set
|
||||
var mFunds: HomeParser.Funds? = null
|
||||
private set
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
if (EhCookieStore.hasSignedIn()) {
|
||||
lifecycleScope.launchIO {
|
||||
runCatching {
|
||||
getImageLimits()
|
||||
}.exceptionOrNull()?.printStackTrace()
|
||||
}
|
||||
}
|
||||
setContentView(R.layout.activity_preference)
|
||||
setSupportActionBar(findViewById(R.id.toolbar))
|
||||
mContentText = findViewById(R.id.tip)
|
||||
@@ -49,6 +67,20 @@ class SettingsActivity : EhActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getImageLimits() = EhEngine.getImageLimits().run {
|
||||
mLimits = limits
|
||||
mFunds = funds
|
||||
}
|
||||
|
||||
suspend fun resetImageLimits() {
|
||||
mLimits =
|
||||
EhEngine.resetImageLimits() ?: HomeParser.Limits(maximum = mLimits!!.maximum).also {
|
||||
withUIContext {
|
||||
showTip(R.string.reset_limits_succeed, BaseScene.LENGTH_SHORT)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun showTip(@StringRes id: Int, length: Int) {
|
||||
showTip(getString(id), length)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user