Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
machiav3lli committed Jul 14, 2024
1 parent b45cbff commit c8268bb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Neo Backup <img title="" src="./fastlane/metadata/android/en-US/images/icon.png" align="left" width="64">
# Neo Backup <img title="" src="./fastlane/metadata/android/en-US/images/icon.png" align="left" width="64" style="padding: 12px;">

[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg)](COC.md)
[![GitHub repo stars](https://img.shields.io/github/stars/NeoApplications/Neo-Backup?style=flat)](https://github.com/NeoApplications/Neo-Backup/stargazers)
Expand Down
65 changes: 38 additions & 27 deletions app/src/androidTest/kotlin/research/Try_Serialization.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ private val valueSerMod = SerializersModule {
}
}

@Serializer(forClass = Map::class)
object MapAnySerializer : KSerializer<Map<String, AnyType>> {

@OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class)
Expand All @@ -65,10 +64,15 @@ object MapAnySerializer : KSerializer<Map<String, AnyType>> {
for ((key, value) in value) {
encodeStringElement(descriptor, 0, key)
when (value) {
is Int -> encodeIntElement(descriptor, 1, value)
is Int -> encodeIntElement(descriptor, 1, value)
is Boolean -> encodeBooleanElement(descriptor, 1, value)
is String -> encodeStringElement(descriptor, 1, value)
else -> encodeSerializableElement(descriptor, 1, PolymorphicSerializer(Any::class), value)
is String -> encodeStringElement(descriptor, 1, value)
else -> encodeSerializableElement(
descriptor,
1,
PolymorphicSerializer(Any::class),
value
)
}
}
}
Expand All @@ -83,22 +87,26 @@ object MapAnySerializer : KSerializer<Map<String, AnyType>> {
0 -> {
val key = decodeStringElement(descriptor, 0)
val value = when (val elemIndex = decodeElementIndex(descriptor)) {
1 -> decodeIntElement(descriptor, 1)
2 -> decodeBooleanElement(descriptor, 2)
3 -> decodeStringElement(descriptor, 3)
else -> decodeSerializableElement(descriptor, elemIndex, PolymorphicSerializer(Any::class))
1 -> decodeIntElement(descriptor, 1)
2 -> decodeBooleanElement(descriptor, 2)
3 -> decodeStringElement(descriptor, 3)
else -> decodeSerializableElement(
descriptor,
elemIndex,
PolymorphicSerializer(Any::class)
)
}
result[key] = value
}
else -> throw SerializationException("Invalid index: $index")

else -> throw SerializationException("Invalid index: $index")
}
}
result
}
}
}

