Skip to content

Commit

Permalink
Fixed crash when trying to use a bubble navigation,
Browse files Browse the repository at this point in the history
Fixed crash when trying to open any other screen from new settings,
Fixed bug when settings would give old values,
Show empty new settings screen with an header,
Better dependency management,
Use black background in new settings for an amoled theme,
Allow setting string, int, float and enum values in new settings

Signed-off-by: MrBoom <github@mrboomdev.ru>
  • Loading branch information
MrBoomDeveloper committed Dec 26, 2024
1 parent 41f104d commit 874b43b
Show file tree
Hide file tree
Showing 20 changed files with 471 additions and 119 deletions.
4 changes: 2 additions & 2 deletions app/src/main/assets/app_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"to": 10,
"value": 0,
"title": "media_columns_count_land",
"description": "Auto if is 0"
"placeholder": "Leave blank to use an automatic value"
},

{
Expand All @@ -38,7 +38,7 @@
"to": 10,
"value": 0,
"title": "media_columns_count_port",
"description": "Auto if is 0",
"placeholder": "Leave blank to use an automatic value",
"show_if": ["!tv"]
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import java.io.File

object ResourcesProvider {
fun getFile(source: Source, path: String): File {
throw UnsupportedOperationException("Stub1!")
throw NotImplementedError("TODO: Implement an Android implementation.")
}

fun createImage(file: File): Image {
throw UnsupportedOperationException("Stub1!")
throw NotImplementedError("TODO: Implement an Android implementation.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,51 @@ package com.mrboomdev.awery.ui.mobile.components
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.selection.selectableGroup
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Surface
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TriStateCheckbox
import androidx.compose.material3.contentColorFor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.state.ToggleableState
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import com.mrboomdev.awery.R
import com.mrboomdev.awery.app.App.Companion.i18n
import com.mrboomdev.awery.app.App.Companion.toast
import com.mrboomdev.awery.ext.data.Setting
import com.mrboomdev.awery.platform.PlatformResources.i18n
import com.mrboomdev.awery.platform.PlatformSetting
import com.mrboomdev.awery.platform.PlatformSettingHandler
import com.mrboomdev.awery.ui.components.MaterialDialog
import com.mrboomdev.awery.utils.compareTo
import com.mrboomdev.awery.utils.toStrippedString

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MobileSetting(
setting: Setting,
Expand All @@ -37,6 +56,7 @@ fun MobileSetting(
) {
var triState by remember { mutableStateOf(setting.value as? Setting.TriState ?: Setting.TriState.EMPTY) }
var isChecked by remember { mutableStateOf(setting.value == true) }
var isDialogShown by remember { mutableStateOf(false) }
val context = LocalContext.current

Surface(
Expand Down Expand Up @@ -67,14 +87,13 @@ fun MobileSetting(

Setting.Type.BOOLEAN -> isChecked = !isChecked
Setting.Type.TRI_STATE -> triState = triState.next()

Setting.Type.FLOAT -> toast("This action isn't done yet!")
Setting.Type.STRING -> toast("This action isn't done yet!")
Setting.Type.SELECT -> toast("This action isn't done yet!")
Setting.Type.INTEGER -> toast("This action isn't done yet!")
Setting.Type.MULTISELECT -> toast("This action isn't done yet!")

Setting.Type.CATEGORY, null -> {}

Setting.Type.FLOAT,
Setting.Type.STRING,
Setting.Type.SELECT,
Setting.Type.INTEGER,
Setting.Type.MULTISELECT -> isDialogShown = true
}
}
) {
Expand All @@ -90,9 +109,7 @@ fun MobileSetting(
(setting.title ?: (if(setting.description == null) setting.key else null))?.let { title ->
Text(
style = MaterialTheme.typography.bodyLarge,
text = setting.takeIf { it is PlatformSetting }?.let {
i18n<R.string>(title)
} ?: title
text = setting.takeIf { it is PlatformSetting }?.let { i18n(title) } ?: title
)
}

Expand All @@ -104,10 +121,7 @@ fun MobileSetting(
Text(
style = if(setting.title == null) MaterialTheme.typography.bodyMedium else MaterialTheme.typography.bodySmall,
color = if(setting.type == Setting.Type.CATEGORY) MaterialTheme.colorScheme.primary else Color.Unspecified,

text = setting.takeIf { it is PlatformSetting }?.let {
i18n<R.string>(description)
} ?: description
text = setting.takeIf { it is PlatformSetting }?.let { i18n(description) } ?: description
)
}
}
Expand All @@ -133,6 +147,221 @@ fun MobileSetting(
}
}
}

if(isDialogShown) {
var newValue by remember { mutableStateOf(setting.value) }

val isValidValue by remember { derivedStateOf {
when(setting.type) {
Setting.Type.STRING -> {
setting.from?.also { from ->
if(((newValue as? String?)?.length ?: 0) < from) {
return@derivedStateOf false to "This text is too short! Minimum length is ${from.toStrippedString()}."
}
}

setting.to?.also { to ->
if(((newValue as? String?)?.length ?: 0) > to) {
return@derivedStateOf false to "This text is too long! Maximum length is ${to.toStrippedString()}."
}
}

true to ""
}

Setting.Type.INTEGER, Setting.Type.FLOAT -> {
setting.from?.also { from ->
if((newValue as? Number? ?: 0) < from) {
return@derivedStateOf false to "This number is too short! Minimum length is ${from.toStrippedString()}."
}
}

setting.to?.also { to ->
if((newValue as? Number? ?: 0) > to) {
return@derivedStateOf false to "This number is too long! Maximum length is ${to.toStrippedString()}."
}
}

if(newValue != null && newValue !is Number) {
return@derivedStateOf false to "This is not a number!"
}

true to ""
}

else -> true to "No checks can be made on this type."
}
}}

MaterialDialog(
modifier = Modifier.padding(horizontal = 8.dp),
onDismissRequest = { isDialogShown = false },

title = setting.title?.let { title -> {
Text(
style = MaterialTheme.typography.headlineMedium,
text = setting.takeIf { it is PlatformSetting }?.let { i18n(title) } ?: title
)
}},

dismissButton = {
TextButton(onClick = this@MaterialDialog::requestDismiss) {
Text(text = stringResource(R.string.cancel))
}
},

confirmButton = {
TextButton(onClick = {
if(!isValidValue.first) return@TextButton
setting.value = newValue
requestDismiss()
}) {
Text(text = stringResource(R.string.confirm))
}
}
) {
Column {
if(setting.description != null) {
val description = setting.description!!

Text(
text = setting.takeIf { it is PlatformSetting }
?.let { i18n(description) } ?: description
)
}

when(setting.type) {
Setting.Type.STRING -> {
OutlinedTextField(
isError = isValidValue.first,
label = if(isValidValue.first) null else {{
Text(isValidValue.second)
}},

placeholder = if(setting is PlatformSetting && setting.placeholder != null) {{
Text(setting.placeholder!!)
}} else null,

singleLine = true,
value = newValue?.toString() ?: "",
onValueChange = { newValue = it },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(onDone = {
if(!isValidValue.first) return@KeyboardActions
setting.value = newValue
requestDismiss()
})
)
}

Setting.Type.INTEGER -> {
OutlinedTextField(
isError = !isValidValue.first,
label = if(isValidValue.first) null else {{
Text(isValidValue.second)
}},

placeholder = if(setting is PlatformSetting && setting.placeholder != null) {{
Text(setting.placeholder!!)
}} else null,

singleLine = true,
value = newValue?.toString() ?: "",

onValueChange = {
newValue = if(it.isBlank()) null else try {
it.toInt()
} catch(e: NumberFormatException) { it }
},

keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Phone,
imeAction = ImeAction.Done
),

keyboardActions = KeyboardActions(onDone = {
if(!isValidValue.first) return@KeyboardActions
setting.value = newValue
requestDismiss()
})
)
}

Setting.Type.FLOAT -> {
OutlinedTextField(
isError = !isValidValue.first,
label = if(isValidValue.first) null else {{
Text(isValidValue.second)
}},

placeholder = if(setting is PlatformSetting && setting.placeholder != null) {{
Text(setting.placeholder!!)
}} else null,

singleLine = true,
value = newValue?.toString() ?: "",

onValueChange = {
newValue = if(it.isBlank()) null else try {
it.toFloat()
} catch(e: NumberFormatException) { it }
},

keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Decimal,
imeAction = ImeAction.Done
),

keyboardActions = KeyboardActions(onDone = {
setting.value = newValue
requestDismiss()
})
)
}

Setting.Type.SELECT -> {
Column(modifier = Modifier.selectableGroup()) {
for(item in setting.items!!) {
Row(modifier = Modifier
.clip(RoundedCornerShape(8.dp))
.fillMaxWidth()
.height(56.dp)
.selectable(
selected = newValue == item.key,
onClick = { newValue = item.key },
role = Role.RadioButton
),
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(
selected = newValue == item.key,
onClick = null
)

Text(
text = item.title?.let { title ->
setting.takeIf { it is PlatformSetting }?.let { i18n(title) } ?: title
} ?: item.key ?: "No title",

style = MaterialTheme.typography.bodyLarge,
modifier = Modifier.padding(start = 16.dp)
)
}
}
}
}

else -> {
Text(
style = MaterialTheme.typography.bodyLarge,
color = Color.Red,
text = "Unsupported setting type!"
)
}
}
}
}
}
}

private fun Setting.TriState.asToggleableState() = when(this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,9 @@ class MainActivity : AppCompatActivity() {

(fun(tab: Int) {
binding!!.pages.setCurrentItem(tab, false)
(binding!!.pages.adapter as? FeedsAdapter)?.fragments?.get(tab)?.get()?.onFocus()
(binding!!.pages.adapter as? FeedsAdapter)?.fragments?.getOrNull(tab)?.get()?.onFocus()
}).let {
binding!!.navbarBubble.onTabSelected = { tab -> it(tab.id) }

binding!!.navbarMaterial.setOnItemSelectedListener { tab ->
it(tab.itemId)
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class SettingsActivity2: ComponentActivity() {
}
} catch(_: CancellationException) {}
}

Row(Modifier.fillMaxSize()) {
Spacer(Modifier.windowInsetsStartWidth(WindowInsets.safeContent))

Expand Down
Loading

0 comments on commit 874b43b

Please sign in to comment.