Refactor: Introduce MainViewModel for onboarding state management

This commit introduces a `MainViewModel` to manage the UI state for the onboarding flow. This change centralizes the onboarding state (including Bluetooth status, location status, error messages, and loading indicators) within the ViewModel, allowing it to survive configuration changes and simplifying state management within `MainActivity`.

Key changes:
- Created `MainViewModel.kt` to hold and manage onboarding-related UI state.
- Moved onboarding state variables (e.g., `onboardingState`, `bluetoothStatus`, `locationStatus`) from `MainActivity` to `MainViewModel`.
- Updated `MainActivity` to observe and update onboarding state through the `MainViewModel`.
- Created `OnboardingState.kt` to define the possible states of the onboarding process.
- Ensured that the onboarding process is not restarted on configuration changes by checking `mainViewModel.onboardingState` before initiating.
This commit is contained in:
Mohamad Hamade
2025-07-15 02:24:41 +03:00
parent 013f0c00cf
commit 245181d736
3 changed files with 153 additions and 101 deletions
@@ -0,0 +1,52 @@
package com.bitchat.android
import androidx.compose.runtime.*
import androidx.lifecycle.ViewModel
import com.bitchat.android.onboarding.BluetoothStatus
import com.bitchat.android.onboarding.LocationStatus
class MainViewModel : ViewModel() {
private var _onboardingState by mutableStateOf(OnboardingState.CHECKING)
val onboardingState: OnboardingState get() = _onboardingState
private var _bluetoothStatus by mutableStateOf(BluetoothStatus.ENABLED)
val bluetoothStatus: BluetoothStatus get() = _bluetoothStatus
private var _locationStatus by mutableStateOf(LocationStatus.ENABLED)
val locationStatus: LocationStatus get() = _locationStatus
private var _errorMessage by mutableStateOf("")
val errorMessage: String get() = _errorMessage
private var _isBluetoothLoading by mutableStateOf(false)
val isBluetoothLoading: Boolean get() = _isBluetoothLoading
private var _isLocationLoading by mutableStateOf(false)
val isLocationLoading: Boolean get() = _isLocationLoading
// Public update functions for MainActivity
fun updateOnboardingState(state: OnboardingState) {
_onboardingState = state
}
fun updateBluetoothStatus(status: BluetoothStatus) {
_bluetoothStatus = status
}
fun updateLocationStatus(status: LocationStatus) {
_locationStatus = status
}
fun updateErrorMessage(message: String) {
_errorMessage = message
}
fun updateBluetoothLoading(loading: Boolean) {
_isBluetoothLoading = loading
}
fun updateLocationLoading(loading: Boolean) {
_isLocationLoading = loading
}
}