diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java index c5d61a6..7743a93 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/fragment/SettingsFragment.java @@ -154,7 +154,7 @@ public class SettingsFragment extends NamedFragment { requireActivity().recreate(); }); - binding.settingsHideCodes.setChecked(SettingsUtil.isHideCodes(requireContext())); // TODO: implement functionality + binding.settingsHideCodes.setChecked(SettingsUtil.isHideCodes(requireContext())); binding.settingsHideCodes.setOnCheckedChangeListener((view, checked) -> SettingsUtil.setHideCodes(requireContext(), checked)); String[] themeNames = new String[Theme.values().length]; @@ -300,7 +300,6 @@ public class SettingsFragment extends NamedFragment { private void loadBackup(Uri uri, SecretKey key, CryptoParameters parameters) throws BackupException, OTPDatabaseException, CryptoException { BackupData data = BackupUtil.loadBackup(requireContext(), uri); OTPDatabase db = data.loadDatabase(key, parameters); - // TODO: prompt user that all current data will be deleted OTPDatabase.promptLoadDatabase(requireActivity(), () -> { OTPDatabase oldDatabase = OTPDatabase.getLoadedDatabase(); try { diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/otplist/OTPListAdapter.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/otplist/OTPListAdapter.java index 6c2dc6c..bc044b9 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/otplist/OTPListAdapter.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/otplist/OTPListAdapter.java @@ -72,23 +72,32 @@ public class OTPListAdapter extends RecyclerView.Adapter { } holder.getBinding().label.setText(String.format("%s%s", data.getIssuer() == null || data.getIssuer().isEmpty() ? "" : data.getIssuer() + ": ", data.getName())); - holder.getBinding().progress.setVisibility(data.getType() == OTPType.TOTP ? View.VISIBLE : View.INVISIBLE); + holder.getBinding().progress.setVisibility(data.getType() == OTPType.TOTP ? View.VISIBLE : View.GONE); + holder.getBinding().refresh.setVisibility(data.getType() == OTPType.HOTP ? View.VISIBLE : View.GONE); + + holder.getBinding().refresh.setOnClickListener(view -> { + if (data.getType() != OTPType.HOTP) return; + + if(!holder.isCodeShown()) { + showCode(holder); + } + + // Click delay for HOTP + view.setEnabled(false); + data.incrementCounter(); + saveOTPs.run(); + + Toast.makeText(view.getContext(), R.string.hotp_generated_new_code, Toast.LENGTH_SHORT).show(); + + handler.postDelayed(() -> view.setEnabled(true), 5000); + }); holder.getBinding().getRoot().setOnClickListener(view -> { if(!editing) { - if (!view.isClickable()) return; - - if(!holder.isCodeShown()) holder.setCodeShown(true); - - if (data.getType() == OTPType.HOTP) { - // Click delay for HOTP - view.setClickable(false); - data.incrementCounter(); - saveOTPs.run(); - - Toast.makeText(view.getContext(), R.string.hotp_generated_new_code, Toast.LENGTH_SHORT).show(); - - handler.postDelayed(() -> view.setClickable(true), 5000); + if(!holder.isCodeShown()) { + showCode(holder); + }else { + holder.setCodeShown(false); } try { @@ -168,6 +177,31 @@ public class OTPListAdapter extends RecyclerView.Adapter { return selected; } + private void showCode(OTPListItem item) { + for(OTPListItem h : getCodes()) { + if(h.isCodeShown()) { + h.setCodeShown(false); + try { + h.refresh(); + } catch (OTPException e) { + DialogUtil.showErrorDialog(context, e.getMessage() == null ? "An error occurred while refreshing the code" : e.getMessage()); + } + } + } + + item.setCodeShown(true); + } + + private List getCodes() { + List is = new ArrayList<>(); + for(int i = 0; i < items.size(); i++) { + OTPListItem vh = (OTPListItem) recyclerView.findViewHolderForAdapterPosition(i); + if(vh == null) continue; + is.add(vh); + } + return is; + } + private void attachTouchHelper(RecyclerView view) { new ItemTouchHelper(new OTPListAdapter.TouchHelperCallback()).attachToRecyclerView(view); } diff --git a/app/src/main/res/drawable/baseline_refresh_24.xml b/app/src/main/res/drawable/baseline_refresh_24.xml new file mode 100644 index 0000000..855c57c --- /dev/null +++ b/app/src/main/res/drawable/baseline_refresh_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/otp_code.xml b/app/src/main/res/layout/otp_code.xml index f58f907..67d1819 100644 --- a/app/src/main/res/layout/otp_code.xml +++ b/app/src/main/res/layout/otp_code.xml @@ -70,6 +70,13 @@ android:layout_height="30dp" app:srcCompat="@drawable/progress_circle" /> + +