Skip to content

Commit 5af0cd8

Browse files
author
Patrick Jackson
committed
[WIP] update to redux-thunk 0.2.8
1 parent d494ac9 commit 5af0cd8

File tree

24 files changed

+157
-138
lines changed

24 files changed

+157
-138
lines changed

android/src/main/java/com/jackson/openlibrary/OpenLibraryApp.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import com.squareup.sqldelight.android.AndroidSqliteDriver
1111
import com.willowtree.common.LibraryDatabase
1212
import com.willowtreeapps.common.LibraryApp
1313
import com.willowtreeapps.common.Logger
14-
import com.willowtreeapps.common.createDatabase
14+
import com.willowtreeapps.common.repo.createDatabase
1515
import kotlinx.coroutines.Dispatchers
16-
import org.reduxkotlin.Dispatcher
1716

1817
class OpenLibraryApp : Application() {
1918

android/src/main/java/com/jackson/openlibrary/store/BaseLibraryViewFragment.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import androidx.fragment.app.Fragment
55
import androidx.lifecycle.*
66
import com.willowtreeapps.common.AppState
77
import com.willowtreeapps.common.external.*
8+
import com.willowtreeapps.common.ui.LibraryBaseView
89

9-
abstract class BaseLibraryViewFragment<V: ViewWithProvider>: Fragment(), ViewWithProvider {
10+
abstract class BaseLibraryViewFragment<V: LibraryBaseView>: Fragment(), LibraryBaseView {
1011

1112
private val presenterObserver = PresenterLifecycleObserver(this)
1213
private var viewRecreated: Boolean = false
@@ -56,7 +57,7 @@ abstract class BaseLibraryViewFragment<V: ViewWithProvider>: Fragment(), ViewWit
5657
}
5758

5859

59-
class PresenterLifecycleObserver(val view: ViewWithProvider): LifecycleObserver {
60+
class PresenterLifecycleObserver(val view: ViewWithProvider<*>): LifecycleObserver {
6061

6162
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
6263
fun onAttach() {
Lines changed: 86 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,101 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
2+
<androidx.constraintlayout.widget.ConstraintLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
34
xmlns:app="http://schemas.android.com/apk/res-auto"
45
xmlns:tools="http://schemas.android.com/tools"
5-
android:background="@color/white"
6+
android:layout_height="match_parent"
67
android:layout_width="match_parent"
7-
android:layout_height="match_parent">
8+
>
9+
<androidx.appcompat.widget.Toolbar
10+
android:id="@+id/toolbar"
11+
app:layout_constraintTop_toTopOf="parent"
12+
app:layout_constraintStart_toStartOf="parent"
13+
app:layout_constraintEnd_toEndOf="parent"
14+
android:layout_width="match_parent"
15+
app:navigationIcon="@drawable/ic_arrow_back_black_24dp"
16+
app:background="@color/colorPrimary"
17+
android:layout_height="56dp"/>
818

9-
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
1020
xmlns:app="http://schemas.android.com/apk/res-auto"
1121
xmlns:tools="http://schemas.android.com/tools"
22+
app:layout_constraintTop_toBottomOf="@id/toolbar"
1223
android:layout_width="match_parent"
13-
android:layout_height="match_parent">
14-
15-
<androidx.appcompat.widget.Toolbar
16-
android:id="@+id/toolbar"
17-
android:layout_width="match_parent"
18-
android:layout_height="45dp"
19-
app:layout_constraintTop_toTopOf="parent"
20-
app:navigationIcon="@drawable/ic_arrow_back_black_24dp"/>
24+
android:layout_height="match_parent"
25+
android:background="@color/white">
2126

22-
<ImageView
23-
android:id="@+id/imgBook"
27+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
28+
xmlns:app="http://schemas.android.com/apk/res-auto"
29+
xmlns:tools="http://schemas.android.com/tools"
2430
android:layout_width="match_parent"
25-
android:layout_height="500dp"
26-
app:layout_constraintTop_toBottomOf="@id/toolbar"
27-
tools:src="@drawable/ic_library_books_white_24dp" />
31+
android:layout_height="match_parent">
32+
33+
<ImageView
34+
android:id="@+id/imgBook"
35+
android:layout_width="match_parent"
36+
android:layout_height="500dp"
37+
app:layout_constraintTop_toBottomOf="parent"
38+
tools:src="@drawable/ic_library_books_white_24dp" />
39+
40+
<TextView
41+
android:id="@+id/txtTitle"
42+
android:layout_width="wrap_content"
43+
android:layout_height="wrap_content"
44+
android:layout_marginStart="16dp"
45+
android:layout_marginTop="16dp"
46+
android:layout_marginEnd="16dp"
47+
android:gravity="center"
48+
android:maxLines="3"
49+
android:textSize="34sp"
50+
app:layout_constraintEnd_toEndOf="parent"
51+
app:layout_constraintStart_toStartOf="parent"
52+
app:layout_constraintTop_toBottomOf="@+id/imgBook"
53+
tools:text="The Picture of Dorian Gray on Saturday" />
54+
55+
<TextView
56+
android:id="@+id/txtAuthorLabel"
57+
android:layout_width="wrap_content"
58+
android:layout_height="wrap_content"
59+
android:layout_marginStart="16dp"
60+
android:layout_marginTop="16dp"
61+
android:text="@string/author_label"
62+
app:layout_constraintStart_toStartOf="parent"
63+
app:layout_constraintTop_toBottomOf="@+id/txtTitle" />
2864

29-
<TextView
30-
android:id="@+id/txtTitle"
31-
android:layout_width="wrap_content"
32-
android:layout_height="wrap_content"
33-
android:layout_marginStart="16dp"
34-
android:layout_marginTop="16dp"
35-
android:layout_marginEnd="16dp"
36-
android:gravity="center"
37-
android:maxLines="3"
38-
android:textSize="34sp"
39-
app:layout_constraintEnd_toEndOf="parent"
40-
app:layout_constraintStart_toStartOf="parent"
41-
app:layout_constraintTop_toBottomOf="@+id/imgBook"
42-
tools:text="The Picture of Dorian Gray on Saturday" />
65+
<TextView
66+
android:id="@+id/txtAuthorName"
67+
android:layout_width="wrap_content"
68+
android:layout_height="wrap_content"
69+
android:layout_marginTop="16dp"
70+
app:layout_constraintStart_toEndOf="@+id/txtAuthorLabel"
71+
app:layout_constraintTop_toBottomOf="@+id/txtTitle"
72+
tools:text="James Barr" />
4373

44-
<TextView
45-
android:id="@+id/txtAuthorLabel"
46-
android:layout_width="wrap_content"
47-
android:layout_height="wrap_content"
48-
android:layout_marginStart="16dp"
49-
android:layout_marginTop="16dp"
50-
android:text="@string/author_label"
51-
app:layout_constraintStart_toStartOf="parent"
52-
app:layout_constraintTop_toBottomOf="@+id/txtTitle" />
5374

54-
<TextView
55-
android:id="@+id/txtAuthorName"
56-
android:layout_width="wrap_content"
57-
android:layout_height="wrap_content"
58-
android:layout_marginTop="16dp"
59-
app:layout_constraintStart_toEndOf="@+id/txtAuthorLabel"
60-
app:layout_constraintTop_toBottomOf="@+id/txtTitle"
61-
tools:text="James Barr" />
6275

63-
<com.google.android.material.button.MaterialButton
64-
android:id="@+id/btnToRead"
65-
android:layout_width="160dp"
66-
android:layout_height="45dp"
67-
android:layout_marginTop="24dp"
68-
android:text="@string/to_read_btn"
69-
app:icon="@drawable/ic_bookmark_white_24dp"
70-
app:layout_constraintEnd_toStartOf="@+id/btnCompleted"
71-
app:layout_constraintHorizontal_bias="0.5"
72-
app:layout_constraintStart_toStartOf="parent"
73-
app:layout_constraintTop_toBottomOf="@id/txtAuthorName" />
76+
<com.google.android.material.button.MaterialButton
77+
android:id="@+id/btnToRead"
78+
android:layout_width="160dp"
79+
android:layout_height="45dp"
80+
android:layout_marginTop="24dp"
81+
android:text="@string/to_read_btn"
82+
app:icon="@drawable/ic_bookmark_white_24dp"
83+
app:layout_constraintEnd_toStartOf="@+id/btnCompleted"
84+
app:layout_constraintHorizontal_bias="0.5"
85+
app:layout_constraintStart_toStartOf="parent"
86+
app:layout_constraintTop_toBottomOf="@id/txtAuthorName" />
7487

75-
<com.google.android.material.button.MaterialButton
76-
android:id="@+id/btnCompleted"
77-
android:layout_width="160dp"
78-
android:layout_height="45dp"
79-
android:layout_marginTop="24dp"
80-
android:text="@string/completed_btn"
81-
app:icon="@drawable/ic_library_books_white_24dp"
82-
app:layout_constraintEnd_toEndOf="parent"
83-
app:layout_constraintHorizontal_bias="0.5"
84-
app:layout_constraintStart_toEndOf="@+id/btnToRead"
85-
app:layout_constraintTop_toBottomOf="@id/txtAuthorName" />
86-
</androidx.constraintlayout.widget.ConstraintLayout>
87-
</ScrollView>
88+
<com.google.android.material.button.MaterialButton
89+
android:id="@+id/btnCompleted"
90+
android:layout_width="160dp"
91+
android:layout_height="45dp"
92+
android:layout_marginTop="24dp"
93+
android:text="@string/completed_btn"
94+
app:icon="@drawable/ic_library_books_white_24dp"
95+
app:layout_constraintEnd_toEndOf="parent"
96+
app:layout_constraintHorizontal_bias="0.5"
97+
app:layout_constraintStart_toEndOf="@+id/btnToRead"
98+
app:layout_constraintTop_toBottomOf="@id/txtAuthorName" />
99+
</androidx.constraintlayout.widget.ConstraintLayout>
100+
</ScrollView>
101+
</androidx.constraintlayout.widget.ConstraintLayout>

common/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ kotlin {
4949
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serializationVersion"
5050
implementation "io.ktor:ktor-client-logging:$ktorVersion"
5151
implementation "com.willowtreeapps:fuzzywuzzy-kotlin:0.2.1"
52-
implementation "org.reduxkotlin:redux-kotlin:0.2.4"
53-
implementation "org.reduxkotlin:redux-kotlin-thunk:0.2.6"
52+
implementation "org.reduxkotlin:redux-kotlin:0.2.6"
53+
implementation "org.reduxkotlin:redux-kotlin-thunk:0.2.8"
5454
implementation "org.reduxkotlin:redux-kotlin-reselect:0.2.7"
5555

5656

common/src/commonMain/kotlin/com/willowtreeapps/common/LibraryApp.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.willowtreeapps.common
22

33
import com.willowtree.common.LibraryDatabase
4-
import com.willowtreeapps.common.external.ViewWithProvider
5-
import com.willowtreeapps.common.external.coroutineDispatcher
6-
import com.willowtreeapps.common.external.presenterEnhancer
7-
import com.willowtreeapps.common.external.presenterMiddleware
4+
import com.willowtreeapps.common.external.*
85
import com.willowtreeapps.common.middleware.*
96
import com.willowtreeapps.common.repo.*
107
import org.reduxkotlin.*
@@ -20,7 +17,7 @@ class LibraryApp(navigator: Navigator,
2017

2118
val store by lazy {
2219
createStore(combineReducers(reducer, navigationReducer), AppState.INITIAL_STATE, compose(listOf(
23-
presenterEnhancer,
20+
presenterEnhancer(),
2421
applyMiddleware(
2522
presenterMiddleware(uiContext),
2623
coroutineDispatcher(uiContext),
@@ -38,7 +35,7 @@ class LibraryApp(navigator: Navigator,
3835
}
3936

4037
val state: AppState
41-
get() = store.state as AppState
38+
get() = store.state
4239
}
4340

4441

common/src/commonMain/kotlin/com/willowtreeapps/common/NavigationReducer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.willowtreeapps.common
22

33

4-
val navigationReducer = castingReducer { state: AppState, action ->
4+
val navigationReducer = reducer { state: AppState, action ->
55
if (action is NavigationActions.GotoScreen) {
66
state.copy(currentScreen = action.screen)
77
} else {

common/src/commonMain/kotlin/com/willowtreeapps/common/NetworkThunks.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package com.willowtreeapps.common
22

33
import com.willowtreeapps.common.repo.*
44
import kotlinx.coroutines.*
5-
import org.reduxkotlin.*
5+
import org.reduxkotlin.Dispatcher
6+
import org.reduxkotlin.GetState
7+
import org.reduxkotlin.createThunk
68
import kotlin.coroutines.CoroutineContext
79

810
/**
@@ -15,7 +17,7 @@ class NetworkThunks(private val networkContext: CoroutineContext,
1517
override val coroutineContext: CoroutineContext
1618
get() = networkContext + job
1719

18-
fun fetchBooksThunk(query: String) = createThunk { dispatch, getState, extraArgument ->
20+
fun fetchBooksThunk(query: String) = thunk { dispatch, _, _ ->
1921
Logger.d("Fetching Books and Feed")
2022
launch {
2123
dispatch(Actions.FetchingItemsStartedAction())
@@ -28,3 +30,9 @@ class NetworkThunks(private val networkContext: CoroutineContext,
2830
}
2931
}
3032
}
33+
34+
/**
35+
* Convenience function so state type does is not needed every time a thunk is created.
36+
*/
37+
fun thunk(thunkLambda: (dispatch: Dispatcher, getState: GetState<AppState>, extraArgument: Any?) -> Any) =
38+
createThunk(thunkLambda)

common/src/commonMain/kotlin/com/willowtreeapps/common/Reducers.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.reduxkotlin.Reducer
99
* Reducers and functions used by reducers are in this file. Functions must be pure functions without
1010
* side effects.
1111
*/
12-
val reducer = castingReducer { state: AppState, action ->
12+
val reducer: Reducer<AppState> = { state, action ->
1313
when (action) {
1414
is ActionTypes.INIT -> {
1515
AppState.INITIAL_STATE
@@ -37,10 +37,7 @@ val reducer = castingReducer { state: AppState, action ->
3737
}
3838

3939

40-
inline fun <reified T> castingReducer(crossinline reducer: ((T, Any) -> Any)): Reducer = { state: Any, action: Any ->
41-
if (state is T) {
42-
reducer(state as T, action)
43-
} else {
44-
{ s: Any, _: Any -> s }
45-
}
40+
inline fun <reified T> reducer(crossinline reducer: ((T, Any) -> T)): Reducer<T> = { state: T, action: Any ->
41+
reducer(state, action)
4642
}
43+

common/src/commonMain/kotlin/com/willowtreeapps/common/external/CoroutineDispatcher.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import kotlin.coroutines.CoroutineContext
99
/*
1010
* Middleware that moves rest of the middleware/reducer chain to a coroutine using the given context.
1111
*/
12-
fun coroutineDispatcher(context: CoroutineContext): Middleware {
12+
fun <State> coroutineDispatcher(context: CoroutineContext): Middleware<State> {
1313
val scope = CoroutineScope(context)
1414
return middleware { store, next, action ->
1515
scope.launch {

0 commit comments

Comments
 (0)