Browse Source

Support hiding alpha selection for just custom RGB

Aidan Follestad 6 năm trước cách đây
mục cha
commit
acc0d1f4d2

+ 19 - 8
color/src/main/java/com/afollestad/materialdialogs/color/DialogColorChooserExt.kt

@@ -31,6 +31,8 @@ import androidx.viewpager.widget.ViewPager
 import com.afollestad.materialdialogs.MaterialDialog
 import com.afollestad.materialdialogs.WhichButton.POSITIVE
 import com.afollestad.materialdialogs.actions.setActionButtonEnabled
+import com.afollestad.materialdialogs.color.utils.below
+import com.afollestad.materialdialogs.color.utils.changeHeight
 import com.afollestad.materialdialogs.color.utils.onPageSelected
 import com.afollestad.materialdialogs.color.utils.progressChanged
 import com.afollestad.materialdialogs.color.utils.textChanged
@@ -38,6 +40,7 @@ import com.afollestad.materialdialogs.customview.customView
 import com.afollestad.materialdialogs.customview.getCustomView
 import com.google.android.material.tabs.TabLayout
 import java.lang.Integer.toHexString
+import java.lang.String.format
 
 typealias ColorCallback = ((dialog: MaterialDialog, color: Int) -> Unit)?
 
@@ -141,13 +144,15 @@ private fun MaterialDialog.updateCustomPage(
 ) {
   val customPage = getPageCustomView()
   val argbPreviewView = customPage.findViewById<View>(R.id.argbPreviewView)
+  val alphaLabel = customPage.findViewById<TextView>(R.id.alpha_label)
   val alphaSeeker = customPage.findViewById<SeekBar>(R.id.alpha_seeker)
-  val redSeeker = customPage.findViewById<SeekBar>(R.id.red_seeker)
-  val greenSeeker = customPage.findViewById<SeekBar>(R.id.green_seeker)
-  val blueSeeker = customPage.findViewById<SeekBar>(R.id.blue_seeker)
   val alphaValue = customPage.findViewById<TextView>(R.id.alpha_value)
+  val redLabel = customPage.findViewById<TextView>(R.id.red_label)
+  val redSeeker = customPage.findViewById<SeekBar>(R.id.red_seeker)
   val redValue = customPage.findViewById<TextView>(R.id.red_value)
+  val greenSeeker = customPage.findViewById<SeekBar>(R.id.green_seeker)
   val greenValue = customPage.findViewById<TextView>(R.id.green_value)
+  val blueSeeker = customPage.findViewById<SeekBar>(R.id.blue_seeker)
   val blueValue = customPage.findViewById<TextView>(R.id.blue_value)
   val hexValue = customPage.findViewById<EditText>(R.id.hex_value)
 
@@ -167,7 +172,13 @@ private fun MaterialDialog.updateCustomPage(
     alphaSeeker.progress = ALPHA_SOLID
   }
 
-//  alphaFrame.visibility = if (supportCustomAlpha) VISIBLE else GONE
+  if (!supportCustomAlpha) {
+    alphaLabel.changeHeight(0)
+    alphaSeeker.changeHeight(0)
+    alphaValue.changeHeight(0)
+    redLabel.below(R.id.hex_frame)
+  }
+
   arrayOf(alphaSeeker, redSeeker, greenSeeker, blueSeeker).progressChanged {
     onCustomValueChanged(
         supportCustomAlpha = supportCustomAlpha,
@@ -256,7 +267,7 @@ private fun MaterialDialog.onCustomValueChanged(
       greenSeeker.progress,
       blueSeeker.progress
   )
-  hexValue.setText(color.hexValue())
+  hexValue.setText(color.hexValue(supportCustomAlpha))
   hexValue.post { hexValue.setSelection(hexValue.text.length) }
 
   val previewDrawable = GradientDrawable()
@@ -299,8 +310,8 @@ private fun SeekBar.tint(color: Int) {
   thumb.setColorFilter(color, SRC_IN)
 }
 
-private fun Int.hexValue() = if (this == 0) {
-  "00000000"
+private fun Int.hexValue(includeAlpha: Boolean) = if (this == 0) {
+  if (includeAlpha) "00000000" else "000000"
 } else {
-  toHexString(this)
+  if (includeAlpha) toHexString(this) else format("%06X", 0xFFFFFF and this)
 }

+ 19 - 0
color/src/main/java/com/afollestad/materialdialogs/color/utils/ViewExt.kt

@@ -9,10 +9,14 @@ import android.text.Editable
 import android.text.TextWatcher
 import android.view.View
 import android.view.View.GONE
+import android.view.View.INVISIBLE
 import android.view.View.VISIBLE
+import android.view.ViewGroup.MarginLayoutParams
+import android.widget.RelativeLayout
 import android.widget.SeekBar
 import android.widget.TextView
 import androidx.annotation.DimenRes
+import androidx.annotation.IdRes
 import androidx.viewpager.widget.ViewPager
 
 internal fun <T : View> T.dimenPx(@DimenRes res: Int): Int {
@@ -72,3 +76,18 @@ internal fun TextView.textChanged(changed: (String) -> Unit) {
     ) = changed(s.toString())
   })
 }
+
+internal fun View.changeHeight(height: Int) {
+  if (height == 0) {
+    visibility = INVISIBLE
+  }
+  val lp = layoutParams as MarginLayoutParams
+  lp.height = height
+  layoutParams = lp
+}
+
+internal fun View.below(@IdRes id: Int) {
+  val lp = layoutParams as RelativeLayout.LayoutParams
+  lp.addRule(RelativeLayout.BELOW, id)
+  layoutParams = lp
+}

+ 24 - 5
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.kt

@@ -52,6 +52,8 @@ import kotlinx.android.synthetic.main.activity_main.colorChooser_accent
 import kotlinx.android.synthetic.main.activity_main.colorChooser_customColors
 import kotlinx.android.synthetic.main.activity_main.colorChooser_customColorsNoSub
 import kotlinx.android.synthetic.main.activity_main.colorChooser_primary
+import kotlinx.android.synthetic.main.activity_main.colorChooser_primary_customArgb
+import kotlinx.android.synthetic.main.activity_main.colorChooser_primary_customRgb
 import kotlinx.android.synthetic.main.activity_main.custom_view
 import kotlinx.android.synthetic.main.activity_main.custom_view_webview
 import kotlinx.android.synthetic.main.activity_main.file_chooser
@@ -86,7 +88,6 @@ import kotlinx.android.synthetic.main.activity_main.single_choice_buttons_titled
 import kotlinx.android.synthetic.main.activity_main.single_choice_disabled_items
 import kotlinx.android.synthetic.main.activity_main.single_choice_long_items
 import kotlinx.android.synthetic.main.activity_main.single_choice_titled
-import kotlinx.android.synthetic.main.activity_main.colorChooser_primaryColorsAndCustomValues
 
 /** @author Aidan Follestad (afollestad) */
 class MainActivity : AppCompatActivity() {
@@ -642,11 +643,29 @@ class MainActivity : AppCompatActivity() {
       }
     }
 
-    colorChooser_primaryColorsAndCustomValues.setOnClickListener {
+    colorChooser_primary_customRgb.setOnClickListener {
       MaterialDialog(this).show {
-        // title does not look good in landscape mode
-        //title(R.string.primary_colors)
-        colorChooser(PRIMARY_COLORS, PRIMARY_COLORS_SUB, allowCustomArgb = true, showAlphaSelector = true) { _, color ->
+        colorChooser(
+            colors = PRIMARY_COLORS,
+            subColors = PRIMARY_COLORS_SUB,
+            allowCustomArgb = true
+        ) { _, color ->
+          toast("Selected color: ${color.toHex()}")
+        }
+        positiveButton(R.string.select)
+        negativeButton(android.R.string.cancel)
+        debugMode(debugMode)
+      }
+    }
+
+    colorChooser_primary_customArgb.setOnClickListener {
+      MaterialDialog(this).show {
+        colorChooser(
+            colors = PRIMARY_COLORS,
+            subColors = PRIMARY_COLORS_SUB,
+            allowCustomArgb = true,
+            showAlphaSelector = true
+        ) { _, color ->
           toast("Selected color: ${color.toHex()}")
         }
         positiveButton(R.string.select)

+ 9 - 1
sample/src/main/res/layout/activity_main.xml

@@ -436,7 +436,15 @@
         />
 
     <Button
-        android:id="@+id/colorChooser_primaryColorsAndCustomValues"
+        android:id="@+id/colorChooser_primary_customRgb"
+        android:layout_width="match_parent"
+        android:layout_height="@dimen/sample_button_height"
+        android:layout_marginTop="@dimen/sample_button_spacing"
+        android:text="Color Chooser, Primary + Custom RGB"
+        />
+
+    <Button
+        android:id="@+id/colorChooser_primary_customArgb"
         android:layout_width="match_parent"
         android:layout_height="@dimen/sample_button_height"
         android:layout_marginTop="@dimen/sample_button_spacing"