Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ object NatConstants {
const val TERMS_OF_USE_URL: String = "https://nativeapptemplate.com/terms"

const val MINIMUM_PASSWORD_LENGTH: Int = 8
const val MAXIMUM_ITEM_TAG_NAME_LENGTH: Int = 100
const val MAXIMUM_ITEM_TAG_DESCRIPTION_LENGTH: Int = 1_000

const val PLACEHOLDER_FULLNAME: String = "John Smith"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,4 @@ interface LoginRepository {
fun isShopDeleted(): Flow<Boolean>

fun didShowTapShopBelowTip(): Flow<Boolean>

fun getMaximumNameLength(): Flow<Int>
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,4 @@ class LoginRepositoryImpl @Inject constructor(
override fun isShopDeleted(): Flow<Boolean> = natPreferencesDataSource.isShopDeleted()

override fun didShowTapShopBelowTip(): Flow<Boolean> = natPreferencesDataSource.didShowTapShopBelowTip()

override fun getMaximumNameLength(): Flow<Int> = natPreferencesDataSource.getMaximumNameLength()
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class NatPreferencesDataSource @Inject constructor(
androidAppVersion = it.androidAppVersion,
shouldUpdatePrivacy = it.shouldUpdatePrivacy,
shouldUpdateTerms = it.shouldUpdateTerms,
maximumNameLength = it.maximumNameLength,
shopLimitCount = it.shopLimitCount,

isEmailUpdated = it.isEmailUpdated,
Expand Down Expand Up @@ -109,7 +108,6 @@ class NatPreferencesDataSource @Inject constructor(

this.shouldUpdatePrivacy = permissions.getShouldUpdatePrivacy()!!
this.shouldUpdateTerms = permissions.getShouldUpdateTerms()!!
this.maximumNameLength = permissions.getMaximumNameLength()!!
this.shopLimitCount = permissions.getShopLimitCount()!!
}
}
Expand Down Expand Up @@ -244,9 +242,4 @@ class NatPreferencesDataSource @Inject constructor(
.map { data ->
data.didShowTapShopBelowTip
}

fun getMaximumNameLength(): Flow<Int> = userPreferences.data
.map { data ->
data.maximumNameLength
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,34 +262,6 @@ private fun SettingsContentView(
)
HorizontalDivider()
}
item {
ListItem(
headlineContent = {
Text(
stringResource(R.string.how_to_use),
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.onPrimaryContainer,
)
},
leadingContent = {
Icon(
Icons.Outlined.Info,
contentDescription = stringResource(R.string.how_to_use),
tint = MaterialTheme.colorScheme.onSurfaceVariant,
)
},
modifier = Modifier
.clickable {
context.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse(NatConstants.HOW_TO_USE_URL),
),
)
},
)
HorizontalDivider()
}
item {
ListItem(
headlineContent = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import androidx.navigation.toRoute
import com.nativeapptemplate.nativeapptemplatefree.NatConstants
import com.nativeapptemplate.nativeapptemplatefree.common.errors.codedDescription
import com.nativeapptemplate.nativeapptemplatefree.data.item_tag.ItemTagRepository
import com.nativeapptemplate.nativeapptemplatefree.data.login.LoginRepository
import com.nativeapptemplate.nativeapptemplatefree.model.ItemTag
import com.nativeapptemplate.nativeapptemplatefree.model.ItemTagBody
import com.nativeapptemplate.nativeapptemplatefree.model.ItemTagBodyDetail
Expand All @@ -18,7 +17,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -28,7 +26,7 @@ data class ItemTagEditUiState(

val name: String = "",
val description: String = "",
val maximumNameLength: Int = -1,
val maximumNameLength: Int = NatConstants.MAXIMUM_ITEM_TAG_NAME_LENGTH,
val isUpdated: Boolean = false,

val isLoading: Boolean = true,
Expand All @@ -39,7 +37,6 @@ data class ItemTagEditUiState(
@HiltViewModel
class ItemTagEditViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val loginRepository: LoginRepository,
private val itemTagRepository: ItemTagRepository,
) : ViewModel() {
private val itemTagId = savedStateHandle.toRoute<ItemTagEditRoute>().id
Expand All @@ -62,29 +59,28 @@ class ItemTagEditViewModel @Inject constructor(

viewModelScope.launch {
val itemTagFlow: Flow<ItemTag> = itemTagRepository.getItemTag(itemTagId)
val maximumNameLengthFlow = loginRepository.getMaximumNameLength()

combine(itemTagFlow, maximumNameLengthFlow) { itemTag, maximumNameLength ->
_uiState.update {
it.copy(
itemTag = itemTag,
name = itemTag.getName(),
description = itemTag.getDescription(),
maximumNameLength = maximumNameLength,
success = true,
isLoading = false,
)

itemTagFlow
.catch { exception ->
val message = exception.codedDescription
_uiState.update {
it.copy(
message = message,
isLoading = false,
)
}
}
}.catch { exception ->
val message = exception.codedDescription
_uiState.update {
it.copy(
message = message,
isLoading = false,
)
.collect { itemTag ->
_uiState.update {
it.copy(
itemTag = itemTag,
name = itemTag.getName(),
description = itemTag.getDescription(),
success = true,
isLoading = false,
)
}
}
}.collect {
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import androidx.navigation.toRoute
import com.nativeapptemplate.nativeapptemplatefree.NatConstants
import com.nativeapptemplate.nativeapptemplatefree.common.errors.codedDescription
import com.nativeapptemplate.nativeapptemplatefree.data.item_tag.ItemTagRepository
import com.nativeapptemplate.nativeapptemplatefree.data.login.LoginRepository
import com.nativeapptemplate.nativeapptemplatefree.model.ItemTag
import com.nativeapptemplate.nativeapptemplatefree.model.ItemTagBody
import com.nativeapptemplate.nativeapptemplatefree.model.ItemTagBodyDetail
Expand All @@ -25,18 +24,17 @@ import javax.inject.Inject
data class ItemTagCreateUiState(
val name: String = "",
val description: String = "",
val maximumNameLength: Int = -1,
val maximumNameLength: Int = NatConstants.MAXIMUM_ITEM_TAG_NAME_LENGTH,
val isCreated: Boolean = false,

val isLoading: Boolean = true,
val success: Boolean = false,
val isLoading: Boolean = false,
val success: Boolean = true,
val message: String = "",
)

@HiltViewModel
class ItemTagCreateViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val loginRepository: LoginRepository,
private val itemTagRepository: ItemTagRepository,
) : ViewModel() {
private val shopId = savedStateHandle.toRoute<ItemTagCreateRoute>().shopId
Expand All @@ -45,40 +43,8 @@ class ItemTagCreateViewModel @Inject constructor(
val uiState: StateFlow<ItemTagCreateUiState> = _uiState.asStateFlow()

fun reload() {
fetchData()
}

private fun fetchData() {
_uiState.update {
it.copy(
isLoading = true,
success = false,
isCreated = false,
)
}

viewModelScope.launch {
val maximumNameLengthFlow = loginRepository.getMaximumNameLength()

maximumNameLengthFlow
.catch { exception ->
val message = exception.codedDescription
_uiState.update {
it.copy(
message = message,
isLoading = false,
)
}
}
.collect { maximumNameLength ->
_uiState.update {
it.copy(
maximumNameLength = maximumNameLength,
success = true,
isLoading = false,
)
}
}
ItemTagCreateUiState()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ class DemoLoginRepository @Inject constructor(

override fun didShowTapShopBelowTip(): Flow<Boolean> = MutableStateFlow(true)

override fun getMaximumNameLength(): Flow<Int> = MutableStateFlow(100)

@OptIn(ExperimentalSerializationApi::class)
private suspend inline fun <reified T> getDataFromJsonFile(fileName: String): T =
withContext(ioDispatcher) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.map

val emptyUserData = UserData()

Expand Down Expand Up @@ -134,8 +133,6 @@ class TestLoginRepository : LoginRepository {

override fun didShowTapShopBelowTip(): Flow<Boolean> = MutableStateFlow(true)

override fun getMaximumNameLength(): Flow<Int> = userData.map { it.maximumNameLength }

/**
* A test-only API.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package com.nativeapptemplate.nativeapptemplatefree.ui.shop_settings.item_tag_de

import androidx.lifecycle.SavedStateHandle
import androidx.navigation.testing.invoke
import com.nativeapptemplate.nativeapptemplatefree.NatConstants
import com.nativeapptemplate.nativeapptemplatefree.model.Attributes
import com.nativeapptemplate.nativeapptemplatefree.model.Data
import com.nativeapptemplate.nativeapptemplatefree.model.ItemTag
import com.nativeapptemplate.nativeapptemplatefree.testing.repository.TestItemTagRepository
import com.nativeapptemplate.nativeapptemplatefree.testing.repository.TestLoginRepository
import com.nativeapptemplate.nativeapptemplatefree.testing.repository.emptyUserData
import com.nativeapptemplate.nativeapptemplatefree.testing.util.MainDispatcherRule
import com.nativeapptemplate.nativeapptemplatefree.ui.shop_settings.navigation.ItemTagEditRoute
import kotlinx.coroutines.flow.collect
Expand Down Expand Up @@ -36,7 +35,6 @@ class ItemTagEditViewModelTest {
@get:Rule
val dispatcherRule = MainDispatcherRule()

private val loginRepository = TestLoginRepository()
private val itemTagRepository = TestItemTagRepository()

private lateinit var viewModel: ItemTagEditViewModel
Expand All @@ -47,7 +45,6 @@ class ItemTagEditViewModelTest {
savedStateHandle = SavedStateHandle(
route = ItemTagEditRoute(id = testInputItemTag.datum!!.id!!),
),
loginRepository = loginRepository,
itemTagRepository = itemTagRepository,
)
}
Expand All @@ -57,11 +54,15 @@ class ItemTagEditViewModelTest {
assertTrue(viewModel.uiState.value.isLoading)
}

@Test
fun maximumNameLength_matchesConstant() = runTest {
assertEquals(NatConstants.MAXIMUM_ITEM_TAG_NAME_LENGTH, viewModel.uiState.value.maximumNameLength)
}

@Test
fun stateItemTag_whenSuccess_matchesItemTagFromRepository() = runTest {
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }

loginRepository.sendUserData(emptyUserData.copy(maximumNameLength = 100))
itemTagRepository.sendItemTag((testInputItemTag))

viewModel.reload()
Expand All @@ -78,12 +79,6 @@ class ItemTagEditViewModelTest {
fun stateIsUpdated_whenUpdatingItemTag_becomesTrue() = runTest {
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }

val maximumNameLength = 100
val userData = emptyUserData.copy(
maximumNameLength = maximumNameLength,
)

loginRepository.sendUserData(userData)
itemTagRepository.sendItemTag(testInputItemTag)

viewModel.reload()
Expand All @@ -102,7 +97,6 @@ class ItemTagEditViewModelTest {
fun unchangedNameAndDescription_isInvalid() = runTest {
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }

loginRepository.sendUserData(emptyUserData.copy(maximumNameLength = 100))
itemTagRepository.sendItemTag(testInputItemTag)
viewModel.reload()

Expand All @@ -114,7 +108,6 @@ class ItemTagEditViewModelTest {
fun changedDescriptionOnly_isValid() = runTest {
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }

loginRepository.sendUserData(emptyUserData.copy(maximumNameLength = 100))
itemTagRepository.sendItemTag(testInputItemTag)
viewModel.reload()

Expand All @@ -127,7 +120,6 @@ class ItemTagEditViewModelTest {
fun blankName_isInvalid() = runTest {
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }

loginRepository.sendUserData(emptyUserData.copy(maximumNameLength = 100))
itemTagRepository.sendItemTag(testInputItemTag)
viewModel.reload()

Expand All @@ -141,7 +133,6 @@ class ItemTagEditViewModelTest {
fun nameWithSymbolsAndUnicode_isValid() = runTest {
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }

loginRepository.sendUserData(emptyUserData.copy(maximumNameLength = 100))
itemTagRepository.sendItemTag(testInputItemTag)
viewModel.reload()

Expand Down
Loading
Loading