-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40bdbf9
commit 60a7ab0
Showing
7 changed files
with
163 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
51 changes: 51 additions & 0 deletions
51
android-app/lynket-playground/src/main/java/arun/com/chromer/halo/Halo.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* | ||
* Lynket | ||
* | ||
* Copyright (C) 2023 Arunkumar | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package arun.com.chromer.halo | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import app.cash.molecule.AndroidUiDispatcher | ||
import app.cash.molecule.RecompositionMode | ||
import app.cash.molecule.launchMolecule | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
|
||
/** | ||
* UDF based ViewModel via Molecule | ||
*/ | ||
abstract class HaloViewModel<Action, State> : ViewModel() { | ||
private val scope = CoroutineScope(viewModelScope.coroutineContext + AndroidUiDispatcher.Main) | ||
|
||
private val actions = MutableSharedFlow<Action>() | ||
|
||
val state: StateFlow<State> by lazy(LazyThreadSafetyMode.NONE) { | ||
scope.launchMolecule(mode = RecompositionMode.ContextClock) { | ||
process(actions.asSharedFlow()) | ||
} | ||
} | ||
|
||
@Composable | ||
protected abstract fun process(actions: Flow<Action>): State | ||
} |
This file contains 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
77 changes: 77 additions & 0 deletions
77
android-app/lynket-playground/src/main/java/arun/com/chromer/home/HomeViewModel.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* | ||
* Lynket | ||
* | ||
* Copyright (C) 2023 Arunkumar | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package arun.com.chromer.home | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import arun.com.chromer.halo.HaloViewModel | ||
import com.deliveryhero.whetstone.viewmodel.ContributesViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
|
||
sealed class HomeAction { | ||
data object Load : HomeAction() | ||
} | ||
|
||
sealed class HomeState { | ||
data object Loading : HomeState() | ||
data class Items(val items: List<String>) : HomeState() | ||
} | ||
|
||
@ContributesViewModel | ||
class HomeViewModel | ||
@Inject | ||
constructor() : HaloViewModel<HomeAction, HomeState>() { | ||
|
||
private val dummyItems by lazy { | ||
flow { | ||
delay(2000) | ||
repeat(100) { | ||
emit(it) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
override fun process(actions: Flow<HomeAction>): HomeState { | ||
var items by remember { mutableStateOf(emptyList<String>()) } | ||
LaunchedEffect(Unit) { | ||
withContext(Dispatchers.IO) { | ||
dummyItems.collect { | ||
items = items + it.toString() | ||
} | ||
} | ||
} | ||
return if (items.isEmpty()) { | ||
HomeState.Loading | ||
} else { | ||
HomeState.Items(items) | ||
} | ||
} | ||
} |