pref: `AppPreferenceUtils` 单例

This commit is contained in:
HuanCheng65 2023-07-18 17:40:00 +08:00
parent b98d2fffaa
commit fa7edbc326
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
3 changed files with 31 additions and 6 deletions

View File

@ -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()

View File

@ -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) {

View File

@ -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<Context> = WeakReference(ctx)
private val context: Context
get() = contextWeakReference.get()!!
private val preferencesDataStore: DataStore<Preferences>
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)
get() = AppPreferencesUtils.getInstance(this)