Skip to content

Commit

Permalink
Add ViewModel and basic setup for FlakerACtivity
Browse files Browse the repository at this point in the history
  • Loading branch information
RotBolt committed Jul 10, 2023
1 parent c9b47cd commit bd57c71
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 5 deletions.
45 changes: 45 additions & 0 deletions .idea/appInsightsSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions flaker-android-retrofit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {

implementation(project(":flaker-android-ui"))
implementation(project(":flaker-retrofit-core"))
implementation(project(":flaker-domain"))

implementation(libs.core.ktx)
implementation(libs.lifecycle.runtime.ktx)
Expand Down
2 changes: 1 addition & 1 deletion flaker-android-retrofit/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:supportsRtl="true"
tools:targetApi="33">
<activity
android:name="io.rotlabs.flakerandroidretrofit.MainActivity"
android:name="io.rotlabs.flakerandroidretrofit.FlakerActivity"
android:exported="true"
android:icon="@mipmap/ic_flaker"
android:label="@string/companion_app_name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,35 @@ package io.rotlabs.flakerandroidretrofit
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import io.rotlabs.flakerandroidapp.ui.listitem.NetworkRequestItemPreview
import io.rotlabs.flakerandroidapp.ui.theme.FlakerAndroidTheme

class MainActivity : ComponentActivity() {
class FlakerActivity : ComponentActivity() {

private val viewModel: FlakerViewModel by viewModels { FlakerViewModel.Factory }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
val state by viewModel.viewStateFlow.collectAsState()

FlakerAndroidTheme {
// A surface container using the 'background' color from the theme
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
NetworkRequestItemPreview()
LazyColumn {
items(state.networkRequests, key = null) {
// Render Items
}
}
}
}
}
Expand Down
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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ fun getStatusCodeColor(statusCode: Long): Color {
}
}

@Suppress("UnusedPrivateMember")
@Composable
@Preview
fun NetworkRequestItemPreview() {
private fun NetworkRequestItemPreview() {
FlakerAndroidTheme {
Column {
repeat(4) {
Expand Down

0 comments on commit bd57c71

Please sign in to comment.