Просмотр исходного кода

Fixed showing actual followings after pull-to-refresh

Vadik Sirekanyan 5 лет назад
Родитель
Сommit
657ff11f3b

+ 16 - 0
app/src/main/java/com/sirekanyan/knigopis/feature/users/UserItemCallback.kt

@@ -0,0 +1,16 @@
+package com.sirekanyan.knigopis.feature.users
+
+import androidx.recyclerview.widget.DiffUtil
+import com.sirekanyan.knigopis.model.UserModel
+
+class UserItemCallback : DiffUtil.ItemCallback<UserModel>() {
+
+    override fun areItemsTheSame(oldItem: UserModel, newItem: UserModel): Boolean =
+        oldItem.id == newItem.id
+
+    override fun areContentsTheSame(oldItem: UserModel, newItem: UserModel): Boolean =
+        oldItem.name == newItem.name &&
+                oldItem.booksCount == newItem.booksCount &&
+                oldItem.newBooksCount == newItem.newBooksCount
+
+}

+ 1 - 1
app/src/main/java/com/sirekanyan/knigopis/feature/users/UserViewHolder.kt

@@ -32,7 +32,7 @@ class UserViewHolder(
         userImage.setCircleImage(model.image)
         userNickname.text = model.name
         totalBooksCount.text = model.booksCount
-        newBooksCount.text = model.newBooksCount
+        newBooksCount.text = model.newBooksCountFormatted
     }
 
 }

+ 1 - 2
app/src/main/java/com/sirekanyan/knigopis/feature/users/UsersAdapter.kt

@@ -3,14 +3,13 @@ package com.sirekanyan.knigopis.feature.users
 import android.view.ViewGroup
 import androidx.recyclerview.widget.ListAdapter
 import com.sirekanyan.knigopis.R
-import com.sirekanyan.knigopis.common.android.adapter.SimpleItemCallback
 import com.sirekanyan.knigopis.common.extensions.inflate
 import com.sirekanyan.knigopis.model.UserModel
 
 class UsersAdapter(
     private val onClick: (UserModel) -> Unit,
     private val onLongClick: (UserModel) -> Unit
-) : ListAdapter<UserModel, UserViewHolder>(SimpleItemCallback(UserModel::id)) {
+) : ListAdapter<UserModel, UserViewHolder>(UserItemCallback()) {
 
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
         UserViewHolder(parent.inflate(R.layout.user), onClick, onLongClick)

+ 12 - 2
app/src/main/java/com/sirekanyan/knigopis/model/UserModel.kt

@@ -1,12 +1,22 @@
 package com.sirekanyan.knigopis.model
 
 import android.text.SpannableString
+import android.text.style.SuperscriptSpan
 
 class UserModel(
     val id: String,
     val name: String,
     val image: String?,
     val booksCount: String?,
-    val newBooksCount: SpannableString?,
+    val newBooksCount: String?,
     val profiles: List<String>
-)
+) {
+
+    val newBooksCountFormatted: SpannableString?
+        get() = newBooksCount?.let { count ->
+            SpannableString(count).also {
+                it.setSpan(SuperscriptSpan(), 0, count.length, 0)
+            }
+        }
+
+}

+ 1 - 8
app/src/main/java/com/sirekanyan/knigopis/model/mappers.kt

@@ -1,8 +1,6 @@
 package com.sirekanyan.knigopis.model
 
-import android.text.SpannableString
 import android.text.format.DateUtils
-import android.text.style.SuperscriptSpan
 import com.sirekanyan.knigopis.MAX_BOOK_PRIORITY
 import com.sirekanyan.knigopis.MIN_BOOK_PRIORITY
 import com.sirekanyan.knigopis.common.functions.createBookImageUrl
@@ -40,12 +38,7 @@ fun Subscription.toUserModel() =
         subUser.name,
         createUserImageUrl(subUser.id),
         subUser.booksCount.takeIf { it > 0 }?.toString(),
-        newBooksCount.takeIf { it > 0 }?.let { count ->
-            val str = "+$count"
-            SpannableString(str).also {
-                it.setSpan(SuperscriptSpan(), 0, str.length, 0)
-            }
-        },
+        newBooksCount.takeIf { it > 0 }?.let { "+$it" },
         subUser.profiles
     )
 

+ 8 - 1
app/src/main/java/com/sirekanyan/knigopis/repository/cache/CommonCache.kt

@@ -3,7 +3,9 @@ package com.sirekanyan.knigopis.repository.cache
 import android.content.Context
 import android.content.Context.MODE_PRIVATE
 import com.google.gson.Gson
+import com.google.gson.JsonSyntaxException
 import com.google.gson.reflect.TypeToken
+import com.sirekanyan.knigopis.common.functions.logError
 import io.reactivex.Completable
 import io.reactivex.Maybe
 import java.lang.reflect.Type
@@ -30,7 +32,12 @@ class CommonCacheImpl(
     override fun <T> find(key: CacheKey, type: Type): Maybe<T> =
         Maybe.fromCallable {
             prefs.getString(key.storeValue, null)?.let { json ->
-                gson.fromJson<T>(json, type)
+                try {
+                    gson.fromJson<T>(json, type)
+                } catch (exception: JsonSyntaxException) {
+                    logError("cannot parse json from cache", exception)
+                    null
+                }
             }
         }