浏览代码

Added long click for user books

Vadik Sirekanyan 7 年之前
父节点
当前提交
4af8dc741c

+ 12 - 9
app/src/main/java/me/vadik/knigopis/BookActivity.kt

@@ -30,6 +30,11 @@ private const val EXTRA_BOOK_FINISHED = "me.vadik.knigopis.extra_book_finished"
 
 fun Context.createNewBookIntent() = Intent(this, BookActivity::class.java)
 
+fun Context.createNewBookIntent(title: String, author: String): Intent =
+    Intent(this, BookActivity::class.java)
+        .putExtra(EXTRA_BOOK_TITLE, title)
+        .putExtra(EXTRA_BOOK_AUTHOR, author)
+
 fun Context.createEditBookIntent(book: PlannedBook): Intent =
     Intent(this, BookActivity::class.java)
         .putExtra(EXTRA_BOOK_ID, book.id)
@@ -149,23 +154,21 @@ class BookActivity : AppCompatActivity() {
                 progressText.text = "$progress%"
                 if (progress == 100) {
                     bookDateInputGroup.showNow()
-                    if (bookId != null && yearEditText.text.isEmpty() && monthEditText.text.isEmpty() && dayEditText.text.isEmpty()) {
+                    if (yearEditText.text.isEmpty() && monthEditText.text.isEmpty() && dayEditText.text.isEmpty()) {
                         yearEditText.setText(today.get(Calendar.YEAR).toString())
-                        monthEditText.setText(today.get(Calendar.MONTH).inc().toString())
-                        dayEditText.setText(today.get(Calendar.DAY_OF_MONTH).toString())
+                        if (bookId != null) {
+                            monthEditText.setText(today.get(Calendar.MONTH).inc().toString())
+                            dayEditText.setText(today.get(Calendar.DAY_OF_MONTH).toString())
+                        }
                     }
                 } else {
                     bookDateInputGroup.hideNow()
                 }
             }
         })
-    }
-
-    override fun onStart() {
-        super.onStart()
+        titleEditText.setText(intent.getStringExtra(EXTRA_BOOK_TITLE))
+        authorEditText.setText(intent.getStringExtra(EXTRA_BOOK_AUTHOR))
         if (bookId != null) {
-            titleEditText.setText(intent.getStringExtra(EXTRA_BOOK_TITLE))
-            authorEditText.setText(intent.getStringExtra(EXTRA_BOOK_AUTHOR))
             progressSeekBar.setProgressSmoothly(intent.getIntExtra(EXTRA_BOOK_PROGRESS, 0))
             notesTextArea.setText(intent.getStringExtra(EXTRA_BOOK_NOTES))
             if (intent.getBooleanExtra(EXTRA_BOOK_FINISHED, false)) {

+ 6 - 0
app/src/main/java/me/vadik/knigopis/adapters/books/BooksAdapter.kt

@@ -3,6 +3,7 @@ package me.vadik.knigopis.adapters.books
 import android.support.v7.widget.RecyclerView
 import android.view.ViewGroup
 import me.vadik.knigopis.R
+import me.vadik.knigopis.createNewBookIntent
 import me.vadik.knigopis.inflate
 
 class BooksAdapter(
@@ -20,5 +21,10 @@ class BooksAdapter(
         holder.title = book.title
         holder.author = book.author
         holder.notes = book.notes
+        val context = holder.view.context
+        holder.view.setOnLongClickListener {
+            context.startActivity(context.createNewBookIntent(book.title, book.author))
+            true
+        }
     }
 }

+ 3 - 2
app/src/main/res/layout/user_book.xml

@@ -3,8 +3,9 @@
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
-    android:layout_marginTop="16dp"
-    android:foreground="?android:attr/selectableItemBackground">
+    android:foreground="?android:attr/selectableItemBackground"
+    android:paddingBottom="16dp"
+    android:paddingTop="16dp">
 
     <ImageView
         android:layout_width="40dp"