Browse Source

Replaced read checkbox with seek bar

sirekanyan 8 năm trước cách đây
mục cha
commit
848843cfad

+ 16 - 21
app/src/main/java/me/vadik/knigopis/BookActivity.kt

@@ -4,8 +4,10 @@ import android.content.Context
 import android.content.Intent
 import android.os.Bundle
 import android.support.v7.app.AppCompatActivity
-import android.view.KeyEvent.ACTION_UP
-import android.view.View.*
+import android.view.View.INVISIBLE
+import android.view.View.VISIBLE
+import android.widget.SeekBar
+import android.widget.SeekBar.OnSeekBarChangeListener
 import kotlinx.android.synthetic.main.book_edit.*
 import me.vadik.knigopis.api.BookCoverSearch
 import me.vadik.knigopis.api.BookCoverSearchImpl
@@ -62,7 +64,7 @@ class BookActivity : AppCompatActivity() {
             when (saveMenuItem.itemId) {
                 R.id.option_save_book -> {
                     hideKeyboard()
-                    if (readCheckbox.isChecked) {
+                    if (progressSeekBar.progress == 100) {
                         repository.saveBook(
                             bookId, FinishedBookToSend(
                                 titleEditText.text.toString(),
@@ -124,23 +126,17 @@ class BookActivity : AppCompatActivity() {
                     })
             }
         }
-        readCheckbox.setOnCheckedChangeListener { _, checked ->
-            if (checked) {
-                bookDateInputGroup.visibility = VISIBLE
-                progressSeekBar.setProgressSmoothly(100)
-                progressSeekBar.hide()
-            } else {
-                bookDateInputGroup.visibility = GONE
-                progressSeekBar.setProgressSmoothly(0)
-                progressSeekBar.show()
-            }
-        }
-        progressSeekBar.setOnTouchListener { _, event ->
-            if (event.action == ACTION_UP && progressSeekBar.progress == 100) {
-                readCheckbox.isChecked = true
+        progressSeekBar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
+            override fun onStartTrackingTouch(seekBar: SeekBar?) {}
+            override fun onStopTrackingTouch(seekBar: SeekBar?) {}
+            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
+                if (progress == 100) {
+                    bookDateInputGroup.showNow()
+                } else {
+                    bookDateInputGroup.hideNow()
+                }
             }
-            false
-        }
+        })
     }
 
     override fun onStart() {
@@ -151,16 +147,15 @@ class BookActivity : AppCompatActivity() {
                 api.getFinishedBook(id)
                     .io2main()
                     .doOnSuccess { finishedBook ->
-                        readCheckbox.isChecked = true
                         yearEditText.setText(finishedBook.readYear)
                         monthEditText.setText(finishedBook.readMonth)
                         dayEditText.setText(finishedBook.readDay)
+                        progressSeekBar.setProgressSmoothly(100)
                     }
             } else {
                 api.getPlannedBook(id)
                     .io2main()
                     .doOnSuccess { plannedBook ->
-                        readCheckbox.isChecked = false
                         notesTextArea.setText(plannedBook.notes)
                         progressSeekBar.setProgressSmoothly(plannedBook.priority)
                     }

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

@@ -82,7 +82,7 @@ class BooksAdapter(
             }
         }
         .bind<ImageView>(R.id.book_image) {
-            hideNow()
+            alpha = 0f
             coverSearch.search(books[it])
                 .subscribe({ coverUrl ->
                     Glide.with(context)

+ 2 - 14
app/src/main/java/me/vadik/knigopis/extensions.kt

@@ -73,11 +73,11 @@ fun <T> RequestBuilder<T>.doOnSuccess(onSuccess: () -> Unit): RequestBuilder<T>
     })
 
 fun View.showNow() {
-    alpha = 1f
+    visibility = View.VISIBLE
 }
 
 fun View.hideNow() {
-    alpha = 0f
+    visibility = View.GONE
 }
 
 fun View.show() {
@@ -92,18 +92,6 @@ fun View.hide() {
         .start()
 }
 
-fun SeekBar.show() {
-    animate().alpha(1f)
-        .withStartAction { isEnabled = true }
-        .start()
-}
-
-fun SeekBar.hide() {
-    animate().alpha(0f)
-        .withEndAction { isEnabled = false }
-        .start()
-}
-
 fun SeekBar.setProgressSmoothly(progress: Int) {
     ObjectAnimator.ofInt(this, "progress", progress).start()
 }

+ 14 - 30
app/src/main/res/layout/book_edit.xml

@@ -36,6 +36,7 @@
                 style="@style/TextInputLayoutStyle"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
+                android:layout_marginTop="16dp"
                 app:layout_constraintLeft_toLeftOf="parent"
                 app:layout_constraintRight_toLeftOf="@id/coverImageViews"
                 app:layout_constraintTop_toTopOf="parent">
@@ -55,6 +56,7 @@
                 style="@style/TextInputLayoutStyle"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
+                android:layout_marginTop="16dp"
                 app:layout_constraintLeft_toLeftOf="parent"
                 app:layout_constraintRight_toLeftOf="@id/coverImageViews"
                 app:layout_constraintTop_toBottomOf="@id/book_title_input">
@@ -83,6 +85,14 @@
                 tools:background="@color/colorAccent"
                 tools:visibility="visible" />
 
+            <android.support.v7.widget.AppCompatSeekBar
+                android:id="@+id/progressSeekBar"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:padding="16dp"
+                app:layout_constraintTop_toBottomOf="@id/book_author_input"
+                tools:progress="20" />
+
             <android.support.design.widget.TextInputLayout
                 android:id="@+id/bookYearInput"
                 style="@style/TextInputLayoutStyle"
@@ -91,7 +101,7 @@
                 app:layout_constraintHorizontal_chainStyle="spread_inside"
                 app:layout_constraintLeft_toLeftOf="parent"
                 app:layout_constraintRight_toLeftOf="@+id/bookMonthInput"
-                app:layout_constraintTop_toBottomOf="@id/book_author_input">
+                app:layout_constraintTop_toBottomOf="@id/progressSeekBar">
 
                 <EditText
                     android:id="@+id/yearEditText"
@@ -110,7 +120,7 @@
                 android:layout_height="wrap_content"
                 app:layout_constraintLeft_toRightOf="@id/bookYearInput"
                 app:layout_constraintRight_toLeftOf="@+id/bookDayInput"
-                app:layout_constraintTop_toBottomOf="@id/book_author_input">
+                app:layout_constraintTop_toBottomOf="@id/progressSeekBar">
 
                 <EditText
                     android:id="@+id/monthEditText"
@@ -128,7 +138,7 @@
                 android:layout_height="wrap_content"
                 app:layout_constraintLeft_toRightOf="@id/bookMonthInput"
                 app:layout_constraintRight_toRightOf="parent"
-                app:layout_constraintTop_toBottomOf="@id/book_author_input">
+                app:layout_constraintTop_toBottomOf="@id/progressSeekBar">
 
                 <EditText
                     android:id="@+id/dayEditText"
@@ -139,32 +149,6 @@
 
             </android.support.design.widget.TextInputLayout>
 
-            <CheckBox
-                android:id="@+id/readCheckbox"
-                android:layout_width="0dp"
-                android:layout_height="wrap_content"
-                android:layout_marginBottom="16dp"
-                android:layout_marginEnd="9dp"
-                android:layout_marginStart="9dp"
-                android:layout_marginTop="16dp"
-                android:paddingEnd="8dp"
-                android:paddingStart="8dp"
-                android:text="@string/book_read_checkbox"
-                android:textSize="16sp"
-                app:layout_constraintLeft_toLeftOf="parent"
-                app:layout_constraintTop_toBottomOf="@id/bookYearInput"
-                tools:checked="true" />
-
-            <android.support.v7.widget.AppCompatSeekBar
-                android:id="@+id/progressSeekBar"
-                android:layout_width="0dp"
-                android:layout_height="0dp"
-                app:layout_constraintBottom_toBottomOf="@id/readCheckbox"
-                app:layout_constraintLeft_toRightOf="@id/readCheckbox"
-                app:layout_constraintRight_toRightOf="parent"
-                app:layout_constraintTop_toTopOf="@id/readCheckbox"
-                tools:progress="20" />
-
             <android.support.v7.widget.AppCompatEditText
                 android:id="@+id/notesTextArea"
                 android:layout_width="match_parent"
@@ -178,7 +162,7 @@
                 android:minLines="2"
                 android:padding="16dp"
                 app:layout_constraintBottom_toBottomOf="parent"
-                app:layout_constraintTop_toBottomOf="@id/readCheckbox"
+                app:layout_constraintTop_toBottomOf="@id/bookYearInput"
                 tools:text="Неистово плюсую" />
 
             <android.support.constraint.Group

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

@@ -13,6 +13,7 @@
     <!-- purple and orange -->
     <color name="colorPrimary">#673AB7</color>
     <color name="colorPrimaryDark">#512DA8</color>
+    <color name="colorPrimaryLight">#9575CD</color>
     <color name="colorAccent">#FF3D00</color>
 
     <color name="white">#FFFFFF</color>

+ 2 - 1
app/src/main/res/values/styles.xml

@@ -30,7 +30,8 @@
     </style>
 
     <style name="TextInputLayoutStyle">
-        <item name="android:layout_margin">16dp</item>
+        <item name="android:layout_marginLeft">16dp</item>
+        <item name="android:layout_marginRight">16dp</item>
         <item name="android:paddingStart">-4dp</item>
         <item name="android:paddingEnd">-4dp</item>
     </style>