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

Fixing the saver of TUISnackbar #234

Open
wants to merge 2 commits into
base: develop
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
31 changes: 23 additions & 8 deletions example/src/main/java/com/tarkalabs/ui/UIComponentListActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,27 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.tarkalabs.tarkaui.components.TUIAppTopBar
import com.tarkalabs.tarkaui.components.TUISnackBarHost
import com.tarkalabs.tarkaui.components.TUISnackBarType
import com.tarkalabs.tarkaui.components.TUITextRow
import com.tarkalabs.tarkaui.components.TextRowStyle
import com.tarkalabs.tarkaui.components.VerticalSpacer
import com.tarkalabs.tarkaui.components.rememberTUISnackBarState
import com.tarkalabs.tarkaui.icons.ChevronRight20
import com.tarkalabs.tarkaui.icons.ErrorCircle24
import com.tarkalabs.tarkaui.icons.TarkaIcons
import com.tarkalabs.tarkaui.icons.TarkaIcons.Filled
import com.tarkalabs.tarkaui.theme.TUITheme
import kotlinx.coroutines.launch

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
class UIComponentListActivity : ComponentActivity() {
Expand All @@ -32,13 +40,12 @@ class UIComponentListActivity : ComponentActivity() {

setContent {
TUITheme {
var query by remember {
mutableStateOf("")
}

var showSearchbar by remember {
mutableStateOf(false)
}
val scope = rememberCoroutineScope()
val state = rememberTUISnackBarState(
type = TUISnackBarType.Information,
hostState = SnackbarHostState(),
leadingIcon = Filled.ErrorCircle24
)

Scaffold(topBar = {
TUIAppTopBar(
Expand All @@ -47,7 +54,16 @@ class UIComponentListActivity : ComponentActivity() {
menuItemIconOne = TarkaIcons.Regular.ChevronRight20,
menuItemIconTwo = TarkaIcons.Regular.ChevronRight20,
menuItemIconThree = TarkaIcons.Regular.ChevronRight20,
onFirstMenuItemClicked = {
scope.launch {
state.showSnackBar("Hola buddy")
}
}
)
}, snackbarHost = {
TUISnackBarHost(
modifier = Modifier.padding(16.dp), state = state
)
}) { paddingValues ->
Column(
modifier = Modifier
Expand All @@ -61,7 +77,6 @@ class UIComponentListActivity : ComponentActivity() {
VerticalSpacer(space = 20)
TUITextRow(title = "Dates", style = TextRowStyle.DateStyle("Not Available","Not Available"))
}

}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tarka-ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ publishing {
run {
groupId = "com.tarkalabs"
artifactId = getLibraryArtifactId()
version = "1.1.19"
version = "1.1.18"
artifact("$buildDir/outputs/aar/tarka-ui-release.aar")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
Expand Down Expand Up @@ -66,11 +67,14 @@ class TUISnackBarState(
) {

companion object {
val Saver = listSaver(save = {
listOf(it.type, it.leadingIcon)
}, restore = {
TUISnackBarState(it[0] as SnackbarHostState, it[1] as TUISnackBarType, it[2] as TarkaIcon?)
})
val Saver : Saver<TUISnackBarState, *> = Saver(
save = {
listOf(it.hostState, it.type, it.leadingIcon)
},
restore = {
TUISnackBarState(it[0] as SnackbarHostState, it[1] as TUISnackBarType, it[2] as TarkaIcon?)
}
)
}

internal val hostState: SnackbarHostState by mutableStateOf(hostState)
Expand Down Expand Up @@ -101,7 +105,7 @@ class TUISnackBarState(
message: String,
actionLabel: String? = null,
withDismissAction: Boolean = false,
duration: SnackbarDuration = if (actionLabel == null) SnackbarDuration.Short else SnackbarDuration.Indefinite
duration: SnackbarDuration = if (actionLabel == null) SnackbarDuration.Short else SnackbarDuration.Indefinite,
): SnackbarResult {
hostState.currentSnackbarData?.dismiss()
return hostState.showSnackbar(message, actionLabel, withDismissAction, duration)
Expand Down Expand Up @@ -151,13 +155,13 @@ fun TUISnackBarHost(
* How to use TUISnackBar() composable function
*
* TUISnackBar(
message = "Task completed successfully!",
type = Success,
leadingIcon = TarkaIcon.Success,
actionLabel = "Dismiss",
tags = TUISnackBarTags(parentTag = "example_snackbar"),
action = { /* Perform action on dismiss */ }
)
message = "Task completed successfully!",
type = Success,
leadingIcon = TarkaIcon.Success,
actionLabel = "Dismiss",
tags = TUISnackBarTags(parentTag = "example_snackbar"),
action = { /* Perform action on dismiss */ }
)
*/
@Composable
internal fun TUISnackBar(
Expand Down Expand Up @@ -238,7 +242,8 @@ internal fun TUISnackBar(
) {
Text(
modifier = Modifier.padding(horizontal = 17.dp),
text = actionLabel, style = TUITheme.typography.body7)
text = actionLabel, style = TUITheme.typography.body7
)
}
}
}
Expand Down Expand Up @@ -274,15 +279,21 @@ fun TUIInformationSnackBarPreview() {
@Composable
fun TUISuccessSnackBarPreview() {
TUISnackBar(
message = "Hello there", actionLabel = "dgsd", leadingIcon = TarkaIcons.Regular.Delete24, type = Success
message = "Hello there",
actionLabel = "dgsd",
leadingIcon = TarkaIcons.Regular.Delete24,
type = Success
)
}

@Preview
@Composable
fun TUIWarningSnackBarPreview() {
TUISnackBar(
message = "Hello there", actionLabel = "dgsd", leadingIcon = TarkaIcons.Regular.Delete24, type = Warning
message = "Hello there",
actionLabel = "dgsd",
leadingIcon = TarkaIcons.Regular.Delete24,
type = Warning
)
}

Expand Down