Skip to content

Commit

Permalink
add file base local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
sannysegue committed Jan 29, 2022
1 parent a7411f2 commit 92a11d5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
20 changes: 19 additions & 1 deletion lib/home.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_complete_guide/main.dart';
import 'package:flutter_complete_guide/transactions_repo.dart';
import 'package:flutter_complete_guide/widgets/custom_appbar.dart';
import 'package:flutter_complete_guide/widgets/header_wallet.dart';
import 'package:flutter_complete_guide/widgets/new_transaction.dart';
Expand All @@ -9,12 +10,15 @@ import 'package:flutter_complete_guide/widgets/transaction_record.dart';
import 'models/transaction.dart';

class MyHomePage extends StatefulWidget {
final TransactionsRepo repo;
MyHomePage({this.repo});

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
final List<Transaction> _userTransactions = [];
List<Transaction> _userTransactions = [];

void _addNewTransaction(
String txTitle,
Expand All @@ -31,6 +35,8 @@ class _MyHomePageState extends State<MyHomePage> {
setState(() {
_userTransactions.add(newTx);
});

widget.repo.saveTransactions(_userTransactions);
}

void _startAddNewTransaction(BuildContext ctx) {
Expand All @@ -49,6 +55,7 @@ class _MyHomePageState extends State<MyHomePage> {
void _deleteTransaction(String id) {
setState(() {
_userTransactions.removeWhere((tx) => tx.id == id);
widget.repo.saveTransactions(_userTransactions);
});
}

Expand All @@ -60,6 +67,17 @@ class _MyHomePageState extends State<MyHomePage> {
return balance;
}

@override
void initState() {
super.initState();
widget.repo.loadTransactions().then((value) {
setState(() {
_userTransactions.clear();
_userTransactions.addAll(value);
});
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down
3 changes: 2 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_complete_guide/transactions_repo.dart';

import 'home.dart';

Expand All @@ -14,7 +15,7 @@ class MyApp extends StatelessWidget {
fontFamily: 'Sanomat',
textTheme: ThemeData.light().textTheme,
),
home: MyHomePage(),
home: MyHomePage(repo: SharedPrefTransactionsRepo()),
);
}
}
Expand Down
16 changes: 16 additions & 0 deletions lib/models/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,20 @@ class Transaction {
String label,
this.isIncoming = false,
});


factory Transaction.fromJson(Map<String, dynamic> json) => Transaction(
id: json['id'],
title: json['title'],
amount: json['amount'],
date: DateTime.now());

Map<String, dynamic> toJson() => {
"id" : id,
"title": title,
"amount": amount,
"date": date.toString(),
"label" : "",
"isIncoming": false
};
}
49 changes: 49 additions & 0 deletions lib/transactions_repo.dart
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));
}
}
4 changes: 1 addition & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ dependencies:
flutter:
sdk: flutter
intl: ^0.15.8

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
flutter_svg: ^0.18.1
path_provider: ^1.5.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 92a11d5

Please sign in to comment.