-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #191 from DroidKaigi/takahirom/add-contributor-vie…
…wmodel/2023-06-14 Add contributor ViewModel
- Loading branch information
Showing
19 changed files
with
144 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,27 @@ | ||
import io.github.droidkaigi.confsched2023.primitive.implementation | ||
import io.github.droidkaigi.confsched2023.primitive.libs | ||
|
||
plugins { | ||
id("droidkaigi.primitive.kmp") | ||
id("droidkaigi.primitive.kmp.android") | ||
id("droidkaigi.primitive.kmp.ios") | ||
id("droidkaigi.primitive.kmp.compose") | ||
id("droidkaigi.primitive.kmp.android.hilt") | ||
id("droidkaigi.primitive.spotless") | ||
} | ||
|
||
android.namespace = "io.github.droidkaigi.confsched2023.core.designsystem" | ||
|
||
kotlin { | ||
sourceSets { | ||
commonMain { | ||
dependencies { | ||
// Fix https://youtrack.jetbrains.com/issue/KT-41821 | ||
implementation(libs.findLibrary("kotlinxAtomicfu").get()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
} |
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
7 changes: 7 additions & 0 deletions
7
core/ui/src/androidMain/kotlin/io/github/droidkaigi/confsched2023/ui/Annotations.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,7 @@ | ||
package io.github.droidkaigi.confsched2023.ui | ||
|
||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import javax.inject.Inject | ||
|
||
actual typealias KmpHiltViewModel = HiltViewModel | ||
actual typealias KmpInject = Inject |
19 changes: 19 additions & 0 deletions
19
...rc/androidMain/kotlin/io/github/droidkaigi/confsched2023/ui/ErrorAndRetryActionHandler.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,19 @@ | ||
package io.github.droidkaigi.confsched2023.ui | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.catch | ||
|
||
fun <T> Flow<T>.handleErrorAndRetryAction( | ||
actionLabel: String, | ||
userMessageStateHolder: UserMessageStateHolder, | ||
retryAction: suspend ((Throwable) -> Unit), | ||
) = catch { throwable -> | ||
val messageResult = userMessageStateHolder.showMessage( | ||
message = throwable.toApplicationErrorMessage(), | ||
actionLabel = actionLabel, | ||
) | ||
|
||
if (messageResult == UserMessageResult.ActionPerformed) { | ||
retryAction(throwable) | ||
} | ||
}.catch { /* Do nothing if the user dose not retry. */ } |
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
10 changes: 10 additions & 0 deletions
10
core/ui/src/androidMain/kotlin/io/github/droidkaigi/confsched2023/ui/KmpViewModel.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,10 @@ | ||
package io.github.droidkaigi.confsched2023.ui | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
actual typealias KmpViewModel = ViewModel | ||
|
||
actual val KmpViewModel.viewModelScope: CoroutineScope | ||
get() = this.viewModelScope |
15 changes: 15 additions & 0 deletions
15
.../src/androidMain/kotlin/io/github/droidkaigi/confsched2023/ui/MessageStateHolderModule.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,15 @@ | ||
package io.github.droidkaigi.confsched2023.ui | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
class MessageStateHolderModule { | ||
@Provides | ||
fun provideMessageStateHolder(): UserMessageStateHolder { | ||
return UserMessageStateHolderImpl() | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
core/ui/src/commonMain/kotlin/io/github/droidkaigi/confsched2023/ui/Annotations.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,4 @@ | ||
package io.github.droidkaigi.confsched2023.ui | ||
|
||
expect annotation class KmpHiltViewModel() | ||
expect annotation class KmpInject() |
7 changes: 7 additions & 0 deletions
7
core/ui/src/commonMain/kotlin/io/github/droidkaigi/confsched2023/ui/KmpViewModel.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,7 @@ | ||
package io.github.droidkaigi.confsched2023.ui | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
|
||
expect abstract class KmpViewModel() | ||
|
||
expect val KmpViewModel.viewModelScope: CoroutineScope |
4 changes: 4 additions & 0 deletions
4
core/ui/src/iosMain/kotlin/io/github/droidkaigi/confsched2023/ui/Annotations.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,4 @@ | ||
package io.github.droidkaigi.confsched2023.ui | ||
|
||
actual annotation class KmpHiltViewModel() | ||
actual annotation class KmpInject() |
9 changes: 9 additions & 0 deletions
9
core/ui/src/iosMain/kotlin/io/github/droidkaigi/confsched2023/ui/KmpViewModel.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,9 @@ | ||
package io.github.droidkaigi.confsched2023.ui | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.MainScope | ||
|
||
actual abstract class KmpViewModel | ||
actual val KmpViewModel.viewModelScope: CoroutineScope | ||
// TODO: Cancel scope | ||
get() = MainScope() |
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
14 changes: 12 additions & 2 deletions
14
...c/commonMain/kotlin/io/github/droidkaigi/confsched2023/contributors/ContributorsScreen.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 |
---|---|---|
@@ -1,11 +1,21 @@ | ||
package io.github.droidkaigi.confsched2023.contributors | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import io.github.droidkaigi.confsched2023.contributors.Contributors | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
|
||
@Composable | ||
fun ContributorsScreen() { | ||
fun ContributorsScreen(viewModel: ContributorsViewModel) { | ||
val contributors = Contributors() | ||
Text(text = contributors.greet()) | ||
val sessions by viewModel.sessions.collectAsState() | ||
Column { | ||
sessions.timetableItems.forEach { | ||
Text( | ||
text = it.title.currentLangTitle, | ||
) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ommonMain/kotlin/io/github/droidkaigi/confsched2023/contributors/ContributorsViewModel.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,22 @@ | ||
package io.github.droidkaigi.confsched2023.contributors | ||
|
||
import io.github.droidkaigi.confsched2023.model.SessionsRepository | ||
import io.github.droidkaigi.confsched2023.model.Timetable | ||
import io.github.droidkaigi.confsched2023.ui.KmpHiltViewModel | ||
import io.github.droidkaigi.confsched2023.ui.KmpInject | ||
import io.github.droidkaigi.confsched2023.ui.KmpViewModel | ||
import io.github.droidkaigi.confsched2023.ui.viewModelScope | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.stateIn | ||
|
||
@KmpHiltViewModel | ||
class ContributorsViewModel @KmpInject constructor(val sessionRepository: SessionsRepository) : | ||
KmpViewModel() { | ||
// FIXME | ||
val sessions = sessionRepository.getSessionsStream() | ||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), Timetable()) | ||
|
||
fun greet(): String { | ||
return "Hello, ${Contributors().greet()}" | ||
} | ||
} |
21 changes: 12 additions & 9 deletions
21
.../src/iosMain/kotlin/io/github/droidkaigi/confsched2023/contributors/DarwinContributors.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 |
---|---|---|
@@ -1,18 +1,21 @@ | ||
package io.github.droidkaigi.confsched2023.contributors | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material3.Text | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.ui.interop.LocalUIViewController | ||
import androidx.compose.ui.window.ComposeUIViewController | ||
import io.github.droidkaigi.confsched2023.data.DefaultSessionsRepository | ||
import io.github.droidkaigi.confsched2023.data.FakeSessionsApi | ||
import platform.UIKit.UIViewController | ||
|
||
@Suppress("UNUSED") | ||
fun viewController(): UIViewController = ComposeUIViewController { | ||
Column( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
) { | ||
Text("Hello") | ||
val viewModel = ContributorsViewModel(DefaultSessionsRepository(FakeSessionsApi())) | ||
val uiViewController = LocalUIViewController.current | ||
LaunchedEffect(uiViewController) { | ||
// uiViewController | ||
// TODO: How to know the destroy event of the ViewController? | ||
// viewModel.viewModelScope.cancel() | ||
} | ||
|
||
ContributorsScreen(viewModel) | ||
} |