Skip to content

Commit e335507

Browse files
author
Patrick Jackson
committed
move list headers into recyclerview
1 parent 85e023b commit e335507

File tree

16 files changed

+93
-94
lines changed

16 files changed

+93
-94
lines changed

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,40 @@ import com.jackson.openlibrary.OpenLibraryApp
99
import com.jackson.openlibrary.R
1010
import com.willowtreeapps.common.Actions
1111
import com.willowtreeapps.common.BookListItemViewState
12+
import com.willowtreeapps.common.ui.ListHeader
1213
import kotlinx.android.synthetic.main.item_book.view.*
14+
import kotlinx.android.synthetic.main.item_list_header.view.*
1315

14-
class BooksAdapter: RecyclerView.Adapter<BookViewHolder>() {
15-
private var data = listOf<BookListItemViewState>()
16+
class BooksAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
17+
private var data = listOf<Any>()
1618

17-
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookViewHolder {
18-
return BookViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_book, parent, false))
19+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
20+
val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
21+
return when (viewType) {
22+
R.layout.item_book -> BookViewHolder(view)
23+
R.layout.item_list_header -> HeaderViewHolder(view)
24+
else -> throw NotImplementedError("BooksAdapter not handling type")
25+
}
1926
}
2027

2128
override fun getItemCount() = data.size
2229

23-
override fun onBindViewHolder(holder: BookViewHolder, position: Int) {
24-
holder.bind(data[position])
30+
override fun getItemViewType(position: Int): Int {
31+
return when (data[position]) {
32+
is ListHeader -> R.layout.item_list_header
33+
is BookListItemViewState -> R.layout.item_book
34+
else -> throw NotImplementedError("not handled in BookAdapter")
35+
}
2536
}
2637

27-
fun setBooks(books: List<BookListItemViewState>) {
38+
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
39+
when (holder) {
40+
is HeaderViewHolder -> holder.bind((data[position] as ListHeader).title)
41+
is BookViewHolder -> holder.bind(data[position] as BookListItemViewState)
42+
}
43+
}
44+
45+
fun setBooks(books: List<Any>) {
2846
data = books
2947
notifyDataSetChanged()
3048
}
@@ -42,4 +60,10 @@ class BookViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
4260
itemView.setOnClickListener { OpenLibraryApp.gameEngine().dispatch(Actions.BookSelected(book)) }
4361
}
4462

45-
}
63+
}
64+
class HeaderViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
65+
fun bind(title: String) {
66+
itemView.tvListTitle.text = title
67+
}
68+
}
69+

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ class CompletedFragment : BaseLibraryViewFragment<CompletedView>(), CoroutineSco
2525
get() = Dispatchers.Main
2626

2727
private val adapter = BooksAdapter()
28-
val divider by lazy {
29-
val attrs = intArrayOf(android.R.attr.listDivider)
30-
val a = context?.obtainStyledAttributes(attrs)
31-
val divider = a?.getDrawable(0)
32-
val inset = resources.getDimensionPixelSize(com.jackson.openlibrary.R.dimen.list_divider_margin_start)
33-
val insetDivider = InsetDrawable(divider, inset, 0, 0, 0)
34-
a?.recycle()
35-
insetDivider
36-
}
3728

3829
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
3930
return inflater.inflate(R.layout.fragment_completed, container, false)
@@ -42,10 +33,6 @@ class CompletedFragment : BaseLibraryViewFragment<CompletedView>(), CoroutineSco
4233
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
4334
completedRecycler.adapter = adapter
4435
completedRecycler.layoutManager = LinearLayoutManager(context)
45-
46-
val dividerItemDecoration = DividerItemDecoration(context, LinearLayout.VERTICAL)
47-
dividerItemDecoration.setDrawable(divider)
48-
completedRecycler.addItemDecoration(dividerItemDecoration)
4936
}
5037

5138
override fun onResume() {
@@ -70,12 +57,7 @@ class CompletedFragment : BaseLibraryViewFragment<CompletedView>(), CoroutineSco
7057
txt_error.text = msg
7158
}
7259

73-
override fun showBooks(books: List<BookListItemViewState>) {
60+
override fun showBooks(books: List<Any>) {
7461
adapter.setBooks(books)
7562
}
76-
77-
override fun showTitle(title: String) {
78-
completedListTitle.text = title
79-
}
80-
8163
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import android.view.LayoutInflater
55
import android.view.View
66
import android.view.ViewGroup
77
import com.jackson.openlibrary.GlideApp
8-
import com.jackson.openlibrary.OpenLibraryApp
98
import com.jackson.openlibrary.R
109
import com.willowtreeapps.common.Actions
1110
import com.willowtreeapps.common.BookListItemViewState
@@ -25,20 +24,17 @@ class DetailsFragment : BaseLibraryViewFragment<DetailsView>(), CoroutineScope,
2524
return inflater.inflate(R.layout.fragment_book_detail, container, false)
2625
}
2726

28-
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
27+
override fun render(bookViewState: BookListItemViewState) {
28+
txtTitle.text = bookViewState.title
29+
txtAuthorName.text = bookViewState.author
30+
GlideApp.with(this)
31+
.load(bookViewState.coverImageUrl)
32+
.into(imgBook)
2933
btnToRead.setOnClickListener {
30-
dispatch(Actions.LoadToRead())
34+
dispatch(Actions.AddToRead(bookViewState.book))
3135
}
3236
btnCompleted.setOnClickListener {
33-
dispatch(Actions.LoadCompleted())
37+
dispatch(Actions.AddToCompleted(bookViewState.book))
3438
}
3539
}
36-
37-
override fun render(book: BookListItemViewState) {
38-
txtTitle.text = book.title
39-
txtAuthorName.text = book.author
40-
GlideApp.with(this)
41-
.load(book.coverImageUrl)
42-
.into(imgBook)
43-
}
4440
}

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

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,9 @@ class ReadingListFragment : BaseLibraryViewFragment<ReadingListView>(), Coroutin
3232

3333
private val adapter = BooksAdapter()
3434

35-
val divider by lazy {
36-
val attrs = intArrayOf(android.R.attr.listDivider)
37-
val a = context?.obtainStyledAttributes(attrs)
38-
val divider = a?.getDrawable(0)
39-
val inset = resources.getDimensionPixelSize(com.jackson.openlibrary.R.dimen.list_divider_margin_start)
40-
val insetDivider = InsetDrawable(divider, inset, 0, 0, 0)
41-
a?.recycle()
42-
insetDivider
43-
}
44-
4535
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
4636
toReadRecycler.adapter = adapter
4737
toReadRecycler.layoutManager = LinearLayoutManager(context)
48-
49-
val dividerItemDecoration = DividerItemDecoration(context, LinearLayout.VERTICAL)
50-
dividerItemDecoration.setDrawable(divider)
51-
toReadRecycler.addItemDecoration(dividerItemDecoration)
5238
}
5339

5440
override fun onResume() {
@@ -74,12 +60,7 @@ class ReadingListFragment : BaseLibraryViewFragment<ReadingListView>(), Coroutin
7460
txt_error.text = msg
7561
}
7662

77-
override fun showBooks(toReadBook: List<BookListItemViewState>) {
63+
override fun showBooks(toReadBook: List<Any>) {
7864
adapter.setBooks(toReadBook)
7965
}
80-
81-
override fun showTitle(title: String) {
82-
readingListTitle.text = title
83-
}
84-
8566
}

android/src/main/res/layout/fragment_completed.xml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,15 @@
99
app:layout_behavior="@string/appbar_scrolling_view_behavior"
1010
tools:context="com.jackson.openlibrary.MainActivity">
1111

12-
<TextView
13-
style="@style/ListTitle"
14-
android:id="@+id/completedListTitle"
15-
android:layout_width="match_parent"
16-
android:layout_height="wrap_content"
17-
app:layout_constraintTop_toTopOf="parent"
18-
tools:text="Completed"
19-
/>
20-
2112
<androidx.recyclerview.widget.RecyclerView
2213
android:id="@+id/completedRecycler"
2314
android:layout_width="match_parent"
2415
android:layout_height="0dp"
25-
app:layout_constraintTop_toBottomOf="@+id/completedListTitle"
16+
app:layout_constraintTop_toTopOf="parent"
2617
app:layout_constraintBottom_toBottomOf="parent"
2718
tools:listitem="@layout/item_book"
2819
/>
20+
2921
<androidx.constraintlayout.widget.ConstraintLayout
3022
android:id="@+id/loading_spinner"
3123
android:layout_width="match_parent"

