|
| 1 | +package com.yapp.ndgl.feature.home.settings |
| 2 | + |
| 3 | +import android.content.ClipData |
| 4 | +import android.content.ClipboardManager |
| 5 | +import androidx.compose.foundation.background |
| 6 | +import androidx.compose.foundation.clickable |
| 7 | +import androidx.compose.foundation.layout.PaddingValues |
| 8 | +import androidx.compose.foundation.layout.Row |
| 9 | +import androidx.compose.foundation.layout.fillMaxSize |
| 10 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 11 | +import androidx.compose.foundation.layout.padding |
| 12 | +import androidx.compose.foundation.layout.size |
| 13 | +import androidx.compose.foundation.layout.statusBarsPadding |
| 14 | +import androidx.compose.foundation.lazy.LazyColumn |
| 15 | +import androidx.compose.foundation.lazy.items |
| 16 | +import androidx.compose.material3.HorizontalDivider |
| 17 | +import androidx.compose.material3.Icon |
| 18 | +import androidx.compose.material3.Scaffold |
| 19 | +import androidx.compose.material3.Text |
| 20 | +import androidx.compose.runtime.Composable |
| 21 | +import androidx.compose.runtime.getValue |
| 22 | +import androidx.compose.ui.Alignment |
| 23 | +import androidx.compose.ui.Modifier |
| 24 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 25 | +import androidx.compose.ui.platform.LocalContext |
| 26 | +import androidx.compose.ui.res.stringResource |
| 27 | +import androidx.compose.ui.res.vectorResource |
| 28 | +import androidx.compose.ui.unit.dp |
| 29 | +import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel |
| 30 | +import com.yapp.ndgl.core.ui.designsystem.NDGLNavigationBar |
| 31 | +import com.yapp.ndgl.core.ui.designsystem.NDGLNavigationBarAttr |
| 32 | +import com.yapp.ndgl.core.ui.theme.NDGLTheme |
| 33 | +import com.yapp.ndgl.core.ui.util.launchBrowser |
| 34 | +import com.yapp.ndgl.feature.home.R |
| 35 | +import com.yapp.ndgl.feature.home.settings.SettingsState.SettingsMenu |
| 36 | +import kotlinx.collections.immutable.ImmutableList |
| 37 | +import com.yapp.ndgl.core.ui.R as CoreR |
| 38 | + |
| 39 | +@Composable |
| 40 | +internal fun SettingsRoute( |
| 41 | + viewModel: SettingsViewModel = hiltViewModel(), |
| 42 | + goBack: () -> Unit, |
| 43 | +) { |
| 44 | + val context = LocalContext.current |
| 45 | + |
| 46 | + val state by viewModel.collectAsState() |
| 47 | + |
| 48 | + SettingsScreen( |
| 49 | + menuItems = state.menuItems, |
| 50 | + goBack = goBack, |
| 51 | + onUrlItemClick = { |
| 52 | + viewModel.onIntent(SettingsIntent.ClickUrlMenu(it)) |
| 53 | + }, |
| 54 | + onCopyIdentifierCodeClick = { |
| 55 | + viewModel.onIntent(SettingsIntent.ClickCopyIdentifierCodeMenu) |
| 56 | + }, |
| 57 | + ) |
| 58 | + |
| 59 | + viewModel.collectSideEffect { sideEffect -> |
| 60 | + when (sideEffect) { |
| 61 | + is SettingsSideEffect.OpenUrl -> context.launchBrowser(sideEffect.url) |
| 62 | + is SettingsSideEffect.CopyIdentifierCode -> { |
| 63 | + val clipboard = context.getSystemService(ClipboardManager::class.java) |
| 64 | + val clip = ClipData.newPlainText( |
| 65 | + context.getString(R.string.home_settings_identification_code), |
| 66 | + sideEffect.code |
| 67 | + ) |
| 68 | + clipboard?.setPrimaryClip(clip) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +@Composable |
| 75 | +private fun SettingsScreen( |
| 76 | + menuItems: ImmutableList<SettingsMenu>, |
| 77 | + goBack: () -> Unit, |
| 78 | + onUrlItemClick: (SettingsState.UrlMenu) -> Unit, |
| 79 | + onCopyIdentifierCodeClick: () -> Unit, |
| 80 | +) { |
| 81 | + Scaffold( |
| 82 | + topBar = { |
| 83 | + NDGLNavigationBar( |
| 84 | + textAlignType = NDGLNavigationBarAttr.TextAlignType.CENTER, |
| 85 | + modifier = Modifier |
| 86 | + .fillMaxWidth() |
| 87 | + .background(color = NDGLTheme.colors.white) |
| 88 | + .statusBarsPadding(), |
| 89 | + headline = stringResource(R.string.home_settings_title), |
| 90 | + leadingIcon = CoreR.drawable.ic_28_chevron_left, |
| 91 | + onLeadingIconClick = goBack, |
| 92 | + ) |
| 93 | + }, |
| 94 | + ) { innerPadding -> |
| 95 | + LazyColumn( |
| 96 | + modifier = Modifier |
| 97 | + .fillMaxSize() |
| 98 | + .padding(innerPadding), |
| 99 | + contentPadding = PaddingValues(24.dp), |
| 100 | + ) { |
| 101 | + items( |
| 102 | + items = menuItems, |
| 103 | + key = { item -> item.labelRes }, |
| 104 | + ) { item -> |
| 105 | + when (item) { |
| 106 | + is SettingsMenu.OpenUrl -> SettingsMenuItem( |
| 107 | + text = stringResource(item.labelRes), |
| 108 | + onClick = { onUrlItemClick(item.menu) }, |
| 109 | + ) |
| 110 | + |
| 111 | + SettingsMenu.CopyIdentifierCode -> SettingsMenuItem( |
| 112 | + text = stringResource(item.labelRes), |
| 113 | + onClick = onCopyIdentifierCodeClick, |
| 114 | + ) |
| 115 | + |
| 116 | + is SettingsMenu.AppVersion -> VersionItem(versionName = item.versionName) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +@Composable |
| 124 | +private fun SettingsMenuItem( |
| 125 | + text: String, |
| 126 | + onClick: () -> Unit, |
| 127 | +) { |
| 128 | + Row( |
| 129 | + modifier = Modifier |
| 130 | + .fillMaxWidth() |
| 131 | + .clickable(onClick = onClick) |
| 132 | + .padding(horizontal = 8.dp, vertical = 16.dp), |
| 133 | + verticalAlignment = Alignment.CenterVertically, |
| 134 | + ) { |
| 135 | + Text( |
| 136 | + text = text, |
| 137 | + style = NDGLTheme.typography.bodyLgRegular, |
| 138 | + color = NDGLTheme.colors.black700, |
| 139 | + modifier = Modifier.weight(1f), |
| 140 | + ) |
| 141 | + Icon( |
| 142 | + imageVector = ImageVector.vectorResource(CoreR.drawable.ic_24_chevron_right), |
| 143 | + contentDescription = null, |
| 144 | + modifier = Modifier.size(24.dp), |
| 145 | + tint = NDGLTheme.colors.black600, |
| 146 | + ) |
| 147 | + } |
| 148 | + HorizontalDivider(color = NDGLTheme.colors.black50) |
| 149 | +} |
| 150 | + |
| 151 | +@Composable |
| 152 | +private fun VersionItem( |
| 153 | + versionName: String, |
| 154 | +) { |
| 155 | + Row( |
| 156 | + modifier = Modifier |
| 157 | + .fillMaxWidth() |
| 158 | + .padding(horizontal = 8.dp, vertical = 16.dp), |
| 159 | + verticalAlignment = Alignment.CenterVertically, |
| 160 | + ) { |
| 161 | + Text( |
| 162 | + text = stringResource(R.string.home_settings_version_info), |
| 163 | + style = NDGLTheme.typography.bodyLgRegular, |
| 164 | + color = NDGLTheme.colors.black700, |
| 165 | + modifier = Modifier.weight(1f), |
| 166 | + ) |
| 167 | + Text( |
| 168 | + text = versionName, |
| 169 | + style = NDGLTheme.typography.bodyLgRegular, |
| 170 | + color = NDGLTheme.colors.black400, |
| 171 | + ) |
| 172 | + } |
| 173 | + HorizontalDivider(color = NDGLTheme.colors.black50) |
| 174 | +} |
0 commit comments