-
Notifications
You must be signed in to change notification settings - Fork 107
[Teacher][MBL-12224] Add mobius and modules moflow to teacher #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
45 changes: 45 additions & 0 deletions
45
apps/teacher/src/main/java/com/instructure/teacher/mobius/common/ConsumerQueueWrapper.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Copyright (C) 2019 - present Instructure, Inc. | ||
| * | ||
| * 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, version 3 of the License. | ||
| * | ||
| * 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 com.instructure.teacher.mobius.common | ||
|
|
||
| import com.spotify.mobius.functions.Consumer | ||
| import java.util.* | ||
|
|
||
| class ConsumerQueueWrapper<T> : Consumer<T> { | ||
|
|
||
| private val queuedEvents = LinkedList<T>() | ||
|
|
||
| private var wrappedConsumer: Consumer<T>? = null | ||
|
|
||
| fun attach(consumer: Consumer<T>) { | ||
| wrappedConsumer = consumer | ||
| while (queuedEvents.isNotEmpty()) consumer.accept(queuedEvents.poll()) | ||
| } | ||
|
|
||
| fun detach() { | ||
| wrappedConsumer = null | ||
| } | ||
|
|
||
| override fun accept(event: T) { | ||
| if (wrappedConsumer == null) { | ||
| queuedEvents.add(event) | ||
| } else { | ||
| wrappedConsumer?.accept(event) | ||
| } | ||
| } | ||
| } | ||
|
|
36 changes: 36 additions & 0 deletions
36
apps/teacher/src/main/java/com/instructure/teacher/mobius/common/CoroutineConnection.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Copyright (C) 2019 - present Instructure, Inc. | ||
| * | ||
| * 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, version 3 of the License. | ||
| * | ||
| * 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 com.instructure.teacher.mobius.common | ||
|
|
||
| import com.spotify.mobius.Connection | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.SupervisorJob | ||
| import kotlinx.coroutines.cancelChildren | ||
| import kotlin.coroutines.CoroutineContext | ||
|
|
||
| abstract class CoroutineConnection<T> : Connection<T>, CoroutineScope { | ||
|
|
||
| private val job = SupervisorJob() | ||
|
|
||
| override val coroutineContext: CoroutineContext | ||
| get() = job + Dispatchers.Main | ||
|
|
||
| fun cancelCoroutine() { | ||
| coroutineContext.cancelChildren() | ||
| } | ||
| } |
86 changes: 86 additions & 0 deletions
86
apps/teacher/src/main/java/com/instructure/teacher/mobius/common/GlobalEvents.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Copyright (C) 2019 - present Instructure, Inc. | ||
| * | ||
| * 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, version 3 of the License. | ||
| * | ||
| * 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 com.instructure.teacher.mobius.common | ||
|
|
||
| import com.spotify.mobius.EventSource | ||
| import com.spotify.mobius.disposables.Disposable | ||
| import com.spotify.mobius.functions.Consumer | ||
| import java.util.* | ||
|
|
||
|
|
||
| sealed class GlobalEvent { | ||
| data class TestEvent(val testValue: Int) : GlobalEvent() | ||
| } | ||
|
|
||
| object GlobalEvents { | ||
|
|
||
| val subscribers: MutableSet<GlobalEventSource<*>> = Collections.newSetFromMap(WeakHashMap<GlobalEventSource<*>, Boolean>()) | ||
|
|
||
| fun post(event: GlobalEvent) { | ||
| publish(event) | ||
| } | ||
|
|
||
| private fun publish(event: GlobalEvent) { | ||
| synchronized(subscribers) { | ||
| subscribers.forEach { it.postEvent(event) } | ||
| } | ||
| } | ||
|
|
||
| fun subscribe(eventSource: GlobalEventSource<*>) { | ||
| synchronized(subscribers) { subscribers += eventSource } | ||
| } | ||
|
|
||
| fun unsubscribe(eventSource: GlobalEventSource<*>) { | ||
| synchronized(subscribers) { subscribers -= eventSource } | ||
| } | ||
| } | ||
|
|
||
| interface GlobalEventMapper<E> { | ||
| fun mapGlobalEvent(event: GlobalEvent): E? = null | ||
| } | ||
|
|
||
| class GlobalEventSource<E>(private val mapper: GlobalEventMapper<E>) : EventSource<E> { | ||
|
|
||
| private val queuedEvents = LinkedList<E>() | ||
|
|
||
| private var consumer: Consumer<E>? = null | ||
|
|
||
| override fun subscribe(eventConsumer: Consumer<E>): Disposable { | ||
| consumer = eventConsumer | ||
| GlobalEvents.subscribe(this) | ||
| while (queuedEvents.isNotEmpty()) { | ||
| consumer?.accept(queuedEvents.poll()) | ||
| } | ||
| return Disposable { consumer = null } | ||
| } | ||
|
|
||
| fun dispose() { | ||
| consumer = null | ||
| GlobalEvents.unsubscribe(this) | ||
| } | ||
|
|
||
| fun postEvent(event: GlobalEvent) { | ||
| mapper.mapGlobalEvent(event)?.let { localEvent -> | ||
| if (consumer == null) { | ||
| queuedEvents.add(localEvent) | ||
| } else { | ||
| consumer?.accept(localEvent) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
33 changes: 33 additions & 0 deletions
33
apps/teacher/src/main/java/com/instructure/teacher/mobius/common/LateInit.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright (C) 2019 - present Instructure, Inc. | ||
| * | ||
| * 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, version 3 of the License. | ||
| * | ||
| * 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 com.instructure.teacher.mobius.common | ||
|
|
||
| import kotlin.properties.ReadWriteProperty | ||
| import kotlin.reflect.KProperty | ||
|
|
||
| class LateInit<T>(val onInit: (T) -> T) : ReadWriteProperty<Any?, T> { | ||
| private var value: T? = null | ||
|
|
||
| override fun getValue(thisRef: Any?, property: KProperty<*>): T { | ||
| return value ?: throw UninitializedPropertyAccessException() | ||
| } | ||
|
|
||
| override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { | ||
| this.value = if (this.value == null) onInit(value) else value | ||
| } | ||
| } | ||
|
|
46 changes: 46 additions & 0 deletions
46
apps/teacher/src/main/java/com/instructure/teacher/mobius/common/MobiusKotlinUtils.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright (C) 2019 - present Instructure, Inc. | ||
| * | ||
| * 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, version 3 of the License. | ||
| * | ||
| * 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 com.instructure.teacher.mobius.common | ||
|
|
||
| import android.content.Context | ||
| import com.spotify.mobius.Connectable | ||
| import com.spotify.mobius.Connection | ||
| import kotlin.reflect.KFunction2 | ||
|
|
||
| fun <I, J, O> Connectable<I, O>.contraMap( | ||
| mapper: KFunction2<J, Context, I>, | ||
| context: Context | ||
| ): Connectable<J, O> { | ||
| return Connectable { output -> | ||
| val delegateConnection = connect(output) | ||
| object : Connection<J> { | ||
| var lastValue: I? = null | ||
|
|
||
| override fun accept(value: J) { | ||
| val mappedValue: I = mapper(value, context) | ||
| // Only push value if it has changed (prevents duplicate renders) | ||
| if (mappedValue != lastValue) { | ||
| lastValue = mappedValue | ||
| delegateConnection.accept(mappedValue) | ||
| } | ||
| } | ||
|
|
||
| override fun dispose() = delegateConnection.dispose() | ||
| } | ||
| } | ||
| } | ||
|
|
38 changes: 38 additions & 0 deletions
38
.../teacher/src/main/java/com/instructure/teacher/mobius/common/modules/ModulesListModels.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright (C) 2019 - present Instructure, Inc. | ||
| * | ||
| * 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, version 3 of the License. | ||
| * | ||
| * 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 com.instructure.teacher.mobius.common.modules | ||
|
|
||
| import com.instructure.canvasapi2.models.CanvasContext | ||
| import com.instructure.canvasapi2.models.ModuleItem | ||
| import com.instructure.canvasapi2.utils.DataResult | ||
|
|
||
| sealed class ModulesListEvent { | ||
| object PullToRefresh : ModulesListEvent() | ||
| data class ModuleClicked(val moduleItem: ModuleItem) : ModulesListEvent() | ||
| data class DataLoaded(val modulesResult: DataResult<ModuleItem>) : ModulesListEvent() | ||
| } | ||
|
|
||
| sealed class ModulesListEffect { | ||
| data class ShowModuleDetailView(val moduleItem: ModuleItem) : ModulesListEffect() | ||
| data class LoadData(val canvasContext: CanvasContext, val forceNetwork: Boolean) : ModulesListEffect() | ||
| } | ||
|
|
||
| data class ModulesListModel( | ||
| val course: CanvasContext, | ||
| val isLoading: Boolean = false, | ||
| val modulesResult: DataResult<ModuleItem>? = null | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.