@Serializer(forClass = Map::class)
object MapSerializerAny : KSerializer<Map<String, AnyType>> {

@OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class)
Expand Down Expand Up @@ -145,30 +153,31 @@ sealed class Primitive {

companion object {
fun <T : Any> from(value: T) =
when(value) {
is Int -> IntValue(value)
when (value) {
is Int -> IntValue(value)
is Boolean -> BooleanValue(value)
is String -> StringValue(value)
else -> throw Exception("only Int, Boolean, String allowed")
is String -> StringValue(value)
else -> throw Exception("only Int, Boolean, String allowed")
}
}
}

typealias MapPrimitive = Map<String, Primitive>

@OptIn(ExperimentalSerializationApi::class, InternalSerializationApi::class)
object PrimitiveSerializer : KSerializer<Primitive> {
@OptIn(InternalSerializationApi::class)
override val descriptor: SerialDescriptor = buildSerialDescriptor("Primitive", PolymorphicKind.SEALED)
override val descriptor: SerialDescriptor =
buildSerialDescriptor("Primitive", PolymorphicKind.SEALED)

override fun serialize(encoder: Encoder, value: Primitive) {
when (value) {
is Primitive.IntValue -> encoder.encodeInt(value.value)
is Primitive.IntValue -> encoder.encodeInt(value.value)
is Primitive.BooleanValue -> encoder.encodeBoolean(value.value)
is Primitive.StringValue -> encoder.encodeString(value.value)
is Primitive.StringValue -> encoder.encodeString(value.value)
//else -> encoder.encodeStructure(descriptor) {
// encodeSerializableElement(descriptor, 3, PolymorphicSerializer(Any::class), value)
//}
else -> error("only Int, Boolean, String allowed")
else -> error("only Int, Boolean, String allowed")
}
}

Expand All @@ -179,11 +188,11 @@ object PrimitiveSerializer : KSerializer<Primitive> {
var stringValue: String? = runCatching { decoder.decodeString() }.getOrNull()
var anyValue: Any? = null
when {
intValue != null -> Primitive.IntValue(intValue)
intValue != null -> Primitive.IntValue(intValue)
booleanValue != null -> Primitive.BooleanValue(booleanValue)
stringValue != null -> Primitive.StringValue(stringValue)
stringValue != null -> Primitive.StringValue(stringValue)
//anyValue != null -> Primitive.AnyValue(anyValue)
else -> error("No values found")
else -> error("No values found")
}
}
}
Expand All @@ -200,15 +209,16 @@ class Try_Serialization {

fun <K, T> mapAnyOf(vararg args: Pair<K, T>) = mapOf<K, T>(*args) //.mapValues { AnyType(it) }

val mapAny : MapAny = mapAnyOf(
val mapAny: MapAny = mapAnyOf(
"int" to 123,
"boolean" to false,
"string" to "abc",
)

fun <K, T : Any> mapPrimitiveOf(vararg args: Pair<K, T>) = mapOf<K, T>(*args).mapValues { Primitive.from(it.value) }
fun <K, T : Any> mapPrimitiveOf(vararg args: Pair<K, T>) =
mapOf<K, T>(*args).mapValues { Primitive.from(it.value) }

val mapPrimitive : MapPrimitive = mapPrimitiveOf(
val mapPrimitive: MapPrimitive = mapPrimitiveOf(
"int" to 123,
"boolean" to false,
"string" to "abc",
Expand Down Expand Up @@ -271,7 +281,8 @@ class Try_Serialization {
fun test_mapPrimitiveSerializer() {
val ser = serializer.encodeToString(mapPrimitiveSerializer, mapPrimitive)
println("ser: '\n$ser\n'")
val obj = serializer.decodeFromString(mapPrimitiveSerializer, ser).mapValues { it.value.asAny() }
val obj =
serializer.decodeFromString(mapPrimitiveSerializer, ser).mapValues { it.value.asAny() }
assertEquals(mapAny, obj)
}

Expand All @@ -287,7 +298,7 @@ class Try_Serialization {
fun test_obj() {
val ser = serializer.encodeToString(aObj)
println("ser: '\n$ser\n'")
val obj : aClass = serializer.decodeFromString(ser)
val obj: aClass = serializer.decodeFromString(ser)
assertEquals(aObj, obj)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class MainActivityX : BaseActivity() {
super.onDestroy()
}

override fun onNewIntent(intent: Intent?) {
override fun onNewIntent(intent: Intent) {
doIntent(intent)
super.onNewIntent(intent)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ open class StorageFile {
if (result != null) {
_uri = result
ok = true
}
} else { }
} catch (e: Throwable) {
logException(e, path, backTrace = false)
ok = false
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/machiav3lli/backup/pages/BatchPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -76,8 +77,7 @@ fun BatchPage(viewModel: BatchViewModel, backupBoolean: Boolean) {
val main = OABX.main!!
val scope = rememberCoroutineScope()
val filteredList by main.viewModel.filteredList.collectAsState(emptyList())
val showBatchSheet = remember { mutableStateOf(false) }
val backupBatchSheet = remember { mutableStateOf(backupBoolean) }
val showBatchSheet = rememberSaveable { mutableStateOf(false) }
val batchSheetState = rememberModalBottomSheetState(true)
val openDialog = remember { mutableStateOf(false) }

Expand Down Expand Up @@ -241,7 +241,7 @@ fun BatchPage(viewModel: BatchViewModel, backupBoolean: Boolean) {
onDismissRequest = dismiss
) {
BatchPrefsSheet(
backupBoolean = backupBatchSheet.value
backupBoolean = backupBoolean
)
}
}
Expand Down

0 comments on commit c8268bb

Please sign in to comment.