-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ViewModel and basic setup for FlakerACtivity
- Loading branch information
Showing
7 changed files
with
109 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
flaker-android-retrofit/src/main/java/io/rotlabs/flakerandroidretrofit/FlakerViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.rotlabs.flakerandroidretrofit | ||
|
||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY | ||
import androidx.lifecycle.createSavedStateHandle | ||
import androidx.lifecycle.viewmodel.initializer | ||
import androidx.lifecycle.viewmodel.viewModelFactory | ||
import io.rotlabs.flakedomain.networkrequest.NetworkRequest | ||
import io.rotlabs.flakerretrofit.data.FlakerRepo | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
|
||
class FlakerViewModel( | ||
private val flakerRepo: FlakerRepo, | ||
private val savedStateHandle: SavedStateHandle | ||
) : ViewModel() { | ||
|
||
private val _viewStateFlow = MutableStateFlow<ViewState>(ViewState()) | ||
val viewStateFlow = _viewStateFlow.asStateFlow() | ||
|
||
data class ViewState( | ||
val isFlakerOn: Boolean = false, | ||
val networkRequests: List<NetworkRequest> = emptyList() | ||
) | ||
|
||
suspend fun loadAllRequests() { | ||
val list = flakerRepo.allRequests() | ||
_viewStateFlow.emit(_viewStateFlow.value.copy(networkRequests = list)) | ||
} | ||
|
||
companion object { | ||
val Factory: ViewModelProvider.Factory = viewModelFactory { | ||
initializer { | ||
val savedStateHandle = createSavedStateHandle() | ||
val flakerRepo = FlakerRepo(checkNotNull(this[APPLICATION_KEY])) | ||
FlakerViewModel(flakerRepo, savedStateHandle) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters