Aidan Follestad 6 年之前
父节点
当前提交
c90e8144fb

+ 3 - 2
core/src/main/java/com/afollestad/materialdialogs/utils/ColorExt.kt

@@ -29,8 +29,9 @@ import com.afollestad.materialdialogs.utils.MDUtil.resolveColors
 @ColorInt @CheckResult
 internal inline fun MaterialDialog.resolveColor(
   @ColorRes res: Int? = null,
-  @AttrRes attr: Int? = null
-): Int = resolveColor(windowContext, res, attr)
+  @AttrRes attr: Int? = null,
+  noinline fallback: (() -> Int)? = null
+): Int = resolveColor(windowContext, res, attr, fallback)
 
 @CheckResult
 internal inline fun MaterialDialog.resolveColors(

+ 14 - 13
core/src/main/java/com/afollestad/materialdialogs/utils/DialogExt.kt

@@ -13,6 +13,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+@file:Suppress("NOTHING_TO_INLINE")
+
 package com.afollestad.materialdialogs.utils
 
 import android.content.Context.INPUT_METHOD_SERVICE
@@ -45,7 +47,7 @@ import com.afollestad.materialdialogs.utils.MDUtil.resolveDrawable
 import com.afollestad.materialdialogs.utils.MDUtil.resolveString
 import kotlin.math.min
 
-internal fun MaterialDialog.setWindowConstraints(
+internal inline fun MaterialDialog.setWindowConstraints(
   @DimenRes maxWidthRes: Int = R.dimen.md_dialog_max_width
 ) {
   val win = window ?: return
@@ -79,11 +81,10 @@ internal fun MaterialDialog.setWindowConstraints(
   }
 }
 
-internal fun MaterialDialog.setDefaults() {
+internal inline fun MaterialDialog.setDefaults() {
   // Background color and corner radius
-  var backgroundColor = resolveColor(attr = R.attr.md_background_color)
-  if (backgroundColor == 0) {
-    backgroundColor = resolveColor(attr = R.attr.colorBackgroundFloating)
+  val backgroundColor = resolveColor(attr = R.attr.md_background_color) {
+    resolveColor(attr = R.attr.colorBackgroundFloating)
   }
   colorBackground(color = backgroundColor)
   // Fonts
@@ -98,7 +99,7 @@ fun MaterialDialog.invalidateDividers(
   atBottom: Boolean
 ) = view.invalidateDividers(scrolledDown, atBottom)
 
-internal fun MaterialDialog.preShow() {
+internal inline fun MaterialDialog.preShow() {
   val customViewNoVerticalPadding = config[CUSTOM_VIEW_NO_VERTICAL_PADDING] as? Boolean == true
   this.preShowListeners.invokeAll(this)
 
@@ -119,7 +120,7 @@ internal fun MaterialDialog.preShow() {
   }
 }
 
-internal fun MaterialDialog.postShow() {
+internal inline fun MaterialDialog.postShow() {
   val negativeBtn = getActionButton(NEGATIVE)
   if (negativeBtn.isVisible()) {
     negativeBtn.post { negativeBtn.requestFocus() }
@@ -169,7 +170,7 @@ internal fun MaterialDialog.populateText(
   }
 }
 
-internal fun MaterialDialog.hideKeyboard() {
+internal inline fun MaterialDialog.hideKeyboard() {
   val imm =
     windowContext.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
   val currentFocus = currentFocus
@@ -183,10 +184,10 @@ internal fun MaterialDialog.hideKeyboard() {
   }
 }
 
-internal fun MaterialDialog.colorBackground(@ColorInt color: Int): MaterialDialog {
-  val drawable = GradientDrawable()
-  drawable.cornerRadius = dimen(attr = R.attr.md_corner_radius)
-  drawable.setColor(color)
-  window?.setBackgroundDrawable(drawable)
+internal inline fun MaterialDialog.colorBackground(@ColorInt color: Int): MaterialDialog {
+  window?.setBackgroundDrawable(GradientDrawable().apply {
+    cornerRadius = dimen(attr = R.attr.md_corner_radius)
+    setColor(color)
+  })
   return this
 }

+ 4 - 2
core/src/main/java/com/afollestad/materialdialogs/utils/IntArrayExt.kt

@@ -13,15 +13,17 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+@file:Suppress("NOTHING_TO_INLINE")
+
 package com.afollestad.materialdialogs.utils
 
-internal fun IntArray.appendAll(values: Collection<Int>): IntArray {
+internal inline fun IntArray.appendAll(values: Collection<Int>): IntArray {
   return toMutableList()
       .apply { addAll(values) }
       .toIntArray()
 }
 
-internal fun IntArray.removeAll(values: Collection<Int>): IntArray {
+internal inline fun IntArray.removeAll(values: Collection<Int>): IntArray {
   return toMutableList().apply { removeAll { values.contains(it) } }
       .toIntArray()
 }

+ 48 - 26
input/src/main/java/com/afollestad/materialdialogs/input/DialogInputExt.kt

@@ -42,10 +42,15 @@ typealias InputCallback = ((MaterialDialog, CharSequence) -> Unit)?
  * @throws IllegalStateException if the dialog is not an input dialog.
  */
 @CheckResult fun MaterialDialog.getInputLayout(): TextInputLayout {
+  val key = "[custom_view_input_layout]"
+  return config[key] as? TextInputLayout ?: lookupInputLayout().also {
+    config[key] = it
+  }
+}
+
+private fun MaterialDialog.lookupInputLayout(): TextInputLayout {
   return getCustomView().findViewById(R.id.md_input_layout) as? TextInputLayout
-      ?: throw IllegalStateException(
-          "You have not setup this dialog as an input dialog."
-      )
+      ?: throw IllegalStateException("You have not setup this dialog as an input dialog.")
 }
 
 /**
@@ -101,6 +106,37 @@ fun MaterialDialog.input(
     positiveButton { callback.invoke(this@input, getInputField().text ?: "") }
   }
 
+  prefillInput(prefill = prefill, prefillRes = prefillRes, allowEmpty = allowEmpty)
+  styleInput(hint = hint, hintRes = hintRes, inputType = inputType)
+
+  if (maxLength != null) {
+    getInputLayout().run {
+      isCounterEnabled = true
+      counterMaxLength = maxLength
+    }
+    invalidateInputMaxLength(allowEmpty)
+  }
+
+  getInputField().textChanged {
+    if (!allowEmpty) {
+      setActionButtonEnabled(POSITIVE, it.isNotEmpty())
+    }
+    maxLength?.let { invalidateInputMaxLength(allowEmpty) }
+
+    if (!waitForPositiveButton && callback != null) {
+      // We aren't waiting for positive, so invoke every time the text changes
+      callback.invoke(this, it)
+    }
+  }
+
+  return this
+}
+
+private fun MaterialDialog.prefillInput(
+  prefill: CharSequence?,
+  prefillRes: Int?,
+  allowEmpty: Boolean
+) {
   val resources = windowContext.resources
   val editText = getInputField()
 
@@ -113,6 +149,15 @@ fun MaterialDialog.input(
       POSITIVE,
       allowEmpty || prefillText.isNotEmpty()
   )
+}
+
+private fun MaterialDialog.styleInput(
+  hint: String?,
+  hintRes: Int?,
+  inputType: Int
+) {
+  val resources = windowContext.resources
+  val editText = getInputField()
 
   editText.hint = hint ?: if (hintRes != null) resources.getString(hintRes) else null
   editText.inputType = inputType
@@ -122,27 +167,4 @@ fun MaterialDialog.input(
       hintAttrRes = R.attr.md_color_hint
   )
   bodyFont?.let(editText::setTypeface)
-
-  if (maxLength != null) {
-    getInputLayout().run {
-      isCounterEnabled = true
-      counterMaxLength = maxLength
-    }
-    invalidateInputMaxLength(allowEmpty)
-  }
-
-  editText.textChanged {
-    if (!allowEmpty) {
-      setActionButtonEnabled(POSITIVE, it.isNotEmpty())
-    }
-    if (maxLength != null) {
-      invalidateInputMaxLength(allowEmpty)
-    }
-    if (!waitForPositiveButton && callback != null) {
-      // We aren't waiting for positive, so invoke every time the text changes
-      callback.invoke(this, it)
-    }
-  }
-
-  return this
 }