8000 whole structure changes · noobcoder17/my_todo@b6df66c · GitHub
[go: up one dir, main page]

Skip to content

Commit b6df66c

Browse files
committed
whole structure changes
1 parent 23d9167 commit b6df66c

File tree

10 files changed

+382
-133
lines changed

10 files changed

+382
-133
lines changed

lib/data/storage.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ class StoreData {
3333
return file.readAsString();
3434
}
3535

36-
Future<File> postData(Map<String,dynamic> newJsonData) async {
36+
Future<File> addType(String newType) async {
37+
File dataFile = await getFile();
38+
print("Storing in existing file");
39+
String prevJsonData = await getData();
40+
Map<String,dynamic> data = jsonDecode(prevJsonData);
41+
data["types"].add(newType.toLowerCase());
42+
return dataFile.writeAsString(jsonEncode(data));
43+
}
44+
45+
Future<File> addTask(Map<String,dynamic> newJsonData) async {
3746
File dataFile = await getFile();
3847
print("Storing in existing file");
3948
String prevJsonData = await getData();
@@ -42,7 +51,7 @@ class StoreData {
4251
return dataFile.writeAsString(jsonEncode(data));
4352
}
4453

45-
Future<File> updateData(String key,Map<String,dynamic> newJsonData) async {
54+
Future<File> updateTask(String key,Map<String,dynamic> newJsonData) async {
4655
File dataFile = await getFile();
4756
String prevJsonData = await getData();
4857
Map<String,dynamic> data = jsonDecode(prevJsonData);
@@ -51,7 +60,7 @@ class StoreData {
5160
return dataFile.writeAsString(jsonEncode(data));
5261
}
5362

54-
Future<File> deleteData(String key) async{
63+
Future<File> deleteTask(String key) async{
5564
File dataFile = await getFile();
5665
String prevJsonData = await getData();
5766
Map<String,dynamic> data = jsonDecode(prevJsonData);

lib/main.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import './screens/add_task_screen.dart';
1212
//providers
1313
import './providers/tasks.dart';
1414
import './providers/user.dart';
15+
import 'package:my_todo/providers/home.dart';
1516

1617
void main(){
1718
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
@@ -26,19 +27,18 @@ class MyApp extends StatelessWidget {
2627
Widget build(BuildContext context) {
2728
return MultiProvider(
2829
providers: [
29-
ChangeNotifierProvider(create: (ctx)=>Tasks(),),
30-
ChangeNotifierProvider(create: (ctx)=>User(),),
30+
ChangeNotifierProvider(create: (ctx)=>HomeProvider(),),
3131
],
32-
child: Consumer<User>(
33-
builder: (context,userProvider,widget){
32+
child: Consumer<HomeProvider>(
33+
builder: (context,homeProvider,widget){
3434
return MaterialApp(
3535
debugShowCheckedModeBanner: false,
3636
title: 'My ToDo',
3737
theme: ThemeData(
3838
primarySwatch: Colors.blue,
3939
),
40-
home: userProvider.isNewUser? ToDo() : FutureBuilder(
41-
future: userProvider.tryToGetData(),
40+
home: homeProvider.isNewUser? ToDo() : FutureBuilder(
41+
future: homeProvider.tryToGetData(),
4242
builder: (context,result){
4343
if(result.connectionState == ConnectionState.waiting){
4444
return LoadingScreen();

lib/models/task.dart

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ import '../data/storage.dart';
55
class Task extends ChangeNotifier {
66
String id;
77
String name;
8+
String type;
89
bool isDone;
9-
bool isStar;
1010

1111
Task({
12-
this.id,
13-
this.name,
14-
this.isDone = false,
15-
this.isStar = false
12+
@required this.id,
13+
@required this.name,
14+
@required this.type,
15+
@required this.isDone,
1616
});
1717

1818
toJson(){
1919
return {
2020
'id' : id,
2121
'name' : name,
22+
'type' : type,
2223
'isDone' : isDone,
23-
'isStar' : isStar
2424
};
2525
}
2626

@@ -30,7 +30,7 @@ class Task extends ChangeNotifier {
3030
Map<String,dynamic> newJsonData = {
3131
this.id : this.toJson()
3232
};
33-
File success = await storage.updateData(this.id, newJsonData);
33+
File success = await storage.updateTask(this.id, newJsonData);
3434
if(success!=null){
3535
return true;
3636
}
@@ -57,21 +57,4 @@ class Task extends ChangeNotifier {
5757
}
5858
notifyListeners();
5959
}
60-
61-
Future<void> toggleStar() async{
62-
bool isStarPrev = isStar;
63-
isStar = !isStar;
64-
try{
65-
bool updateSuccess = await updateTask();
66-
if(updateSuccess){
67-
print("Task star updated");
68-
}else{
69-
throw updateSuccess;
70-
}
71-
}catch(e){
72-
isStar = isStarPrev;
73-
print("${this.id} Task star update failed");
74-
}
75-
notifyListeners();
76-
}
7760
}

lib/providers/home.dart

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import 'dart:io';
2+
import 'dart:convert';
3+
import 'package:flutter/foundation.dart';
4+
import '../data/storage.dart';
5+
6+
7+
//models
8+
import '../models/task.dart';
9+
import './tasks.dart';
10+
11+
class HomeProvider extends ChangeNotifier {
12+
StoreData storage = new StoreData();
13+
14+
15+
bool _isNewUser;
16+
String _userName;
17+
List<String> _types = [];
18+
List<TasksProvider> _taskProviders = [];
19+
20+
bool get isNewUser {
21+
return _isNewUser!=null;
22+
}
23+
24+
String get getUserName {
25+
return _userName;
26+
}
27+
28+
List<String> get getTypes{
29+
return [..._types];
30+
}
31+
32+
List<TasksProvider> get getTaskProviders {
33+
return [..._taskProviders];
34+
}
35+
36+
Future<bool> addTask() async {
37+
38+
}
39+
40+
41+
Future<void> initializeAllData() async {
42+
try{
43+
bool doesFileExits = await storage.fileExits();
44+
if(doesFileExits){
45+
String jsonData = await storage.getData();
46+
Map<String,dynamic> data = jsonDecode(jsonData);
47+
48+
//assigning data
49+
_userName = data['userName'];
50+
_types = data['types'];
51+
print("ok");
52+
List<TasksProvider> tempList = [];
53+
_types.forEach((type){
54+
Map<String,dynamic> _eachType = data['tasks'][type];
55+
TasksProvider _newTasksProvider = TasksProvider(type,_eachType);
56+
tempList.add(_newTasksProvider);
57+
});
58+
_taskProviders = tempList;
59+
}
60+
print("All initializatiion done");
61+
notifyListeners();
62+
}catch(e){
63+
print("Initializatiion failed");
64+
print(e);
65+
}
66+
}
67+
68+
Future<bool> addType(String type) async{
69+
try {
70+
File success = await storage.addType(type.toLowerCase());
71+
if(success!=null){
72+
print("$type Type added");
73+
_types.add(type.toLowerCase());
74+
notifyListeners();
75+
return true;
76+
}
77+
}catch(e){
78+
print("Type adding failed");
79+
print(e);
80+
return false;
81+
}
82+
return false;
83+
}
84+
85+
Future<bool> createNewUserData() async {
86+
Map<String,dynamic> newUserData = {
87+
"userName" : "Akash Debnath",
88+
"types" : ["common"],
89+
"tasks" : {
90+
"common" : {
91+
DateTime.now().toString() : Task(id: DateTime.now().toString(),type: "common",name: "Common 1",isDone: false)
92+
}
93+
}
94+
};
95+
try{
96+
File success = await storage.createNewUserFile(newUserData);
97+
if(success!=null){
98+
print("New User Created");
99+
_isNewUser = true;
100+
notifyListeners();
101+
return true;
102+
}
103+
}catch(e){
104+
print(e);
105+
print("New User Creation failed");
106+
return false;
107+
}
108+
return false;
109+
}
110+
111+
Future<bool> tryToGetData() async {
112+
bool doesUserExists = await storage.fileExits();
113+
if(doesUserExists==false){
114+
return false;
115+
}
116+
_isNewUser = true;
117+
notifyListeners();
118+
return true;
119+
}
120+
}

lib/providers/tasks.dart

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,44 @@
11
import 'dart:io';
2-
import 'dart:convert';
32
import 'package:flutter/foundation.dart';
43
import '../models/task.dart';
54

65
import '../data/storage.dart' ;
76

8-
class Tasks extends ChangeNotifier {
7+
class TasksProvider extends ChangeNotifier {
98
StoreData storage = new StoreData();
10-
List<String> _types = [];
9+
String _type;
1110
List<Task> _tasks = [];
1211

13-
14-
Future<bool> createNewUserData() async {
15-
Map<String,dynamic> newUserData = {
16-
"userName" : "Akash Debnath",
17-
"typesList" : {},
18-
"tasks" : {}
19-
};
20-
21-
try{
22-
File success = await storage.createNewUserFile(newUserData);
23-
if(success!=null){
24-
print("New User Created");
25-
return true;
26-
}
27-
}catch(e){
28-
print(e);
29-
print("New User Creation failed");
30-
return false;
31-
}
32-
return false;
12+
TasksProvider(String type,Map<String,dynamic> tasks){
13+
_type = type;
14+
Task tempTask;
15+
List<Task> tempList = [];
16+
tasks.forEach((key,data){
17+
tempTask = new Task(
18+
id: data['id'].toString(),
19+
name: data['name'].toString(),
20+
type: data['type'].toString(),
21+
isDone: data['isDone']
22+
);
23+
tempList.add(tempTask);
24+
});
25+
_tasks = tempList;
3326
}
3427

35-
36-
Future<void> initializeTaskList() async {
37-
try{
38-
bool doesFileExits = await storage.fileExits();
39-
if(doesFileExits){
40-
String jsonData = await storage.getData();
41-
Map<String,dynamic> data = jsonDecode(jsonData);
42-
List<Task> tempList = [];
43-
Task tempTask;
44-
data.forEach((key,data){
45-
tempTask = new Task(
46-
id: data['id'].toString(),
47-
name: data['name'].toString(),
48-
isStar: data['isStar'],
49-
isDone: data['isDone']
50-
);
51-
tempList.add(tempTask);
52-
});
53-
_tasks = tempList;
54-
}
55-
print("Task initializatiion done");
56-
notifyListeners();
57-
}catch(e){
58-
print("Task initializatiion failed");
59-
print(e);
60-
}
28+
String get getType{
29+
return _type;
6130
}
6231

6332
List<Task> get getTasks{
6433
return [..._tasks];
6534
}
6635

67-
6836
Future<void> addTask(Task newTask) async {
6937
Map<String,dynamic> newJsonData = {
7038
newTask.id : newTask.toJson()
7139
};
7240
try {
73-
File success = await storage.postData(newJsonData);
41+
File success = await storage.addTask(newJsonData);
7442
if(success!=null){
7543
print("Task added");
7644
_tasks.add(newTask);
@@ -83,7 +51,7 @@ class Tasks extends ChangeNotifier {
8351

8452
Future<void> removeTask(String key) async {
8553
try{
86-
await storage.deleteData(key);
54+
await storage.deleteTask(key);
8755
print("task deleted");
8856
_tasks.removeWhere((task)=>task.id==key);
8957
notifyListeners();

lib/providers/user.dart

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import 'package:flutter/foundation.dart';
2-
import '../data/storage.dart';
1+
// import 'package:flutter/foundation.dart';
2+
// import '../data/storage.dart';
33

4-
class User extends ChangeNotifier{
5-
StoreData _storeData = new StoreData();
6-
bool _isNewUser;
4+
// class UserProvider extends ChangeNotifier{
5+
// StoreData _storeData = new StoreData();
6+
// bool _isNewUser;
77

8-
bool get isNewUser {
9-
return _isNewUser!=null;
10-
}
8+
// bool get isNewUser {
9+
// return _isNewUser!=null;
10+
// }
1111

12-
Future<bool> tryToGetData() async {
13-
bool doesUserExists = await _storeData.fileExits();
14-
if(doesUserExists==false){
15-
return false;
16-
}
17-
_isNewUser = true;
18-
notifyListeners();
19-
return true;
20-
}
21-
}
12+
// Future<bool> tryToGetData() async {
13+
// bool doesUserExists = await _storeData.fileExits();
14+
// if(doesUserExists==false){
15+
// return false;
16+
// }
17+
// _isNewUser = true;
18+
// notifyListeners();
19+
// return true;
20+
// }
21+
// }

0 commit comments

Comments
 (0)
0