Skip to content

Commit

Permalink
notes: Add initial api url change impl
Browse files Browse the repository at this point in the history
  • Loading branch information
HrX03 committed Aug 4, 2020
1 parent 9494456 commit ea257ec
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 11 deletions.
8 changes: 8 additions & 0 deletions lib/data/dao/note_helper.dart
Expand Up @@ -122,6 +122,14 @@ class NoteHelper extends DatabaseAccessor<AppDatabase> with _$NoteHelperMixin {
Loggy.d(message: "The note id to delete: " + note.id);
return delete(notes).delete(note);
}

Future<void> deleteAllNotes() async {
List<Note> notes = await listNotes(ReturnMode.ALL);

notes.forEach((note) async {
await deleteNote(note);
});
}
}

class SearchQuery {
Expand Down
4 changes: 3 additions & 1 deletion 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 {
Expand Down Expand Up @@ -101,7 +102,8 @@ class SharedPrefs {
}

Future<String> 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 {
Expand Down
10 changes: 5 additions & 5 deletions lib/internal/sync/sync_routine.dart
Expand Up @@ -26,7 +26,7 @@ class SyncRoutine {

SyncRoutine();

Future<bool> checkOnlineStatus() async {
static Future<bool> checkOnlineStatus() async {
try {
var url = prefs.apiUrl + "/notes/ping";
Loggy.d(message: "Going to send GET to " + url);
Expand All @@ -46,7 +46,7 @@ class SyncRoutine {
}
}

Future<bool> checkLoginStatus() async {
static Future<bool> checkLoginStatus() async {
try {
var url = prefs.apiUrl + NoteController.NOTES_PREFIX + "/secure-ping";
Loggy.d(message: "Going to send GET to " + url);
Expand Down Expand Up @@ -176,7 +176,7 @@ class SyncRoutine {
return true;
}

Future<void> sendSettingUpdates() async {
static Future<void> sendSettingUpdates() async {
Map<String, String> changedSettings;
try {
changedSettings = await SettingController.getChanged(prefs.lastUpdated);
Expand Down Expand Up @@ -240,13 +240,13 @@ class SyncRoutine {
prefs.triggerRefresh();
}

Future<void> saveSynced(Note note) async {
static Future<void> saveSynced(Note note) async {
await helper.saveNote(note.copyWith(synced: true));
var syncedNote = note.copyWith(id: note.id + "-synced");
await helper.saveNote(syncedNote);
}

Future<void> deleteSynced(Note note) async {
static Future<void> deleteSynced(Note note) async {
helper.deleteNote(note);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/internal/utils.dart
Expand Up @@ -532,4 +532,6 @@ class Utils {
});
return Note.fromJson(newMap);
}

static String get defaultApiUrl => "https://sync.potatoproject.co/api/v2";
}
27 changes: 22 additions & 5 deletions lib/routes/settings_page.dart
Expand Up @@ -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;
Expand Down Expand Up @@ -81,10 +82,7 @@ class _SettingsPageState extends State<SettingsPage> {
icon: Icon(CommunityMaterialIcons.database_remove),
title: Text(LocaleStrings.settingsPage.debugClearDatabase),
onTap: () async {
List<Note> notes = await helper.listNotes(ReturnMode.ALL);

notes.forEach(
(element) async => await helper.deleteNote(element));
await helper.deleteAllNotes();
},
),
SettingsTile(
Expand Down Expand Up @@ -284,6 +282,24 @@ class _SettingsPageState extends State<SettingsPage> {
},
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(
Expand Down Expand Up @@ -311,7 +327,8 @@ class _SettingsPageState extends State<SettingsPage> {

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);
}
Expand Down
90 changes: 90 additions & 0 deletions 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<SyncUrlEditor> {
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,
),
],
),
),
],
),
);
}
}

0 comments on commit ea257ec

Please sign in to comment.