Ver código fonte

Refactoring: added book interface, removed wishes-adapter

sirekanyan 8 anos atrás
pai
commit
611cfd797b

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

@@ -1,10 +1,10 @@
 package me.vadik.knigopis
 
 import io.reactivex.Single
-import me.vadik.knigopis.model.Book
+import me.vadik.knigopis.model.FinishedBook
 import me.vadik.knigopis.model.Credentials
 import me.vadik.knigopis.model.User
-import me.vadik.knigopis.model.Wish
+import me.vadik.knigopis.model.PlannedBook
 import retrofit2.http.GET
 import retrofit2.http.Query
 
@@ -14,14 +14,14 @@ interface Endpoint {
   fun getCredentials(@Query("token") token: String): Single<Credentials>
 
   @GET("books")
-  fun getBooks(@Query("access-token") accessToken: String): Single<List<Book>>
+  fun getFinishedBooks(@Query("access-token") accessToken: String): Single<List<FinishedBook>>
 
   @GET("wishes")
-  fun getWishes(@Query("access-token") accessToken: String): Single<List<Wish>>
+  fun getPlannedBooks(@Query("access-token") accessToken: String): Single<List<PlannedBook>>
 
   @GET("users/latest")
   fun getLatestUsers(): Single<Map<String, User>>
 
   @GET("books/latest-notes")
-  fun getLatestBooksWithNotes(): Single<Map<String, Book>>
+  fun getLatestBooksWithNotes(): Single<Map<String, FinishedBook>>
 }

+ 31 - 32
app/src/main/java/me/vadik/knigopis/MainActivity.kt

@@ -12,12 +12,11 @@ import android.view.View
 import me.vadik.knigopis.CurrentTab.*
 import me.vadik.knigopis.adapters.BooksAdapter
 import me.vadik.knigopis.adapters.UsersAdapter
-import me.vadik.knigopis.adapters.WishesAdapter
 import me.vadik.knigopis.auth.KAuth
 import me.vadik.knigopis.auth.KAuthImpl
-import me.vadik.knigopis.model.Book
+import me.vadik.knigopis.model.FinishedBook
 import me.vadik.knigopis.model.User
-import me.vadik.knigopis.model.Wish
+import me.vadik.knigopis.model.PlannedBook
 
 private const val ULOGIN_REQUEST_CODE = 0
 
@@ -27,23 +26,23 @@ class MainActivity : AppCompatActivity() {
   private val imageApi by lazy { app().imageApi.create(ImageEndpoint::class.java) }
   private val auth by lazy { KAuthImpl(applicationContext, api) as KAuth }
   private val users = mutableListOf<User>()
-  private val books = mutableListOf<Book>()
-  private val wishes = mutableListOf<Wish>()
+  private val finishedBooks = mutableListOf<FinishedBook>()
+  private val plannedBooks = mutableListOf<PlannedBook>()
   private val usersAdapter = UsersAdapter.create(users)
-  private val booksAdapter by lazy { BooksAdapter(imageApi).create(books) }
-  private val wishesAdapter by lazy { WishesAdapter(imageApi).create(wishes) }
-  private lateinit var usersRecyclerView: RecyclerView
-  private lateinit var booksRecyclerView: RecyclerView
-  private lateinit var wishesRecyclerView: RecyclerView
+  private val finishedBooksAdapter by lazy { BooksAdapter(imageApi).create(finishedBooks) }
+  private val plannedBooksAdapter by lazy { BooksAdapter(imageApi).create(plannedBooks) }
+  private lateinit var usersView: RecyclerView
+  private lateinit var finishedBooksView: RecyclerView
+  private lateinit var plannedBooksView: RecyclerView
   private lateinit var loginOption: MenuItem
   private lateinit var currentTab: CurrentTab
 
   override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
     setContentView(R.layout.activity_main)
-    usersRecyclerView = initRecyclerView(findView(R.id.users_recycler_view), usersAdapter)
-    booksRecyclerView = initRecyclerView(findView(R.id.books_recycler_view), booksAdapter)
-    wishesRecyclerView = initRecyclerView(findView(R.id.wishes_recycler_view), wishesAdapter)
+    usersView = initRecyclerView(findView(R.id.users_recycler_view), usersAdapter)
+    finishedBooksView = initRecyclerView(findView(R.id.finished_books_view), finishedBooksAdapter)
+    plannedBooksView = initRecyclerView(findView(R.id.planned_books_view), plannedBooksAdapter)
     initNavigationView(findView(R.id.navigation))
     initToolbar(findView(R.id.toolbar))
   }
