Przeglądaj źródła

Added dividers between grouped books

sirekanyan 8 lat temu
rodzic
commit
f931f94f59

+ 5 - 5
app/src/main/java/me/vadik/knigopis/adapters/Adapter.kt

@@ -11,11 +11,11 @@ class Adapter<T>(
     private val layout: (T) -> Int
 ) {
 
-  val binders = mutableMapOf<@IdRes Int, (View, T) -> Unit>()
+  val binders = mutableMapOf<@IdRes Int, (View, Int) -> Unit>()
 
-  inline fun <reified V : View> bind(@IdRes id: Int, crossinline binder: V.(T) -> Unit): Adapter<T> {
-    binders[id] = { view, model ->
-      binder(view as V, model)
+  inline fun <reified V : View> bind(@IdRes id: Int, crossinline binder: V.(Int) -> Unit): Adapter<T> {
+    binders[id] = { view, position ->
+      binder(view as V, position)
     }
     return this
   }
@@ -31,7 +31,7 @@ class Adapter<T>(
     override fun onBindViewHolder(holder: ViewsHolder, position: Int) =
         binders.forEach { (id, binder) ->
           holder.views[id]?.let { view ->
-            binder(view, items[position])
+            binder(view, position)
           }
         }
 

+ 9 - 5
app/src/main/java/me/vadik/knigopis/adapters/BooksAdapter.kt

@@ -1,5 +1,6 @@
 package me.vadik.knigopis.adapters
 
+import android.view.View
 import android.widget.ImageView
 import android.widget.TextView
 import com.bumptech.glide.Glide
@@ -19,8 +20,8 @@ class BooksAdapter(private val coverSearch: BookCoverSearch) {
       R.layout.book
     }
   }
-      .bind<ImageView>(R.id.book_image) { book ->
-        coverSearch.search(book)
+      .bind<ImageView>(R.id.book_image) {
+        coverSearch.search(books[it])
             .subscribe({ coverUrl ->
               Glide.with(context)
                   .load(coverUrl)
@@ -31,13 +32,16 @@ class BooksAdapter(private val coverSearch: BookCoverSearch) {
             })
       }
       .bind<TextView>(R.id.book_title) {
-        text = it.title
+        text = books[it].title
+      }
+      .bind<View>(R.id.header_divider) {
+        visibility = if (it == 0) View.INVISIBLE else View.VISIBLE
       }
       .bind<TextView>(R.id.book_author) {
-        text = if (it.author.isEmpty()) {
+        text = if (books[it].author.isEmpty()) {
           "(автор не указан)"
         } else {
-          it.author
+          books[it].author
         }
       }
       .build()

+ 4 - 3
app/src/main/java/me/vadik/knigopis/adapters/UsersAdapter.kt

@@ -7,11 +7,12 @@ import me.vadik.knigopis.model.User
 object UsersAdapter {
   fun create(users: List<User>) = Adapter(users, { R.layout.user })
       .bind<TextView>(R.id.user_name) {
-        text = it.nickname
+        text = users[it].nickname
       }
       .bind<TextView>(R.id.book_count) {
-        text = it.booksCount.toString()
-        setTextColor(it.color)
+        val user = users[it]
+        text = user.booksCount.toString()
+        setTextColor(user.color)
       }
       .build()
 }

+ 23 - 10
app/src/main/res/layout/header.xml

@@ -1,14 +1,27 @@
 <?xml version="1.0" encoding="utf-8"?>
-<TextView
+<FrameLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
-    android:id="@+id/book_title"
     android:layout_width="match_parent"
-    android:layout_height="48dp"
-    android:fontFamily="sans-serif-medium"
-    android:gravity="center_vertical"
-    android:paddingEnd="16dp"
-    android:paddingStart="16dp"
-    android:textColor="@color/black_54"
-    android:textSize="14sp"
-    tools:text="К прочтению"/>
+    android:layout_height="48dp">
+
+    <View
+        android:id="@+id/header_divider"
+        android:layout_width="match_parent"
+        android:layout_height="0.5dp"
+        android:background="@color/white_12"
+        android:visibility="invisible"/>
+
+    <TextView
+        android:id="@+id/book_title"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center_vertical"
+        android:layout_marginEnd="16dp"
+        android:layout_marginStart="16dp"
+        android:fontFamily="sans-serif-medium"
+        android:textColor="@color/black_54"
+        android:textSize="14sp"
+        tools:text="К прочтению"/>
+
+</FrameLayout>

+ 2 - 0
app/src/main/res/values/colors.xml

@@ -4,5 +4,7 @@
     <color name="colorPrimaryDark">#303F9F</color>
     <color name="colorAccent">#FF4081</color>
     <color name="white">#FFFFFF</color>
+    <color name="white_12">#E1E1E1</color>
+    <color name="black_12">#1F1F1F</color>
     <color name="black_54">#757575</color>
 </resources>