From bea0c1499dd0497f654f25d2c1fdd64a816b4ca0 Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Thu, 19 Oct 2023 19:41:24 +0200 Subject: [PATCH] Add NoGroupsFragment, Navigate to main fragment if current group deleted --- .../code_guard/MainActivity.java | 7 ++++- .../code_guard/fragment/GroupFragment.java | 4 +++ .../fragment/MenuDrawerFragment.java | 18 +++++++++++++ .../code_guard/fragment/NoGroupsFragment.java | 27 +++++++++++++++++++ .../main/res/layout/fragment_no_groups.xml | 22 +++++++++++++++ app/src/main/res/values-de/strings.xml | 2 ++ app/src/main/res/values/strings.xml | 2 ++ 7 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/cringe_studios/code_guard/fragment/NoGroupsFragment.java create mode 100644 app/src/main/res/layout/fragment_no_groups.xml diff --git a/app/src/main/java/com/cringe_studios/code_guard/MainActivity.java b/app/src/main/java/com/cringe_studios/code_guard/MainActivity.java index d456f36..9db9663 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/MainActivity.java +++ b/app/src/main/java/com/cringe_studios/code_guard/MainActivity.java @@ -23,6 +23,7 @@ import com.cringe_studios.code_guard.fragment.AboutFragment; import com.cringe_studios.code_guard.fragment.EditOTPFragment; import com.cringe_studios.code_guard.fragment.GroupFragment; import com.cringe_studios.code_guard.fragment.NamedFragment; +import com.cringe_studios.code_guard.fragment.NoGroupsFragment; import com.cringe_studios.code_guard.fragment.SettingsFragment; import com.cringe_studios.code_guard.icon.IconPack; import com.cringe_studios.code_guard.icon.IconPackException; @@ -260,12 +261,16 @@ public class MainActivity extends BaseActivity { } } - private void navigateToMainGroup() { + public void navigateToMainGroup() { List groups = SettingsUtil.getGroups(this); if(!groups.isEmpty()) { Bundle bundle = new Bundle(); bundle.putString(GroupFragment.BUNDLE_GROUP, SettingsUtil.getGroups(this).get(0)); NavigationUtil.navigate(this, GroupFragment.class, bundle); + }else { + ActionBar bar = getSupportActionBar(); + if(bar != null) bar.setTitle(R.string.app_name); + NavigationUtil.navigate(this, NoGroupsFragment.class, null); } } diff --git a/app/src/main/java/com/cringe_studios/code_guard/fragment/GroupFragment.java b/app/src/main/java/com/cringe_studios/code_guard/fragment/GroupFragment.java index b4026c9..5bb35fb 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/fragment/GroupFragment.java +++ b/app/src/main/java/com/cringe_studios/code_guard/fragment/GroupFragment.java @@ -71,6 +71,10 @@ public class GroupFragment extends NamedFragment { return binding.getRoot(); } + public String getGroupID() { + return groupID; + } + private void saveOTPs() { OTPDatabase.promptLoadDatabase(requireActivity(), () -> { try { diff --git a/app/src/main/java/com/cringe_studios/code_guard/fragment/MenuDrawerFragment.java b/app/src/main/java/com/cringe_studios/code_guard/fragment/MenuDrawerFragment.java index 388973d..b69d2b9 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/fragment/MenuDrawerFragment.java +++ b/app/src/main/java/com/cringe_studios/code_guard/fragment/MenuDrawerFragment.java @@ -7,7 +7,9 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import com.cringe_studios.code_guard.MainActivity; import com.cringe_studios.code_guard.R; import com.cringe_studios.code_guard.crypto.CryptoException; import com.cringe_studios.code_guard.databinding.FragmentMenuDrawerBinding; @@ -70,6 +72,13 @@ public class MenuDrawerFragment extends BottomSheetDialogFragment { String id = UUID.randomUUID().toString(); SettingsUtil.addGroup(requireContext(), id, groupName); groupListAdapter.add(id); + + Fragment fragment = NavigationUtil.getCurrentFragment(getParentFragmentManager()); + if(fragment instanceof NoGroupsFragment) { + Bundle bundle = new Bundle(); + bundle.putString(GroupFragment.BUNDLE_GROUP, id); + NavigationUtil.navigate(this, GroupFragment.class, bundle); + } }, null); } @@ -95,6 +104,15 @@ public class MenuDrawerFragment extends BottomSheetDialogFragment { removeGroup(item.getGroupId()); } + Fragment fragment = NavigationUtil.getCurrentFragment(getParentFragmentManager()); + if(fragment instanceof GroupFragment) { + GroupFragment groupFragment = (GroupFragment) fragment; + if(!SettingsUtil.getGroups(requireContext()).contains(groupFragment.getGroupID())) { + // Currently selected group was deleted, navigate to main group + ((MainActivity) requireActivity()).navigateToMainGroup(); + } + } + groupListAdapter.finishEditing(); }, null); } diff --git a/app/src/main/java/com/cringe_studios/code_guard/fragment/NoGroupsFragment.java b/app/src/main/java/com/cringe_studios/code_guard/fragment/NoGroupsFragment.java new file mode 100644 index 0000000..46f91f9 --- /dev/null +++ b/app/src/main/java/com/cringe_studios/code_guard/fragment/NoGroupsFragment.java @@ -0,0 +1,27 @@ +package com.cringe_studios.code_guard.fragment; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.cringe_studios.code_guard.R; +import com.cringe_studios.code_guard.databinding.FragmentNoGroupsBinding; + +public class NoGroupsFragment extends NamedFragment { + + @Override + public String getName() { + return requireActivity().getString(R.string.fragment_no_groups); + } + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + FragmentNoGroupsBinding binding = FragmentNoGroupsBinding.inflate(inflater); + return binding.getRoot(); + } +} diff --git a/app/src/main/res/layout/fragment_no_groups.xml b/app/src/main/res/layout/fragment_no_groups.xml new file mode 100644 index 0000000..d84b304 --- /dev/null +++ b/app/src/main/res/layout/fragment_no_groups.xml @@ -0,0 +1,22 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index d2ad142..94d2f11 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -82,6 +82,7 @@ Einstellungen Über OTP bearbeiten + Keine Gruppen Blau/Grün Rot/Blau Pink/Grün @@ -167,4 +168,5 @@ Doppeltes OTP Biometrische Authentifizierung ist deaktiviert, da sie auf deinem Gerät nicht eingerichtet oder nicht verfügbar ist Systemstandard + Es existieren momentan keine Gruppen. Öffne das Menü und drücke auf das \'+\'-Symbol, um eine Gruppe zu erstellen \ 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 f1adc34..808f206 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -66,6 +66,7 @@ Settings About Edit OTP + No Groups View Edit @@ -173,6 +174,7 @@ An OTP with the name of the OTP you\'re trying to add already exists.\n\nDo you want to automatically rename the new OTP to distinguish them from each other? Duplicate OTP Biometric authentication is disabled because it is not set up or not available on your device + No groups exist yet. Open the menu and press the \'+\' button to create one Image from icon pack Image from gallery