Skip to content

Commit c79bdf7

Browse files
committed
Fix bug where duplicate entries could be saved from Android AutoFill
Could only reproduce on Android 14 but am unsure what could explain that so probably also affected older Android versions sometimes too.
1 parent 12ee232 commit c79bdf7

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

lib/cubit/autofill_cubit.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class AutofillCubit extends Cubit<AutofillState> {
6666
}
6767

6868
Future<void> finishSaving() async {
69+
l.t('Autofillcubit.finishSaving');
6970
if (state is AutofillSaving || state is AutofillSaved) {
7071
emit(AutofillSaved((state as AutofillModeActive).androidMetadata));
7172
await AutofillService().onSaveComplete();

lib/cubit/entry_cubit.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ class EntryCubit extends Cubit<EntryState> {
1515
EntryCubit() : super(EntryInitial());
1616

1717
void startEditing(KdbxEntry entry, {bool startDirty = false}) {
18+
l.t('EntryCubit.startEditing');
1819
final newEntry = EditEntryViewModel.fromKdbxEntry(entry).let((it) => startDirty ? it.copyWith(isDirty: true) : it);
1920
emit(EntryLoaded(newEntry));
2021
}
2122

2223
void endEditing(KdbxEntry? entry) {
24+
l.t('EntryCubit.endEditing');
2325
if (entry != null) {
2426
(state as EntryLoaded).entry.commit(entry);
2527
}

lib/cubit/vault_cubit.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ class VaultCubit extends Cubit<VaultState> {
279279
final rootGroup = vault.files.current.body.rootGroup;
280280
initAutofillPersistentQueue(rootGroup.uuid.uuidUrlSafe);
281281
if (isAutofilling()) {
282+
// Make sure that any previous autofill operations have their adjustments
283+
// applied before we present the entry list or entry saving interface to the user.
282284
await _persistentQueueAfAssociations?.ready;
283285
final pendingItems = (await _persistentQueueAfAssociations?.toList());
284286
if (pendingItems != null) {
@@ -1272,6 +1274,7 @@ class VaultCubit extends Cubit<VaultState> {
12721274
KdbxEntry createEntry({
12731275
required KdbxGroup group,
12741276
}) {
1277+
l.t('VaultCubit.createEntry');
12751278
final destinationGroup = group;
12761279
final entry = KdbxEntry.create(currentVaultFile!.files.current, destinationGroup);
12771280
destinationGroup.addEntry(entry);

lib/widgets/autofill_save.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:keevault/cubit/vault_cubit.dart';
99
import 'package:keevault/extension_methods.dart';
1010
import 'package:keevault/widgets/loading_spinner.dart';
1111
import 'package:matomo_tracker/matomo_tracker.dart';
12+
import '../logging/logger.dart';
1213
import 'coloured_safe_area_widget.dart';
1314
import 'entry.dart';
1415

@@ -28,6 +29,7 @@ class _AutofillSaveWidgetState extends State<AutofillSaveWidget> with TraceableC
2829
KdbxEntry? newEntry;
2930
@override
3031
void initState() {
32+
l.t('_AutofillSaveWidgetState.initState');
3133
super.initState();
3234
final autofillState = BlocProvider.of<AutofillCubit>(context).state as AutofillModeActive;
3335
final vaultCubit = BlocProvider.of<VaultCubit>(context);
@@ -39,6 +41,10 @@ class _AutofillSaveWidgetState extends State<AutofillSaveWidget> with TraceableC
3941
final password = autofillState.androidMetadata.saveInfo!.password;
4042
//TODO:f: expose value of save compat mode to Entry widget:
4143
//final isCompatMode = autofillState.androidMetadata.saveInfo!.isCompatMode;
44+
45+
// We'll get duplicate entries if multiple instances of this widget are built.
46+
// Should move the business logic to somewhere else if possible and have this
47+
// widget just show whatever new entry has been supplied to it (e.g. by the cubit).
4248
setState(() {
4349
newEntry = vaultCubit.createEntry(group: vault.files.current.body.rootGroup);
4450
});

lib/widgets/kee_vault_app.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class KeeVaultAppState extends State<KeeVaultApp> with WidgetsBindingObserver, T
235235
RemoteVaultRepository(userService, storageService),
236236
LocalVaultRepository(quickUnlocker),
237237
entryCubit,
238-
autofillCubit.isAutofilling,
238+
() => autofillCubit.isAutofilling() || autofillCubit.isAutofillSaving(),
239239
generatorProfilesCubit,
240240
accountCubit,
241241
)),

lib/widgets/vault.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ class _VaultWidgetState extends State<VaultWidget> with WidgetsBindingObserver {
4040

4141
Future<void> _refresh() async {
4242
final user = BlocProvider.of<AccountCubit>(context).currentUserIfKnown;
43+
if (user == null) {
44+
return;
45+
}
4346
final AutofillState autofillState = BlocProvider.of<AutofillCubit>(context).state;
44-
if (autofillState is AutofillModeActive || user == null) {
47+
if (autofillState is AutofillModeActive) {
48+
l.t('Skip refresh due to state: ${autofillState.runtimeType}');
4549
return;
4650
}
4751
await BlocProvider.of<VaultCubit>(context).refresh(user);

0 commit comments

Comments
 (0)