소스 검색

Show random book on profile screen

Vadik Sirekanyan 7 년 전
부모
커밋
a90b557530

+ 5 - 1
app/src/main/java/me/vadik/knigopis/model/Book.kt

@@ -8,5 +8,9 @@ interface Book {
     val author: String
     val titleOrDefault get() = title.orDefault("(без названия)")
     val authorOrDefault get() = author.orDefault("(автор не указан)")
-    val fullTitle get() = "$titleOrDefault — $authorOrDefault"
+    val fullTitle
+        get() = when {
+            author.isEmpty() -> titleOrDefault
+            else -> "$titleOrDefault — $authorOrDefault"
+        }
 }

+ 41 - 13
app/src/main/java/me/vadik/knigopis/profile/ProfileActivity.kt

@@ -16,7 +16,10 @@ import me.vadik.knigopis.auth.KAuth
 import me.vadik.knigopis.common.createTextShareIntent
 import me.vadik.knigopis.io2main
 import me.vadik.knigopis.logError
+import me.vadik.knigopis.model.Book
+import me.vadik.knigopis.model.PlannedBook
 import org.koin.android.ext.android.inject
+import java.util.*
 
 fun Context.createProfileIntent() = Intent(this, ProfileActivity::class.java)
 
@@ -24,6 +27,10 @@ class ProfileActivity : AppCompatActivity() {
 
     private val api by inject<Endpoint>()
     private val auth by inject<KAuth>()
+    private val random = Random()
+    private val todoList = mutableListOf<Book>()
+    private val doingList = mutableListOf<Book>()
+    private val doneList = mutableListOf<Book>()
     private var profileUrl: String? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -33,6 +40,24 @@ class ProfileActivity : AppCompatActivity() {
         profileTodoCount.text = getString(R.string.profile_caption_todo, 0)
         profileDoingCount.text = getString(R.string.profile_caption_doing, 0)
         profileDoneCount.text = getString(R.string.profile_caption_done, 0)
+        profileTodoCount.setOnClickListener {
+            setRandomFooterBook(todoList)
+        }
+        profileDoingCount.setOnClickListener {
+            setRandomFooterBook(doingList)
+        }
+        profileDoneCount.setOnClickListener {
+            setRandomFooterBook(doneList)
+        }
+    }
+
+    private fun setRandomFooterBook(books: List<Book>) {
+        val book = books.random() ?: return
+        randomProfileBook.text = getString(
+            R.string.profile_book_random,
+            book.titleOrDefault,
+            (book as? PlannedBook)?.priority ?: 100
+        )
     }
 
     override fun onStart() {
@@ -57,25 +82,18 @@ class ProfileActivity : AppCompatActivity() {
         api.getFinishedBooks(auth.getAccessToken())
             .io2main()
             .subscribe({ finishedBooks ->
-                profileDoneCount.text = getString(
-                    R.string.profile_caption_done,
-                    finishedBooks.size
-                )
+                doneList.clearAndAddAll(finishedBooks)
+                profileDoneCount.text = getString(R.string.profile_caption_done, doneList.size)
             }, {
                 logError("cannot check finished books count", it)
             })
         api.getPlannedBooks(auth.getAccessToken())
             .io2main()
             .subscribe({ plannedBooks ->
-                val inProgressCount = plannedBooks.count { it.priority > 0 }
-                profileDoingCount.text = getString(
-                    R.string.profile_caption_doing,
-                    inProgressCount
-                )
-                profileTodoCount.text = getString(
-                    R.string.profile_caption_todo,
-                    plannedBooks.size - inProgressCount
-                )
+                doingList.clearAndAddAll(plannedBooks.filter { it.priority > 0 })
+                profileDoingCount.text = getString(R.string.profile_caption_doing, doingList.size)
+                todoList.clearAndAddAll(plannedBooks.filter { it.priority == 0 })
+                profileTodoCount.text = getString(R.string.profile_caption_todo, todoList.size)
             }, {
                 logError("cannot check planned books count", it)
             })
@@ -105,4 +123,14 @@ class ProfileActivity : AppCompatActivity() {
         }
     }
 
+    private fun <T> List<T>.random(): T? {
+        if (size == 0) return null
+        return get(random.nextInt(size))
+    }
+
+    private fun <T> MutableCollection<T>.clearAndAddAll(collection: Collection<T>) {
+        clear()
+        addAll(collection)
+    }
+
 }

+ 8 - 2
app/src/main/res/layout/profile_activity.xml

@@ -81,8 +81,14 @@
         android:layout_height="1dp"
         android:background="#BBFFFFFF" />
 
-    <View
+    <TextView
+        android:id="@+id/randomProfileBook"
         android:layout_width="match_parent"
-        android:layout_height="56dp" />
+        android:layout_height="56dp"
+        android:gravity="bottom|center"
+        android:padding="8dp"
+        android:textColor="@android:color/secondary_text_dark"
+        android:textSize="12sp"
+        tools:text="Мастер и маргарита — 90%" />
 
 </LinearLayout>

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -22,6 +22,7 @@
     <string name="profile_caption_done">%d\ndone</string>
     <string name="profile_hint_nickname">nickname</string>
     <string name="profile_hint_homepage">homepage</string>
+    <string name="profile_book_random" translatable="false">%s — %d%%</string>
 
     <!-- profile menu -->
     <string name="profile_option_edit">Edit</string>