From df5febabb70d5f050263181f74aa4d1f34f74774 Mon Sep 17 00:00:00 2001 From: Henrik Lepson Date: Wed, 24 Nov 2021 18:20:22 +0200 Subject: [PATCH 01/11] deleted unused code --- MobileAuthApp/app/build.gradle | 4 --- .../mobileauthapp/network/TokenApiService.kt | 34 ------------------- .../mobileauthapp/network/TokenItem.kt | 9 ----- 3 files changed, 47 deletions(-) delete mode 100644 MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenApiService.kt delete mode 100644 MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenItem.kt diff --git a/MobileAuthApp/app/build.gradle b/MobileAuthApp/app/build.gradle index bc4a034..38896fb 100644 --- a/MobileAuthApp/app/build.gradle +++ b/MobileAuthApp/app/build.gradle @@ -68,8 +68,4 @@ dependencies { 'io.jsonwebtoken:jjwt-gson:0.11.2' implementation 'com.koushikdutta.ion:ion:3.1.0' - - // Retrofit + Moshi Converter - implementation 'com.squareup.retrofit2:converter-moshi:2.9.0' - implementation 'com.squareup.moshi:moshi-kotlin:1.9.3' } \ No newline at end of file diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenApiService.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenApiService.kt deleted file mode 100644 index 67b952b..0000000 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenApiService.kt +++ /dev/null @@ -1,34 +0,0 @@ -package com.tarkvaraprojekt.mobileauthapp.network - -import com.squareup.moshi.Moshi -import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory -import retrofit2.Response -import retrofit2.Retrofit -import retrofit2.converter.moshi.MoshiConverterFactory -import retrofit2.http.Body -import retrofit2.http.GET -import retrofit2.http.Headers -import retrofit2.http.POST - -/** - * Class for making HTTP requests - * Based on https://developer.android.com/courses/pathways/android-basics-kotlin-unit-4-pathway-2 - */ -const val BASE_URL = - "https://6bb0-85-253-195-252.ngrok.io" - -private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build() -private val retrofit = Retrofit.Builder().addConverterFactory(MoshiConverterFactory.create(moshi)) - .baseUrl(BASE_URL).build() - -interface TokenApiService { - @Headers("Content-Type: application/json") - @POST("/auth/authentication") - suspend fun postToken(@Body data: String): Response -} - -object TokenApi { - val retrofitService : TokenApiService by lazy { - retrofit.create(TokenApiService::class.java) - } -} \ No newline at end of file diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenItem.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenItem.kt deleted file mode 100644 index b7bb0c1..0000000 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenItem.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.tarkvaraprojekt.mobileauthapp.network - -/** - * TokenItem for making POST request. - */ -data class TokenItem ( - val token: String, - val challenge: String, -) \ No newline at end of file From edc444c0272a8e4dadf6fea4805f09fb18156e12 Mon Sep 17 00:00:00 2001 From: Henrik Lepson Date: Wed, 24 Nov 2021 18:24:21 +0200 Subject: [PATCH 02/11] MOB-40 added xml res files for styles and dimensions --- MobileAuthApp/app/src/main/res/values/dimens.xml | 9 +++++++++ MobileAuthApp/app/src/main/res/values/styles.xml | 7 +++++++ 2 files changed, 16 insertions(+) create mode 100644 MobileAuthApp/app/src/main/res/values/dimens.xml create mode 100644 MobileAuthApp/app/src/main/res/values/styles.xml diff --git a/MobileAuthApp/app/src/main/res/values/dimens.xml b/MobileAuthApp/app/src/main/res/values/dimens.xml new file mode 100644 index 0000000..c240995 --- /dev/null +++ b/MobileAuthApp/app/src/main/res/values/dimens.xml @@ -0,0 +1,9 @@ + + + 8dp + 16dp + 24dp + 24sp + 32sp + 16sp + \ No newline at end of file diff --git a/MobileAuthApp/app/src/main/res/values/styles.xml b/MobileAuthApp/app/src/main/res/values/styles.xml new file mode 100644 index 0000000..ab85623 --- /dev/null +++ b/MobileAuthApp/app/src/main/res/values/styles.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file From f085076631e318bf91fa455c2316621c15728b55 Mon Sep 17 00:00:00 2001 From: Henrik Lepson Date: Wed, 24 Nov 2021 20:44:40 +0200 Subject: [PATCH 03/11] MOB-40 improved CAN and PIN views --- .../mobileauthapp/CanFragment.kt | 76 +++----- .../mobileauthapp/HomeFragment.kt | 2 + .../mobileauthapp/MainActivity.kt | 1 + .../mobileauthapp/PinFragment.kt | 123 ++++++------- .../app/src/main/res/layout/fragment_can.xml | 103 +++++------ .../app/src/main/res/layout/fragment_pin.xml | 163 +++++++++++------- .../app/src/main/res/values-en/strings.xml | 16 ++ .../app/src/main/res/values-et/strings.xml | 16 ++ .../app/src/main/res/values/colors.xml | 8 +- .../app/src/main/res/values/strings.xml | 16 ++ 10 files changed, 272 insertions(+), 252 deletions(-) diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt index 1b281aa..e7a478f 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt @@ -8,6 +8,7 @@ import android.view.View import android.view.ViewGroup import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.core.widget.addTextChangedListener import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.navigation.fragment.findNavController @@ -44,13 +45,10 @@ class CanFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) checkIfSkip() - // If the user arrives from the settings menu then the button should say - // save instead of continue. - if (args.saving) { - binding!!.nextButton.text = getString(R.string.save_text) + binding!!.canTextField.editText?.addTextChangedListener { + checkEnteredCan() } - binding!!.nextButton.setOnClickListener { checkEnteredCan() } - binding!!.cancelButton.setOnClickListener { goToTheStart() } + binding!!.buttonCancel.setOnClickListener { goToTheStart() } } /** @@ -71,60 +69,12 @@ class CanFragment : Fragment() { findNavController().navigate(action) } - /** - * Checks whether the user has entered a 6 digit can to the input field. - * If yes then the user is allowed to continue otherwise the user is - * allowed to modify the entered can. - */ - private fun checkEnteredCan() { - val enteredCan = binding!!.canEditText.editText?.text.toString() - if (enteredCan.length == 6) { - viewModel.setUserCan(enteredCan) - if (args.saving) { - viewModel.storeCan(requireContext()) - goToTheStart() - } else { - val storeCanQuestion = getDialog() - storeCanQuestion?.show() - } - } else { - Toast.makeText(requireContext(), getString(R.string.length_can), Toast.LENGTH_SHORT) - .show() - } - } - - /** - * Builds a dialog that asks the user whether the entered CAN should be saved - * on the device or not. - */ - private fun getDialog(): AlertDialog? { - return activity?.let { frag -> - val builder = AlertDialog.Builder(frag) - builder.apply { - // If response is positive then save the CAN on the device. - setPositiveButton(R.string.save_text) { _, _ -> - viewModel.storeCan( - requireContext() - ) - goToTheNextFragment() - } - setNegativeButton(R.string.deny_text) { _, _ -> - goToTheNextFragment() - } - } - builder.setMessage(R.string.can_save_request) - builder.setTitle(R.string.save_can_title) - builder.create() - } - } - /** * Navigates the user back to the start depending on where the user arrived. * If the user arrived from the settings menu then the start is the settings menu * not the HomeFragment. */ private fun goToTheStart() { - // TODO: Needs special handling when the app is launched with intent. Temporary solution at the moment. if (args.saving) { findNavController().navigate(R.id.action_canFragment_to_settingsFragment) } else if (args.auth || args.mobile) { @@ -140,6 +90,24 @@ class CanFragment : Fragment() { } } + /** + * Checks whether the user has entered a 6 digit can to the input field. + * If yes then the user is allowed to continue otherwise the user is + * allowed to modify the entered can. + */ + private fun checkEnteredCan() { + val enteredCan = binding!!.canTextField.editText?.text.toString() + if (enteredCan.length == 6) { + viewModel.setUserCan(enteredCan) + viewModel.storeCan(requireContext()) //Maybe storeCan should always automatically call setUserCan method as well because these methods usually are used together + if (args.saving) { + goToTheStart() + } else { + goToTheNextFragment() + } + } + } + override fun onDestroy() { super.onDestroy() binding = null diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt index e1888f4..4819fb2 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt @@ -53,6 +53,7 @@ class HomeFragment : Fragment() { } val mobile = requireActivity().intent.getBooleanExtra("mobile", false) if (auth || mobile){ + try { if (mobile) { // We use !! because we want an exception when something is not right. @@ -73,6 +74,7 @@ class HomeFragment : Fragment() { requireActivity().setResult(AppCompatActivity.RESULT_CANCELED, resultIntent) requireActivity().finish() } + goToTheNextFragment(true, mobile) } binding!!.beginButton.setOnClickListener { goToTheNextFragment() } diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/MainActivity.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/MainActivity.kt index 638d88e..f205c38 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/MainActivity.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/MainActivity.kt @@ -40,6 +40,7 @@ class MainActivity : AppCompatActivity() { R.id.menu_settings_option -> { if (menuAvailable) { navigationController.navigate(R.id.action_homeFragment_to_settingsFragment) + menuAvailable = false true } else { Toast.makeText(this, getString(R.string.unavailable), Toast.LENGTH_SHORT).show() diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/PinFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/PinFragment.kt index 86dbbc1..a0df1f3 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/PinFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/PinFragment.kt @@ -32,6 +32,9 @@ class PinFragment : Fragment() { // not require PIN 1 so it is not necessary to ask it. private val args: PinFragmentArgs by navArgs() + // TODO: Should be persistent and read when launching the app + private var saveToggle = true + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -44,26 +47,22 @@ class PinFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) checkIfSkip() - // If the user arrives from the settings menu then the button says - // save instead of continue. + // Switch should be not visible when user is in savings mode if (args.saving) { - binding!!.nextButton.text = getString(R.string.save_text) - } - binding!!.nextButton.setOnClickListener { checkEnteredPin() } - binding!!.cancelButton.setOnClickListener { goToTheStart() } - } - - /** - * Checks if the current fragment can be skipped or not. - * If the user has PIN 1 saved on the device or PIN 1 is not required - * then the PIN 1 won't be asked. - */ - private fun checkIfSkip() { - if (args.reading) { - goToTheNextFragment() - } else if (viewModel.userPin.length in 4..12) { - goToTheNextFragment() + binding!!.savePinQuestion.visibility = View.GONE + binding!!.saveLayout.visibility = View.GONE + } else { + binding!!.saveSwitch.setOnCheckedChangeListener { _, isChecked -> + if (isChecked) { + binding!!.saveStatus.text = getString(R.string.pin_save_on) + } else { + binding!!.saveStatus.text = getString(R.string.pin_save_off) + } + saveToggle = !saveToggle + } } + binding!!.buttonContinue.setOnClickListener { checkEnteredPin() } + binding!!.buttonCancel.setOnClickListener { goToTheStart() } } /** @@ -74,60 +73,13 @@ class PinFragment : Fragment() { findNavController().navigate(action) } - /** - * Checks whether the user has entered a PIN 1 with length between [4, 12] in the - * input field. If yes then the user is allowed to continue otherwise the user is - * allowed to modify the entered PIN 1. - */ - private fun checkEnteredPin() { - val enteredPin = binding!!.pinEditText.editText?.text.toString() - if (enteredPin.length in 4..12) { - viewModel.setUserPin(enteredPin) - if (args.saving) { - viewModel.storePin(requireContext()) - goToTheStart() - } else { - val storePinQuestion = getDialog() - storePinQuestion?.show() - } - } else { - Toast.makeText(requireContext(), getString(R.string.length_pin), Toast.LENGTH_SHORT) - .show() - } - } - - /** - * Builds a dialog that asks the user whether the entered PIN 1 should be saved - * on the device or not. - */ - private fun getDialog(): AlertDialog? { - return activity?.let { frag -> - val builder = AlertDialog.Builder(frag) - builder.apply { - // If response is positive save the PIN 1 on the device. - setPositiveButton(R.string.save_text) { _, _ -> - viewModel.storePin( - requireContext() - ) - goToTheNextFragment() - } - setNegativeButton(R.string.deny_text) { _, _ -> - goToTheNextFragment() - } - } - builder.setMessage(R.string.pin_save_request) - builder.setTitle(R.string.save_pin_title) - builder.create() - } - } - /** * Returns user to the start. If the user arrived from the settings menu then the start is * settings menu not the HomeFragment. */ private fun goToTheStart() { if (args.saving) { - findNavController().navigate(R.id.action_canFragment_to_settingsFragment) + findNavController().navigate(R.id.action_pinFragment_to_settingsFragment) } else if (args.auth || args.mobile) { if (args.mobile) { val resultIntent = Intent() @@ -141,6 +93,45 @@ class PinFragment : Fragment() { } } + /** + * Checks if the current fragment can be skipped or not. + * If the user has PIN 1 saved on the device or PIN 1 is not required + * then the PIN 1 won't be asked. + * + * NOTE: maybe args.reading can be removed after changing the nav_graph + */ + private fun checkIfSkip() { + if (args.reading) { + goToTheNextFragment() + } else if (viewModel.userPin.length in 4..12) { + goToTheNextFragment() + } + } + + /** + * Checks whether the user has entered a PIN 1 with length between [4, 12] in the + * input field. If yes then the user is allowed to continue otherwise the user is + * allowed to modify the entered PIN 1. + */ + private fun checkEnteredPin() { + val enteredPin = binding!!.pinTextField.editText?.text.toString() + if (enteredPin.length in 4..12) { + viewModel.setUserPin(enteredPin) + if (args.saving) { + viewModel.storePin(requireContext()) + goToTheStart() + } else { + if (saveToggle) { + viewModel.storePin(requireContext()) + } + goToTheNextFragment() + } + } else { + Toast.makeText(requireContext(), getString(R.string.length_pin), Toast.LENGTH_SHORT) + .show() + } + } + override fun onDestroy() { super.onDestroy() binding = null diff --git a/MobileAuthApp/app/src/main/res/layout/fragment_can.xml b/MobileAuthApp/app/src/main/res/layout/fragment_can.xml index f7bcd1e..1fe0ebd 100644 --- a/MobileAuthApp/app/src/main/res/layout/fragment_can.xml +++ b/MobileAuthApp/app/src/main/res/layout/fragment_can.xml @@ -4,82 +4,57 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:padding="24dp" - tools:context=".CanFragment"> + android:padding="@dimen/padding" + tools:context=".MainActivity"> - + app:layout_constraintTop_toTopOf="parent"/> - + + + android:textSize="@dimen/regular_text" + android:fontFamily="sans-serif" + android:inputType="number" + android:singleLine="true" + /> - - - - - - - - - - - +