@@ -22,12 +22,8 @@ class HomeProvider extends ChangeNotifier {
22
22
DocumentReference _userRef;
23
23
DocumentReference _currentWeek;
24
24
Location _location = Location ();
25
- Map <String ,dynamic > weather;
26
-
27
- // HomeProvider(){
28
- // _weekColRef = _firebaseFirestore.collection('users').doc(_uid).collection('week');
29
- // _userRef = _firebaseFirestore.collection('users').doc(_uid);
30
- // }
25
+ Map <String ,dynamic > _weather;
26
+ LocationData _locationData;
31
27
32
28
void update (User user){
33
29
print ('Updating user in home provider' );
@@ -36,6 +32,7 @@ class HomeProvider extends ChangeNotifier {
36
32
_weekColRef = _firebaseFirestore.collection ('users' ).doc (_uid).collection ('weeks' );
37
33
_userRef = _firebaseFirestore.collection ('users' ).doc (_uid);
38
34
}else {
35
+ _isInited = false ;
39
36
_uid = null ;
40
37
_appUser = null ;
41
38
_weekColRef = null ;
@@ -44,6 +41,10 @@ class HomeProvider extends ChangeNotifier {
44
41
notifyListeners ();
45
42
}
46
43
44
+ Map <String ,dynamic > get weather {
45
+ return _weather;
46
+ }
47
+
47
48
String get dailyTarget {
48
49
int target = _appUser.dailyTarget;
49
50
if (target< 1000 ){
@@ -71,6 +72,8 @@ class HomeProvider extends ChangeNotifier {
71
72
return consumed/ target;
72
73
}
73
74
75
+ AppUser get appUser => _appUser;
76
+
74
77
Future <void > init ()async {
75
78
if (_isInited== false ){
76
79
try {
@@ -88,17 +91,21 @@ class HomeProvider extends ChangeNotifier {
88
91
_weeklyData = WeeklyData .fromDoc (snapshot.data ());
89
92
}
90
93
_isInited = true ;
94
+ bool canGetLocation = await getLocationService ();
95
+ print (canGetLocation);
96
+ if (canGetLocation){
97
+ _locationData = await _location.getLocation ();
98
+ print (_locationData.latitude);
99
+ print (_locationData.longitude);
100
+ http.Response response = await http.get (
101
+ 'https://api.openweathermap.org/data/2.5/weather?lat=${_locationData .latitude }&lon=${_locationData .longitude }&appid=5c079888a15f3da50f160e44ce22723e&units=metric'
102
+ );
103
+ if (response.statusCode== 200 ){
104
+ final weatherInfo = jsonDecode (response.body);
105
+ _weather = weatherInfo['weather' ][0 ];
106
+ }
107
+ }
91
108
notifyListeners ();
92
- // LocationData _locationData = await _location.getLocation();
93
- // print(_locationData.latitude);
94
- // print(_locationData.longitude);
95
- // http.Response response = await http.get(
96
- // 'https://api.openweathermap.org/data/2.5/weather?lat=${_locationData.latitude}&lon=${_locationData.longitude}&appid=5c079888a15f3da50f160e44ce22723e&units=metric'
97
- // );
98
- // if(response.statusCode==200){
99
- // final weatherInfo = jsonDecode(response.body);
100
- // print(weatherInfo['main']);
101
- // }
102
109
}catch (e){
103
110
print (e);
104
111
}
@@ -107,49 +114,64 @@ class HomeProvider extends ChangeNotifier {
107
114
}
108
115
}
109
116
110
- Future <void > addWater (int amount) async {
117
+ Future <void > addWater (int amount, DateTime time ) async {
111
118
try {
112
- int weekday = DateTime .now ().weekday;
119
+ int weekday = time.weekday;
120
+ int week = getWeek (time);
121
+ String weekId = '${time .year }_$week ' ;
113
122
_firebaseFirestore.runTransaction ((transaction)async {
114
- DocumentReference yearDocRef = _firebaseFirestore.collection ('users' ).doc (_uid).collection ('years' ).doc ('${_today .year }' );
115
- DocumentReference monthDocRef = _firebaseFirestore.collection ('users' ).doc (_uid).collection ('months' ).doc ('${_today .year }_${_today .month }' );
123
+ DocumentReference weekDocRef = _firebaseFirestore.collection ('users' ).doc (_uid).collection ('weeks' ).doc (weekId);
124
+ DocumentReference yearDocRef = _firebaseFirestore.collection ('users' ).doc (_uid).collection ('years' ).doc ('${time .year }' );
125
+ DocumentReference monthDocRef = _firebaseFirestore.collection ('users' ).doc (_uid).collection ('months' ).doc ('${time .year }_${time .month }' );
116
126
DocumentSnapshot yearDocSnap = await transaction.get (yearDocRef);
117
127
DocumentSnapshot monthDocSnap = await transaction.get (monthDocRef);
128
+ DocumentSnapshot weekDocSnap = await transaction.get (weekDocRef);
118
129
119
130
if (! yearDocSnap.exists){
120
131
transaction.set (yearDocRef, {
121
- 'year' : _today .year
132
+ 'year' : time .year
122
133
},SetOptions (merge: true ));
123
134
}
124
135
125
136
if (! monthDocSnap.exists){
126
137
transaction.set (monthDocRef, {
127
- 'year' : _today.year,
128
- 'month' : _today.month
138
+ 'year' : time.year,
139
+ 'month' : time.month
140
+ },SetOptions (merge: true ));
141
+ }
142
+
143
+ if (! weekDocSnap.exists){
144
+ transaction.set (weekDocRef, {
145
+ 'year' : time.year,
146
+ 'month' : time.month,
147
+ 'week' : week,
148
+ 'id' : weekId
129
149
},SetOptions (merge: true ));
130
150
}
131
151
132
152
transaction.update (yearDocRef, {
133
- 'amounts.${_today .month }' : FieldValue .increment (amount)
153
+ 'amounts.${time .month }' : FieldValue .increment (amount)
134
154
});
135
155
136
156
transaction.update (monthDocRef, {
137
- 'amounts.${_today .day }' : FieldValue .increment (amount)
157
+ 'amounts.${time .day }' : FieldValue .increment (amount)
138
158
});
139
- transaction.update (_currentWeek , {
159
+ transaction.update (weekDocRef , {
140
160
'amounts.$weekday ' : FieldValue .increment (amount)
141
161
});
142
162
143
163
});
144
- _weeklyData.amounts[weekday.toString ()] += amount;
145
- notifyListeners ();
164
+ if (_weeklyData.id== weekId){
165
+ _weeklyData.amounts[weekday.toString ()] += amount;
166
+ notifyListeners ();
167
+ }
146
168
}catch (e){
147
169
print (e);
148
170
}
149
171
150
172
}
151
173
152
- Future <void > getLocation ()async {
174
+ Future <bool > getLocationService ()async {
153
175
bool isServiceEnabled = await _location.serviceEnabled ();
154
176
print (isServiceEnabled);
155
177
@@ -158,19 +180,29 @@ class HomeProvider extends ChangeNotifier {
158
180
if (_enabled) {
159
181
print ('Service is enabled now' );
160
182
}else {
161
- return ;
183
+ return false ;
162
184
}
163
185
}
164
- print ('Service is already enables' );
165
186
166
187
PermissionStatus permissionGranted = await _location.hasPermission ();
167
188
if (permissionGranted == PermissionStatus .denied) {
168
189
PermissionStatus _isGranted = await _location.requestPermission ();
169
190
if (_isGranted != PermissionStatus .granted) {
170
- return ;
191
+ return false ;
171
192
}
172
193
}
173
- print ( 'Ok' ) ;
194
+ return true ;
174
195
}
196
+
197
+ Future <void > updateUser (AppUser appUser)async {
198
+ try {
199
+ print (appUser.toDoc ());
200
+ await _userRef.update (appUser.toDoc ());
201
+ _appUser = appUser;
202
+ notifyListeners ();
203
+ }catch (e){
204
+ print (e);
205
+ }
206
+ }
175
207
}
176
208
0 commit comments