From b23c2de29c419961233052f2ad979bd8c56a89d0 Mon Sep 17 00:00:00 2001 From: dgudim Date: Sun, 14 Jan 2024 20:12:30 +0200 Subject: [PATCH] Fixed exporter, added translations --- .../ImportExportSettingsEntryConfig.java | 28 ++++++++- .../utilities/misc/SettingsExporter.java | 61 ------------------- app/src/main/res/values-ru/strings.xml | 7 +++ app/src/main/res/values-uk/strings.xml | 7 +++ 4 files changed, 39 insertions(+), 64 deletions(-) delete mode 100644 app/src/main/java/prototype/xd/scheduler/utilities/misc/SettingsExporter.java diff --git a/app/src/main/java/prototype/xd/scheduler/entities/settings_entries/ImportExportSettingsEntryConfig.java b/app/src/main/java/prototype/xd/scheduler/entities/settings_entries/ImportExportSettingsEntryConfig.java index bc0aeef..cb31099 100644 --- a/app/src/main/java/prototype/xd/scheduler/entities/settings_entries/ImportExportSettingsEntryConfig.java +++ b/app/src/main/java/prototype/xd/scheduler/entities/settings_entries/ImportExportSettingsEntryConfig.java @@ -2,10 +2,17 @@ import static prototype.xd.scheduler.entities.settings_entries.SettingsEntryType.IMPORT_EXPORT_SETTINGS; import static prototype.xd.scheduler.utilities.Logger.logException; +import static prototype.xd.scheduler.utilities.Static.ENTRIES_FILE; +import static prototype.xd.scheduler.utilities.Static.ENTRIES_FILE_BACKUP; +import static prototype.xd.scheduler.utilities.Static.GROUPS_FILE; +import static prototype.xd.scheduler.utilities.Static.GROUPS_FILE_BACKUP; +import static prototype.xd.scheduler.utilities.Static.SETTINGS_FILE; +import static prototype.xd.scheduler.utilities.Static.SETTINGS_FILE_BACKUP; import static prototype.xd.scheduler.utilities.Utilities.displayToast; import static prototype.xd.scheduler.utilities.Utilities.getFile; import android.annotation.SuppressLint; +import android.content.SharedPreferences; import android.net.Uri; import androidx.activity.result.ActivityResultLauncher; @@ -19,13 +26,14 @@ import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.StandardCopyOption; +import java.util.Map; import prototype.xd.scheduler.R; import prototype.xd.scheduler.databinding.ImportExportSettingsEntryBinding; import prototype.xd.scheduler.utilities.DateManager; import prototype.xd.scheduler.utilities.Static; +import prototype.xd.scheduler.utilities.Utilities; import prototype.xd.scheduler.utilities.misc.ContextWrapper; -import prototype.xd.scheduler.utilities.misc.SettingsExporter; import prototype.xd.scheduler.utilities.services.BackgroundSetterService; public class ImportExportSettingsEntryConfig extends SettingsEntryConfig { @@ -60,13 +68,20 @@ public int getRecyclerViewType() { public static void notifyFileChosen(@NonNull ContextWrapper wrapper, @Nullable Uri uri) { wrapper.uriToStream(uri, InputStream.class, stream -> { File tempFile = getFile(Static.EXPORT_FILE); + // Copy from user's directory to internal Files.copy(stream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + // Extract everything try (var zip = new ZipFile(tempFile)) { zip.extractAll(wrapper.context.getExternalFilesDir(null).getPath()); } - SettingsExporter.importSettings(); + // Load prefs + SharedPreferences.Editor editor = Static.clearAll(); + Map prefs = Utilities.loadObjectWithBackup(SETTINGS_FILE, SETTINGS_FILE_BACKUP); + prefs.forEach((key, value) -> Static.putAnyEditor(editor, key, value)); + // Overwrite immediately + editor.commit(); displayToast(wrapper.context, R.string.import_settings_successful); try { @@ -90,8 +105,14 @@ public static void notifySaveLocationChosen(@NonNull ContextWrapper wrapper, @Nu File tempFile = getFile(Static.EXPORT_FILE); Files.deleteIfExists(tempFile.toPath()); try (var zip = new ZipFile(tempFile)) { - zip.addFile(SettingsExporter.exportSettings()); + // Add preferences + zip.addFile(Utilities.saveObjectWithBackup(SETTINGS_FILE, SETTINGS_FILE_BACKUP, Static.getAll())); + zip.addFile(getFile(GROUPS_FILE)); + zip.addFile(getFile(GROUPS_FILE_BACKUP)); + zip.addFile(getFile(ENTRIES_FILE)); + zip.addFile(getFile(ENTRIES_FILE_BACKUP)); + // Add all backgrounds for (String bgName : DateManager.BG_NAMES_ROOT) { File bgFile = getFile(bgName); if (bgFile.exists()) { @@ -99,6 +120,7 @@ public static void notifySaveLocationChosen(@NonNull ContextWrapper wrapper, @Nu } } + // Copy from internal temp file to target Files.copy(tempFile.toPath(), stream); displayToast(wrapper.context, R.string.export_settings_successful); diff --git a/app/src/main/java/prototype/xd/scheduler/utilities/misc/SettingsExporter.java b/app/src/main/java/prototype/xd/scheduler/utilities/misc/SettingsExporter.java deleted file mode 100644 index 56ff64b..0000000 --- a/app/src/main/java/prototype/xd/scheduler/utilities/misc/SettingsExporter.java +++ /dev/null @@ -1,61 +0,0 @@ -package prototype.xd.scheduler.utilities.misc; - -import static prototype.xd.scheduler.utilities.Static.SETTINGS_FILE; -import static prototype.xd.scheduler.utilities.Static.SETTINGS_FILE_BACKUP; -import static prototype.xd.scheduler.utilities.Utilities.loadObjectWithBackup; - -import android.content.SharedPreferences; - -import androidx.annotation.NonNull; - -import java.io.File; -import java.io.IOException; -import java.io.Serial; -import java.io.Serializable; -import java.util.Collection; -import java.util.Map; - -import prototype.xd.scheduler.entities.Group; -import prototype.xd.scheduler.entities.TodoEntry; -import prototype.xd.scheduler.utilities.Logger; -import prototype.xd.scheduler.utilities.Static; -import prototype.xd.scheduler.utilities.Utilities; - -public final class SettingsExporter implements Serializable { - - @Serial - private static final long serialVersionUID = 123123123L; - // TODO: fix exporter, use fields from TodoManager - public static final String NAME = SettingsExporter.class.getSimpleName(); - - private final Collection entries; - private final Collection groups; - private final Map prefs; // NOSONAR prefs return a HashMap - - private SettingsExporter() throws IOException, ClassNotFoundException { - entries = Utilities.loadFromEntriesFile(); - groups = Utilities.loadFromGroupsFile(); - prefs = Static.getAll(); - Logger.info(NAME, ">>>>>>>>> Settings export created with " + entries.size() + " entries, " + groups.size() + " groups and " + prefs.size() + " prefs"); - } - - @NonNull - public static File exportSettings() throws IOException, ClassNotFoundException { - return Utilities.saveObjectWithBackup(SETTINGS_FILE, SETTINGS_FILE_BACKUP, new SettingsExporter()); - } - - public static void importSettings() throws IOException, ClassNotFoundException { - SettingsExporter exporter = loadObjectWithBackup(SETTINGS_FILE, SETTINGS_FILE_BACKUP); - Utilities.saveToEntriesFile(exporter.entries); - Utilities.saveToGroupsFile(exporter.groups); - SharedPreferences.Editor editor = Static.clearAll(); - exporter.prefs.forEach((key, value) -> Static.putAnyEditor(editor, key, value)); - // Overwrite immediately - editor.commit(); - Logger.info(NAME, ">>>>>>>>> Settings imported: " - + exporter.entries.size() + " entries, " - + exporter.groups.size() + " groups and " - + exporter.prefs.size() + " prefs"); - } - -} diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 9a3a822..2b208ce 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -236,7 +236,14 @@ "Позиция надписи “Глобальный” " События, которые видны на всех днях Событие создано + Событие сохранено Событие удалено Добавить свечение к прозрачности Выделить внутренние края + Повторяется + Посмотреть события календаля + Все события календаря + Это все события из календаря + Настройки для определенных цветов + В календаре могут быть события разного цвета, здесь можно задать отдельные стили для каждого цвета. \ No newline at end of file diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index 09e2520..12bc6a3 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -236,7 +236,14 @@ Перед подією Заховано Подія створена + Подія збережена Подія видалена Додати свічення до прозорості Виділити внутрішні краї + Повторюється + Подивитись події календалю + Усі події календаря + Це всі події з календаря + Налаштування для певних кольорів + У календарі можуть бути події різних кольорів, тут ви можете встановити окремі стилі для кожного кольору