populateDefaultMapping method

void populateDefaultMapping(
  1. Language language
)

Populate default mapping if it does not exist in the database.

Implementation

void populateDefaultMapping(Language language) async {
  if (_database.ankiMappings.where().findAllSync().isEmpty) {
    _database.writeTxnSync(() {
      _database.ankiMappings.putSync(AnkiMapping.defaultMapping(
        language: language,
        order: 0,
      ));
    });
  } else {
    AnkiMapping standardProfile = _database.ankiMappings
        .where()
        .labelEqualTo(AnkiMapping.standardProfileName)
        .findFirstSync()!;
    if (standardProfile.model != AnkiMapping.standardModelName) {
      String newLabel = 'Legacy Standard';
      int attempts = 1;

      while (_database.ankiMappings
          .where()
          .labelEqualTo(newLabel)
          .findAllSync()
          .isNotEmpty) {
        attempts += 1;
        newLabel = 'Legacy Standard ($attempts)';
      }

      AnkiMapping legacyProfile = standardProfile.copyWith(
        label: newLabel,
      );

      _database.writeTxnSync(() {
        _database.ankiMappings.putAllSync(
          [
            legacyProfile,
            AnkiMapping.defaultMapping(
              language: language,
              order: nextMappingOrder,
            ),
          ],
        );
      });

      await showDialog(
        barrierDismissible: true,
        context: _navigatorKey.currentContext!,
        builder: (context) => AlertDialog(
          title: Text(t.info_standard_update),
          content: Text(
            t.info_standard_update_content,
          ),
          actions: [
            TextButton(
              child: Text(t.dialog_close),
              onPressed: () => Navigator.pop(context),
            ),
          ],
        ),
      );
    }
  }
}