Skip to content
This repository was archived by the owner on Dec 17, 2023. It is now read-only.

Commit c11401c

Browse files
committed
reworked Favorites
1 parent 24ad01c commit c11401c

File tree

6 files changed

+142
-114
lines changed

6 files changed

+142
-114
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ App with list of virtual tables for MHD in Bratislava.
55
## TODO
66

77
Main
8+
89
- ios version
910

1011
Add nearme
@@ -53,4 +54,4 @@ Final tuches
5354
- agree with gmaps rights ✔
5455
- agree with licence ✔
5556
- cloud save favourites like duckduckgo
56-
57+
- rework AllStops favorites ✔

lib/AllStops.dart

Lines changed: 89 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:developer';
2+
13
import 'package:flutter/material.dart';
24
import 'package:diacritic/diacritic.dart';
35
import 'package:draggable_scrollbar/draggable_scrollbar.dart';
@@ -20,23 +22,28 @@ class AllStopsPage extends StatefulWidget {
2022
class _AllStopsState extends State<AllStopsPage> {
2123
List<Stop> stops = List<Stop>();
2224
List<Stop> saved = List<Stop>();
25+
List<Stop> favorites = List<Stop>();
2326
List<Stop> _stopsForDisplay = List<Stop>();
2427
List<Stop> _savedForDisplay = List<Stop>();
28+
List<Stop> _favoritesForDisplay = List<Stop>();
2529
File stopsFile;
2630
File savedFile;
31+
File favoritesFile;
2732
Directory dir;
2833
String stopsFileName = 'stops.json';
2934
String savedFileName = 'saved.json';
35+
String favoritesFileName = 'favorites.json';
3036
bool stopsExists = false;
3137
bool savedExists = false;
38+
bool favoritesExists = false;
3239
bool _isLoading = true;
3340
bool _networkStatus = false;
3441

3542
final ScrollController _scrollController = ScrollController();
3643
final TextEditingController _textController = TextEditingController();
3744

3845
Future<List<Stop>> fetchStops() async {
39-
var url = 'https://api.magicsk.eu/stops2';
46+
var url = 'https://api.magicsk.eu/stops';
4047
var response = await http.get(url);
4148

4249
var _stops = List<Stop>();
@@ -70,18 +77,33 @@ class _AllStopsState extends State<AllStopsPage> {
7077
savedExists = savedFile.existsSync();
7178
if (savedExists) {
7279
print('saved.json exists');
73-
var _saved = List<Stop>();
74-
var savedFileJson = json.decode((savedFile.readAsStringSync()));
75-
for (var saveFileJson in savedFileJson) {
76-
_saved.add(Stop.fromJson(saveFileJson));
80+
savedFile.delete();
81+
print('saved.json deleted');
82+
}
83+
favoritesFile = new File(dir.path + '/' + favoritesFileName);
84+
favoritesExists = favoritesFile.existsSync();
85+
if (favoritesExists) {
86+
print('favorites.json exists');
87+
var _favorites = List<Stop>();
88+
var favoritesFileJson = json.decode((favoritesFile.readAsStringSync()));
89+
for (var favoritesFileJson in favoritesFileJson) {
90+
_favorites.add(Stop.fromJson(favoritesFileJson));
91+
}
92+
favorites.addAll(_favorites);
93+
print('favorites loaded');
94+
// favorites.sort((a, b) {
95+
// return a.name.toLowerCase().compareTo(b.name.toLowerCase());
96+
// });
97+
int i, s;
98+
for (i = 0; i < favorites.length; i++){
99+
for (s = 0; s < stops.length; s++){
100+
if (favorites[i].id == stops[s].id) {
101+
_favoritesForDisplay.add(stops[s]);
102+
}
103+
}
77104
}
78-
saved.addAll(_saved);
79-
print('saved loaded');
80-
saved.sort((a, b) {
81-
return a.name.toLowerCase().compareTo(b.name.toLowerCase());
82-
});
83105
setState(() {
84-
_savedForDisplay = saved;
106+
_favoritesForDisplay = favorites;
85107
});
86108
}
87109
stopsFile = new File(dir.path + '/' + stopsFileName);
@@ -95,66 +117,66 @@ class _AllStopsState extends State<AllStopsPage> {
95117
}
96118
stops.addAll(_stops);
97119
int i, s;
98-
for (s = 0; s < saved.length; s++) {
99-
for (i = 0; i < stops.length; i++) {
100-
if (saved[s].name == stops[i].name) {
101-
stops.remove(stops[i]);
120+
for (i = 0; i < favorites.length; i++) {
121+
for (s = 0; s < stops.length; s++) {
122+
if (favorites[i].id == stops[s].id) {
123+
stops.remove(stops[s]);
102124
}
103125
}
104126
}
105127
print('stops cleared');
106-
stops.sort((a, b) {
107-
return a.name.toLowerCase().compareTo(b.name.toLowerCase());
108-
});
109-
print('stops sorted');
128+
// stops.sort((a, b) {
129+
// return a.name.toLowerCase().compareTo(b.name.toLowerCase());
130+
// });
131+
// print('stops sorted');
110132
setState(() {
111133
_stopsForDisplay = stops;
112134
_isLoading = false;
113135
});
114-
} else {
115-
checkNetworkStatus().then((status) {
116-
if (status) {
117-
fetchStops().then((value) {
118-
setState(() {
119-
stops.addAll(value);
120-
getApplicationDocumentsDirectory().then((Directory directory) {
121-
File file = new File(directory.path + "/" + stopsFileName);
122-
file.createSync();
123-
file.writeAsStringSync(json.encode(stops));
124-
print('stops saved');
125-
int i, s;
126-
for (s = 0; s < saved.length; s++) {
127-
for (i = 0; i < stops.length; i++) {
128-
if (saved[s].name == stops[i].name) {
129-
stops.remove(stops[i]);
130-
}
131-
}
136+
}
137+
checkNetworkStatus().then((status) {
138+
if (status) {
139+
fetchStops().then((value) {
140+
setState(() {
141+
stops.removeRange(0, stops.length);
142+
stops.addAll(value);
143+
int i, s;
144+
for (i = 0; i < favorites.length; i++) {
145+
for (s = 0; s < stops.length; s++) {
146+
if (favorites[i].id == stops[s].id) {
147+
stops.remove(stops[s]);
132148
}
133-
print('stops cleared');
134-
stops.sort((a, b) {
135-
return a.name.toLowerCase().compareTo(b.name.toLowerCase());
136-
});
137-
print('stops sorted');
138-
setState(() {
139-
_stopsForDisplay = stops;
140-
_isLoading = false;
141-
});
142-
});
149+
}
150+
}
151+
print('stops cleared');
152+
getApplicationDocumentsDirectory().then((Directory dir) {
153+
File file = new File(dir.path + "/" + stopsFileName);
154+
file.createSync();
155+
file.writeAsStringSync(json.encode(stops));
156+
print('stops saved');
157+
});
158+
// stops.sort((a, b) {
159+
// return a.name.toLowerCase().compareTo(b.name.toLowerCase());
160+
// });
161+
// print('stops sorted');
162+
setState(() {
163+
_stopsForDisplay = stops;
164+
_isLoading = false;
143165
});
144166
});
145-
}
146-
});
147-
}
167+
});
168+
}
169+
});
148170
});
149171
});
150172
super.initState();
151173
}
152174

153175
File createFile() {
154-
File file = new File(dir.path + "/" + savedFileName);
176+
File file = File(dir.path + "/" + favoritesFileName);
155177
file.createSync();
156-
file.writeAsStringSync(json.encode(saved));
157-
print('saved');
178+
file.writeAsStringSync(json.encode(favorites));
179+
print('favorites created');
158180
return file;
159181
}
160182

@@ -191,9 +213,9 @@ class _AllStopsState extends State<AllStopsPage> {
191213
child: ListView.builder(
192214
controller: _scrollController,
193215
scrollDirection: Axis.vertical,
194-
itemCount: _savedForDisplay.length + _stopsForDisplay.length,
216+
itemCount: _favoritesForDisplay.length + _stopsForDisplay.length,
195217
itemBuilder: (context, index) {
196-
return index < _savedForDisplay.length ? _listSavedItem(index) : _listItem(index);
218+
return index < _favoritesForDisplay.length ? _listFavoritesItem(index) : _listItem(index);
197219
},
198220
))),
199221
);
@@ -221,7 +243,7 @@ class _AllStopsState extends State<AllStopsPage> {
221243
var stopName = removeDiacritics(stop.name).toLowerCase();
222244
return stopName.contains(text);
223245
}).toList();
224-
_savedForDisplay = saved.where((stop) {
246+
_favoritesForDisplay = favorites.where((stop) {
225247
var stopName = removeDiacritics(stop.name).toLowerCase();
226248
return stopName.contains(text);
227249
}).toList();
@@ -241,7 +263,7 @@ class _AllStopsState extends State<AllStopsPage> {
241263
var stopName = removeDiacritics(stop.name).toLowerCase();
242264
return stopName.contains(text);
243265
}).toList();
244-
_savedForDisplay = saved.where((stop) {
266+
_favoritesForDisplay = favorites.where((stop) {
245267
var stopName = removeDiacritics(stop.name).toLowerCase();
246268
return stopName.contains(text);
247269
}).toList();
@@ -252,7 +274,7 @@ class _AllStopsState extends State<AllStopsPage> {
252274
}
253275

254276
_listItem(index) {
255-
index = index - _savedForDisplay.length;
277+
index = index - _favoritesForDisplay.length;
256278
return FlatButton(
257279
onPressed: () {
258280
Navigator.push(context, MaterialPageRoute(builder: (context) => StopWebView(_stopsForDisplay[index])));
@@ -276,17 +298,17 @@ class _AllStopsState extends State<AllStopsPage> {
276298
icon: Icon(Icons.star_border),
277299
onPressed: () {
278300
setState(() {
279-
saved.add(_stopsForDisplay[index]);
301+
favorites.add(_stopsForDisplay[index]);
280302
stops.remove(_stopsForDisplay[index]);
281-
saved.sort((a, b) {
303+
favorites.sort((a, b) {
282304
return a.name.toLowerCase().compareTo(b.name.toLowerCase());
283305
});
284306
var text = _textController.text;
285307
_stopsForDisplay = stops.where((stop) {
286308
var stopName = removeDiacritics(stop.name).toLowerCase();
287309
return stopName.contains(text);
288310
}).toList();
289-
_savedForDisplay = saved.where((stop) {
311+
_favoritesForDisplay = favorites.where((stop) {
290312
var stopName = removeDiacritics(stop.name).toLowerCase();
291313
return stopName.contains(text);
292314
}).toList();
@@ -305,10 +327,10 @@ class _AllStopsState extends State<AllStopsPage> {
305327
));
306328
}
307329

308-
_listSavedItem(index) {
330+
_listFavoritesItem(index) {
309331
return FlatButton(
310332
onPressed: () {
311-
Navigator.push(context, MaterialPageRoute(builder: (context) => StopWebView(_savedForDisplay[index])));
333+
Navigator.push(context, MaterialPageRoute(builder: (context) => StopWebView(_favoritesForDisplay[index])));
312334
},
313335
child: Column(
314336
children: <Widget>[
@@ -321,7 +343,7 @@ class _AllStopsState extends State<AllStopsPage> {
321343
Padding(
322344
padding: const EdgeInsets.only(left: 16.0),
323345
child: Text(
324-
_savedForDisplay[index].name,
346+
_favoritesForDisplay[index].name,
325347
style: TextStyle(fontSize: 17.5, fontWeight: FontWeight.normal),
326348
),
327349
),
@@ -334,8 +356,8 @@ class _AllStopsState extends State<AllStopsPage> {
334356
color: Colors.yellow[800],
335357
onPressed: () {
336358
setState(() {
337-
stops.add(_savedForDisplay[index]);
338-
saved.remove(_savedForDisplay[index]);
359+
stops.add(_favoritesForDisplay[index]);
360+
favorites.remove(_favoritesForDisplay[index]);
339361
stops.sort((a, b) {
340362
return a.name.toLowerCase().compareTo(b.name.toLowerCase());
341363
});
@@ -344,7 +366,7 @@ class _AllStopsState extends State<AllStopsPage> {
344366
var stopName = removeDiacritics(stop.name).toLowerCase();
345367
return stopName.contains(text);
346368
}).toList();
347-
_savedForDisplay = saved.where((stop) {
369+
_favoritesForDisplay = favorites.where((stop) {
348370
var stopName = removeDiacritics(stop.name).toLowerCase();
349371
return stopName.contains(text);
350372
}).toList();

lib/TripPlanner.dart

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ class _TripPlannerState extends State<TripPlannerPage> {
3030
bool _isSearched = false;
3131
List<Stop> stops = List<Stop>();
3232
File stopsFile;
33-
File savedFile;
33+
File favoritesFile;
3434
Directory dir;
3535
String stopsFileName = 'stops.json';
36-
String savedFileName = 'saved.json';
36+
String favoritesFileName = 'favorites.json';
3737
String _selectedFromStop;
3838
String _selectedToStop;
3939
String _fromHint = "From...";
4040
String arrivalDeparatureText = '&departure_time=';
4141
String arrivalDeparatureTime = DateTime.now().millisecondsSinceEpoch.toString().replaceAll(RegExp(r'\d(\d{0,2}$)'), '');
4242
bool arrivalDeparature = false;
4343
bool stopsExists = false;
44-
bool savedExists = false;
44+
bool favoritesExists = false;
4545
bool _typeError = false;
4646
bool _networkError = false;
4747
bool _isSearching = false;
@@ -72,21 +72,21 @@ class _TripPlannerState extends State<TripPlannerPage> {
7272
getApplicationDocumentsDirectory().then((Directory directory) {
7373
dir = directory;
7474
stopsFile = new File(dir.path + '/' + stopsFileName);
75-
savedFile = new File(dir.path + '/' + savedFileName);
75+
favoritesFile = new File(dir.path + '/' + favoritesFileName);
7676
stopsExists = stopsFile.existsSync();
77-
savedExists = savedFile.existsSync();
78-
var _saved = List<Stop>();
79-
if (savedExists) {
80-
print('saved.json exists');
81-
var savedFileJson = json.decode((savedFile.readAsStringSync()));
82-
for (var saveFileJson in savedFileJson) {
83-
_saved.add(Stop.fromJson(saveFileJson));
77+
favoritesExists = favoritesFile.existsSync();
78+
var _favorites = List<Stop>();
79+
if (favoritesExists) {
80+
print('favorites.json exists');
81+
var favoritesFileJson = json.decode((favoritesFile.readAsStringSync()));
82+
for (var saveFileJson in favoritesFileJson) {
83+
_favorites.add(Stop.fromJson(saveFileJson));
8484
}
85-
_saved.sort((a, b) {
85+
_favorites.sort((a, b) {
8686
return a.name.toLowerCase().compareTo(b.name.toLowerCase());
8787
});
88-
stops.addAll(_saved);
89-
print('saved loaded');
88+
stops.addAll(_favorites);
89+
print('favorites loaded');
9090
}
9191
if (stopsExists) {
9292
var _stops = List<Stop>();
@@ -98,9 +98,9 @@ class _TripPlannerState extends State<TripPlannerPage> {
9898
return a.name.toLowerCase().compareTo(b.name.toLowerCase());
9999
});
100100
int i, s;
101-
for (s = 0; s < _saved.length; s++) {
101+
for (s = 0; s < _favorites.length; s++) {
102102
for (i = 0; i < _stops.length; i++) {
103-
if (_saved[s].name == _stops[i].name) {
103+
if (_favorites[s].name == _stops[i].name) {
104104
_stops.remove(_stops[i]);
105105
}
106106
}
@@ -393,7 +393,7 @@ class _TripPlannerState extends State<TripPlannerPage> {
393393
)),
394394
),
395395
title: Container(
396-
child: (isItStop(_toTextController.text))
396+
child: (!isItStop(_toTextController.text))
397397
? Text("Prejdite na zastávku ${_toTextController.text}. (${details["duration"]["text"]})", style: TextStyle(fontSize: 14))
398398
: Text(
399399
last

0 commit comments

Comments
 (0)