Explorar el Código

Nullability cleanup

Aidan Follestad hace 6 años
padre
commit
ec343db216

+ 1 - 1
files/src/main/java/com/afollestad/materialdialogs/files/DialogFileChooserExt.kt

@@ -97,7 +97,7 @@ fun MaterialDialog.fileChooser(
   customView(R.layout.md_file_chooser_base, noVerticalPadding = true)
   setActionButtonEnabled(POSITIVE, false)
 
-  val customView = getCustomView() ?: return this
+  val customView = getCustomView()
   val list: DialogRecyclerView = customView.findViewById(R.id.list)
   val emptyText: TextView = customView.findViewById(R.id.empty_text)
   emptyText.setText(emptyTextRes)

+ 1 - 2
files/src/main/java/com/afollestad/materialdialogs/files/DialogFolderChooserExt.kt

@@ -37,8 +37,7 @@ import java.io.File
 /** Gets the selected folder for the current folder chooser dialog. */
 @CheckResult
 fun MaterialDialog.selectedFolder(): File? {
-  val customView = getCustomView() ?: return null
-  val list: DialogRecyclerView = customView.findViewById(R.id.list)
+  val list: DialogRecyclerView = getCustomView().findViewById(R.id.list)
   return (list.adapter as? FileChooserAdapter)?.selectedFile
 }
 

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

@@ -96,11 +96,11 @@ fun MaterialDialog.input(
 
   if (callback != null && waitForPositiveButton) {
     // Add an additional callback to invoke the input listener after the positive AB is pressed
-    positiveButton { callback.invoke(this@input, getInputField()?.text ?: "") }
+    positiveButton { callback.invoke(this@input, getInputField().text ?: "") }
   }
 
   val resources = windowContext.resources
-  val editText = getInputField() ?: return this
+  val editText = getInputField()
 
   val prefillText = prefill ?: if (prefillRes != null) resources.getString(prefillRes) else null
   if (prefillText != null) {
@@ -116,7 +116,7 @@ fun MaterialDialog.input(
   editText.inputType = inputType
 
   if (maxLength != null) {
-    getInputLayout()?.run {
+    getInputLayout().run {
       isCounterEnabled = true
       counterMaxLength = maxLength
     }

+ 4 - 5
input/src/main/java/com/afollestad/materialdialogs/input/InputUtilExt.kt

@@ -23,20 +23,19 @@ import com.afollestad.materialdialogs.WhichButton.POSITIVE
 import com.afollestad.materialdialogs.actions.setActionButtonEnabled
 
 internal fun MaterialDialog.invalidateInputMaxLength() {
-  val maxLength = getInputLayout()?.counterMaxLength ?: return
-  val currentLength = getInputField()?.text?.length ?: 0
+  val maxLength = getInputLayout().counterMaxLength
+  val currentLength = getInputField().text?.length ?: 0
   if (maxLength > 0) {
     setActionButtonEnabled(POSITIVE, currentLength <= maxLength)
   }
 }
 
 internal fun MaterialDialog.showKeyboardIfApplicable() {
-  val editText = getInputField() ?: return
-  editText.postRun {
+  getInputField().postRun {
     requestFocus()
     val imm =
       windowContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
-    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
+    imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
   }
 }
 

+ 3 - 6
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.kt

@@ -732,8 +732,7 @@ class MainActivity : AppCompatActivity() {
       customView(R.layout.custom_view, scrollable = true)
       positiveButton(R.string.connect) { dialog ->
         // Pull the password out of the custom view when the positive button is pressed
-        val customView = dialog.getCustomView() ?: return@positiveButton
-        val passwordInput: EditText = customView.findViewById(R.id.password)
+        val passwordInput: EditText = dialog.getCustomView().findViewById(R.id.password)
         toast("Password: $passwordInput")
       }
       negativeButton(android.R.string.cancel)
@@ -741,7 +740,7 @@ class MainActivity : AppCompatActivity() {
     }
 
     // Setup custom view content
-    val customView = dialog.getCustomView() ?: return
+    val customView = dialog.getCustomView()
     val passwordInput: EditText = customView.findViewById(R.id.password)
     val showPasswordCheck: CheckBox = customView.findViewById(R.id.showPassword)
     showPasswordCheck.setOnCheckedChangeListener { _, isChecked ->
@@ -759,9 +758,7 @@ class MainActivity : AppCompatActivity() {
     }
 
     dialog.onShow {
-      val customView = it.getCustomView() ?: return@onShow
-      val webView: WebView = customView.findViewById(R.id.web_view)
-
+      val webView: WebView = it.getCustomView().findViewById(R.id.web_view)
       webView.loadData(
           "<h3>WebView Custom View</h3>\n" +
               "\n" +

+ 3 - 3
sample/src/main/res/values/styles.xml

@@ -1,19 +1,19 @@
 <resources xmlns:tools="http://schemas.android.com/tools">
 
   <!-- http://www.google.com/design/spec/style/color.html#color-color-palette -->
-  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
+  <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
     <item name="colorPrimary">@color/primary</item>
     <item name="colorPrimaryDark">@color/primaryDark</item>
     <item name="colorAccent">@color/accent</item>
   </style>
 
-  <style name="AppTheme.Dark" parent="Theme.AppCompat">
+  <style name="AppTheme.Dark" parent="Theme.MaterialComponents">
     <item name="colorPrimary">@color/primary</item>
     <item name="colorPrimaryDark">@color/primaryDark</item>
     <item name="colorAccent">@color/accent</item>
   </style>
 
-  <style name="AppTheme.Custom" parent="Theme.AppCompat.Light">
+  <style name="AppTheme.Custom" parent="Theme.MaterialComponents.Light">
     <item name="colorPrimary">@color/primary_custom</item>
     <item name="colorPrimaryDark">@color/primaryDark_custom</item>
     <item name="colorAccent">@color/accent_custom</item>