Vadik Sirekanyan 7 жил өмнө
parent
commit
fa6866b69d

+ 28 - 24
app/src/main/java/me/vadik/knigopis/adapters/users/UserViewHolder.kt

@@ -7,31 +7,35 @@ import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
 import com.bumptech.glide.request.RequestOptions
 import kotlinx.android.synthetic.main.user.view.*
 import me.vadik.knigopis.R
+import me.vadik.knigopis.getHtmlString
+import me.vadik.knigopis.showNow
 
 class UserViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
 
-    var avatarUrl: String? = null
-        set(value) {
-            field = value
-            Glide.with(view.context)
-                .load(value)
-                .apply(
-                    RequestOptions.circleCropTransform()
-                        .placeholder(R.drawable.oval_placeholder_background)
-                )
-                .transition(DrawableTransitionOptions.withCrossFade())
-                .into(view.userAvatar)
-        }
-
-    var nickname: String
-        get() = view.userNickname.text.toString()
-        set(value) {
-            view.userNickname.text = value
-        }
-
-    var profile: String
-        get() = view.userProfile.text.toString()
-        set(value) {
-            view.userProfile.text = value
-        }
+    private val context = view.context.applicationContext
+
+    fun setAvatarUrl(url: String?) =
+        Glide.with(view.context)
+            .load(url)
+            .apply(
+                RequestOptions.circleCropTransform()
+                    .placeholder(R.drawable.oval_placeholder_background)
+            )
+            .transition(DrawableTransitionOptions.withCrossFade())
+            .into(view.userAvatar)
+
+    fun setNickname(nickname: String) {
+        view.userNickname.text = nickname
+    }
+
+    fun setBooksCount(count: Int) {
+        view.totalBooksCount.showNow(count > 0)
+        view.totalBooksCount.text = count.toString()
+    }
+
+    fun setNewBooksCount(count: Int) {
+        view.newBooksCount.showNow(count > 0)
+        view.newBooksCount.text = context.getHtmlString(R.string.user_new_books_count, count)
+    }
+
 }

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

@@ -23,13 +23,13 @@ class UsersAdapter(
 
     override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
         val user = users[position]
-        holder.nickname = user.subUser.name
-        holder.avatarUrl = user.subUser.avatar
-        holder.profile = "${user.subUser.booksCount} прочитано" + user.newBooksCount?.let {
-            " (+$it новых)"
-        }.orEmpty()
+        holder.setAvatarUrl(user.subUser.avatar)
+        holder.setNickname(user.subUser.name)
+        holder.setBooksCount(user.subUser.booksCount)
+        holder.setNewBooksCount(user.newBooksCount)
         holder.view.setOnClickListener {
             router.openUserScreen(user)
         }
     }
+
 }

+ 18 - 0
app/src/main/java/me/vadik/knigopis/extensions.kt

@@ -5,9 +5,12 @@ import android.app.Activity
 import android.content.Context
 import android.content.Intent
 import android.net.Uri
+import android.os.Build
 import android.support.annotation.IdRes
 import android.support.annotation.LayoutRes
 import android.support.annotation.StringRes
+import android.text.Html
+import android.text.Spanned
 import android.util.Log
 import android.view.LayoutInflater
 import android.view.View
@@ -87,6 +90,10 @@ fun View.showNow() {
     visibility = View.VISIBLE
 }
 
+fun View.showNow(value: Boolean) {
+    visibility = if (value) View.VISIBLE else View.GONE
+}
+
 fun View.hideNow() {
     visibility = View.GONE
 }
@@ -117,5 +124,16 @@ fun Activity.hideKeyboard() {
 fun String.toUriOrNull() =
     Uri.parse(this).takeIf(Uri::isValidHttpLink)
 
+fun Context.getHtmlString(resId: Int, vararg args: Any) =
+    getString(resId, *args).fromHtml()
+
+private fun String.fromHtml(): Spanned =
+    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+        Html.fromHtml(this, Html.FROM_HTML_MODE_LEGACY)
+    } else {
+        @Suppress("DEPRECATION")
+        Html.fromHtml(this)
+    }
+
 private fun Uri.isValidHttpLink() =
     scheme in HTTP_SCHEMES && !host.isNullOrBlank()

