diff --git a/app/src/main/java/io/legado/app/ui/config/ConfigViewModel.kt b/app/src/main/java/io/legado/app/ui/config/ConfigViewModel.kt new file mode 100644 index 000000000..b824cf730 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/config/ConfigViewModel.kt @@ -0,0 +1,22 @@ +package io.legado.app.ui.config + +import android.app.Application +import io.legado.app.R +import io.legado.app.base.BaseViewModel +import io.legado.app.help.BookHelp +import io.legado.app.utils.FileUtils +import io.legado.app.utils.toastOnUi + +class ConfigViewModel(application: Application) : BaseViewModel(application) { + + fun clearCache() { + execute { + BookHelp.clearCache() + FileUtils.delete(context.cacheDir.absolutePath) + }.onSuccess { + context.toastOnUi(R.string.clear_cache_success) + } + } + + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt index d0b6da530..4e7192418 100644 --- a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt @@ -6,6 +6,7 @@ import android.content.SharedPreferences import android.content.pm.PackageManager import android.os.Bundle import android.view.View +import androidx.fragment.app.activityViewModels import androidx.preference.ListPreference import androidx.preference.Preference import io.legado.app.R @@ -13,7 +14,6 @@ import io.legado.app.base.BasePreferenceFragment import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.databinding.DialogEditTextBinding -import io.legado.app.help.BookHelp import io.legado.app.help.config.AppConfig import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.primaryColor @@ -29,6 +29,7 @@ import splitties.init.appCtx class OtherConfigFragment : BasePreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener { + private val viewModel by activityViewModels() private val packageManager = appCtx.packageManager private val componentName = ComponentName( appCtx, @@ -185,9 +186,7 @@ class OtherConfigFragment : BasePreferenceFragment(), messageResource = R.string.sure_del ) { okButton { - BookHelp.clearCache() - FileUtils.delete(requireActivity().cacheDir.absolutePath) - toastOnUi(R.string.clear_cache_success) + viewModel.clearCache() } noButton() }