Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 상대전적 세부 화면으로 갈 때 recomposition이 불필요하게 일어나는 문제 해결 #84

Open
wants to merge 2 commits into
base: android
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,7 @@ dependencies {

// coil
implementation("io.coil-kt:coil-compose:2.6.0")

// Gson
implementation("com.google.code.gson:gson:2.8.6")
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ fun MainScreen(navigator: NavHostController = rememberNavController()) {
)

detailRelativeMatchNavGraph(
navController = navigator,
onBackPressedClick = { navigator.popBackStack() }
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,10 @@ fun DetailRelativeMatchScreen(
onSelectionChanged: (MatchTypeUiModel) -> Unit = {},
selectedMatchType: MatchTypeUiModel
) {
val title =
if (matchesUiModels.isNotEmpty()) matchesUiModels[0].value[0].opponentName else "뒤로가기"

Scaffold(
topBar = {
FossTopBar(
title = title,
title = matchesUiModels[0].value[0].opponentName,
onBackPressedClick = onBackPressedClick,
onRefreshClick = onRefreshClick
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,54 @@ package com.foss.foss.feature.matchsearching.relativematch.detail.navigation
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptions
import androidx.navigation.NavType
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.foss.foss.feature.matchsearching.relativematch.detail.DetailRelativeMatchRoute
import com.foss.foss.model.MatchesUiModel
import com.foss.foss.util.LocalDateAdapter
import com.foss.foss.util.LocalDateTimeAdapter
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import java.lang.reflect.Type
import java.time.LocalDate
import java.time.LocalDateTime

const val DETAILRELATIVEMATCH_ROUTE = "DETAILRELATIVEMATCH"

fun NavController.navigateToDetailRelativeMatch(
matchesUiModels: List<MatchesUiModel>,
navOptions: NavOptions? = null
) {
currentBackStackEntry?.savedStateHandle?.set("relativeMatch", matchesUiModels)
navigate(DETAILRELATIVEMATCH_ROUTE, navOptions)
val matchesUiModelsJson = buildGson().toJson(matchesUiModels)

navigate("$DETAILRELATIVEMATCH_ROUTE/$matchesUiModelsJson", navOptions)
}

fun NavGraphBuilder.detailRelativeMatchNavGraph(
navController: NavController,
onBackPressedClick: () -> Unit
) {
composable(route = DETAILRELATIVEMATCH_ROUTE) {
val data =
navController.previousBackStackEntry?.savedStateHandle?.get<List<MatchesUiModel>>(
"relativeMatch"
composable(
route = "$DETAILRELATIVEMATCH_ROUTE/{matchesUiModels}",
arguments = listOf(
navArgument("matchesUiModels") { type = NavType.StringType }
)
) { backStackEntry ->

backStackEntry?.arguments?.getString("matchesUiModels")?.let { json ->
val type: Type = object : TypeToken<List<MatchesUiModel>>() {}.type
val matchesUiModels = buildGson().fromJson<List<MatchesUiModel>>(json, type)

DetailRelativeMatchRoute(
matchesUiModels = matchesUiModels,
onBackPressedClick = onBackPressedClick
)
if (data != null) {
DetailRelativeMatchRoute(matchesUiModels = data, onBackPressedClick = onBackPressedClick)
} else {
DetailRelativeMatchRoute(matchesUiModels = emptyList(), onBackPressedClick = onBackPressedClick)
}
}
}

fun buildGson(): Gson = GsonBuilder()
.registerTypeAdapter(LocalDate::class.java, LocalDateAdapter())
.registerTypeAdapter(LocalDateTime::class.java, LocalDateTimeAdapter())
.create()
29 changes: 29 additions & 0 deletions android/app/src/main/java/com/foss/foss/util/LocalDateAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.foss.foss.util

import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonPrimitive
import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer
import java.lang.reflect.Type
import java.time.LocalDate
import java.time.format.DateTimeFormatter

class LocalDateAdapter : JsonSerializer<LocalDate>, JsonDeserializer<LocalDate> {
override fun serialize(
src: LocalDate?,
typeOfSrc: Type?,
context: JsonSerializationContext?
): JsonElement {
return JsonPrimitive(src?.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
}

override fun deserialize(
json: JsonElement?,
typeOfT: Type?,
context: JsonDeserializationContext?
): LocalDate {
return LocalDate.parse(json?.asString, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.foss.foss.util

import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonPrimitive
import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer
import java.lang.reflect.Type
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class LocalDateTimeAdapter : JsonSerializer<LocalDateTime>, JsonDeserializer<LocalDateTime> {

override fun serialize(
src: LocalDateTime?,
typeOfSrc: Type?,
context: JsonSerializationContext?
): JsonElement {
return JsonPrimitive(src?.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm")))
}

override fun deserialize(
json: JsonElement?,
typeOfT: Type?,
context: JsonDeserializationContext?
): LocalDateTime {
return LocalDateTime.parse(json?.asString, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm"))
}
}