@@ -121,9 +120,9 @@ class MainActivity : AppCompatActivity() {
   }
 
   private fun refreshHomeTab() {
-    usersRecyclerView.visibility = View.VISIBLE
-    booksRecyclerView.visibility = View.GONE
-    wishesRecyclerView.visibility = View.GONE
+    usersView.visibility = View.VISIBLE
+    finishedBooksView.visibility = View.GONE
+    plannedBooksView.visibility = View.GONE
     api.getLatestUsers()
         .io2main()
         .subscribe({ latestUsers ->
@@ -136,32 +135,32 @@ class MainActivity : AppCompatActivity() {
   }
 
   private fun refreshDoneTab() {
-    usersRecyclerView.visibility = View.GONE
-    booksRecyclerView.visibility = View.VISIBLE
-    wishesRecyclerView.visibility = View.GONE
-    api.getBooks(auth.getAccessToken())
+    usersView.visibility = View.GONE
+    finishedBooksView.visibility = View.VISIBLE
+    plannedBooksView.visibility = View.GONE
+    api.getFinishedBooks(auth.getAccessToken())
         .io2main()
         .subscribe({
-          books.clear()
-          books.addAll(it)
-          usersAdapter.notifyDataSetChanged()
+          finishedBooks.clear()
+          finishedBooks.addAll(it)
+          finishedBooksAdapter.notifyDataSetChanged()
         }, {
-          logError("cannot load books", it)
+          logError("cannot load finished books", it)
         })
   }
 
   private fun refreshTodoTab() {
-    usersRecyclerView.visibility = View.GONE
-    booksRecyclerView.visibility = View.GONE
-    wishesRecyclerView.visibility = View.VISIBLE
-    api.getWishes(auth.getAccessToken())
+    usersView.visibility = View.GONE
+    finishedBooksView.visibility = View.GONE
+    plannedBooksView.visibility = View.VISIBLE
+    api.getPlannedBooks(auth.getAccessToken())
         .io2main()
         .subscribe({
-          wishes.clear()
-          wishes.addAll(it)
-          wishesAdapter.notifyDataSetChanged()
+          plannedBooks.clear()
+          plannedBooks.addAll(it)
+          plannedBooksAdapter.notifyDataSetChanged()
         }, {
-          logError("cannot load wishes", it)
+          logError("cannot load planned books", it)
         })
   }
 }

+ 13 - 3
app/src/main/java/me/vadik/knigopis/adapters/BooksAdapter.kt

@@ -18,7 +18,7 @@ class BooksAdapter(private val imageEndpoint: ImageEndpoint) {
       books,
       R.layout.book,
       Adapter(R.id.book_image) { book ->
-        imageEndpoint.searchImage(book.title + " " + book.author)
+        imageEndpoint.searchImage("${book.title} ${book.author}")
             .delay((Math.random() * 3000).toLong(), TimeUnit.MICROSECONDS)
             .io2main()
             .subscribe({ thumbnail ->
@@ -30,7 +30,17 @@ class BooksAdapter(private val imageEndpoint: ImageEndpoint) {
               logError("cannot load thumbnail", it)
             })
       },
-      Adapter(R.id.book_title) { this as TextView; text = it.title },
-      Adapter(R.id.book_author) { this as TextView; text = it.author }
+      Adapter(R.id.book_title) {
+        this as TextView
+        text = it.title
+      },
+      Adapter(R.id.book_author) {
+        this as TextView
+        text = if (it.author.isEmpty()) {
+          "(автор не указан)"
+        } else {
+          it.author
+        }
+      }
   )
 }

+ 0 - 46
app/src/main/java/me/vadik/knigopis/adapters/WishesAdapter.kt

@@ -1,46 +0,0 @@
-package me.vadik.knigopis.adapters
-
-import android.view.View
-import android.widget.ImageView
-import android.widget.TextView
-import com.bumptech.glide.Glide
-import com.bumptech.glide.request.RequestOptions
-import me.vadik.knigopis.ImageEndpoint
-import me.vadik.knigopis.R
-import me.vadik.knigopis.io2main
-import me.vadik.knigopis.logError
-import me.vadik.knigopis.model.Wish
-import java.util.concurrent.TimeUnit
-
-class WishesAdapter(private val imageEndpoint: ImageEndpoint) {
-
-  fun create(wishes: List<Wish>) = createAdapter<Wish, View>(
-      wishes,
-      R.layout.book,
-      Adapter(R.id.book_image) { book ->
-        imageEndpoint.searchImage(book.title)
-            .delay((Math.random() * 3000).toLong(), TimeUnit.MICROSECONDS)
-            .io2main()
-            .subscribe({ thumbnail ->
-              Glide.with(context)
-                  .load("https:" + thumbnail.url)
-                  .apply(RequestOptions.circleCropTransform())
-                  .into(this as ImageView)
-            }, {
-              logError("cannot load thumbnail", it)
-            })
-      },
-      Adapter(R.id.book_title) {
-        this as TextView
-        text = it.title
-      },
-      Adapter(R.id.book_author) {
-        this as TextView
-        text = if (it.author.isEmpty()) {
-          "(автор не указан)"
-        } else {
-          it.author
-        }
-      }
-  )
-}

+ 4 - 8
app/src/main/java/me/vadik/knigopis/model/Book.kt

@@ -1,10 +1,6 @@
 package me.vadik.knigopis.model
 
-class Book(
-    val id: String,
-    val title: String,
-    val author: String,
-    val notes: String,
-    val createdAt: String,
-    val user: User
-)
+interface Book {
+  val title: String
+  val author: String
+}

+ 10 - 0
app/src/main/java/me/vadik/knigopis/model/FinishedBook.kt

@@ -0,0 +1,10 @@
+package me.vadik.knigopis.model
+
+class FinishedBook(
+    val id: String,
+    override val title: String,
+    override val author: String,
+    val notes: String,
+    val createdAt: String,
+    val user: User
+) : Book

+ 4 - 4
app/src/main/java/me/vadik/knigopis/model/Wish.kt → app/src/main/java/me/vadik/knigopis/model/PlannedBook.kt

@@ -1,12 +1,12 @@
 package me.vadik.knigopis.model
 
-class Wish(
+class PlannedBook(
     val id: String,
     val userId: String,
     val createdAt: String,
     val updatedAt: String,
-    val title: String,
-    val author: String,
+    override val title: String,
+    override val author: String,
     val priority: Int,
     val notes: String
-)
+) : Book

+ 2 - 2
app/src/main/res/layout/activity_main.xml

@@ -32,7 +32,7 @@
         tools:visibility="visible"/>
 
     <android.support.v7.widget.RecyclerView
-        android:id="@+id/books_recycler_view"
+        android:id="@+id/finished_books_view"
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="1"
@@ -41,7 +41,7 @@
         tools:visibility="visible"/>
 
     <android.support.v7.widget.RecyclerView
-        android:id="@+id/wishes_recycler_view"
+        android:id="@+id/planned_books_view"
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="1"