-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Showing
32 changed files
with
423 additions
and
319 deletions.
There are no files selected for viewing
45 changes: 0 additions & 45 deletions
45
androidApp/src/main/kotlin/app/softwork/composetodo/Container.kt
This file was deleted.
Oops, something went wrong.
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
48 changes: 34 additions & 14 deletions
48
clients/src/commonMain/kotlin/app/softwork/composetodo/AppContainer.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,29 +1,49 @@ | ||
package app.softwork.composetodo | ||
|
||
import app.softwork.composetodo.repository.* | ||
import app.softwork.composetodo.viewmodels.* | ||
import io.ktor.client.* | ||
import io.ktor.utils.io.errors.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.flow.* | ||
|
||
interface AppContainer { | ||
fun todoViewModel(api: API.LoggedIn): TodoViewModel | ||
fun loginViewModel(api: API.LoggedOut): LoginViewModel | ||
fun registerViewModel(api: API.LoggedOut): RegisterViewModel | ||
class AppContainer( | ||
val client: HttpClient, | ||
val db: ComposeTodoDB | ||
) : ViewModel() { | ||
|
||
val client: HttpClient | ||
fun todoViewModel(): TodoViewModel { | ||
val api = api.value as API.LoggedIn | ||
return TodoViewModel(TodoRepository(api, db.todoQueries)) | ||
} | ||
fun loginViewModel(): LoginViewModel { | ||
val api = api.value as API.LoggedOut | ||
return LoginViewModel(api) { | ||
this.api.value = it | ||
} | ||
} | ||
fun registerViewModel(): RegisterViewModel { | ||
val api = api.value as API.LoggedOut | ||
return RegisterViewModel(api) { | ||
this.api.value = it | ||
} | ||
} | ||
|
||
suspend fun logout() { | ||
when (val login = api.value) { | ||
is API.LoggedIn -> { | ||
try { | ||
login.logout() | ||
} catch (_: IOException) { | ||
fun logout() { | ||
lifecycleScope.launch { | ||
when (val login = api.value) { | ||
is API.LoggedIn -> { | ||
try { | ||
login.logout() | ||
} catch (_: IOException) { | ||
} | ||
api.value = API.LoggedOut(client) | ||
} | ||
api.value = API.LoggedOut(client) | ||
|
||
is API.LoggedOut -> {} | ||
} | ||
is API.LoggedOut -> {} | ||
} | ||
} | ||
|
||
val api: MutableStateFlow<API> | ||
val api: MutableStateFlow<API> = MutableStateFlow(API.LoggedOut(client)) | ||
} |
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
File renamed without changes.
41 changes: 41 additions & 0 deletions
41
clients/src/darwinMain/kotlin/app/softwork/composetodo/IosContainer.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,41 @@ | ||
package app.softwork.composetodo | ||
|
||
import app.cash.sqldelight.driver.native.* | ||
import app.softwork.composetodo.repository.* | ||
import io.ktor.client.* | ||
import io.ktor.client.engine.darwin.* | ||
import io.ktor.client.plugins.* | ||
import io.ktor.client.plugins.contentnegotiation.* | ||
import io.ktor.client.plugins.cookies.* | ||
import io.ktor.client.plugins.logging.* | ||
import io.ktor.client.plugins.resources.* | ||
import io.ktor.http.* | ||
import io.ktor.serialization.kotlinx.json.* | ||
|
||
fun IosContainer( | ||
protocol: URLProtocol, | ||
host: String, | ||
storage: CookiesStorage | ||
): AppContainer { | ||
val client = HttpClient(Darwin) { | ||
install(HttpCookies) { | ||
this.storage = storage | ||
} | ||
install(DefaultRequest) { | ||
url { | ||
this.protocol = protocol | ||
this.host = host | ||
} | ||
} | ||
install(Logging) { | ||
level = LogLevel.ALL | ||
} | ||
install(Resources) | ||
install(ContentNegotiation) { | ||
json() | ||
} | ||
} | ||
val db = TodoRepository.createDatabase(NativeSqliteDriver(ComposeTodoDB.Schema, "composetodo.db")) | ||
|
||
return AppContainer(client, db) | ||
} |
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
File renamed without changes.
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
Oops, something went wrong.