Skip to content

Commit

Permalink
Add SettingViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Aug 1, 2024
1 parent 77d5a2f commit 881d848
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import dev.sanmer.github.artifacts.Const
import dev.sanmer.github.artifacts.R
Expand All @@ -29,9 +30,11 @@ import dev.sanmer.github.artifacts.ui.ktx.navigateSingleTopTo
import dev.sanmer.github.artifacts.ui.main.Screen
import dev.sanmer.github.artifacts.ui.screen.setting.component.SettingIcon
import dev.sanmer.github.artifacts.ui.screen.setting.component.SettingItem
import dev.sanmer.github.artifacts.viewmodel.SettingViewModel

@Composable
fun SettingScreen(
viewModel: SettingViewModel = hiltViewModel(),
navController: NavController
) {
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
Expand Down Expand Up @@ -82,7 +85,8 @@ fun SettingScreen(
},
title = stringResource(id = R.string.settings_repo_title),
text = stringResource(id = R.string.settings_repo_desc),
onClick = { navController.navigateSingleTopTo(Screen.Repo()) }
onClick = { navController.navigateSingleTopTo(Screen.Repo()) },
enabled = viewModel.hasToken
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ fun SettingItem(
icon: @Composable () -> Unit,
title: String,
text: String,
onClick: () -> Unit
onClick: () -> Unit,
enabled: Boolean = true
) = Row(
modifier = Modifier
.fillMaxWidth()
Expand All @@ -37,7 +38,10 @@ fun SettingItem(
border = CardDefaults.outlinedCardBorder(),
shape = MaterialTheme.shapes.large
)
.clickable(onClick = onClick)
.clickable(
enabled = enabled,
onClick = onClick
)
.padding(all = 20.dp),
horizontalArrangement = Arrangement.spacedBy(20.dp),
verticalAlignment = Alignment.CenterVertically
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.sanmer.github.artifacts.viewmodel

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dev.sanmer.github.artifacts.repository.DbRepository
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
class SettingViewModel @Inject constructor(
private val dbRepository: DbRepository
) : ViewModel() {
var hasToken by mutableStateOf(false)
private set

init {
Timber.d("SettingViewModel init")
tokenObserver()
}

private fun tokenObserver() {
viewModelScope.launch {
dbRepository.tokenFlow
.collect { tokens ->
hasToken = tokens.isNotEmpty()
}
}
}
}

0 comments on commit 881d848

Please sign in to comment.