This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typealias SnackFinishedListener = () -> Unit | |
| typealias SnackActionListener = () -> Unit | |
| /** | |
| * Representation of [Snackbar] data that can be exposed by a view model, observed, and bound to | |
| * the current UI | |
| */ | |
| sealed class SnackData | |
| /** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Representation of [Toast] data that can be exposed by a view model, observed, and bound to | |
| * the current UI | |
| */ | |
| sealed class ToastData | |
| data class ShortToast(@StringRes val msgId:Int) : ToastData() | |
| data class LongToast(@StringRes val msgId:Int) : ToastData() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| viewModel.toastPub.subscribe { toastData -> | |
| when(toastData) { | |
| is ShortToast -> Toast.makeText(this, toastData.msgId, Toast.LENGTH_SHORT).show() | |
| is LongToast -> Toast.makeText(this, toastData.msgId, Toast.LENGTH_LONG).show() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val toastPub:PublishSubject<ToastData> = PublishSubject.create() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val snackPub:PublishSubject<SnackData> = PublishSubject.create() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun bindViewModel(root:View, viewModel: BaseViewModel) { | |
| viewModel.snackPub.subscribe { snackData -> | |
| showSnackbar(rootView, snackData) | |
| } | |
| } | |
| /** | |
| * Maps the passed [SnackData] to the appropriate UI representation of a [Snackbar] | |
| */ | |
| private fun showSnackbar(root: View, data: SnackData) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Lifecycle States | |
| sealed class ViewModelLifecycleState | |
| object Active : ViewModelLifecycleState() | |
| object FinishedOk : ViewModelLifecycleState() | |
| object Cancelled : ViewModelLifecycleState() | |
| data class FinishedWithResult( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| viewModel.lifecycleStatePub.subscribe { state -> | |
| when (state) { | |
| is Active -> {} | |
| is FinishedOk, Cancelled -> finish() | |
| is FinishedWithResult -> { | |
| setResult(state.result, state.data) | |
| finish() | |
| } | |
| is StartActivity -> { | |
| val intent = Intent(this, state.target).apply { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Indicate the the screen should be closed | |
| class ImageViewModel() : BaseViewModel() { | |
| @BoundMethod | |
| fun onExitFullscreenClicked() { | |
| lifecycleState.set(FinishedOk) | |
| } | |
| } | |
| // indicate that the screen should be finished and a result is returned |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MainActivity : Activity() { | |
| private val TAG = MainActivity::class.java.simpleName | |
| private val blinkHandler = Handler() | |
| private var redLED: Gpio? = null | |
| private val blinkRunnable = object : Runnable { | |
| override fun run() { | |
| redLED?.also { |