Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/backup import improvements #25

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/datamodels/user_backup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class UserBackup {
final Map<String, dynamic> vocabSpacedRepetitionDataEnglish;
final Map<String, dynamic> kanjiSpacedRepetitionData;
final Map<String, dynamic> kanjiSpacedRepetitionDataEnglish;
final List<String> searchHistory;
final List<String> textAnalysisHistory;

const UserBackup({
required this.dictionaryVersion,
Expand All @@ -21,6 +23,8 @@ class UserBackup {
required this.vocabSpacedRepetitionDataEnglish,
required this.kanjiSpacedRepetitionData,
required this.kanjiSpacedRepetitionDataEnglish,
required this.searchHistory,
required this.textAnalysisHistory,
});

String toBackupJson() {
Expand All @@ -40,7 +44,10 @@ class UserBackup {
SagaseDictionaryConstants.backupKanjiSpacedRepetitionData:
kanjiSpacedRepetitionData,
SagaseDictionaryConstants.backupKanjiSpacedRepetitionDataEnglish:
kanjiSpacedRepetitionDataEnglish
kanjiSpacedRepetitionDataEnglish,
SagaseDictionaryConstants.backupSearchHistory: searchHistory,
SagaseDictionaryConstants.backupTextAnalysisHistory:
textAnalysisHistory,
},
);
}
Expand Down Expand Up @@ -74,6 +81,11 @@ class UserBackup {
kanjiSpacedRepetitionDataEnglish:
map[SagaseDictionaryConstants.backupKanjiSpacedRepetitionDataEnglish]
.cast<String, dynamic>(),
searchHistory: (map[SagaseDictionaryConstants.backupSearchHistory] ?? [])
.cast<String>(),
textAnalysisHistory:
(map[SagaseDictionaryConstants.backupTextAnalysisHistory] ?? [])
.cast<String>(),
);
}
}
53 changes: 53 additions & 0 deletions lib/services/dictionary_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,23 @@ class DictionaryService {
}
}

Future<bool> restoreFromBackup(String backupFilePath) async {
bool result = false;

await _database.transaction(() async {
// Delete existing user data
await _database.flashcardSetsDao.deleteAll();
await _database.myDictionaryListsDao.deleteAll();
await _database.spacedRepetitionDatasDao.deleteAll();
await deleteSearchHistory();

// Import user data from backup
result = await importUserData(backupFilePath);
});

return result;
}

Future<String?> exportUserData() async {
try {
// My dictionary lists
Expand Down Expand Up @@ -553,6 +570,20 @@ class DictionaryService {
}
}

// Search history
final List<String> searchHistoryBackups = [];
final searchHistoryItems = await getSearchHistory();
for (final item in searchHistoryItems) {
searchHistoryBackups.add(item.searchText);
}

// Text analysis history
final List<String> textAnalysisHistoryBackups = [];
final textAnalysisHistoryItems = await getTextAnalysisHistory();
for (final item in textAnalysisHistoryItems) {
textAnalysisHistoryBackups.add(item.analysisText);
}

// Create instance
DateTime now = DateTime.now();
final backup = UserBackup(
Expand All @@ -566,6 +597,8 @@ class DictionaryService {
kanjiSpacedRepetitionData: kanjiSpacedRepetitionDataBackups,
kanjiSpacedRepetitionDataEnglish:
kanjiSpacedRepetitionDataEnglishBackups,
searchHistory: searchHistoryBackups,
textAnalysisHistory: textAnalysisHistoryBackups,
);

// Create file and write to it
Expand Down Expand Up @@ -646,6 +679,26 @@ class DictionaryService {

await _database.spacedRepetitionDatasDao.set(spacedRepetitionData);
}

// Search history
for (int i = 0; i < userBackup.searchHistory.length; i++) {
await setSearchHistoryItem(
SearchHistoryItem(
id: userBackup.searchHistory.length - i,
searchText: userBackup.searchHistory[i],
),
);
}

// Text analysis history
for (int i = 0; i < userBackup.textAnalysisHistory.length; i++) {
await setTextAnalysisHistoryItem(
TextAnalysisHistoryItem(
id: userBackup.textAnalysisHistory.length - i,
analysisText: userBackup.textAnalysisHistory[i],
),
);
}
});

return true;
Expand Down
12 changes: 12 additions & 0 deletions lib/services/isar_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ class IsarService {
kanji.spacedRepetitionDataEnglish!.toBackupJson();
}

// Search history
final List<String> searchHistoryBackups = [];
final searchHistoryItems = await _isar.searchHistoryItems
.where()
.sortByTimestampDesc()
.findAll();
for (final item in searchHistoryItems) {
searchHistoryBackups.add(item.searchQuery);
}

