Skip to content

Commit

Permalink
Refactor to single activity (#24)
Browse files Browse the repository at this point in the history
* feat: implement Screen

* chore: remove unnecessary logic

* chore: remove default parameter

* chore: reformat

* feat: implement GrowTreeApp

* chore: enable to start at MainActivity

* chore: update FinalCharacter navigation Logic

* chore: update HomeScreen navigation Logic

* chore: delete other activities

* chore: delete UiModels.kt

* chore: update FinalCharacter Screen

* chore: remove navigateToFinalCharacter SharedFlow

* chore: update navigate function

* chore: enable EdgeToEdge

* chore: add arguments to home

* chore: functionalize

* chore: handle onBackPressed in home
  • Loading branch information
giovannijunseokim authored Apr 16, 2024
1 parent ddfbc49 commit c511d6b
Show file tree
Hide file tree
Showing 19 changed files with 272 additions and 345 deletions.
5 changes: 1 addition & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".feature.login.LoginActivity"
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<activity android:name=".feature.home.HomeActivity" />
<activity android:name=".feature.character.growfinal.FinalCharacterActivity" />
</application>

</manifest>
54 changes: 32 additions & 22 deletions app/src/main/java/univ/earthbreaker/namu/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,51 @@ package univ.earthbreaker.namu

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import univ.earthbreaker.namu.feature.home.HomeViewModel
import univ.earthbreaker.namu.feature.navigation.GrowTreeApp
import univ.earthbreaker.namu.ui.theme.GTTheme
import univ.earthbreaker.namu.util.ContentUriRequestBody
import univ.earthbreaker.namu.util.extension.getBase64FromUri

class MainActivity : ComponentActivity() {
private val homeViewModel: HomeViewModel by viewModels()
private val pickMedia =
registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
uri?.let {
homeViewModel.setMissionImage(
imageUri = uri,
base64 = getBase64FromUri(uri = uri),
contentUriRequestBody = ContentUriRequestBody(this, uri),
)
}
}

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

makeStatusBarTransparent()
setContent {
GTTheme {
GrowTreeApp()
GrowTreeApp(onClickCameraIcon = {
pickMedia.launch(
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly),
)
})
}
}
}
}

