Skip to content

Commit 92a1995

Browse files
authored
Merge pull request #1 from reduxkotlin/bugfix/bookselection
fix selecting books not working
2 parents 5699290 + 0ffa21e commit 92a1995

File tree

14 files changed

+50
-61
lines changed

14 files changed

+50
-61
lines changed

android/src/main/java/org/reduxkotlin/readinglist/store/BooksAdapter.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ import android.view.View
55
import android.view.ViewGroup
66
import androidx.recyclerview.widget.RecyclerView
77
import org.reduxkotlin.readinglist.GlideApp
8-
import org.reduxkotlin.readinglist.OpenLibraryApp
9-
import org.reduxkotlin.readinglist.common.ui.UiActions
108
import org.reduxkotlin.readinglist.common.ui.BookListItemViewState
119
import org.reduxkotlin.readinglist.common.ui.ListHeader
1210
import kotlinx.android.synthetic.main.item_book.view.*
1311
import kotlinx.android.synthetic.main.item_list_header.view.*
1412
import org.reduxkotlin.readinglist.R
1513

16-
class BooksAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
14+
class BooksAdapter(private val onClickListener: (Int) -> Unit): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
1715
private var data = listOf<Any>()
1816

17+
1918
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
2019
val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
2120
return when (viewType) {
@@ -38,7 +37,7 @@ class BooksAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
3837
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
3938
when (holder) {
4039
is HeaderViewHolder -> holder.bind((data[position] as ListHeader).title)
41-
is BookViewHolder -> holder.bind(data[position] as BookListItemViewState)
40+
is BookViewHolder -> holder.bind(data[position] as BookListItemViewState, onClickListener)
4241
}
4342
}
4443

@@ -51,13 +50,13 @@ class BooksAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
5150

5251
class BookViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
5352

54-
fun bind(book: BookListItemViewState) {
53+
fun bind(book: BookListItemViewState, clickListener: (Int) -> Unit) {
5554
itemView.tvAuthor.text = book.author
5655
itemView.tvTitle.text = book.title
5756
GlideApp.with(itemView)
5857
.load(book.coverImageUrl)
5958
.into(itemView.ivBookCover)
60-
itemView.setOnClickListener { OpenLibraryApp.dispatch(UiActions.BookTapped(adapterPosition)) }
59+
itemView.setOnClickListener {clickListener(adapterPosition)}
6160
}
6261

6362
}

android/src/main/java/org/reduxkotlin/readinglist/store/CompletedFragment.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ import org.reduxkotlin.readinglist.common.ui.CompletedView
1111
import kotlinx.android.synthetic.main.fragment_completed.*
1212
import kotlinx.android.synthetic.main.fragment_reading_list.loading_spinner
1313
import kotlinx.android.synthetic.main.fragment_reading_list.txt_error
14-
import kotlinx.coroutines.CoroutineScope
15-
import kotlinx.coroutines.Dispatchers
1614
import org.reduxkotlin.readinglist.R
1715
import org.reduxkotlin.rootDispatch
18-
import kotlin.coroutines.CoroutineContext
1916

