Skip to content

Commit a91c59e

Browse files
author
Patrick Jackson
committed
add convenience fun for createThunk
1 parent 05531fb commit a91c59e

File tree

10 files changed

+65
-75
lines changed

10 files changed

+65
-75
lines changed

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@ import kotlinx.coroutines.Dispatchers
1313
import org.reduxkotlin.Dispatcher
1414
import kotlin.coroutines.CoroutineContext
1515

16-
open class BaseLibraryViewFragment<V: LibraryView>: Fragment(), LibraryView, CoroutineScope,
17-
LibraryProvider by OpenLibraryApp.gameEngine() {
16+
open class BaseLibraryViewFragment<V: LibraryView>: Fragment(), LibraryView {
1817

1918
private var viewRecreated: Boolean = false
2019
override lateinit var dispatch: Dispatcher
2120
override var selectorBuilder: SelectorSubscriberBuilder<AppState>? = null
2221

23-
override val coroutineContext: CoroutineContext
24-
get() = Dispatchers.Main
25-
26-
2722
override fun onViewCreated(view: android.view.View, savedInstanceState: Bundle?) {
2823
super.onViewCreated(view, savedInstanceState)
2924
if (savedInstanceState == null)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import androidx.recyclerview.widget.RecyclerView
77
import com.jackson.openlibrary.GlideApp
88
import com.jackson.openlibrary.OpenLibraryApp
99
import com.jackson.openlibrary.R
10-
import com.willowtreeapps.common.Actions
1110
import com.willowtreeapps.common.UiActions
1211
import com.willowtreeapps.common.ui.BookListItemViewState
1312
import com.willowtreeapps.common.ui.ListHeader

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import android.view.ViewGroup
77
import androidx.recyclerview.widget.LinearLayoutManager
88
import com.jackson.openlibrary.MainActivity
99
import com.jackson.openlibrary.R
10-
import com.willowtreeapps.common.Actions
10+
import com.willowtreeapps.common.UiActions
1111
import com.willowtreeapps.common.ui.CompletedView
1212
import kotlinx.android.synthetic.main.fragment_completed.*
1313
import kotlinx.android.synthetic.main.fragment_reading_list.loading_spinner
@@ -34,7 +34,7 @@ class CompletedFragment : BaseLibraryViewFragment<CompletedView>(), CoroutineSco
3434
override fun onResume() {
3535
super.onResume()
3636
(activity as MainActivity).showFab()
37-
dispatch(Actions.LoadCompleted())
37+
dispatch(UiActions.CompletedListShown())
3838
}
3939

4040
override fun hideLoading() {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import android.view.View
66
import android.view.ViewGroup
77
import androidx.recyclerview.widget.LinearLayoutManager
88
import com.jackson.openlibrary.MainActivity
9-
import com.jackson.openlibrary.OpenLibraryApp
10-
import com.willowtreeapps.common.Actions
119
import com.willowtreeapps.common.ui.ReadingListView
1210
import kotlinx.android.synthetic.main.fragment_reading_list.*
1311
import kotlinx.coroutines.CoroutineScope
1412
import kotlinx.coroutines.Dispatchers
1513
import kotlin.coroutines.CoroutineContext
1614
import com.jackson.openlibrary.R
15+
import com.willowtreeapps.common.UiActions
1716

1817

1918
class ReadingListFragment : BaseLibraryViewFragment<ReadingListView>(), CoroutineScope, ReadingListView {
@@ -36,7 +35,7 @@ class ReadingListFragment : BaseLibraryViewFragment<ReadingListView>(), Coroutin
3635

3736
override fun onResume() {
3837
super.onResume()
39-
dispatch(Actions.LoadToRead())
38+
dispatch(UiActions.ReadingListShown())
4039
(activity as MainActivity).showFab()
4140
}
4241

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,15 @@ import com.willowtreeapps.common.external.presenterMiddleware
66
import org.reduxkotlin.createStore
77
import org.reduxkotlin.applyMiddleware
88
import com.willowtreeapps.common.middleware.*
9-
import com.willowtreeapps.common.middleware.NavigationMiddleware
109
import com.willowtreeapps.common.repo.*
1110
import com.willowtreeapps.common.ui.*
12-
import kotlinx.coroutines.CoroutineScope
13-
import kotlinx.coroutines.launch
1411
import org.reduxkotlin.combineReducers
1512
import kotlin.coroutines.CoroutineContext
1613

1714
class LibraryApp(navigator: Navigator,
1815
networkContext: CoroutineContext,
1916
private val uiContext: CoroutineContext,
2017
libraryDatabase: LibraryDatabase) : LibraryProvider {
21-
private val navigationMiddleware = NavigationMiddleware(navigator)
22-
private val localStorageRepo = BookDatabaseRepo(libraryDatabase)
23-
private val databaseMiddleware = DatabaseMiddleware(localStorageRepo)
2418
private val bookRepository: BookRepository by lazy { KtorOpenBookRepository(networkContext) }
2519
override val networkThunks = NetworkThunks(networkContext, bookRepository)
2620

@@ -30,16 +24,14 @@ class LibraryApp(navigator: Navigator,
3024
coroutineDispatcher(uiContext),
3125
createThunkMiddleware2(),
3226
uiActionMiddleware(networkThunks),
33-
databaseMiddleware.middleware,
34-
navigationMiddleware::dispatch,
27+
databaseMiddleware(BookDatabaseRepo(libraryDatabase)),
28+
navigationMiddleware(navigator),
3529
loggerMiddleware))
3630
}
3731

3832
init {
39-
CoroutineScope(uiContext).launch {
40-
//do any initialization here
41-
store.dispatch(NavigationActions.GotoScreen(state.currentScreen))
42-
}
33+
//do any initialization here
34+
store.dispatch(NavigationActions.GotoScreen(state.currentScreen))
4335
}
4436

4537
val state: AppState

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,47 @@ class NetworkThunks(private val networkContext: CoroutineContext,
5353
}
5454
}
5555

56-
fun fetchBooksThunk2(query: String): Thunk2 = object : Thunk2 {
57-
override fun dispatch(dispatch: Dispatcher, getState: GetState, extraArgument: Any?) {
58-
Logger.d("Fetching Books and Feed")
59-
launch {
60-
dispatch(Actions.FetchingItemsStartedAction())
61-
val result = repo.search(query)
62-
if (result.isSuccessful) {
63-
dispatch(Actions.FetchingItemsSuccessAction(result.response!!))
64-
} else {
65-
dispatch(Actions.FetchingItemsFailedAction(result.message!!))
66-
}
56+
fun fetchBooksThunk2(query: String) = createThunk { dispatch, getState, extraArgument ->
57+
Logger.d("Fetching Books and Feed")
58+
launch {
59+
dispatch(Actions.FetchingItemsStartedAction())
60+
val result = repo.search(query)
61+
if (result.isSuccessful) {
62+
dispatch(Actions.FetchingItemsSuccessAction(result.response!!))
63+
} else {
64+
dispatch(Actions.FetchingItemsFailedAction(result.message!!))
6765
}
6866
}
6967
}
7068
}
7169

7270
interface Thunk2 {
73-
fun dispatch(dispatch: Dispatcher, getState: GetState, extraArgument: Any?)
71+
fun dispatch(dispatch: Dispatcher, getState: GetState, extraArgument: Any?): Any
72+
}
73+
74+
/**
75+
* Convenience function for creating thunks.
76+
* Usage:
77+
* val myThunk = createThunk { dispatch, getState, extraArgument ->
78+
* //do async stuff
79+
* dispatch(NewAction())
80+
* }
81+
*/
82+
fun createThunk(thunkLambda: (dispatch: Dispatcher, getState: GetState, extraArgument: Any?) -> Any): Thunk2 {
83+
return object : Thunk2 {
84+
override fun dispatch(dispatch: Dispatcher, getState: GetState, extraArgument: Any?): Any =
85+
thunkLambda(dispatch, getState, extraArgument)
86+
87+
}
88+
7489
}
7590

7691
fun createThunkMiddleware2(extraArgument: Any? = null): ThunkMiddleware =
7792
{ store ->
7893
{ next: Dispatcher ->
7994
{ action: Any ->
8095
if (action is Thunk2) {
96+
createThunk { dispatch, getState, extraArgument -> }
8197
try {
8298
action.dispatch(store.dispatch, store.getState, extraArgument)
8399
} catch (e: Exception) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ interface PresenterProvider<S : Any> {
3030
interface ViewWithProvider<S : Any> : View<S>, PresenterProvider<S>
3131

3232
/**
33-
* PresenterFactory that creates presenters for all views in the application.
34-
* Each view must attach/detach itself as it becomes visible/not visible.
35-
* Attaching returns a presenter to the view.
33+
* PresenterMiddleware that attaches presenters with views and calls subscribers (Presenters)
34+
* to update the view when state changes.
35+
* Each view must attach/detach itself as it becomes visible/not visible by dispatching AttachView or DetachView
36+
* Attaching sets the presenter to the view.
3637
* PresenterFactory subscribes to changes in state, and passes state to presenters.
3738
*/
3839
//TODO handle config changes on android where view has been destroyed and must be recreated. Probably

common/src/commonMain/kotlin/com/willowtreeapps/common/middleware/DatabaseMiddleware.kt

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@ import com.willowtreeapps.common.AppState
55
import com.willowtreeapps.common.repo.BookDatabaseRepo
66
import org.reduxkotlin.middleware
77

8-
class DatabaseMiddleware(private val bookDatabaseRepo: BookDatabaseRepo) {
9-
val middleware = middleware { store, next, action ->
10-
when (action) {
11-
is Actions.AddCurrentToCompleted ->
12-
bookDatabaseRepo.insertCompleted((store.state as AppState).selectedBook!!)
13-
is Actions.AddCurrentToRead ->
14-
bookDatabaseRepo.insertToRead((store.state as AppState).selectedBook!!)
15-
is Actions.LoadToRead ->
16-
next(Actions.ToReadLoaded(bookDatabaseRepo.loadToRead()))
17-
is Actions.LoadCompleted ->
18-
next(Actions.CompletedLoaded(bookDatabaseRepo.loadCompleted()))
19-
}
20-
next(action)
8+
fun databaseMiddleware(bookDatabaseRepo: BookDatabaseRepo) = middleware { store, next, action ->
9+
when (action) {
10+
is Actions.AddCurrentToCompleted ->
11+
bookDatabaseRepo.insertCompleted((store.state as AppState).selectedBook!!)
12+
is Actions.AddCurrentToRead ->
13+
bookDatabaseRepo.insertToRead((store.state as AppState).selectedBook!!)
14+
is Actions.LoadToRead ->
15+
next(Actions.ToReadLoaded(bookDatabaseRepo.loadToRead()))
16+
is Actions.LoadCompleted ->
17+
next(Actions.CompletedLoaded(bookDatabaseRepo.loadCompleted()))
2118
}
19+
next(action)
2220
}
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.willowtreeapps.common.middleware
22

3-
import com.willowtreeapps.common.Actions
43
import com.willowtreeapps.common.Logger
54
import org.reduxkotlin.*
65

@@ -10,11 +9,3 @@ val loggerMiddleware = middleware { store, next, action ->
109
Logger.d("********************************************")
1110
next(action)
1211
}
13-
14-
val splitterMiddleware = middleware { store, next, action ->
15-
val dispatch = store.dispatch
16-
when(action) {
17-
is Actions.NextBook -> dispatch(Actions.NextBook())
18-
}
19-
next(action)
20-
}

common/src/commonMain/kotlin/com/willowtreeapps/common/middleware/NavigationMiddleware.kt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@ package com.willowtreeapps.common.middleware
22

33
import com.willowtreeapps.common.Actions
44
import com.willowtreeapps.common.NavigationActions
5-
import org.reduxkotlin.Dispatcher
6-
import org.reduxkotlin.Store
5+
import org.reduxkotlin.Middleware
76

8-
internal class NavigationMiddleware(private val navigator: Navigator) {
9-
10-
fun dispatch(store: Store) = { next: Dispatcher ->
11-
{ action: Any ->
12-
when (action) {
13-
is NavigationActions.GotoScreen -> navigator.goto(action.screen)
14-
}
15-
next(action)
16-
when (action) {
17-
is Actions.PrevBook, is Actions.NextBook -> navigator.goto(Screen.BOOK_DETAILS)
7+
internal fun navigationMiddleware(navigator: Navigator): Middleware =
8+
{ store ->
9+
{ next ->
10+
{ action ->
11+
when (action) {
12+
is NavigationActions.GotoScreen -> navigator.goto(action.screen)
13+
}
14+
next(action)
15+
when (action) {
16+
is Actions.PrevBook, is Actions.NextBook -> navigator.goto(Screen.BOOK_DETAILS)
17+
}
18+
}
1819
}
1920
}
20-
}
21-
}
2221

2322
enum class Screen {
2423
READING_LIST,

0 commit comments

Comments
 (0)