clean up settings and some other things

This commit is contained in:
2024-07-16 02:12:19 +03:00
parent 46d3fe63fa
commit b252c03be7
60 changed files with 698 additions and 613 deletions
@@ -9,14 +9,15 @@ import androidx.lifecycle.viewModelScope
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.PermissionStatus
import com.meloda.app.fast.auth.AuthGraph
import com.meloda.app.fast.common.LongPollController
import com.meloda.app.fast.common.UserConfig
import com.meloda.app.fast.common.extensions.ifEmpty
import com.meloda.app.fast.common.extensions.listenValue
import com.meloda.app.fast.common.extensions.setValue
import com.meloda.app.fast.common.model.LongPollState
import com.meloda.app.fast.data.db.GetCurrentAccountUseCase
import com.meloda.app.fast.datastore.SettingsController
import com.meloda.app.fast.datastore.AppSettings
import com.meloda.app.fast.datastore.UserSettings
import com.meloda.app.fast.datastore.model.LongPollState
import com.meloda.app.fast.model.BaseError
import com.meloda.app.fast.navigation.Main
import kotlinx.coroutines.Dispatchers
@@ -54,7 +55,8 @@ interface MainViewModel {
class MainViewModelImpl(
private val getCurrentAccountUseCase: GetCurrentAccountUseCase,
private val userSettings: UserSettings
private val userSettings: UserSettings,
private val longPollController: LongPollController
) : MainViewModel, ViewModel() {
init {
@@ -83,22 +85,21 @@ class MainViewModelImpl(
override fun onAppResumed() {
if (isNeedToShowNotificationsRationaleDialog.value) {
isNeedToShowNotificationsRationaleDialog.update { false }
isNeedToCheckNotificationsPermission.update { true }
}
userSettings.onLanguageChanged(
AppCompatDelegate.getApplicationLocales()
val newLanguage = AppCompatDelegate.getApplicationLocales()
.toLanguageTags()
.ifEmpty { null }
?: LocaleListCompat.getDefault()
.toLanguageTags()
.ifEmpty { null }
?: LocaleListCompat.getDefault()
.toLanguageTags()
.split(",")
.firstOrNull()
.orEmpty()
.take(5)
)
.split(",")
.firstOrNull()
.orEmpty()
.take(5)
userSettings.updateUsingDarkTheme()
userSettings.onAppLanguageChanged(newLanguage)
}
@ExperimentalPermissionsApi
@@ -151,7 +152,7 @@ class MainViewModelImpl(
}
private fun listenLongPollState() {
userSettings.longPollStateToApply.listenValue { newState ->
longPollController.stateToApply.listenValue { newState ->
if (newState == LongPollState.Background) {
isNeedToCheckNotificationsPermission.update { true }
}
@@ -174,8 +175,8 @@ class MainViewModelImpl(
this.trustedHash = currentAccount.trustedHash
}
userSettings.setLongPollStateToApply(
if (SettingsController.isLongPollInBackgroundEnabled) {
longPollController.setStateToApply(
if (AppSettings.Debug.longPollInBackground) {
LongPollState.Background
} else {
LongPollState.InApp
@@ -191,7 +192,7 @@ class MainViewModelImpl(
}
private fun disableBackgroundLongPoll() {
SettingsController.isLongPollInBackgroundEnabled = false
userSettings.setLongPollStateToApply(LongPollState.InApp)
AppSettings.Debug.longPollInBackground = false
longPollController.setStateToApply(LongPollState.InApp)
}
}
@@ -5,7 +5,7 @@ import androidx.preference.PreferenceManager
import coil.ImageLoader
import coil.ImageLoaderFactory
import com.meloda.app.fast.common.di.applicationModule
import com.meloda.app.fast.datastore.SettingsController
import com.meloda.app.fast.datastore.AppSettings
import org.koin.android.ext.android.get
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
@@ -17,7 +17,7 @@ class AppGlobal : Application(), ImageLoaderFactory {
super.onCreate()
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
SettingsController.init(preferences)
AppSettings.init(preferences)
UserConfig.init(preferences)
initKoin()
@@ -33,15 +33,17 @@ import com.google.accompanist.permissions.rememberPermissionState
import com.meloda.app.fast.MainViewModel
import com.meloda.app.fast.MainViewModelImpl
import com.meloda.app.fast.common.AppConstants
import com.meloda.app.fast.common.LongPollController
import com.meloda.app.fast.common.extensions.isSdkAtLeast
import com.meloda.app.fast.datastore.SettingsController
import com.meloda.app.fast.common.model.LongPollState
import com.meloda.app.fast.datastore.AppSettings
import com.meloda.app.fast.datastore.UserSettings
import com.meloda.app.fast.datastore.model.LongPollState
import com.meloda.app.fast.service.OnlineService
import com.meloda.app.fast.service.longpolling.LongPollingService
import com.meloda.app.fast.ui.model.ThemeConfig
import com.meloda.app.fast.ui.theme.AppTheme
import com.meloda.app.fast.ui.theme.LocalTheme
import com.meloda.app.fast.ui.theme.LocalThemeConfig
import com.meloda.app.fast.ui.util.isNeedToEnableDarkMode
import org.koin.androidx.compose.koinViewModel
import org.koin.compose.KoinContext
import org.koin.compose.koinInject
@@ -53,7 +55,7 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
SettingsController.deviceId = Settings.Secure.getString(
AppSettings.deviceId = Settings.Secure.getString(
contentResolver,
Settings.Secure.ANDROID_ID
)
@@ -78,10 +80,12 @@ class MainActivity : AppCompatActivity() {
setContent {
KoinContext {
val context = LocalContext.current
val userSettings: UserSettings = koinInject()
val longPollCurrentState by userSettings.longPollCurrentState.collectAsStateWithLifecycle()
val longPollStateToApply by userSettings.longPollStateToApply.collectAsStateWithLifecycle()
val userSettings: UserSettings = koinInject()
val longPollController: LongPollController = koinInject()
val longPollCurrentState by longPollController.currentState.collectAsStateWithLifecycle()
val longPollStateToApply by longPollController.stateToApply.collectAsStateWithLifecycle()
val viewModel: MainViewModel = koinViewModel<MainViewModelImpl>()
@@ -136,9 +140,9 @@ class MainActivity : AppCompatActivity() {
}
}
val isOnline by userSettings.online.collectAsStateWithLifecycle()
LifecycleResumeEffect(isOnline) {
toggleOnlineService(isOnline)
val sendOnline by userSettings.sendOnlineStatus.collectAsStateWithLifecycle()
LifecycleResumeEffect(sendOnline) {
toggleOnlineService(sendOnline)
onPauseOrDispose {
toggleOnlineService(false)
@@ -151,25 +155,22 @@ class MainActivity : AppCompatActivity() {
}
}
val theme by userSettings.theme.collectAsStateWithLifecycle()
CompositionLocalProvider(
LocalTheme provides ThemeConfig(
usingDarkStyle = theme.usingDarkStyle,
usingDynamicColors = theme.usingDynamicColors,
selectedColorScheme = theme.selectedColorScheme,
usingAmoledBackground = theme.usingAmoledBackground,
usingBlur = theme.usingBlur,
isMultiline = theme.isMultiline,
isDeviceCompact = isDeviceCompact
)
) {
val currentTheme = LocalTheme.current
val themeConfig = ThemeConfig(
darkMode = isNeedToEnableDarkMode(userSettings.darkMode.value),
dynamicColors = userSettings.enableDynamicColors.value,
selectedColorScheme = 0,
amoledDark = userSettings.enableAmoledDark.value,
enableBlur = userSettings.useBlur.value,
enableMultiline = userSettings.enableMultiline.value,
isDeviceCompact = isDeviceCompact
)
CompositionLocalProvider(LocalThemeConfig provides themeConfig) {
AppTheme(
useDarkTheme = currentTheme.usingDarkStyle,
useDynamicColors = currentTheme.usingDynamicColors,
selectedColorScheme = currentTheme.selectedColorScheme,
useAmoledBackground = currentTheme.usingAmoledBackground,
useDarkTheme = themeConfig.darkMode,
useDynamicColors = themeConfig.dynamicColors,
selectedColorScheme = themeConfig.selectedColorScheme,
useAmoledBackground = themeConfig.amoledDark,
) {
RootScreen(viewModel = viewModel)
}
@@ -220,7 +221,7 @@ class MainActivity : AppCompatActivity() {
private fun toggleLongPollService(
enable: Boolean,
inBackground: Boolean = SettingsController.isLongPollInBackgroundEnabled
inBackground: Boolean = AppSettings.Debug.longPollInBackground
) {
if (enable) {
val longPollIntent = Intent(this, LongPollingService::class.java)
@@ -246,7 +247,7 @@ class MainActivity : AppCompatActivity() {
private fun stopServices() {
toggleOnlineService(enable = false)
val asForeground = SettingsController.isLongPollInBackgroundEnabled
val asForeground = AppSettings.Debug.longPollInBackground
if (!asForeground) {
toggleLongPollService(enable = false)
@@ -28,7 +28,7 @@ import androidx.navigation.compose.rememberNavController
import com.meloda.app.fast.conversations.navigation.conversationsScreen
import com.meloda.app.fast.ui.theme.LocalBottomPadding
import com.meloda.app.fast.ui.theme.LocalHazeState
import com.meloda.app.fast.ui.theme.LocalTheme
import com.meloda.app.fast.ui.theme.LocalThemeConfig
import com.meloda.app.fast.friends.navigation.friendsScreen
import com.meloda.app.fast.model.BaseError
import com.meloda.app.fast.model.BottomNavigationItem
@@ -47,7 +47,7 @@ fun MainScreen(
onSettingsButtonClicked: () -> Unit = {},
onConversationItemClicked: (conversationId: Int) -> Unit = {}
) {
val currentTheme = LocalTheme.current
val currentTheme = LocalThemeConfig.current
val hazeState = remember { HazeState() }
val navController = rememberNavController()
@@ -60,7 +60,7 @@ fun MainScreen(
NavigationBar(
modifier = Modifier
.then(
if (currentTheme.usingBlur) {
if (currentTheme.enableBlur) {
Modifier.hazeChild(
state = hazeState,
style = HazeMaterials.thick()
@@ -69,7 +69,7 @@ fun MainScreen(
)
.fillMaxWidth(),
containerColor = NavigationBarDefaults.containerColor.copy(
alpha = if (currentTheme.usingBlur) 0f else 1f
alpha = if (currentTheme.enableBlur) 0f else 1f
)
) {
navigationItems.forEachIndexed { index, item ->
@@ -105,11 +105,11 @@ fun MainScreen(
Box(
modifier = Modifier
.fillMaxSize()
.padding(bottom = if (currentTheme.usingBlur) 0.dp else padding.calculateBottomPadding())
.padding(bottom = if (currentTheme.enableBlur) 0.dp else padding.calculateBottomPadding())
) {
CompositionLocalProvider(
LocalHazeState provides hazeState,
LocalBottomPadding provides if (currentTheme.usingBlur) padding.calculateBottomPadding() else 0.dp
LocalBottomPadding provides if (currentTheme.enableBlur) padding.calculateBottomPadding() else 0.dp
) {
NavHost(
navController = navController,
@@ -1,13 +1,13 @@
package com.meloda.app.fast.provider
import com.meloda.app.fast.common.ApiLanguage
import com.meloda.app.fast.common.model.ApiLanguage
import com.meloda.app.fast.common.provider.Provider
import com.meloda.app.fast.datastore.UserSettings
class ApiLanguageProvider(private val userSettings: UserSettings) : Provider<ApiLanguage> {
override fun provide(): ApiLanguage? {
val language = userSettings.language.value
val language = userSettings.appLanguage.value
return when {
language == "ru-RU" -> "ru"
@@ -3,7 +3,6 @@ package com.meloda.app.fast.service.longpolling
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
import android.os.Build
import android.os.IBinder
@@ -13,19 +12,18 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat
import com.conena.nanokt.android.app.stopForegroundCompat
import com.meloda.app.fast.common.AppConstants
import com.meloda.app.fast.common.LongPollController
import com.meloda.app.fast.common.UserConfig
import com.meloda.app.fast.common.VkConstants
import com.meloda.app.fast.common.extensions.listenValue
import com.meloda.app.fast.common.model.LongPollState
import com.meloda.app.fast.data.LongPollUpdatesParser
import com.meloda.app.fast.data.LongPollUseCase
import com.meloda.app.fast.data.processState
import com.meloda.app.fast.datastore.SettingsController
import com.meloda.app.fast.datastore.SettingsKeys
import com.meloda.app.fast.datastore.UserSettings
import com.meloda.app.fast.datastore.model.LongPollState
import com.meloda.app.fast.ui.R
import com.meloda.app.fast.datastore.AppSettings
import com.meloda.app.fast.model.api.data.LongPollUpdates
import com.meloda.app.fast.model.api.data.VkLongPollData
import com.meloda.app.fast.ui.R
import com.meloda.app.fast.util.NotificationsUtils
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
@@ -40,7 +38,7 @@ import kotlin.coroutines.suspendCoroutine
class LongPollingService : Service() {
private val userSettings: UserSettings by inject()
private val longPollController: LongPollController by inject()
private val job = SupervisorJob()
@@ -51,8 +49,8 @@ class LongPollingService : Service() {
throwable.printStackTrace()
}
userSettings.updateLongPollCurrentState(LongPollState.Exception)
userSettings.setLongPollStateToApply(LongPollState.Exception)
longPollController.updateCurrentState(LongPollState.Exception)
longPollController.setStateToApply(LongPollState.Exception)
}
private val coroutineContext: CoroutineContext
@@ -62,7 +60,6 @@ class LongPollingService : Service() {
private val longPollUseCase: LongPollUseCase by inject()
private val updatesParser: LongPollUpdatesParser by inject()
private val preferences: SharedPreferences by inject()
private var currentJob: Job? = null
@@ -79,10 +76,7 @@ class LongPollingService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (startId > 1) return START_STICKY
val inBackground = preferences.getBoolean(
SettingsKeys.KEY_FEATURES_LONG_POLL_IN_BACKGROUND,
SettingsKeys.DEFAULT_VALUE_FEATURES_LONG_POLL_IN_BACKGROUND
)
val inBackground = AppSettings.Debug.longPollInBackground
Log.d(
STATE_TAG,
@@ -114,7 +108,7 @@ class LongPollingService : Service() {
PendingIntent.FLAG_IMMUTABLE
)
userSettings.updateLongPollCurrentState(
longPollController.updateCurrentState(
if (inBackground) LongPollState.Background
else LongPollState.InApp
)
@@ -254,11 +248,9 @@ class LongPollingService : Service() {
override fun onDestroy() {
Log.d(STATE_TAG, "onDestroy")
userSettings.updateLongPollCurrentState(LongPollState.Stopped)
longPollController.updateCurrentState(LongPollState.Stopped)
try {
SettingsController.edit {
putBoolean(KEY_LONG_POLL_WAS_DESTROYED, true)
}
AppSettings.edit { putBoolean(KEY_LONG_POLL_WAS_DESTROYED, true) }
job.cancel()
} catch (e: Exception) {
e.printStackTrace()
@@ -268,6 +260,7 @@ class LongPollingService : Service() {
override fun onLowMemory() {
Log.d(STATE_TAG, "onLowMemory")
longPollController.updateCurrentState(LongPollState.Stopped)
super.onLowMemory()
}