20-
class CompletedFragment : BaseLibraryViewFragment<CompletedView>(), CoroutineScope, CompletedView {
21-
override val coroutineContext: CoroutineContext
22-
get() = Dispatchers.Main
17+
class CompletedFragment : BaseLibraryViewFragment<CompletedView>(), CompletedView {
2318

24-
private val adapter = BooksAdapter()
19+
private val adapter = BooksAdapter { pos -> rootDispatch(UiActions.CompletedBookTapped(pos - 1))}
2520

2621
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
2722
return inflater.inflate(R.layout.fragment_completed, container, false)

android/src/main/java/org/reduxkotlin/readinglist/store/ReadingListFragment.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,21 @@ import androidx.recyclerview.widget.LinearLayoutManager
88
import org.reduxkotlin.readinglist.MainActivity
99
import org.reduxkotlin.readinglist.common.ui.ReadingListView
1010
import kotlinx.android.synthetic.main.fragment_reading_list.*
11-
import kotlinx.coroutines.CoroutineScope
12-
import kotlinx.coroutines.Dispatchers
13-
import kotlin.coroutines.CoroutineContext
1411
import org.reduxkotlin.readinglist.R
1512
import org.reduxkotlin.readinglist.common.ui.UiActions
1613
import org.reduxkotlin.rootDispatch
1714

1815

19-
class ReadingListFragment : BaseLibraryViewFragment<ReadingListView>(), CoroutineScope, ReadingListView {
20-
21-
22-
override val coroutineContext: CoroutineContext
23-
get() = Dispatchers.Main
24-
16+
class ReadingListFragment : BaseLibraryViewFragment<ReadingListView>(), ReadingListView {
2517

2618
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
2719
return inflater.inflate(R.layout.fragment_reading_list, container, false)
2820
}
2921

30-
private val adapter = BooksAdapter()
22+
private val adapter = BooksAdapter { pos -> rootDispatch(UiActions.ReadingListBookTapped(pos - 1))}
3123

3224
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
25+
super.onViewCreated(view, savedInstanceState)
3326
toReadRecycler.adapter = adapter
3427
toReadRecycler.layoutManager = LinearLayoutManager(context)
3528
}

android/src/main/java/org/reduxkotlin/readinglist/store/SearchFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import java.util.*
2020

2121

2222
class SearchFragment : BaseLibraryViewFragment<SearchView>(), SearchView {
23-
private val adapter = BooksAdapter()
23+
private val adapter = BooksAdapter {pos -> rootDispatch(UiActions.SearchBookTapped(pos))}
2424

2525
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
2626
return inflater.inflate(R.layout.fragment_search, container, false)

common/build.gradle

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,6 @@ kotlin {
7272
implementation "io.ktor:ktor-client-serialization-jvm:$ktorVersion"
7373
}
7474
}
75-
// jvmMain {
76-
// dependencies {
77-
// implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
78-
// implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
79-
//
80-
// implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion"
81-
//
82-
// implementation "io.ktor:ktor-client-core-jvm:$ktorVersion"
83-
// implementation "io.ktor:ktor-client-json-jvm:$ktorVersion"
84-
// implementation "io.ktor:ktor-client-logging-jvm:$ktorVersion"
85-
// implementation "com.squareup.sqldelight:sqlite-driver:$sqldelightVersion"
86-
// implementation "io.ktor:ktor-client-serialization-jvm:$ktorVersion"
87-
// }
88-
// }
89-
// jvmTest {
90-
// dependencies {
91-
// implementation 'org.jetbrains.kotlin:kotlin-test'
92-
// implementation 'org.jetbrains.kotlin:kotlin-test-junit'
93-
// }
94-
// }
95-
9675
iosMain {
9776
dependencies {
9877
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutinesVersion"

common/src/commonMain/kotlin/org/reduxkotlin/readinglist/common/Actions.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package org.reduxkotlin.readinglist.common
22

33
import org.reduxkotlin.readinglist.common.middleware.Screen
44
import org.reduxkotlin.readinglist.common.repo.Book
5+
import kotlin.reflect.KProperty1
56

67
internal sealed class Actions {
78
class FetchingItemsStartedAction
89
data class FetchingItemsSuccessAction(val itemsHolder: List<Book>)
910
data class FetchingItemsFailedAction(val message: String)
1011

11-
data class BookSelected(val position: Int)
12+
data class ReadingListBookSelected(val position: Int)
13+
data class CompletedBookSelected(val position: Int)
14+
data class SearchBookSelected(val position: Int)
1215

1316
class AddCurrentToCompleted
1417
class AddCurrentToRead

common/src/commonMain/kotlin/org/reduxkotlin/readinglist/common/AppState.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ data class AppState(val isLoadingItems: Boolean = false,
99
val searchBooks: List<Book> = listOf(),
1010
val selectedBook: Book? = null,
1111
val errorLoadingItems: Boolean = false,
12-
val readingList: Set<Book> = setOf(),
13-
val completedList: Set<Book> = setOf(),
12+
val readingList: List<Book> = listOf(),
13+
val completedList: List<Book> = listOf(),
1414
//experimental
15-
val currentList: KProperty0<Set<Book>>? = null,
15+
val currentList: KProperty0<List<Book>>? = null,
1616
val errorMsg: String = "",
1717
val currentScreen: Screen = Screen.READING_LIST,
1818
val settings: UserSettings = UserSettings.defaults()) {

common/src/commonMain/kotlin/org/reduxkotlin/readinglist/common/Reducers.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ val reducer: Reducer<AppState> = { state, action ->
1919
searchBooks = action.itemsHolder)
2020
}
2121
is FetchingItemsFailedAction -> state.copy(isLoadingItems = false, errorLoadingItems = true, errorMsg = action.message)
22-
is BookSelected -> {
22+
is ReadingListBookSelected -> {
23+
val book = state.readingList[action.position]
24+
state.copy(selectedBook = book)
25+
}
26+
is CompletedBookSelected -> {
27+
val book = state.completedList[action.position]
28+
state.copy(selectedBook = book)
29+
}
30+
is SearchBookSelected -> {
2331
val book = state.searchBooks[action.position]
2432
state.copy(selectedBook = book)
2533
}
26-
27-
is ToReadLoaded -> state.copy(readingList = action.books.toSet())
28-
is CompletedLoaded -> state.copy(completedList = action.books.toSet())
34+
is ToReadLoaded -> state.copy(readingList = action.books)
35+
is CompletedLoaded -> state.copy(completedList = action.books)
2936
is PrevBook -> state.copy(selectedBook = state.searchBooks[state.currentSearchIndex() - 1])
3037
is NextBook -> state.copy(selectedBook = state.searchBooks[state.currentSearchIndex() + 1])
3138

common/src/commonMain/kotlin/org/reduxkotlin/readinglist/common/middleware/UiMiddleware.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ fun uiActionMiddleware(networkThunks: NetworkThunks) = middleware<AppState> { st
1515
val result = next(action)
1616
when (action) {
1717
is UiActions.SearchQueryEntered -> dispatch(networkThunks.fetchBooksThunk(action.query))
18-
is UiActions.BookTapped -> {
19-
dispatch(Actions.BookSelected(action.bookPosition))
18+
is UiActions.ReadingListBookTapped -> {
19+
dispatch(Actions.ReadingListBookSelected(action.position))
20+
dispatch(NavigationActions.GotoScreen(Screen.BOOK_DETAILS))
21+
}
22+
is UiActions.CompletedBookTapped -> {
23+
dispatch(Actions.CompletedBookSelected(action.position))
24+
dispatch(NavigationActions.GotoScreen(Screen.BOOK_DETAILS))
25+
}
26+
is UiActions.SearchBookTapped -> {
27+
dispatch(Actions.SearchBookSelected(action.position))
2028
dispatch(NavigationActions.GotoScreen(Screen.BOOK_DETAILS))
2129
}
2230
is UiActions.AddToCompletedButtonTapped -> dispatch(Actions.AddCurrentToCompleted())

common/src/commonMain/kotlin/org/reduxkotlin/readinglist/common/ui/UiActions.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ sealed class UiActions {
1212
class SearchBtnTapped
1313
class ReadingListBtnTapped
1414
class CompletedListBtnTapped
15-
class BookTapped(val bookPosition: Int)
15+
class ReadingListBookTapped(val position: Int)
16+
class CompletedBookTapped(val position: Int)
17+
class SearchBookTapped(val position: Int)
1618
data class SearchQueryEntered(val query: String)
1719
class AddToReadingButtonTapped
1820
class RemoveFromReadingButtonTapped

0 commit comments

Comments
 (0)