Skip to content

Commit

Permalink
feature/Boolti-347 브릿지에 스낵바 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
mangbaam committed Jan 1, 2025
1 parent c505023 commit 6c4fa08
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -36,6 +37,7 @@ import com.nexters.boolti.presentation.component.BTDialog
import com.nexters.boolti.presentation.component.BtBackAppBar
import com.nexters.boolti.presentation.component.BtCircularProgressIndicator
import com.nexters.boolti.presentation.component.BtWebView
import com.nexters.boolti.presentation.screen.LocalSnackbarController
import com.nexters.boolti.presentation.util.bridge.BridgeCallbackHandler
import com.nexters.boolti.presentation.util.bridge.BridgeManager
import com.nexters.boolti.presentation.util.bridge.NavigateOption
Expand Down Expand Up @@ -68,6 +70,8 @@ fun ShowRegistrationScreen(
var webviewProgress by remember { mutableIntStateOf(0) }
val loading by remember { derivedStateOf { webviewProgress < 100 } }

val snackbarHostState = LocalSnackbarController.current

LaunchedEffect(webView != null) {
webView?.setBridgeManager(
BridgeManager(
Expand All @@ -87,6 +91,10 @@ fun ShowRegistrationScreen(
}
}
}

override fun showSnackbar(message: String, duration: SnackbarDuration) {
snackbarHostState.showMessage(message = message, duration = duration)
}
},
scope = scope,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nexters.boolti.presentation.util

import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHostState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
Expand All @@ -12,10 +13,11 @@ class SnackbarController(
fun showMessage(
message: String,
dismissPrevious: Boolean = true,
duration: SnackbarDuration = SnackbarDuration.Short,
) {
coroutineScope.launch {
if (dismissPrevious) snackbarHostState.currentSnackbarData?.dismiss()
snackbarHostState.showSnackbar(message)
snackbarHostState.showSnackbar(message, duration = duration)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nexters.boolti.presentation.util.bridge

import androidx.compose.material3.SnackbarDuration
import kotlinx.serialization.Serializable

/**
Expand All @@ -10,6 +11,7 @@ import kotlinx.serialization.Serializable
interface BridgeCallbackHandler {
suspend fun fetchToken(): TokenDto
fun navigateTo(route: String, navigateOption: NavigateOption = NavigateOption.PUSH)
fun showSnackbar(message: String, duration: SnackbarDuration = SnackbarDuration.Short)
}

enum class NavigateOption {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nexters.boolti.presentation.util.bridge

import androidx.compose.material3.SnackbarDuration
import com.nexters.boolti.presentation.screen.MainDestination
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -59,6 +60,26 @@ class BridgeManager(
callbackToWeb(data, null)
}

CommandType.SHOW_TOAST -> {
data.data?.jsonObject?.let {
val message = it["message"]?.toString() ?: run {
Timber.tag("bridge").d("토스트 메시지 출력 실패: message 없음")
callbackToWeb(data, null)
return@let
}
val duration = it["duration"]?.toString()?.let { durationStr ->
if (durationStr.equals("long", true)) {
SnackbarDuration.Long
} else {
SnackbarDuration.Short
}
} ?: SnackbarDuration.Short

callbackHandler.showSnackbar(message, duration)
}
callbackToWeb(data, null)
}

else -> callbackToWeb(data, null)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum class CommandType {
NAVIGATE_TO_SHOW_DETAIL,
NAVIGATE_BACK,
REQUEST_TOKEN,
SHOW_TOAST,
UNKNOWN;

companion object {
Expand Down

0 comments on commit 6c4fa08

Please sign in to comment.