-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7411f2
commit 92a11d5
Showing
5 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:path_provider/path_provider.dart'; | ||
|
||
import 'models/transaction.dart'; | ||
|
||
abstract class TransactionsRepo { | ||
Future<List<Transaction>> loadTransactions(); | ||
void saveTransactions(List<Transaction> trans); | ||
} | ||
|
||
class SharedPrefTransactionsRepo extends TransactionsRepo { | ||
|
||
Future<String> get _localPath async { | ||
final directory = await getApplicationDocumentsDirectory(); | ||
return directory.path; | ||
} | ||
|
||
Future<File> get _localFile async { | ||
final path = await _localPath; | ||
return File("$path/transactions.json"); | ||
} | ||
|
||
@override | ||
Future<List<Transaction>> loadTransactions() async { | ||
try { | ||
final file = await _localFile; | ||
final String content = await file.readAsString(); | ||
final decode = json.decode(content); | ||
|
||
final List<Transaction> trans = []; | ||
decode.forEach((element) { | ||
trans.add(Transaction.fromJson(element)); | ||
}); | ||
|
||
return trans; | ||
} catch (e) { | ||
return List.empty(); | ||
} | ||
} | ||
|
||
@override | ||
void saveTransactions(List<Transaction> trans) async { | ||
final file = await _localFile; | ||
file.writeAsString(jsonEncode(trans)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters