-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prototype on how ui state/controller could work
- Loading branch information
Showing
7 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
app/src/main/java/com/dropbox/android/sample/ui/uistate/UIStateFragment.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,61 @@ | ||
package com.dropbox.android.sample.ui.uistate | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.lifecycleScope | ||
import com.dropbox.android.external.store4.Fetcher | ||
import com.dropbox.android.external.store4.StoreBuilder | ||
import com.dropbox.android.external.store4.StoreRequest | ||
import com.dropbox.android.external.store4.UIController | ||
import com.dropbox.android.sample.R | ||
import kotlinx.android.synthetic.main.ui_state.buttonRefresh | ||
import kotlinx.android.synthetic.main.ui_state.loadingInfo | ||
import kotlinx.android.synthetic.main.ui_state.streamValue | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.flow | ||
|
||
|
||
class UIStateFragment : Fragment() { | ||
val store = StoreBuilder.from<Int, Int>( | ||
fetcher = Fetcher.ofFlow {key:Int -> | ||
flow { | ||
var latest = key | ||
while(true) { | ||
delay(2000) | ||
emit(latest) | ||
latest += 2 | ||
|
||
} | ||
} | ||
} | ||
).build() | ||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
return inflater.inflate(R.layout.ui_state, container, false) | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
val controller = UIController( | ||
store = store, | ||
request = StoreRequest.fresh(3) | ||
) | ||
buttonRefresh.setOnClickListener { | ||
controller.refresh() | ||
} | ||
viewLifecycleOwner.lifecycleScope.launchWhenStarted { | ||
controller.state.collect { | ||
streamValue.text = "${it.data?.dataOrNull()}" | ||
loadingInfo.text = "loading: ${it.isLoading()}" | ||
} | ||
} | ||
} | ||
} |
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,20 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" | ||
android:orientation="vertical" | ||
android:layout_height="match_parent"> | ||
<Button | ||
android:id="@+id/buttonRefresh" | ||
android:text="refresh" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content"/> | ||
<TextView | ||
android:id="@+id/streamValue" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"/> | ||
<TextView | ||
android:id="@+id/loadingInfo" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content"/> | ||
|
||
</LinearLayout> |
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
71 changes: 71 additions & 0 deletions
71
store/src/main/java/com/dropbox/android/external/store4/UIState.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,71 @@ | ||
package com.dropbox.android.external.store4 | ||
|
||
import kotlinx.coroutines.channels.ConflatedBroadcastChannel | ||
import kotlinx.coroutines.flow.asFlow | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.emitAll | ||
import kotlinx.coroutines.flow.flatMapLatest | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.fold | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.mapLatest | ||
|
||
data class UIState<T>( | ||
val loading: StoreResponse.Loading? = null, | ||
val data: StoreResponse.Data<T>? = null, | ||
val error: StoreResponse.Error? = null | ||
) { | ||
fun isLoading() = loading != null | ||
internal fun combine(response: StoreResponse<T>) : UIState<T> { | ||
return when(response) { | ||
is StoreResponse.Loading -> { | ||
copy( | ||
loading = response, | ||
error = null | ||
) | ||
} | ||
is StoreResponse.Data -> { | ||
copy( | ||
loading = null, | ||
data = response, | ||
error = null | ||
) | ||
} | ||
is StoreResponse.Error -> { | ||
copy( | ||
error = response, | ||
loading = null | ||
) | ||
} | ||
is StoreResponse.NoNewData -> { | ||
copy( | ||
loading = null | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
class UIController<Key : Any, Output : Any>( | ||
val store: Store<Key, Output>, | ||
val request: StoreRequest<Key> | ||
) { | ||
// use mutable state flow instead when we can | ||
private val refreshTrigger = ConflatedBroadcastChannel<Unit>().also { | ||
it.offer(Unit) | ||
} | ||
|
||
|
||
fun refresh() = refreshTrigger.offer(Unit) | ||
val state = flow<UIState<Output>> { | ||
var prevState : UIState<Output> = UIState() | ||
emitAll(refreshTrigger.asFlow().flatMapLatest { | ||
store.stream(request) | ||
}.map { | ||
prevState.combine(it).also { | ||
prevState = it | ||
} | ||
prevState | ||
}) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
store/src/main/java/com/dropbox/android/external/store4/impl/FallbackController.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,24 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.dropbox.android.external.store4.impl | ||
|
||
/** | ||
* Internal helper class to help with fallback logic from Fetcher to local data | ||
*/ | ||
internal class FallbackController { | ||
|
||
} |