android/src/main/res/layout/fragment_reading_list.xml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,11 @@
88
android:focusableInTouchMode="true"
99
app:layout_behavior="@string/appbar_scrolling_view_behavior"
1010
tools:context="com.jackson.openlibrary.MainActivity">
11-
12-
<TextView
13-
style="@style/ListTitle"
14-
android:id="@+id/readingListTitle"
15-
android:layout_width="match_parent"
16-
android:layout_height="wrap_content"
17-
app:layout_constraintTop_toTopOf="parent"
18-
tools:text="Reading List"
19-
/>
20-
2111
<androidx.recyclerview.widget.RecyclerView
2212
android:id="@+id/toReadRecycler"
2313
android:layout_width="match_parent"
2414
android:layout_height="0dp"
25-
app:layout_constraintTop_toBottomOf="@+id/readingListTitle"
15+
app:layout_constraintTop_toTopOf="parent"
2616
app:layout_constraintBottom_toBottomOf="parent"
2717
tools:listitem="@layout/item_book"
2818
/>

android/src/main/res/layout/item_book.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
android:layout_marginLeft="8dp"
3434
android:layout_toRightOf="@+id/ivBookCover"
3535
android:layout_below="@+id/tvTitle"
36+
android:layout_marginBottom="16dp"
3637
android:text="Author"
3738
android:textSize="12dp" />
39+
40+
<View
41+
android:layout_width="match_parent"
42+
android:layout_height="1dp"
43+
android:background="@color/divider_gray"
44+
android:layout_alignParentBottom="true"
45+
android:layout_marginStart="@dimen/list_divider_margin_start"
46+
/>
3847
</RelativeLayout>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="wrap_content"
4+
android:layout_height="wrap_content"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
xmlns:app="http://schemas.android.com/apk/res-auto">
7+
8+
<TextView
9+
android:id="@+id/tvListTitle"
10+
style="@style/ListTitle"
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:layout_marginStart="16dp"
14+
app:layout_constraintStart_toStartOf="parent"
15+
app:layout_constraintTop_toTopOf="parent"
16+
tools:text="Reading List" />
17+
18+
</androidx.constraintlayout.widget.ConstraintLayout>

android/src/main/res/values/dimens.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<dimen name="txt_timer_size">62sp</dimen>
1010
<dimen name="txt_list_title_margin_horizontal">16dp</dimen>
1111
<dimen name="txt_list_title_margin_top">32dp</dimen>
12-
<dimen name="list_divider_margin_start">78dp</dimen>
12+
<dimen name="list_divider_margin_start">80dp</dimen>
1313

1414
</resources>

common/src/commonMain/kotlin/com/willowtreeapps/common/boundary/TransformFunctions.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@ package com.willowtreeapps.common.boundary
33
import com.jackson.openlibrary.Books
44
import com.willowtreeapps.common.*
55
import com.willowtreeapps.common.repo.Book
6+
import com.willowtreeapps.common.ui.ListHeader
67

78
/**
89
* Functions for transforming AppState data into ViewState data to be used by Views.
910
*/
1011

1112
fun Book.toBookListViewState() = BookListItemViewState(title = title,
1213
author = authorName.firstOrNull() ?: "Unknown",
13-
coverImageUrl = coverUrl,
14-
id = openLibraryId)
14+
coverImageUrl = largeCoverUrl,
15+
id = openLibraryId,
16+
book = this
17+
)
18+
19+
fun Collection<Book>.toBookListViewState(title: String) =
20+
listOf<Any>(ListHeader(title)).plus(map { it.toBookListViewState() })
1521

1622
fun Collection<Book>.toBookListViewState() = map { it.toBookListViewState() }
1723

1824
fun Books.toBook() = Book(cover_edition_key = this.openLibraryId, authorName = listOf(this.author ?: "unknown"), title = this.title!!)
1925

2026
fun List<Books>.toBook() = map { it.toBook() }
27+

0 commit comments

Comments
 (0)