Skip to content

Commit

Permalink
v1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamKwokX committed Sep 12, 2023
1 parent 95c12df commit 3abe582
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ tasks.withType(KotlinCompile).configureEach{
// publish
mavenPublishing {
def artifactId = "mvb-android"
def version = "1.0.3"
def version = "1.0.4"
def isSnapshot = false

if (isSnapshot) version += "-SNAPSHOT"
Expand Down
5 changes: 0 additions & 5 deletions api/src/main/java/pers/shawxingkwok/mvb/android/Saver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.annotation.SuppressLint
import android.os.Parcel
import android.os.Parcelable
import pers.shawxingkwok.ktutil.updateIf
import java.util.*

internal class Saver(
var value: Any?,
Expand All @@ -23,7 +22,6 @@ internal class Saver(

companion object CREATOR : Parcelable.Creator<Saver> {
var parcelableLoader: ClassLoader? = null
var arrayClass: Class<Array<*>>? = null
var recover: ((Any?) -> Any?)? = null

override fun createFromParcel(parcel: Parcel): Saver {
Expand All @@ -35,9 +33,6 @@ internal class Saver(
UNINITIALIZED
else
parcel.readValue(parcelableLoader)
.updateIf({ arrayClass != null }){
Arrays.copyOf(it as Array<*>, it.size, arrayClass!!)
}
.updateIf({ recover != null }){ recover!!(it) }

return Saver(value)
Expand Down
1 change: 1 addition & 0 deletions api/src/main/java/pers/shawxingkwok/mvb/android/observe.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pers.shawxingkwok.mvb.android

import androidx.fragment.app.Fragment
import androidx.lifecycle.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
Expand Down
14 changes: 2 additions & 12 deletions api/src/main/java/pers/shawxingkwok/mvb/android/save.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,22 @@ public class SavableMVBData<LV, T, C> @PublishedApi internal constructor(
@PublishedApi internal var convert: ((Any?) -> Any?)? = null
@PublishedApi internal var recover: ((Any?) -> Any?)? = null

@Suppress("UNCHECKED_CAST")
override val saver by lazy(Saver.CREATOR){
val state = vm.state
when(val bundle = state.get<Bundle>(key)){
null -> Saver(UNINITIALIZED, convert).also { state[key] = bundleOf("" to it) }
else -> {
Saver.parcelableLoader = (parcelableComponent ?: savedType.parcelableComponent)?.java?.classLoader
Saver.arrayClass = savedType.java.takeIf { it.isArray && !it.componentType.isPrimitive } as Class<Array<*>>?
Saver.recover = recover

// update [convert] to remove any possible references to old [thisRef].
val saver =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
bundle.getParcelable("", Saver::class.java)!!
else
bundle.getParcelable("")!!

Saver.recover = null
// update [convert] to remove any possible references to old [thisRef].
saver.convert = convert
saver
}
Expand Down Expand Up @@ -141,15 +139,7 @@ public inline fun <LV, reified T> LV.saveMutableSharedFlow(
convert = { it.replayCache },
recover = { cache ->
val flow = MutableSharedFlow<T>(replay, extraBufferCapacity, onBufferOverflow)

cache.map{
if(it is Array<*>)
Arrays.copyOf(it as Array<*>, it.size, T::class.java as Class<out Array<T>>) as T
else
it
}
.forEach(flow::tryEmit)

cache.forEach(flow::tryEmit)
flow
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import pers.shawxingkwok.mvb.android.saveMutableStateFlow
import java.math.BigDecimal

class MainActivity : ComponentActivity() {
private val sharedFlow by saveMutableSharedFlow<_, List<Array<P>>>(
private val sharedFlow by saveMutableSharedFlow<_, Array<Any>>(
replay = 3,
parcelableComponent = P::class
)

private val _sharedFlow by saveMutableSharedFlow<_, Array<P>>(
replay = 3,
parcelableComponent = P::class
)
Expand All @@ -35,8 +40,10 @@ class MainActivity : ComponentActivity() {
assert(emptyLiveData.value == null)
liveData.value = 1
nullableLiveData.value = null
sharedFlow.tryEmit(listOf(arrayOf(P(2))))
sharedFlow.tryEmit(listOf(arrayOf(P(3))))
sharedFlow.tryEmit(arrayOf(P(2)))
sharedFlow.tryEmit(arrayOf(""))
_sharedFlow.tryEmit(arrayOf(P(2)))
_sharedFlow.tryEmit(arrayOf(P(3)))
sparseArray.append(0, P(0))
number += BigDecimal(1)
ints[0]++
Expand All @@ -45,7 +52,8 @@ class MainActivity : ComponentActivity() {
assert(emptyLiveData.value == null)
assert(liveData.value == 1)
assert(nullableLiveData.value == null)
assert(sharedFlow.replayCache.first().first().first().i == 2)
assert(sharedFlow.replayCache.first().first() is P)
assert(_sharedFlow.replayCache.first().first().i == 2)
assert(sparseArray.valueAt(0) == P(0))
assert(number.toInt() == 1)
assert(ints.first() == 1)
Expand Down

0 comments on commit 3abe582

Please sign in to comment.