@Composable
fun Greeting(
name: String,
modifier: Modifier = Modifier,
) {
Text(
text = "Hello $name!",
modifier = modifier,
)
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
GTTheme {
Greeting("Android")
private fun makeStatusBarTransparent() {
enableEdgeToEdge(
statusBarStyle = SystemBarStyle.dark(
android.graphics.Color.TRANSPARENT,
),
)
}
}
23 changes: 0 additions & 23 deletions app/src/main/java/univ/earthbreaker/namu/UiModels.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data class GrowCharacterEndToFinalResponse(
fun toEntity(): FinalCharacterInfo = FinalCharacterInfo(
backgroundImageURL = backgroundImageUrl,
characterImageURL = mainImageUrl,
characterNo = characterNo,
messages = script.split("\n"),
characterNo = characterNo,
)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import univ.earthbreaker.namu.feature.character.growfinal.model.FinalCharacterAc
import univ.earthbreaker.namu.feature.character.growfinal.model.FinalCharacterUiState

@Composable
fun FinalCharacterRoute() {
fun FinalCharacterRoute(navigateToHome: (characterNo: Int) -> Unit) {
val viewModel: FinalCharacterViewModel = viewModel()

// State observing and declarations
Expand All @@ -23,6 +23,7 @@ fun FinalCharacterRoute() {
state = uiState as FinalCharacterUiState.Success,
actions = rememberFinalCharacterActions(
viewModel = viewModel,
navigateToHome = navigateToHome,
),
)
}
Expand All @@ -31,9 +32,11 @@ fun FinalCharacterRoute() {
@Composable
fun rememberFinalCharacterActions(
viewModel: FinalCharacterViewModel,
navigateToHome: (characterNo: Int) -> Unit,
): FinalCharacterActions {
return remember(viewModel) {
FinalCharacterActions(
navigateToHome = navigateToHome,
moveToNextMessage = viewModel::moveToNextMessage,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fun FinalCharacterScreen(state: FinalCharacterUiState.Success, actions: FinalCha
.padding(horizontal = 20.dp)
.background(color = GTTheme.colors.green3, shape = RoundedCornerShape(15.dp))
.padding(all = 20.dp)
.noRippleClickable(onClick = actions.moveToNextMessage),
.noRippleClickable(onClick = { actions.moveToNextMessage(actions.navigateToHome) }),
style = GTTheme.typography.titleSemiBold16,
textAlign = TextAlign.Center,
)
Expand All @@ -56,13 +56,13 @@ private fun FinaCharacterScreenPreview() {
FinalCharacterScreen(
state = FinalCharacterUiState.Success(
finalCharacterInfo = FinalCharacterInfo(
backgroundImageURL = "https://namu-bucket.s3.ap-northeast-2.amazonaws.com/default-character/admin_ddf95620-b7b3-482c-836c-aaf0251e28f8_sinensis-bg.png",
characterImageURL = "https://namu-bucket.s3.ap-northeast-2.amazonaws.com/default-character/admin_d6bc4178-b6f5-4bba-b4b2-91aec66e2938_sinensis.png",
backgroundImageURL = "",
characterImageURL = "",
messages = listOf("안녕 ! 난 강인한 생명력을 가진 팽나무야"),
characterNo = 0,
),
messageIndex = 0,
),
actions = rememberFinalCharacterActions(viewModel = viewModel()),
actions = rememberFinalCharacterActions(viewModel = viewModel(), navigateToHome = {}),
)
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
package univ.earthbreaker.namu.feature.character.growfinal

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import univ.earthbreaker.namu.ToFinalCharacterUiModel
import univ.earthbreaker.namu.domain.entity.grow.FinalCharacterInfo
import univ.earthbreaker.namu.feature.character.growfinal.model.FinalCharacterUiState
import univ.earthbreaker.namu.feature.home.HomeViewModel.Companion.MESSAGES_STRING_SEPARATOR
import univ.earthbreaker.namu.feature.navigation.Screen.FinalCharacter.NAV_ARGUMENT_BACKGROUND_IMAGE_URL_KEY
import univ.earthbreaker.namu.feature.navigation.Screen.FinalCharacter.NAV_ARGUMENT_CHARACTER_IMAGE_URL_KEY
import univ.earthbreaker.namu.feature.navigation.Screen.FinalCharacter.NAV_ARGUMENT_CHARACTER_NO_KEY
import univ.earthbreaker.namu.feature.navigation.Screen.FinalCharacter.NAV_ARGUMENT_MESSAGES_KEY

class FinalCharacterViewModel : ViewModel() {
class FinalCharacterViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
private val _finalCharacterUiStateFlow: MutableStateFlow<FinalCharacterUiState> =
MutableStateFlow(FinalCharacterUiState.Loading)
val finalCharacterUiStateFlow: StateFlow<FinalCharacterUiState> =
_finalCharacterUiStateFlow.asStateFlow()
private var messageIndex: Int = 0
private lateinit var backgroundImageURL: String
private lateinit var characterImageURL: String
private lateinit var messages: List<String>
private var characterNo: Int? = null
private val _navigateToHome: MutableSharedFlow<Unit> = MutableSharedFlow()
val navigateToHome: SharedFlow<Unit> = _navigateToHome.asSharedFlow()
private val backgroundImageURL: String =
checkNotNull(savedStateHandle[NAV_ARGUMENT_BACKGROUND_IMAGE_URL_KEY])
private val characterImageURL: String =
checkNotNull(savedStateHandle[NAV_ARGUMENT_CHARACTER_IMAGE_URL_KEY])
private val messages: List<String> =
checkNotNull(savedStateHandle[NAV_ARGUMENT_MESSAGES_KEY]).let {
(it as String).removeSurrounding("[", "]").replace(
oldValue = "QuestionMark",
newValue = "?",
).split(MESSAGES_STRING_SEPARATOR)
}
private val characterNo: Int =
checkNotNull(savedStateHandle[NAV_ARGUMENT_CHARACTER_NO_KEY])

init {
updateUiState()
}

private fun updateUiState() {
viewModelScope.launch {
Expand All @@ -33,35 +46,21 @@ class FinalCharacterViewModel : ViewModel() {
finalCharacterInfo = FinalCharacterInfo(
backgroundImageURL = backgroundImageURL,
characterImageURL = characterImageURL,
messages = messages,
characterNo = characterNo ?: throw NullPointerException(),
messages = messages.toList(),
characterNo = characterNo,
),
messageIndex = messageIndex,
),
)
}
}

fun setFinalCharacterInfo(toFinalCharacterUiModel: ToFinalCharacterUiModel) {
backgroundImageURL = toFinalCharacterUiModel.backgroundImageURL
characterImageURL = toFinalCharacterUiModel.characterImageURL
messages = toFinalCharacterUiModel.messages
characterNo = toFinalCharacterUiModel.characterNo
updateUiState()
}

fun moveToNextMessage() {
fun moveToNextMessage(navigateToHome: (characterNo: Int) -> Unit) {
if (messageIndex < messages.size - 1) {
messageIndex++
updateUiState()
} else {
navigateToHome()
}
}

private fun navigateToHome() {
viewModelScope.launch {
_navigateToHome.emit(Unit)
navigateToHome(characterNo)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package univ.earthbreaker.namu.feature.character.growfinal.model

data class FinalCharacterActions(
val moveToNextMessage: () -> Unit,
val navigateToHome: (characterNo: Int) -> Unit,
val moveToNextMessage: (navigateToHome: (characterNo: Int) -> Unit) -> Unit,
)
Loading

0 comments on commit c511d6b

Please sign in to comment.