From e093153eee369a3d16ea84a09ef86b5330502629 Mon Sep 17 00:00:00 2001 From: Jannik Date: Sun, 11 Oct 2020 14:44:38 +0200 Subject: [PATCH] add login dialog * show dialog if no credentials are present --- app/build.gradle | 2 + .../java/org/mosad/teapod/MainActivity.kt | 9 +- .../mosad/teapod/ui/components/LoginDialog.kt | 95 +++++++++++++++++++ app/src/main/res/layout/dialog_login.xml | 30 ++++++ app/src/main/res/values/strings.xml | 9 ++ 5 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/mosad/teapod/ui/components/LoginDialog.kt create mode 100644 app/src/main/res/layout/dialog_login.xml diff --git a/app/build.gradle b/app/build.gradle index dff453a..f71d9e4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -54,6 +54,8 @@ dependencies { implementation 'org.jsoup:jsoup:1.13.1' implementation 'com.github.bumptech.glide:glide:4.11.0' + implementation 'com.afollestad.material-dialogs:core:3.3.0' + implementation 'com.afollestad.material-dialogs:bottomsheets:3.3.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.2' diff --git a/app/src/main/java/org/mosad/teapod/MainActivity.kt b/app/src/main/java/org/mosad/teapod/MainActivity.kt index 0be7892..25df3db 100644 --- a/app/src/main/java/org/mosad/teapod/MainActivity.kt +++ b/app/src/main/java/org/mosad/teapod/MainActivity.kt @@ -13,6 +13,7 @@ import org.mosad.teapod.parser.AoDParser import org.mosad.teapod.preferences.EncryptedPreferences import org.mosad.teapod.ui.MediaFragment import org.mosad.teapod.ui.account.AccountFragment +import org.mosad.teapod.ui.components.LoginDialog import org.mosad.teapod.ui.home.HomeFragment import org.mosad.teapod.ui.library.LibraryFragment import org.mosad.teapod.ui.search.SearchFragment @@ -77,8 +78,12 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS if (EncryptedPreferences.password.isEmpty()) { Log.i(javaClass.name, "please login!") - // TODO show login dialog - //EncryptedPreferences.saveCredentials("", "", this) + LoginDialog(this).positiveButton { + EncryptedPreferences.saveCredentials(email, password, context) + }.negativeButton { + Log.i(javaClass.name, "Login canceled, exiting.") + finish() + }.show() } } diff --git a/app/src/main/java/org/mosad/teapod/ui/components/LoginDialog.kt b/app/src/main/java/org/mosad/teapod/ui/components/LoginDialog.kt new file mode 100644 index 0000000..4466c0a --- /dev/null +++ b/app/src/main/java/org/mosad/teapod/ui/components/LoginDialog.kt @@ -0,0 +1,95 @@ +/** + * ProjectLaogai + * + * Copyright 2019-2020 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +package org.mosad.teapod.ui.components + +import android.content.Context +import android.widget.EditText +import com.afollestad.materialdialogs.MaterialDialog +import com.afollestad.materialdialogs.WhichButton +import com.afollestad.materialdialogs.actions.getActionButton +import com.afollestad.materialdialogs.bottomsheets.BottomSheet +import com.afollestad.materialdialogs.bottomsheets.setPeekHeight +import com.afollestad.materialdialogs.customview.customView +import com.afollestad.materialdialogs.customview.getCustomView +import org.mosad.teapod.R + +class LoginDialog(val context: Context) { + + private val dialog = MaterialDialog(context, BottomSheet()) + + private val editTextLogin: EditText + private val editTextPassword: EditText + + var email = "" + var password = "" + + init { + dialog.title(R.string.login) + .message(R.string.login_desc) + .customView(R.layout.dialog_login) + .positiveButton(R.string.save) + .negativeButton(R.string.cancel) + .setPeekHeight(900) + + editTextLogin = dialog.getCustomView().findViewById(R.id.edit_text_login) + editTextPassword = dialog.getCustomView().findViewById(R.id.edit_text_password) + + // fix not working accent color + //dialog.getActionButton(WhichButton.POSITIVE).updateTextColor(Preferences.colorAccent) + //dialog.getActionButton(WhichButton.NEGATIVE).updateTextColor(Preferences.colorAccent) + } + + fun positiveButton(func: LoginDialog.() -> Unit): LoginDialog = apply { + dialog.positiveButton { + email = editTextLogin.text.toString() + password = editTextPassword.text.toString() + + func() + } + } + + fun negativeButton(func: LoginDialog.() -> Unit): LoginDialog = apply { + dialog.negativeButton { + func() + } + } + + fun show() { + dialog.show() + } + + fun show(func: LoginDialog.() -> Unit): LoginDialog = apply { + func() + + editTextLogin.setText(email) + editTextPassword.setText(password) + + show() + } + + @Suppress("unused") + fun dismiss() { + dialog.dismiss() + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_login.xml b/app/src/main/res/layout/dialog_login.xml new file mode 100644 index 0000000..17468d7 --- /dev/null +++ b/app/src/main/res/layout/dialog_login.xml @@ -0,0 +1,30 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e5789b3..2da7b4b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,6 +5,15 @@ Search Account + + save + @android:string/cancel + + + Login + You need to login before you can use Teapod. Your Login-Data will be stored encrypted on your device. + Password + org.mosad.teapod.encrypted_preferences org.mosad.teapod.user_login