diff --git a/lib/data/dao/note_helper.dart b/lib/data/dao/note_helper.dart index d8cd300a..2d65d17b 100644 --- a/lib/data/dao/note_helper.dart +++ b/lib/data/dao/note_helper.dart @@ -122,6 +122,14 @@ class NoteHelper extends DatabaseAccessor with _$NoteHelperMixin { Loggy.d(message: "The note id to delete: " + note.id); return delete(notes).delete(note); } + + Future deleteAllNotes() async { + List notes = await listNotes(ReturnMode.ALL); + + notes.forEach((note) async { + await deleteNote(note); + }); + } } class SearchQuery { diff --git a/lib/internal/shared_prefs.dart b/lib/internal/shared_prefs.dart index bf8fed6b..48c2fd03 100644 --- a/lib/internal/shared_prefs.dart +++ b/lib/internal/shared_prefs.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:loggy/loggy.dart'; +import 'package:potato_notes/internal/utils.dart'; import 'package:shared_preferences/shared_preferences.dart'; class SharedPrefs { @@ -101,7 +102,8 @@ class SharedPrefs { } Future getApiUrl() async { - return prefs.getString("api_url") ?? "http://stats.corbellum.nl/api/v2"; + // http://stats.corbellum.nl/api/v2 + return prefs.getString("api_url") ?? Utils.defaultApiUrl; } void setApiUrl(String value) async { diff --git a/lib/internal/sync/sync_routine.dart b/lib/internal/sync/sync_routine.dart index 1cb3c46b..89ca2224 100644 --- a/lib/internal/sync/sync_routine.dart +++ b/lib/internal/sync/sync_routine.dart @@ -26,7 +26,7 @@ class SyncRoutine { SyncRoutine(); - Future checkOnlineStatus() async { + static Future checkOnlineStatus() async { try { var url = prefs.apiUrl + "/notes/ping"; Loggy.d(message: "Going to send GET to " + url); @@ -46,7 +46,7 @@ class SyncRoutine { } } - Future checkLoginStatus() async { + static Future checkLoginStatus() async { try { var url = prefs.apiUrl + NoteController.NOTES_PREFIX + "/secure-ping"; Loggy.d(message: "Going to send GET to " + url); @@ -176,7 +176,7 @@ class SyncRoutine { return true; } - Future sendSettingUpdates() async { + static Future sendSettingUpdates() async { Map changedSettings; try { changedSettings = await SettingController.getChanged(prefs.lastUpdated); @@ -240,13 +240,13 @@ class SyncRoutine { prefs.triggerRefresh(); } - Future saveSynced(Note note) async { + static Future saveSynced(Note note) async { await helper.saveNote(note.copyWith(synced: true)); var syncedNote = note.copyWith(id: note.id + "-synced"); await helper.saveNote(syncedNote); } - Future deleteSynced(Note note) async { + static Future deleteSynced(Note note) async { helper.deleteNote(note); } diff --git a/lib/internal/utils.dart b/lib/internal/utils.dart index 6401705b..32d10c1c 100644 --- a/lib/internal/utils.dart +++ b/lib/internal/utils.dart @@ -532,4 +532,6 @@ class Utils { }); return Note.fromJson(newMap); } + + static String get defaultApiUrl => "https://sync.potatoproject.co/api/v2"; } diff --git a/lib/routes/settings_page.dart b/lib/routes/settings_page.dart index 1e4859d9..f1d29f1e 100644 --- a/lib/routes/settings_page.dart +++ b/lib/routes/settings_page.dart @@ -17,6 +17,7 @@ import 'package:potato_notes/widget/pass_challenge.dart'; import 'package:potato_notes/widget/rgb_color_picker.dart'; import 'package:potato_notes/widget/settings_category.dart'; import 'package:potato_notes/widget/settings_tile.dart'; +import 'package:potato_notes/widget/sync_url_editor.dart'; class SettingsPage extends StatefulWidget { final bool trimmed; @@ -81,10 +82,7 @@ class _SettingsPageState extends State { icon: Icon(CommunityMaterialIcons.database_remove), title: Text(LocaleStrings.settingsPage.debugClearDatabase), onTap: () async { - List notes = await helper.listNotes(ReturnMode.ALL); - - notes.forEach( - (element) async => await helper.deleteNote(element)); + await helper.deleteAllNotes(); }, ), SettingsTile( @@ -284,6 +282,24 @@ class _SettingsPageState extends State { }, subtitle: Text(currentLocaleName), ), + SettingsTile( + icon: Icon(Icons.autorenew), + title: Text("Change sync API url"), + onTap: () async { + bool status = await showInfoSheet( + context, + content: + "If you decide to change the sync api url every note will get deleted to prevent conflicts. Do this only if you know what are you doing.", + buttonAction: LocaleStrings.common.goOn, + ); + if (status) + Utils.showNotesModalBottomSheet( + context: context, + isScrollControlled: true, + builder: (context) => SyncUrlEditor(), + ); + }, + ) ], ), SettingsCategory( @@ -311,7 +327,8 @@ class _SettingsPageState extends State { setState(() => removingMasterPass = true); for (int i = 0; i < notes.length; i++) { - await helper.saveNote(Utils.markNoteChanged(notes[i]).copyWith(lockNote: false)); + await helper.saveNote(Utils.markNoteChanged(notes[i]) + .copyWith(lockNote: false)); } setState(() => removingMasterPass = false); } diff --git a/lib/widget/sync_url_editor.dart b/lib/widget/sync_url_editor.dart new file mode 100644 index 00000000..baa3f135 --- /dev/null +++ b/lib/widget/sync_url_editor.dart @@ -0,0 +1,90 @@ +import 'package:flutter/material.dart'; +import 'package:potato_notes/internal/locale_strings.dart'; +import 'package:potato_notes/internal/providers.dart'; +import 'package:potato_notes/internal/utils.dart'; + +class SyncUrlEditor extends StatefulWidget { + @override + _SyncUrlEditorState createState() => _SyncUrlEditorState(); +} + +class _SyncUrlEditorState extends State { + TextEditingController controller; + + @override + void initState() { + controller = TextEditingController(text: prefs.apiUrl); + super.initState(); + } + + @override + Widget build(BuildContext context) { + return Padding( + padding: + EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + Padding( + padding: EdgeInsets.all(24), + child: Text( + "Change sync API url", + style: TextStyle( + fontSize: 20, + fontWeight: FontWeight.w500, + ), + ), + ), + Padding( + padding: EdgeInsets.symmetric(vertical: 8, horizontal: 24), + child: TextFormField( + decoration: InputDecoration( + labelText: "URL", + border: UnderlineInputBorder(), + ), + controller: controller, + ), + ), + Padding( + padding: EdgeInsets.symmetric(vertical: 8, horizontal: 24), + child: Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + FlatButton( + onPressed: () => Navigator.pop(context), + child: Text(LocaleStrings.common.cancel), + textColor: Theme.of(context).accentColor, + ), + Spacer(), + FlatButton( + onPressed: () async { + prefs.apiUrl = Utils.defaultApiUrl; + await helper.deleteAllNotes(); + Navigator.pop(context); + }, + child: Text(LocaleStrings.common.reset), + textColor: Theme.of(context).accentColor, + ), + FlatButton( + onPressed: controller.text.isNotEmpty + ? () async { + prefs.apiUrl = controller.text; + await helper.deleteAllNotes(); + Navigator.pop(context); + } + : null, + child: Text(LocaleStrings.common.save), + color: Theme.of(context).accentColor, + disabledColor: Theme.of(context).disabledColor, + textColor: Theme.of(context).cardColor, + disabledTextColor: Theme.of(context).cardColor, + ), + ], + ), + ), + ], + ), + ); + } +}