// Create instance
DateTime now = DateTime.now();
final backup = UserBackup(
Expand All @@ -119,6 +129,8 @@ class IsarService {
kanjiSpacedRepetitionData: kanjiSpacedRepetitionDataBackups,
kanjiSpacedRepetitionDataEnglish:
kanjiSpacedRepetitionDataEnglishBackups,
searchHistory: searchHistoryBackups,
textAnalysisHistory: [],
);

// Create file and write to it
Expand Down
7 changes: 3 additions & 4 deletions lib/ui/views/search/search_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class SearchViewModel extends FutureViewModel {

@override
Future<void> futureToRun() async {
searchHistory = await _dictionaryService.getSearchHistory();
notifyListeners();
await loadSearchHistory();
}

void navigateToVocab(Vocab vocab) {
Expand Down Expand Up @@ -157,8 +156,8 @@ class SearchViewModel extends FutureViewModel {
notifyListeners();
}

void clearSearchHistory() {
searchHistory.clear();
Future<void> loadSearchHistory() async {
searchHistory = await _dictionaryService.getSearchHistory();
_currentSearchHistoryItem = null;
notifyListeners();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/views/settings/settings_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ class SettingsView extends StatelessWidget {
onPressed: (_) => viewModel.backupData(),
),
SettingsTile.navigation(
title: const Text('Import backup'),
title: const Text('Restore from backup'),
description: const Text(
'This will merge the current app data with the data from the backup file. Conflicting data will be overwritten by the backup data.',
'This will delete all user data and then import new user data from the selected backup file.',
),
onPressed: (_) => viewModel.importData(),
onPressed: (_) => viewModel.restoreFromBackup(),
),
],
),
Expand Down
63 changes: 38 additions & 25 deletions lib/ui/views/settings/settings_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class SettingsViewModel extends BaseViewModel {

if (response != null && response.confirmed) {
_dictionaryService.deleteSearchHistory();
locator<SearchViewModel>().clearSearchHistory();
locator<SearchViewModel>().loadSearchHistory();
}
}

Expand Down Expand Up @@ -200,36 +200,49 @@ class SettingsViewModel extends BaseViewModel {
await File(path).delete();
}

Future<void> importData() async {
// Ask user for the file they want to import
String? filePath;
try {
filePath = await FlutterFileDialog.pickFile(
params: const OpenFileDialogParams(fileExtensionsFilter: ['sagase']),
);
} catch (_) {
filePath = null;
}
Future<void> restoreFromBackup() async {
final response = await _dialogService.showCustomDialog(
variant: DialogType.confirmation,
title: 'Restore from backup?',
description:
'This will delete all user data and then import new user data from the selected backup file.',
mainButtonTitle: 'Confirm',
secondaryButtonTitle: 'Cancel',
barrierDismissible: true,
);

if (filePath != null) {
// Show progress indicator dialog
_dialogService.showCustomDialog(
variant: DialogType.progressIndicator,
title: 'Importing data',
barrierDismissible: false,
);
if (response != null && response.confirmed) {
// Ask user for the file they want to import
String? filePath;
try {
filePath = await FlutterFileDialog.pickFile(
params: const OpenFileDialogParams(fileExtensionsFilter: ['sagase']),
);
} catch (_) {
filePath = null;
}

if (filePath != null) {
// Show progress indicator dialog
_dialogService.showCustomDialog(
variant: DialogType.progressIndicator,
title: 'Importing data',
barrierDismissible: false,
);

bool result = await _dictionaryService.importUserData(filePath);
bool result = await _dictionaryService.restoreFromBackup(filePath);

_dialogService.completeDialog(DialogResponse());
_dialogService.completeDialog(DialogResponse());

if (result) {
_snackbarService.showSnackbar(message: 'Import successful');
if (result) {
locator<SearchViewModel>().loadSearchHistory();
_snackbarService.showSnackbar(message: 'Import successful');
} else {
_snackbarService.showSnackbar(message: 'Import failed');
}
} else {
_snackbarService.showSnackbar(message: 'Import failed');
_snackbarService.showSnackbar(message: 'Import cancelled');
}
} else {
_snackbarService.showSnackbar(message: 'Import cancelled');
}
}

Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: d9bfefd
resolved-ref: d9bfefd2a69b49955c44ebbc04595368b3a139c0
ref: "4638c14"
resolved-ref: "4638c143437ed1e78f8d4c630aea3890e869bdf5"
url: "https://github.com/Moseco/sagase_dictionary"
source: git
version: "1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ dependencies:
sagase_dictionary:
git:
url: https://github.com/Moseco/sagase_dictionary
ref: 'd9bfefd'
ref: '4638c14'
url_launcher: ^6.1.12
dio: ^5.3.3
in_app_review: ^2.0.8
Expand Down
Loading