Rocket Science!
This commit is contained in:
parent
3ea762b5d1
commit
d206d7c391
2 changed files with 58 additions and 53 deletions
|
@ -116,8 +116,10 @@ class AddFragment : DialogFragment() {
|
||||||
// Set foreground description text
|
// Set foreground description text
|
||||||
subscribeForegroundDescription.text = getString(R.string.add_dialog_foreground_description, shortUrl(appBaseUrl))
|
subscribeForegroundDescription.text = getString(R.string.add_dialog_foreground_description, shortUrl(appBaseUrl))
|
||||||
|
|
||||||
// Show/hide based on flavor
|
// Show/hide based on flavor (faster shortcut for validateInputSubscribeView, which can only run onShow)
|
||||||
subscribeInstantDeliveryBox.visibility = instantCheckboxVisible()
|
if (!BuildConfig.FIREBASE_AVAILABLE) {
|
||||||
|
subscribeInstantDeliveryBox.visibility = View.GONE
|
||||||
|
}
|
||||||
|
|
||||||
// Add baseUrl auto-complete behavior
|
// Add baseUrl auto-complete behavior
|
||||||
lifecycleScope.launch(Dispatchers.IO) {
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
|
@ -137,16 +139,8 @@ class AddFragment : DialogFragment() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Username/password validation on type
|
// Username/password validation on type
|
||||||
val loginTextWatcher = object : TextWatcher {
|
val loginTextWatcher = AfterChangedTextWatcher {
|
||||||
override fun afterTextChanged(s: Editable?) {
|
validateInputLoginView()
|
||||||
validateInputLoginView()
|
|
||||||
}
|
|
||||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
|
||||||
// Nothing
|
|
||||||
}
|
|
||||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
|
||||||
// Nothing
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
loginUsernameText.addTextChangedListener(loginTextWatcher)
|
loginUsernameText.addTextChangedListener(loginTextWatcher)
|
||||||
loginPasswordText.addTextChangedListener(loginTextWatcher)
|
loginPasswordText.addTextChangedListener(loginTextWatcher)
|
||||||
|
@ -176,38 +170,18 @@ class AddFragment : DialogFragment() {
|
||||||
negativeButton.setOnClickListener {
|
negativeButton.setOnClickListener {
|
||||||
negativeButtonClick()
|
negativeButtonClick()
|
||||||
}
|
}
|
||||||
val subscribeTextWatcher = object : TextWatcher {
|
val subscribeTextWatcher = AfterChangedTextWatcher {
|
||||||
override fun afterTextChanged(s: Editable?) {
|
validateInputSubscribeView()
|
||||||
validateInputSubscribeView()
|
|
||||||
}
|
|
||||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
|
||||||
// Nothing
|
|
||||||
}
|
|
||||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
|
||||||
// Nothing
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
subscribeTopicText.addTextChangedListener(subscribeTextWatcher)
|
subscribeTopicText.addTextChangedListener(subscribeTextWatcher)
|
||||||
subscribeBaseUrlText.addTextChangedListener(subscribeTextWatcher)
|
subscribeBaseUrlText.addTextChangedListener(subscribeTextWatcher)
|
||||||
subscribeInstantDeliveryCheckbox.setOnCheckedChangeListener { _, isChecked ->
|
subscribeInstantDeliveryCheckbox.setOnCheckedChangeListener { _, _ ->
|
||||||
subscribeInstantDeliveryDescription.visibility = if (isChecked) View.VISIBLE else View.GONE
|
|
||||||
}
|
|
||||||
subscribeUseAnotherServerCheckbox.setOnCheckedChangeListener { _, isChecked ->
|
|
||||||
if (isChecked) {
|
|
||||||
subscribeUseAnotherServerDescription.visibility = View.VISIBLE
|
|
||||||
subscribeBaseUrlLayout.visibility = View.VISIBLE
|
|
||||||
subscribeInstantDeliveryBox.visibility = instantCheckboxVisible()
|
|
||||||
subscribeInstantDeliveryDescription.visibility = View.GONE
|
|
||||||
} else {
|
|
||||||
subscribeUseAnotherServerDescription.visibility = View.GONE
|
|
||||||
subscribeBaseUrlLayout.visibility = View.GONE
|
|
||||||
subscribeInstantDeliveryBox.visibility = instantCheckboxVisible()
|
|
||||||
subscribeInstantDeliveryDescription.visibility = if (subscribeInstantDeliveryBox.visibility == View.VISIBLE && subscribeInstantDeliveryCheckbox.isChecked) View.VISIBLE else View.GONE
|
|
||||||
}
|
|
||||||
validateInputSubscribeView()
|
validateInputSubscribeView()
|
||||||
}
|
}
|
||||||
subscribeInstantDeliveryBox.visibility = instantCheckboxVisible()
|
subscribeUseAnotherServerCheckbox.setOnCheckedChangeListener { _, _ ->
|
||||||
subscribeForegroundDescription.visibility = if (!BuildConfig.FIREBASE_AVAILABLE || subscribeInstantDeliveryBox.visibility == View.VISIBLE) View.GONE else View.VISIBLE
|
validateInputSubscribeView()
|
||||||
|
}
|
||||||
|
validateInputSubscribeView()
|
||||||
|
|
||||||
// Focus topic text (keyboard is shown too, see above)
|
// Focus topic text (keyboard is shown too, see above)
|
||||||
subscribeTopicText.requestFocus()
|
subscribeTopicText.requestFocus()
|
||||||
|
@ -216,17 +190,6 @@ class AddFragment : DialogFragment() {
|
||||||
return dialog
|
return dialog
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun instantCheckboxVisible(): Int {
|
|
||||||
if (!BuildConfig.FIREBASE_AVAILABLE) {
|
|
||||||
return View.GONE
|
|
||||||
} else if (subscribeUseAnotherServerCheckbox.isChecked && subscribeBaseUrlText.text.toString() == appBaseUrl) {
|
|
||||||
return View.VISIBLE
|
|
||||||
} else if (!subscribeUseAnotherServerCheckbox.isChecked && defaultBaseUrl == null) {
|
|
||||||
return View.VISIBLE
|
|
||||||
}
|
|
||||||
return View.GONE
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun positiveButtonClick() {
|
private fun positiveButtonClick() {
|
||||||
val topic = subscribeTopicText.text.toString()
|
val topic = subscribeTopicText.text.toString()
|
||||||
val baseUrl = getBaseUrl()
|
val baseUrl = getBaseUrl()
|
||||||
|
@ -329,6 +292,35 @@ class AddFragment : DialogFragment() {
|
||||||
|
|
||||||
private fun validateInputSubscribeView() {
|
private fun validateInputSubscribeView() {
|
||||||
if (!this::positiveButton.isInitialized) return // As per crash seen in Google Play
|
if (!this::positiveButton.isInitialized) return // As per crash seen in Google Play
|
||||||
|
|
||||||
|
// Show/hide things: This logic is intentionally kept simple. Do not simplify "just because it's pretty".
|
||||||
|
val instantToggleAllowed = if (!BuildConfig.FIREBASE_AVAILABLE) {
|
||||||
|
false
|
||||||
|
} else if (subscribeUseAnotherServerCheckbox.isChecked && subscribeBaseUrlText.text.toString() == appBaseUrl) {
|
||||||
|
true
|
||||||
|
} else if (!subscribeUseAnotherServerCheckbox.isChecked && defaultBaseUrl == null) {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
if (subscribeUseAnotherServerCheckbox.isChecked) {
|
||||||
|
subscribeUseAnotherServerDescription.visibility = View.VISIBLE
|
||||||
|
subscribeBaseUrlLayout.visibility = View.VISIBLE
|
||||||
|
} else {
|
||||||
|
subscribeUseAnotherServerDescription.visibility = View.GONE
|
||||||
|
subscribeBaseUrlLayout.visibility = View.GONE
|
||||||
|
}
|
||||||
|
if (instantToggleAllowed) {
|
||||||
|
subscribeInstantDeliveryBox.visibility = View.VISIBLE
|
||||||
|
subscribeInstantDeliveryDescription.visibility = if (subscribeInstantDeliveryCheckbox.isChecked) View.VISIBLE else View.GONE
|
||||||
|
subscribeForegroundDescription.visibility = View.GONE
|
||||||
|
} else {
|
||||||
|
subscribeInstantDeliveryBox.visibility = View.GONE
|
||||||
|
subscribeInstantDeliveryDescription.visibility = View.GONE
|
||||||
|
subscribeForegroundDescription.visibility = if (BuildConfig.FIREBASE_AVAILABLE) View.VISIBLE else View.GONE
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable/disable "Subscribe" button
|
||||||
lifecycleScope.launch(Dispatchers.IO) {
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
val baseUrl = getBaseUrl()
|
val baseUrl = getBaseUrl()
|
||||||
val topic = subscribeTopicText.text.toString()
|
val topic = subscribeTopicText.text.toString()
|
||||||
|
@ -343,9 +335,6 @@ class AddFragment : DialogFragment() {
|
||||||
} else {
|
} else {
|
||||||
positiveButton.isEnabled = validTopic(topic)
|
positiveButton.isEnabled = validTopic(topic)
|
||||||
}
|
}
|
||||||
subscribeInstantDeliveryBox.visibility = instantCheckboxVisible()
|
|
||||||
subscribeInstantDeliveryDescription.visibility = if (subscribeInstantDeliveryBox.visibility == View.VISIBLE && subscribeInstantDeliveryCheckbox.isChecked) View.VISIBLE else View.GONE
|
|
||||||
subscribeForegroundDescription.visibility = if (!BuildConfig.FIREBASE_AVAILABLE || subscribeInstantDeliveryBox.visibility == View.VISIBLE) View.GONE else View.VISIBLE
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@ import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
import android.provider.OpenableColumns
|
import android.provider.OpenableColumns
|
||||||
|
import android.text.Editable
|
||||||
|
import android.text.TextWatcher
|
||||||
import android.util.TypedValue
|
import android.util.TypedValue
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.Window
|
import android.view.Window
|
||||||
|
@ -297,3 +299,17 @@ fun View.makeEndIconSmaller(resources: Resources) {
|
||||||
endIconImageView.minimumWidth = dimension.toInt()
|
endIconImageView.minimumWidth = dimension.toInt()
|
||||||
requestLayout()
|
requestLayout()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TextWatcher that only implements the afterTextChanged method
|
||||||
|
class AfterChangedTextWatcher(val afterTextChangedFn: (s: Editable?) -> Unit) : TextWatcher {
|
||||||
|
override fun afterTextChanged(s: Editable?) {
|
||||||
|
afterTextChangedFn(s)
|
||||||
|
}
|
||||||
|
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
||||||
|
// Nothing
|
||||||
|
}
|
||||||
|
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
||||||
|
// Nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue