Skip to content

Commit 4a25b4e

Browse files
committed
saving progress. Switching to files. Lots of work ahead :)
1 parent 69986b2 commit 4a25b4e

File tree

4 files changed

+68
-32
lines changed

4 files changed

+68
-32
lines changed

lib/HomePage.dart

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import 'package:flutter/material.dart';
2-
import 'package:shared_preferences/shared_preferences.dart';
32
import 'dart:io';
43
import 'package:flutter_string_encryption/flutter_string_encryption.dart';
54
import 'dart:async';
65
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
76
import 'package:path_provider/path_provider.dart';
7+
import 'package:shared_preferences/shared_preferences.dart'; // TODO get rid of this
88

99
// TODO: encrypt notes and their titles
1010
// decrypt all titles (seperate file), to build the listView
@@ -31,7 +31,7 @@ class HomePage extends StatefulWidget {
3131
}
3232

3333
class HomePageState extends State<HomePage> {
34-
// DECLARATIONS
34+
3535
final GlobalKey<ScaffoldState> _scaffoldState = new GlobalKey<ScaffoldState>();
3636

3737
final _storage = new FlutterSecureStorage(); // to securely store the salt
@@ -55,9 +55,11 @@ class HomePageState extends State<HomePage> {
5555
debugPrint(_generatedKey);
5656
}
5757

58-
void _loadIDsFromMemory() async {
59-
SharedPreferences prefs = await SharedPreferences.getInstance();
60-
setState( () => _noteIDs = new Set.from(prefs.getStringList("noteIDs")) ?? new Set());
58+
void _loadIDsFromMemory() async { // TODO fix this!!!!! test this!!!!!
59+
String _noteIDsString = await readTitles();
60+
String separator = "\$";
61+
List<String> _noteIDsList = _noteIDsString.split(separator);
62+
setState( () => _noteIDs = new Set.from(_noteIDsList) ?? new Set());
6163
}
6264

6365
@override
@@ -116,7 +118,7 @@ class HomePageState extends State<HomePage> {
116118
new ListTile(
117119
leading: new Icon(Icons.library_books),
118120
title: new Text("aj"),
119-
onTap: () => Navigator.of(context).pushNamed("/noteEditor/aj"),
121+
onTap: readTitles,
120122
),
121123
new ListTile(
122124
leading: new Icon(Icons.exit_to_app),
@@ -128,32 +130,29 @@ class HomePageState extends State<HomePage> {
128130
);
129131
}
130132

131-
Future<String> get _localPath async {
132-
final directory = await getApplicationDocumentsDirectory();
133-
return directory.path;
134-
}
135-
136-
Future<File> get _titleFile async {
137-
final path = await _localPath;
138-
return new File('$path/note_titles.txt');
139-
}
140-
141-
Future<File> writeTitles(String titles) async {
142-
final file = await _titleFile;
143-
return file.writeAsString('$titles'); // Write the file
133+
void writeTitles(String titles) async {
134+
final path = (await getApplicationDocumentsDirectory()).path;
135+
final file = new File('$path/note_titles.txt');
136+
file.writeAsString('$titles'); // Write the file
144137
}
145138

146-
Future<int> readCounter() async {
139+
Future<String> readTitles() async {
147140
try {
148-
final file = await _titleFile;
141+
final path = (await getApplicationDocumentsDirectory()).path;
142+
final file = new File('$path/note_titles.txt');
149143
String contents = await file.readAsString(); // Read the file
150-
return int.parse(contents);
144+
debugPrint("he");
145+
debugPrint(contents);
146+
return contents;
151147
}
152-
catch (e) {
153-
return 0; // If we encounter an error, return 0
148+
catch (e) { // No file yet
149+
debugPrint("er");
150+
return ""; // If we encounter an error, return empty str
154151
}
155152
}
156153

154+
155+
157156

158157

159158
}

lib/LoginPage.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
33
import 'package:flutter_string_encryption/flutter_string_encryption.dart';
4-
import 'package:shared_preferences/shared_preferences.dart';
54
import 'package:crypt/crypt.dart';
65

76
// TODO: animated feedback when decrypting (spinning loader or something)

lib/NoteEditor.dart

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import 'package:flutter/material.dart';
2-
import 'package:shared_preferences/shared_preferences.dart';
2+
import 'dart:async';
3+
import 'dart:io';
4+
import 'package:path_provider/path_provider.dart';
5+
6+
// TODO: make a separate file for a class Note, with ID, encryptedTitle, encryptedContent
7+
// TODO: and methods to decrypt them using a supplied password
38

49
class NoteEditor extends StatefulWidget {
510
final String id;
@@ -24,7 +29,6 @@ class NoteEditorState extends State<NoteEditor> {
2429
static String _note;
2530
bool _editorMode = false;
2631

27-
2832
TextEditingController _textController = new TextEditingController(text: _note);
2933

3034
@override
@@ -34,8 +38,8 @@ class NoteEditorState extends State<NoteEditor> {
3438
}
3539

3640
void _loadNoteFromMemory() async {
37-
SharedPreferences prefs = await SharedPreferences.getInstance();
38-
setState( () => _note = prefs.getString(id) ?? "An error occurred. ID not found." );
41+
String dummy = await readNote(id); // dummy needed to await
42+
setState( () => _note = dummy);
3943
_textController.text = _note;
4044
}
4145

@@ -91,8 +95,42 @@ class NoteEditorState extends State<NoteEditor> {
9195
}
9296

9397
void _writeNoteToMemory(String value) async {
94-
SharedPreferences prefs = await SharedPreferences.getInstance();
95-
setState( () => prefs.setString(id, value));
98+
bool succeeded = await writeNote(id, value);
99+
if (succeeded) {
100+
debugPrint("written!");
101+
}
96102
}
97103

104+
// TODO write titles? set?
105+
106+
Future<bool> writeNote(String noteID, String noteContent) async {
107+
final path = (await getApplicationDocumentsDirectory()).path;
108+
final file = new File('$path/${noteID}.txt');
109+
try {
110+
file.writeAsString('$noteContent'); // Write the file
111+
return true;
112+
}
113+
catch (e) {
114+
debugPrint("damn :(");
115+
return false;
116+
}
117+
}
118+
119+
Future<String> readNote(String noteID) async {
120+
try {
121+
final path = (await getApplicationDocumentsDirectory()).path;
122+
final file = new File('$path/${noteID}.txt');
123+
String contents = await file.readAsString(); // Read the file
124+
debugPrint("he");
125+
debugPrint(contents);
126+
return contents;
127+
}
128+
catch (e) { // No file yet
129+
//writeNote(noteTitle, "");
130+
debugPrint("er");
131+
return ""; // If we encounter an error, return empty str
132+
}
133+
}
134+
135+
98136
}

lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'LoginPage.dart';
55
import 'NoteEditor.dart';
66

77
void main() => runApp(new MaterialApp(
8-
home: new LoginPage(),
8+
home: new HomePage(),
99
onGenerateRoute: (RouteSettings settings) {
1010
if (settings.name == "/"){
1111
return new CustomRoute(

0 commit comments

Comments
 (0)