Skip to content

Commit

Permalink
revert old methods for binary compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyKorshun committed Aug 1, 2023
1 parent 4452d07 commit 68bea57
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.plus
import ru.tinkoff.kotea.core.Store
Expand Down Expand Up @@ -41,6 +42,29 @@ fun <T : Store<*, *, *>> storeViaViewModel(
}
}

@Deprecated("Use storeViewViewModel function with coroutine context param")
fun <T : Store<*, *, *>> storeViaViewModel(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
sharedViewModelKey: String? = null,
factory: () -> T
): ReadOnlyProperty<ViewModelStoreOwner, T> {
return object : ReadOnlyProperty<ViewModelStoreOwner, T> {
private var value: T? = null

override fun getValue(thisRef: ViewModelStoreOwner, property: KProperty<*>): T {
return value ?: run {
val key = sharedViewModelKey ?: keyFromProperty(thisRef, property)
val vm = thisRef.viewModelStore.get(key) { StoreViewModel(factory(), dispatcher) }
vm.store.also { value = it }
}
}

private fun keyFromProperty(thisRef: ViewModelStoreOwner, property: KProperty<*>): String {
return thisRef::class.java.canonicalName!! + "#" + property.name
}
}
}

private class StoreViewModel<T : Store<*, *, *>>(
val store: T,
coroutineContext: CoroutineContext
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/ru/tinkoff/kotea/core/KoteaStoreFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ fun <State : Any, Event : Any, UiEvent : Event, Command : Any, News : Any> Kotea
): Store<State, UiEvent, News> {
return StoreImpl(initialState, initialCommands, commandsFlowHandlers, update)
}

@Deprecated("Use factory without uncaughtExceptionHandler (don't calling rigt now)")
fun <State : Any, Event : Any, UiEvent : Event, Command : Any, News : Any> KoteaStore(
initialState: State,
initialCommands: List<Command> = emptyList(),
commandsFlowHandlers: List<CommandsFlowHandler<Command, Event>> = emptyList(),
update: Update<State, Event, Command, News> = Update { _, _ -> Next() },
uncaughtExceptionHandler: UncaughtExceptionHandler = UncaughtExceptionHandler { }
): Store<State, UiEvent, News> {
return StoreImpl(initialState, initialCommands, commandsFlowHandlers, update)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.tinkoff.kotea.core

@Deprecated("For handling exceptions please use CoroutinesExceptionHandler")
fun interface UncaughtExceptionHandler {

fun handle(throwable: Throwable)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ru.tinkoff.kotea.core.CommandsFlowHandler
import ru.tinkoff.kotea.core.KoteaStore
import ru.tinkoff.kotea.core.Next
import ru.tinkoff.kotea.core.Store
import ru.tinkoff.kotea.core.UncaughtExceptionHandler
import ru.tinkoff.kotea.core.Update

/**
Expand Down Expand Up @@ -40,3 +41,37 @@ fun <State : Any, Event : Any, UiEvent : Event, Command : Any, News : Any> Kotea
),
)
}

@Deprecated("Use factory without uncaughtExceptionHandler (don't calling rigt now)")
fun <State : Any, Event : Any, UiEvent : Event, Command : Any, News : Any> KoteaLoggingStore(
initialState: State,
initialCommands: List<Command> = emptyList(),
commandsFlowHandlers: List<CommandsFlowHandler<Command, Event>> = emptyList(),
update: Update<State, Event, Command, News> = Update { _, _ -> Next() },
uncaughtExceptionHandler: UncaughtExceptionHandler = UncaughtExceptionHandler { },
tag: String = update.javaClass.simpleName,
logger: KoteaLogger = DefaultKoteaLogger(),
): Store<State, UiEvent, News> {

logger.debug(tag, "Initial State: ${initialState.toString().removePackage(update.javaClass)}")

if (initialCommands.isNotEmpty()) {
logger.debug(tag, "=== Initial Commands ===")
for (command in initialCommands) {
logger.debug(tag, "Command: ${command.toString().removePackage(update.javaClass)}")
}
}

logger.debug(tag, "=== End of Initial Commands ===")

return KoteaStore(
initialState,
initialCommands,
commandsFlowHandlers,
LoggingUpdate(
delegate = update,
tag = tag,
logger = logger,
),
)
}

0 comments on commit 68bea57

Please sign in to comment.