Add warning dialog if entering an email as username

This commit is contained in:
Bnyro 2022-12-01 15:28:41 +01:00
parent 53ebd3363f
commit ab3ec2d442
3 changed files with 29 additions and 11 deletions

View File

@ -12,10 +12,9 @@ import com.github.libretube.api.obj.Login
import com.github.libretube.databinding.DialogLoginBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.TextUtils
import com.github.libretube.util.ThemeHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import retrofit2.HttpException
import java.io.IOException
class LoginDialog : DialogFragment() {
private lateinit var binding: DialogLoginBinding
@ -34,7 +33,20 @@ class LoginDialog : DialogFragment() {
}
}
binding.register.setOnClickListener {
if (isInsertionValid()) {
if (isEmail(binding.username.text.toString())) {
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.privacy_alert)
.setMessage(R.string.username_email)
.setNegativeButton(R.string.proceed) { _, _ ->
signIn(
binding.username.text.toString(),
binding.password.text.toString(),
true
)
}
.setPositiveButton(R.string.cancel, null)
.show()
} else if (isInsertionValid()) {
signIn(
binding.username.text.toString(),
binding.password.text.toString(),
@ -65,15 +77,9 @@ class LoginDialog : DialogFragment() {
} else {
RetrofitInstance.authApi.login(login)
}
} catch (e: IOException) {
Log.e(TAG(), "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} catch (e: Exception) {
Log.e(TAG(), e.toString())
Toast.makeText(context, e.localizedMessage, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
}
@ -96,4 +102,8 @@ class LoginDialog : DialogFragment() {
activity?.recreate()
}
}
private fun isEmail(text: String): Boolean {
return TextUtils.EMAIL_REGEX.toRegex().matches(text)
}
}

View File

@ -6,6 +6,11 @@ object TextUtils {
*/
const val SEPARATOR = ""
/**
* Regex to check for e-mails
*/
const val EMAIL_REGEX = "^[A-Za-z](.*)([@]{1})(.{1,})(\\.)(.{1,})"
fun toTwoDecimalsString(num: Int): String {
return if (num >= 10) num.toString() else "0$num"
}

View File

@ -408,6 +408,9 @@
<string name="app_backup">App Backup</string>
<string name="backup_restore_summary">Import &amp; export subscriptions, playlists, …</string>
<string name="exportsuccess">Successfully exported!</string>
<string name="privacy_alert">Privacy alert</string>
<string name="username_email">You inserted an e-mail address as username which is not recommended. Are you sure you want to proceed?</string>
<string name="proceed">Proceed</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>