forked from JetBrains/compose-multiplatform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add web app for the TodoApp example (JetBrains#778)
* Prepare the TodoApp example for adding the JavaScript app * Add the JavaScript app for the TodoApp example * TodoApp. Update Compose to 0.5.0-build225.
- Loading branch information
Showing
38 changed files
with
1,124 additions
and
160 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
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
13 changes: 0 additions & 13 deletions
13
...database/src/androidMain/kotlin/example/todo/common/database/TestDatabaseDriverFactory.kt
This file was deleted.
Oops, something went wrong.
91 changes: 91 additions & 0 deletions
91
.../database/src/commonMain/kotlin/example/todo/common/database/DefaultTodoSharedDatabase.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,91 @@ | ||
package example.todo.common.database | ||
|
||
import com.badoo.reaktive.base.setCancellable | ||
import com.badoo.reaktive.completable.Completable | ||
import com.badoo.reaktive.maybe.Maybe | ||
import com.badoo.reaktive.observable.Observable | ||
import com.badoo.reaktive.observable.autoConnect | ||
import com.badoo.reaktive.observable.firstOrError | ||
import com.badoo.reaktive.observable.map | ||
import com.badoo.reaktive.observable.observable | ||
import com.badoo.reaktive.observable.observeOn | ||
import com.badoo.reaktive.observable.replay | ||
import com.badoo.reaktive.scheduler.ioScheduler | ||
import com.badoo.reaktive.single.Single | ||
import com.badoo.reaktive.single.asCompletable | ||
import com.badoo.reaktive.single.asObservable | ||
import com.badoo.reaktive.single.doOnBeforeSuccess | ||
import com.badoo.reaktive.single.flatMapObservable | ||
import com.badoo.reaktive.single.map | ||
import com.badoo.reaktive.single.mapNotNull | ||
import com.badoo.reaktive.single.observeOn | ||
import com.badoo.reaktive.single.singleOf | ||
import com.squareup.sqldelight.Query | ||
import com.squareup.sqldelight.db.SqlDriver | ||
import example.todo.database.TodoDatabase | ||
|
||
class DefaultTodoSharedDatabase(driver: Single<SqlDriver>) : TodoSharedDatabase { | ||
|
||
constructor(driver: SqlDriver) : this(singleOf(driver)) | ||
|
||
private val queries: Single<TodoDatabaseQueries> = | ||
driver | ||
.map { TodoDatabase(it).todoDatabaseQueries } | ||
.asObservable() | ||
.replay() | ||
.autoConnect() | ||
.firstOrError() | ||
|
||
override fun observeAll(): Observable<List<TodoItemEntity>> = | ||
query(TodoDatabaseQueries::selectAll) | ||
.observe { it.executeAsList() } | ||
|
||
override fun select(id: Long): Maybe<TodoItemEntity> = | ||
query { it.select(id = id) } | ||
.mapNotNull { it.executeAsOneOrNull() } | ||
|
||
override fun add(text: String): Completable = | ||
execute { it.add(text = text) } | ||
|
||
override fun setText(id: Long, text: String): Completable = | ||
execute { it.setText(id = id, text = text) } | ||
|
||
override fun setDone(id: Long, isDone: Boolean): Completable = | ||
execute { it.setDone(id = id, isDone = isDone) } | ||
|
||
override fun delete(id: Long): Completable = | ||
execute { it.delete(id = id) } | ||
|
||
override fun clear(): Completable = | ||
execute { it.clear() } | ||
|
||
private fun <T : Any> query(query: (TodoDatabaseQueries) -> Query<T>): Single<Query<T>> = | ||
queries | ||
.observeOn(ioScheduler) | ||
.map(query) | ||
|
||
private fun execute(query: (TodoDatabaseQueries) -> Unit): Completable = | ||
queries | ||
.observeOn(ioScheduler) | ||
.doOnBeforeSuccess(query) | ||
.asCompletable() | ||
|
||
private fun <T : Any, R> Single<Query<T>>.observe(get: (Query<T>) -> R): Observable<R> = | ||
flatMapObservable { it.observed() } | ||
.observeOn(ioScheduler) | ||
.map(get) | ||
|
||
private fun <T : Any> Query<T>.observed(): Observable<Query<T>> = | ||
observable { emitter -> | ||
val listener = | ||
object : Query.Listener { | ||
override fun queryResultsChanged() { | ||
emitter.onNext(this@observed) | ||
} | ||
} | ||
|
||
emitter.onNext(this@observed) | ||
addListener(listener) | ||
emitter.setCancellable { removeListener(listener) } | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
...todoapp/common/database/src/commonMain/kotlin/example/todo/common/database/ReaktiveExt.kt
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
.../database/src/commonMain/kotlin/example/todo/common/database/TestDatabaseDriverFactory.kt
This file was deleted.
Oops, something went wrong.
105 changes: 105 additions & 0 deletions
105
...mon/database/src/commonMain/kotlin/example/todo/common/database/TestTodoSharedDatabase.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,105 @@ | ||
package example.todo.common.database | ||
|
||
import com.badoo.reaktive.base.invoke | ||
import com.badoo.reaktive.completable.Completable | ||
import com.badoo.reaktive.completable.completableFromFunction | ||
import com.badoo.reaktive.completable.observeOn | ||
import com.badoo.reaktive.maybe.Maybe | ||
import com.badoo.reaktive.maybe.observeOn | ||
import com.badoo.reaktive.observable.Observable | ||
import com.badoo.reaktive.observable.map | ||
import com.badoo.reaktive.observable.observeOn | ||
import com.badoo.reaktive.scheduler.Scheduler | ||
import com.badoo.reaktive.single.notNull | ||
import com.badoo.reaktive.single.singleFromFunction | ||
import com.badoo.reaktive.subject.behavior.BehaviorSubject | ||
|
||
// There were problems when using real database in JS tests, hence the in-memory test implementation | ||
class TestTodoSharedDatabase( | ||
private val scheduler: Scheduler | ||
) : TodoSharedDatabase { | ||
|
||
private val itemsSubject = BehaviorSubject<Map<Long, TodoItemEntity>>(emptyMap()) | ||
private val itemsObservable = itemsSubject.observeOn(scheduler) | ||
val testing: Testing = Testing() | ||
|
||
override fun observeAll(): Observable<List<TodoItemEntity>> = | ||
itemsObservable.map { it.values.toList() } | ||
|
||
override fun select(id: Long): Maybe<TodoItemEntity> = | ||
singleFromFunction { testing.select(id = id) } | ||
.notNull() | ||
.observeOn(scheduler) | ||
|
||
override fun add(text: String): Completable = | ||
execute { testing.add(text = text) } | ||
|
||
override fun setText(id: Long, text: String): Completable = | ||
execute { testing.setText(id = id, text = text) } | ||
|
||
override fun setDone(id: Long, isDone: Boolean): Completable = | ||
execute { testing.setDone(id = id, isDone = isDone) } | ||
|
||
override fun delete(id: Long): Completable = | ||
execute { testing.delete(id = id) } | ||
|
||
override fun clear(): Completable = | ||
execute { testing.clear() } | ||
|
||
private fun execute(block: () -> Unit): Completable = | ||
completableFromFunction(block) | ||
.observeOn(scheduler) | ||
|
||
inner class Testing { | ||
fun select(id: Long): TodoItemEntity? = | ||
itemsSubject.value[id] | ||
|
||
fun selectRequired(id: Long): TodoItemEntity = | ||
requireNotNull(select(id = id)) | ||
|
||
fun add(text: String) { | ||
updateItems { items -> | ||
val nextId = items.keys.maxOrNull()?.plus(1L) ?: 1L | ||
|
||
val item = | ||
TodoItemEntity( | ||
id = nextId, | ||
orderNum = items.size.toLong(), | ||
text = text, | ||
isDone = false | ||
) | ||
|
||
items + (nextId to item) | ||
} | ||
} | ||
|
||
fun setText(id: Long, text: String) { | ||
updateItem(id = id) { it.copy(text = text) } | ||
} | ||
|
||
fun setDone(id: Long, isDone: Boolean) { | ||
updateItem(id = id) { it.copy(isDone = isDone) } | ||
} | ||
|
||
fun delete(id: Long) { | ||
updateItems { it - id } | ||
} | ||
|
||
fun clear() { | ||
updateItems { emptyMap() } | ||
} | ||
|
||
fun getLastInsertId(): Long? = | ||
itemsSubject.value.values.lastOrNull()?.id | ||
|
||
private fun updateItems(func: (Map<Long, TodoItemEntity>) -> Map<Long, TodoItemEntity>) { | ||
itemsSubject(func(itemsSubject.value)) | ||
} | ||
|
||
private fun updateItem(id: Long, func: (TodoItemEntity) -> TodoItemEntity) { | ||
updateItems { | ||
it + (id to it.getValue(id).let(func)) | ||
} | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
.../common/database/src/commonMain/kotlin/example/todo/common/database/TodoSharedDatabase.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 example.todo.common.database | ||
|
||
import com.badoo.reaktive.completable.Completable | ||
import com.badoo.reaktive.maybe.Maybe | ||
import com.badoo.reaktive.observable.Observable | ||
|
||
interface TodoSharedDatabase { | ||
|
||
fun observeAll(): Observable<List<TodoItemEntity>> | ||
|
||
fun select(id: Long): Maybe<TodoItemEntity> | ||
|
||
fun add(text: String): Completable | ||
|
||
fun setText(id: Long, text: String): Completable | ||
|
||
fun setDone(id: Long, isDone: Boolean): Completable | ||
|
||
fun delete(id: Long): Completable | ||
|
||
fun clear(): Completable | ||
} |
13 changes: 0 additions & 13 deletions
13
...database/src/desktopMain/kotlin/example/todo/common/database/TestDatabaseDriverFactory.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.