Skip to content

Instantly share code, notes, and snippets.

View motorro's full-sized avatar

Nikolai Kotchetkov motorro

View GitHub Profile
@motorro
motorro / prepopulate-MainActivityModel.kt
Created March 12, 2020 15:40
New database instance
val db = Room
.databaseBuilder(getApplication(), CitiesDb::class.java, "cities.db")
.createFromAsset("databases/cities.db")
.fallbackToDestructiveMigration() // On version update - just copy over
.build()
@motorro
motorro / BaseActivity.kt
Created May 29, 2020 19:28
AppUpdateWrapper base activity for multi-activity setup
abstract class BaseActivity: AppCompatActivity(), AppUpdateView {
companion object {
/**
* Request code for ready dialog
*/
val REQUEST_CODE_UPDATE = REQUEST_ID_GENERATOR.requestId()
/**
* Request code for ready dialog
*/
@motorro
motorro / RxTest.kt
Created June 20, 2020 10:38
Rx throw in create
class RxTest {
@Test
fun throwsAnError() {
val source = Observable.create<Int> { emitter ->
emitter.onNext(1)
throw RuntimeException()
}
source.test()
.assertValue(1)
@motorro
motorro / ApplicationModule.kt
Last active June 30, 2020 10:03
An update wrapper factory (a part of Dagger module)
/**
* Application update manager
*/
@Provides
@Singleton
fun updateManager(context: Context): AppUpdateManager = AppUpdateManagerFactory.create(context)
/**
* Update wrapper factory
*/
@motorro
motorro / UpdateFlowBreaker.kt
Last active October 10, 2020 10:20
Update flow breaker
/**
* Checks if user has already refused to install update and terminates update flow
*/
interface UpdateFlowBreaker: TimeCancelledStorage {
/**
* Checks if enough time has passed since user had explicitly cancelled update
*/
fun isEnoughTimePassedSinceLatestCancel(): Boolean
/**
@motorro
motorro / prepopulate-enable-schema.groovy
Last active January 11, 2021 11:26
Enabling Room schema export
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments += [
"room.schemaLocation": "$projectDir/schemas".toString()
]
}
}
}
@motorro
motorro / lcestates.md
Created July 29, 2022 11:39
States and gestures of LCE application
Logical state Ui-State Gesture/Event Next state Output
ItemList ItemList Back Terminated Finishes activity
ItemClicked Loading Loads requested item
Loading Loading Back Item list Cancels load and returns to list
onContent Content Displays loaded item
onError Error Displays load error
Content Item Back Item list Returns to the item list
Error Error Back Item list Returns to the item list
Retry Loading Retries load operation
@motorro
motorro / CoroutineState.kt
Created July 29, 2022 15:13
Coroutine state
abstract class CoroutineState<G: Any, U: Any>: CommonMachineState<G, U>() {
protected val stateScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
override fun doClear() {
stateScope.cancel()
}
}
@motorro
motorro / CommonStateMachine.kt
Last active July 31, 2022 06:52
State machine implementation
/**
* Common state machine
* @param G UI gesture
* @param U UI state
*/
interface CommonStateMachine<G: Any, U: Any> : MachineInput<G>, MachineOutput<G, U> {
/**
* Base state-machine implementation
* @param init Initial state producer
@motorro
motorro / LceGesture.kt
Created July 31, 2022 15:43
LCE app gestures
sealed class LceGesture {
/**
* Item to load clicked
* @property id Item ID to load
*/
data class ItemClicked(val id: ItemId) : LceGesture()
/**
* Retry operation clicked
*/