+ 1 - 1
app/src/main/java/me/vadik/knigopis/model/subscription/Subscription.kt

@@ -4,5 +4,5 @@ class Subscription(
     val subUser: SubUser,
     private val lastBooksCount: Int
 ) {
-    val newBooksCount get() = (subUser.booksCount - lastBooksCount).takeIf { it > 0 }
+    val newBooksCount get() = subUser.booksCount - lastBooksCount
 }

+ 34 - 32
app/src/main/res/layout/user.xml

@@ -1,48 +1,50 @@
 <?xml version="1.0" encoding="utf-8"?>
-<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="72dp"
-    android:foreground="?android:attr/selectableItemBackground">
+    android:foreground="?android:attr/selectableItemBackground"
+    android:paddingEnd="16dp"
+    android:paddingStart="16dp">
 
     <ImageView
         android:id="@+id/userAvatar"
         android:layout_width="40dp"
         android:layout_height="40dp"
         android:layout_gravity="center_vertical"
-        android:layout_marginStart="16dp"
         android:background="@drawable/oval_placeholder_background"
         tools:ignore="ContentDescription" />
 
-    <LinearLayout
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:layout_gravity="center_vertical"
-        android:orientation="vertical"
-        android:paddingEnd="16dp"
-        android:paddingStart="72dp">
-
-        <TextView
-            android:id="@+id/userNickname"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:ellipsize="end"
-            android:maxLines="1"
-            android:textColor="@android:color/primary_text_light"
-            android:textSize="16sp"
-            tools:text="Павел Дуров" />
+    <TextView
+        android:id="@+id/userNickname"
+        android:layout_width="0dp"
+        android:layout_height="match_parent"
+        android:layout_marginStart="16dp"
+        android:layout_weight="1"
+        android:ellipsize="end"
+        android:gravity="center_vertical"
+        android:maxLines="1"
+        android:textColor="@android:color/primary_text_light"
+        android:textSize="16sp"
+        tools:text="Павел Дуров Павел Дуров Павел Дуров Павел Дуров" />
 
-        <TextView
-            android:id="@+id/userProfile"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:autoLink="web"
-            android:ellipsize="end"
-            android:maxLines="1"
-            android:textColor="@android:color/tertiary_text_light"
-            android:textSize="14sp"
-            tools:text="https://vk.com/id1" />
+    <TextView
+        android:id="@+id/totalBooksCount"
+        android:layout_width="wrap_content"
+        android:layout_height="match_parent"
+        android:layout_marginStart="16dp"
+        android:gravity="center_vertical"
+        android:textColor="@android:color/tertiary_text_light"
+        android:textSize="16sp"
+        tools:text="40" />
 
-    </LinearLayout>
+    <TextView
+        android:id="@+id/newBooksCount"
+        android:layout_width="wrap_content"
+        android:layout_height="match_parent"
+        android:gravity="center_vertical"
+        android:textColor="@android:color/holo_green_dark"
+        android:textSize="16sp"
+        tools:text="+2" />
 
-</FrameLayout>
+</LinearLayout>

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

@@ -43,6 +43,9 @@
     <string name="cannot_delete_book">Cannot delete book</string>
     <string name="book_hint_notes">Notes</string>
 
+    <!-- following -->
+    <string name="user_new_books_count" translatable="false"><![CDATA[<sup><small>+%d</small><sup>]]></string>
+
     <!-- about -->
     <string name="about_idea">API:\nhttp://knigopis.com</string>
     <string name="about_developer">Development:\nvadik@sirekanyan.com</string>