From fa7edbc3269c5292176a35af1d114737dc6410dc Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Tue, 18 Jul 2023 17:40:00 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20`AppPreferenceUtils`=20=E5=8D=95?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tieba/post/activities/BaseActivity.kt | 14 ++++++++++--- .../tieba/post/fragments/BaseFragment.kt | 2 +- .../tieba/post/utils/AppPreferencesUtils.kt | 21 +++++++++++++++++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/activities/BaseActivity.kt b/app/src/main/java/com/huanchengfly/tieba/post/activities/BaseActivity.kt index 1d07cb12..92a60eb0 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/activities/BaseActivity.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/activities/BaseActivity.kt @@ -37,8 +37,16 @@ import com.huanchengfly.tieba.post.ui.common.theme.interfaces.ExtraRefreshable import com.huanchengfly.tieba.post.ui.common.theme.utils.ThemeUtils import com.huanchengfly.tieba.post.ui.widgets.VoicePlayerView import com.huanchengfly.tieba.post.ui.widgets.theme.TintToolbar -import com.huanchengfly.tieba.post.utils.* -import kotlinx.coroutines.* +import com.huanchengfly.tieba.post.utils.AppPreferencesUtils +import com.huanchengfly.tieba.post.utils.DialogUtil +import com.huanchengfly.tieba.post.utils.HandleBackUtil +import com.huanchengfly.tieba.post.utils.ThemeUtil +import com.huanchengfly.tieba.post.utils.calcStatusBarColorInt +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.CoroutineStart +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch import kotlin.coroutines.CoroutineContext abstract class BaseActivity : AppCompatActivity(), ExtraRefreshable, CoroutineScope { @@ -54,7 +62,7 @@ abstract class BaseActivity : AppCompatActivity(), ExtraRefreshable, CoroutineSc private var customStatusColor = -1 private var statusBarTinted = false - val appPreferences: AppPreferencesUtils by lazy { AppPreferencesUtils(this) } + val appPreferences: AppPreferencesUtils by lazy { AppPreferencesUtils.getInstance(this) } override fun onPause() { super.onPause() diff --git a/app/src/main/java/com/huanchengfly/tieba/post/fragments/BaseFragment.kt b/app/src/main/java/com/huanchengfly/tieba/post/fragments/BaseFragment.kt index bd32aedb..7f1fbd82 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/fragments/BaseFragment.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/fragments/BaseFragment.kt @@ -67,7 +67,7 @@ abstract class BaseFragment : Fragment(), BackHandledInterface, CoroutineScope { return mContext!! } protected val appPreferences: AppPreferencesUtils - get() = AppPreferencesUtils(attachContext) + get() = AppPreferencesUtils.getInstance(attachContext) @TargetApi(23) override fun onAttach(context: Context) { diff --git a/app/src/main/java/com/huanchengfly/tieba/post/utils/AppPreferencesUtils.kt b/app/src/main/java/com/huanchengfly/tieba/post/utils/AppPreferencesUtils.kt index 408bd468..84894f6a 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/utils/AppPreferencesUtils.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/utils/AppPreferencesUtils.kt @@ -22,13 +22,30 @@ import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch +import java.lang.ref.WeakReference import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty -open class AppPreferencesUtils(private val context: Context) { +open class AppPreferencesUtils private constructor(ctx: Context) { + companion object { + private var instance: AppPreferencesUtils? = null + + fun getInstance(context: Context): AppPreferencesUtils { + return instance ?: AppPreferencesUtils(context).also { + instance = it + } + } + } + + private val contextWeakReference: WeakReference = WeakReference(ctx) + + private val context: Context + get() = contextWeakReference.get()!! + private val preferencesDataStore: DataStore get() = context.dataStore + private val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) var userLikeLastRequestUnix by DataStoreDelegates.long(defaultValue = 0L) @@ -377,4 +394,4 @@ open class AppPreferencesUtils(private val context: Context) { } val Context.appPreferences: AppPreferencesUtils - get() = AppPreferencesUtils(this) \ No newline at end of file + get() = AppPreferencesUtils.getInstance(this) \ No newline at end of file