Kaynağa Gözat

Added bottom sheet dialog on home screen

Vadik Sirekanyan 7 yıl önce
ebeveyn
işleme
9cd99b5974

+ 10 - 1
app/src/main/java/me/vadik/knigopis/MainActivity.kt

@@ -23,6 +23,7 @@ import me.vadik.knigopis.adapters.users.UsersAdapter
 import me.vadik.knigopis.api.BookCoverSearch
 import me.vadik.knigopis.api.Endpoint
 import me.vadik.knigopis.auth.KAuth
+import me.vadik.knigopis.dialog.BottomSheetDialogFactory
 import me.vadik.knigopis.model.Book
 import me.vadik.knigopis.model.CurrentTab
 import me.vadik.knigopis.model.CurrentTab.*
@@ -51,7 +52,15 @@ class MainActivity : AppCompatActivity(), Router {
     private val allBooks = mutableListOf<Book>()
     private val allUsers = mutableListOf<Subscription>()
     private val allNotes = mutableListOf<Note>()
-    private val booksAdapter by lazy { BooksAdapter(bookCoverSearch, api, auth, this) }
+    private val booksAdapter by lazy {
+        BooksAdapter(
+            bookCoverSearch,
+            api,
+            auth,
+            this,
+            BottomSheetDialogFactory(this)
+        )
+    }
     private val allBooksAdapter by lazy { booksAdapter.build(allBooks) }
     private val usersAdapter by lazy { UsersAdapter(allUsers, this) }
     private val notesAdapter by lazy { NotesAdapter(allNotes, this) }

+ 12 - 10
app/src/main/java/me/vadik/knigopis/adapters/BooksAdapter.kt

@@ -12,6 +12,8 @@ import me.vadik.knigopis.*
 import me.vadik.knigopis.api.BookCoverSearch
 import me.vadik.knigopis.api.Endpoint
 import me.vadik.knigopis.auth.KAuth
+import me.vadik.knigopis.dialog.DialogFactory
+import me.vadik.knigopis.dialog.DialogItem
 import me.vadik.knigopis.model.Book
 import me.vadik.knigopis.model.BookHeader
 import me.vadik.knigopis.model.FinishedBook
@@ -21,7 +23,8 @@ class BooksAdapter(
     private val coverSearch: BookCoverSearch,
     private val api: Endpoint,
     private val auth: KAuth,
-    private val router: Router
+    private val router: Router,
+    private val dialogs: DialogFactory
 ) {
 
     fun build(books: MutableList<Book>) = Adapter(books) {
@@ -70,16 +73,15 @@ class BooksAdapter(
                 router.openEditBookScreen(book)
             }
             setOnLongClickListener {
-                AlertDialog.Builder(context)
-                    .setTitle(book.fullTitle)
-                    .setItems(R.array.book_context_menu) { dialog, menu ->
-                        when (menu) {
-                            0 -> router.openEditBookScreen(book)
-                            1 -> onDeleteClicked(book)
-                        }
-                        dialog.dismiss()
+                dialogs.showDialog(
+                    book.fullTitle,
+                    DialogItem(R.string.book_option_edit, R.drawable.ic_edit) {
+                        router.openEditBookScreen(book)
+                    },
+                    DialogItem(R.string.book_option_delete, R.drawable.ic_delete) {
+                        onDeleteClicked(book)
                     }
-                    .show()
+                )
                 true
             }
         }

+ 36 - 0
app/src/main/java/me/vadik/knigopis/dialog/DialogFactory.kt

@@ -0,0 +1,36 @@
+package me.vadik.knigopis.dialog
+
+import android.content.Context
+import android.support.design.widget.BottomSheetDialog
+import kotlinx.android.synthetic.main.bottom_sheet_dialog_item.view.*
+import kotlinx.android.synthetic.main.bottom_sheet_dialog_view.*
+import me.vadik.knigopis.R
+import me.vadik.knigopis.inflate
+
+interface DialogFactory {
+
+    fun showDialog(title: String, vararg items: DialogItem)
+
+}
+
+class BottomSheetDialogFactory(private val context: Context) : DialogFactory {
+
+    override fun showDialog(title: String, vararg items: DialogItem) {
+        val dialog = BottomSheetDialog(context)
+        dialog.setContentView(R.layout.bottom_sheet_dialog_view)
+        dialog.bottomSheetTitle.text = title
+        val container = dialog.bottomSheetContainer
+        items.forEach { item ->
+            val itemView = container.inflate(R.layout.bottom_sheet_dialog_item)
+            itemView.bottomSheetItemIcon.setImageResource(item.iconRes)
+            itemView.bottomSheetItemText.setText(item.titleRes)
+            itemView.setOnClickListener {
+                item.onClick()
+                dialog.hide()
+            }
+            container.addView(itemView)
+        }
+        dialog.show()
+    }
+
+}

+ 10 - 0
app/src/main/java/me/vadik/knigopis/dialog/DialogItem.kt

@@ -0,0 +1,10 @@
+package me.vadik.knigopis.dialog
+
+import android.support.annotation.DrawableRes
+import android.support.annotation.StringRes
+
+class DialogItem(
+    @StringRes val titleRes: Int,
+    @DrawableRes val iconRes: Int,
+    val onClick: () -> Unit
+)

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

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

+ 9 - 0
app/src/main/res/drawable/ic_delete.xml

@@ -0,0 +1,9 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
+</vector>

+ 9 - 0
app/src/main/res/drawable/ic_edit.xml

@@ -0,0 +1,9 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/>
+</vector>

+ 30 - 0
app/src/main/res/layout/bottom_sheet_dialog_item.xml

@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="48dp"
+    android:background="?android:attr/selectableItemBackground"
+    android:paddingEnd="16dp"
+    android:paddingStart="16dp"
+    tools:background="@color/white">
+
+    <ImageView
+        android:id="@+id/bottomSheetItemIcon"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center_vertical"
+        android:tint="#777777"
+        tools:ignore="ContentDescription"
+        tools:src="@drawable/ic_edit" />
+
+    <TextView
+        android:id="@+id/bottomSheetItemText"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center_vertical"
+        android:layout_marginStart="56dp"
+        android:textColor="@color/black_87"
+        android:textSize="16sp"
+        tools:text="@string/book_option_edit" />
+
+</FrameLayout>

+ 24 - 0
app/src/main/res/layout/bottom_sheet_dialog_view.xml

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:id="@+id/bottomSheetContainer"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical"
+    android:paddingBottom="8dp"
+    tools:background="@color/white">
+
+    <TextView
+        android:id="@+id/bottomSheetTitle"
+        android:layout_width="match_parent"
+        android:layout_height="56dp"
+        android:ellipsize="end"
+        android:gravity="center_vertical"
+        android:maxLines="1"
+        android:paddingEnd="16dp"
+        android:paddingStart="16dp"
+        android:textColor="@color/black_54"
+        android:textSize="16sp"
+        tools:text="Мастер и Маргарита — Михаил Булгаков" />
+
+</LinearLayout>

+ 5 - 3
app/src/main/res/values/colors.xml

@@ -8,9 +8,11 @@
     <color name="colorPrimary50">#EDE7F6</color>
     <color name="colorAccent">#FF3D00</color>
     <color name="white">#FFFFFF</color>
-    <color name="white_12">#E1E1E1</color>
-    <color name="black_12">#1F1F1F</color>
-    <color name="black_54">#757575</color>
+    <color name="white_12">#1FFFFFFF</color>
+    <color name="black_12">#1F000000</color>
+    <color name="black_20">#33000000</color>
+    <color name="black_54">#8A000000</color>
+    <color name="black_87">#DE000000</color>
     <color name="image_placeholder_color">@color/colorPrimary50</color>
     <color name="ic_launcher_background">#512DA8</color>
 </resources>

+ 2 - 4
app/src/main/res/values/strings.xml

@@ -32,10 +32,8 @@
     <string name="book_read_checkbox">Done</string>
 
     <!-- book menu -->
-    <array name="book_context_menu">
-        <item>Edit</item>
-        <item>Delete</item>
-    </array>
+    <string name="book_option_edit">Edit</string>
+    <string name="book_option_delete">Delete</string>
     <string name="book_delete_confirmation_title">Confirmation</string>
     <string name="book_delete_confirm_text">Are you sure you want to delete \"%s\"?</string>
     <string name="book_cancel_delete">Cancel</string>