From 7da841d2de9ac0526c3863e85d7d603facabb95c Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Thu, 20 Jan 2022 20:43:14 -0600 Subject: Implemented feature to switch to different Traccar servers by URL and removed annoying auto-generated redundant qualifiers --- .../TrackerMap/android/session/LoginFragment.kt | 21 ++++++++++++++++++++- .../TrackerMap/android/session/LoginViewModel.kt | 4 ++-- .../android/session/UserInformationActivity.kt | 5 +++++ 3 files changed, 27 insertions(+), 3 deletions(-) (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap/android/session') diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginFragment.kt index f1b23a6..c87e134 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginFragment.kt @@ -7,7 +7,6 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast -import androidx.core.widget.doAfterTextChanged import androidx.fragment.app.Fragment import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.preference.PreferenceManager @@ -43,6 +42,14 @@ class LoginFragment : Fragment() { setupEvents() setupObservers() broadcastManager = LocalBroadcastManager.getInstance(activity!!) + binding.urlEditText.setText( + PreferenceManager + .getDefaultSharedPreferences(activity) + .getString( + PREFERENCE_SERVER_URL, + getString(R.string.default_server_url) + ) ?: getString(R.string.default_server_url) + ) loginViewModel.restoreSession() } @@ -67,6 +74,7 @@ class LoginFragment : Fragment() { loginViewModel.login( binding.usernameEditText.text.toString(), binding.passwordEditText.text.toString(), + binding.urlEditText.text.toString(), PreferenceManager .getDefaultSharedPreferences(activity) .getString(PREFERENCE_TOKEN, null) @@ -84,6 +92,10 @@ class LoginFragment : Fragment() { SessionController.LoginState.Loading -> { binding.infoLoading.root.visibility = View.VISIBLE } + SessionController.LoginState.UrlMissing -> { + binding.infoLoading.root.visibility = View.GONE + Toast.makeText(context, getString(R.string.login_url_missing), Toast.LENGTH_SHORT).show() + } SessionController.LoginState.EmailMissing -> { binding.infoLoading.root.visibility = View.GONE Toast.makeText(context, getString(R.string.login_username_missing), Toast.LENGTH_SHORT).show() @@ -97,6 +109,12 @@ class LoginFragment : Fragment() { Toast.makeText(context, getString(R.string.login_login_failed), Toast.LENGTH_SHORT).show() } SessionController.LoginState.Success -> { + PreferenceManager + .getDefaultSharedPreferences(activity) + .edit() + .putString(PREFERENCE_SERVER_URL, binding.urlEditText.text.toString()) + .apply() + broadcastManager.sendBroadcast(Intent(EVENT_LOGIN)) val activity = requireActivity() val intent = Intent(activity.applicationContext, UnitsActivity::class.java) @@ -126,6 +144,7 @@ class LoginFragment : Fragment() { const val EVENT_TOKEN = "eventToken" const val KEY_TOKEN = "keyToken" + const val PREFERENCE_SERVER_URL = "server_url" const val PREFERENCE_TOKEN = "token" } } \ No newline at end of file diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginViewModel.kt index 88b56cf..b5516e7 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginViewModel.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/LoginViewModel.kt @@ -40,7 +40,7 @@ class LoginViewModel : ViewModel(), KoinComponent { sessionController.restoreSession() } - fun login(email: String, password: String, token: String?) { - sessionController.login(SessionBody(email, password, token)) + fun login(email: String, password: String, url: String, token: String?) { + sessionController.login(SessionBody(url, email, password, token)) } } \ No newline at end of file diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationActivity.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationActivity.kt index 5fad118..81577c7 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationActivity.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/session/UserInformationActivity.kt @@ -65,6 +65,11 @@ class UserInformationActivity : AppCompatActivity() { emailInfo.text = user.email ?: "" idInfo.text = "${user.id ?: "--"}" adminInfo.text = "${user.administrator}" + serverInfo.text = "${ + PreferenceManager + .getDefaultSharedPreferences(this@UserInformationActivity) + .getString(LoginFragment.PREFERENCE_SERVER_URL, getString(R.string.default_server_url)) + }" user.deviceLimit?.let { deviceLimitInfo.text = "${user.deviceLimit ?: "--"}" if (it > 0) { -- cgit v1.2.3