Skip to content

Commit

Permalink
database reads
Browse files Browse the repository at this point in the history
  • Loading branch information
dltompki committed Apr 24, 2020
1 parent 86f0b68 commit 2b3ea1f
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 49 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added contact_tracer/assets/covid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 22 additions & 17 deletions contact_tracer/lib/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,35 @@ class Event {
final TimeOfDay time;
final Coordinates location;
final String locationName;
int id;

Utility util = Utility();

/// Default contruction for when event is created by the user
Event(Coordinates location, String locationName, String formattedPeople, DateTime date, TimeOfDay time)
Event(Coordinates location, String locationName, String formattedPeople,
DateTime date, TimeOfDay time)
: this.location = location,
this.formattedPeople = formattedPeople,
this.date = date,
this.time = time,
this.locationName = locationName {
people = formattedPeople.split(AddEvent.personDelimiter);
for (var i = 0; i < people.length; i++) {
people[i] = people[i].trim();
}
}
people = formattedPeople.split(AddEvent.personDelimiter);
for (var i = 0; i < people.length; i++) {
people[i] = people[i].trim();
}
}

/// Parsing constructor for importing from the database accoring to the format established by [toMap]
// Event.parse(Map<String, dynamic> map)
// : this.location = map['location'],
// this.people = map['person'], /// TODO this isn't right
// this.date = DateTime.parse(map['date']),
// this.time = TimeOfDay(
// hour: int.parse(map['hour']), minute: int.parse(map['minute'])),
// this.locationName = map['locationName'];
Event.parse(Map<String, dynamic> map)
: this.location = Coordinates(map['latitude'], map['longitude']),
this.id = map['id'],
this.locationName = map['locationName'],
this.formattedPeople = map['people'],
this.date = DateTime.parse(map['date']),
this.time = TimeOfDay(
hour: map['hour'],
minute: map['minute'],
);

String get formatDate => Utility.formatDate(date);
String get formatTime => Utility.formatTime(time);
Expand All @@ -44,12 +49,12 @@ class Event {
List<Map<String, dynamic>> output = [];
people.forEach((person) {
output.add({
'id' : id,
'id': id,
'latitude': location.latitude,
'longitude' : location.longitude,
'locationName' : locationName,
'longitude': location.longitude,
'locationName': locationName,
'people': formattedPeople,
'person' : person,
'person': person,
'date': date.toIso8601String(),
'hour': time.hour,
'minute': time.minute,
Expand Down
28 changes: 28 additions & 0 deletions contact_tracer/lib/eventDatabase.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'event.dart';

class EventDatabase {
final Future<Database> database;
Expand Down Expand Up @@ -40,4 +41,31 @@ class EventDatabase {
event,
);
}

Future<List<Event>> getHomelistData() async {
/// Get database refrence
final Database db = await database;

/// Get all the maps in the events table
final List<Map<String, dynamic>> maps = await db.query('events');

/// Parse maps into events
final List<Event> unfilteredEvents = List.generate(maps.length, (index) {
return Event.parse(maps[index]);
});

/// Define list of events we will return
List<Event> filteredEvents = [];
/// Define list of ids that have already been entered into the filteredEvents list
List<int> ids = [];

unfilteredEvents.forEach((e) {
if(!ids.contains(e.id)) {
ids.add(e.id);
filteredEvents.add(e);
}
});

return filteredEvents;
}
}
71 changes: 39 additions & 32 deletions contact_tracer/lib/homeList.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ class HomeList extends StatefulWidget {
}

class _HomeListState extends State<HomeList> {
/// Default [eventList] for testing
List<Event> eventList = [
Event(Coordinates(51.5079, 0.0877), 'London Bridge', 'The Queen',
DateTime(2003, 7, 8), TimeOfDay(hour: 15, minute: 0)),
];

EventDatabase db = new EventDatabase();

List<String> _getPeople() {
List<String> _getPeople(List<Event> eventList) {
var people = List<String>();
eventList.forEach((event) {
people.add(event.formattedPeople);
Expand All @@ -45,19 +39,16 @@ class _HomeListState extends State<HomeList> {
maps.forEach((map) {
db.insertEvent(map);
});

setState(() {
eventList.add(e);
});
setState(() {});
}

/// Opens the [AddEvent] screen, passing the [addEventToList] function to enable [Event]s to be sent back to the [HomeList]
void _pushAddEvent() {
void _pushAddEvent(List<Event> eventList) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return new AddEvent(
people: _getPeople(),
people: _getPeople(eventList),
addEventToDatabase: addEventToDatabase,
);
},
Expand Down Expand Up @@ -96,26 +87,42 @@ class _HomeListState extends State<HomeList> {
return rows;
}

return Scaffold(
appBar: AppBar(
title: Text('Contact Tracer Events'),
actions: <Widget>[
Container(
padding: EdgeInsets.all(8),
child: RaisedButton(
onPressed: _pushAddEvent,
color: accentColor,
child: Icon(
Icons.add,
color: Colors.black,
),
return FutureBuilder(
future: db.getHomelistData(),
builder: (BuildContext context, AsyncSnapshot<List<Event>> snapshot) {
if (snapshot.hasData) {
return Scaffold(
appBar: AppBar(
title: Text('Contact Tracer Events'),
actions: <Widget>[
Container(
padding: EdgeInsets.all(8),
child: RaisedButton(
onPressed: () {
_pushAddEvent(snapshot.data);
},
color: accentColor,
child: Icon(
Icons.add,
color: Colors.black,
),
),
),
],
),
body: ListView(
children: _buildRows(snapshot.data),
),
);
} else {
return Scaffold(
appBar: AppBar(
title: Text('Contact Tracer Events'),
),
),
],
),
body: ListView(
children: _buildRows(eventList),
),
body: CircularProgressIndicator(),
);
}
},
);
}
}
14 changes: 14 additions & 0 deletions contact_tracer/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_launcher_icons:
dependency: "direct main"
description:
name: flutter_launcher_icons
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.4"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
Expand Down Expand Up @@ -308,6 +315,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.6.1"
yaml:
dependency: transitive
description:
name: yaml
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
sdks:
dart: ">=2.7.0 <3.0.0"
flutter: ">=1.12.13+hotfix.5 <2.0.0"
8 changes: 8 additions & 0 deletions contact_tracer/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies:
search_map_place: ^0.3.0
firebase_remote_config: ^0.3.0+3
sqflite: ^1.3.0
flutter_launcher_icons: "^0.7.3"
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.3
Expand All @@ -38,6 +39,10 @@ dev_dependencies:
flutter_test:
sdk: flutter

flutter_icons:
android: "launcher_icon"
image_path: "assets/covid.png"

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

Expand All @@ -54,6 +59,9 @@ flutter:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg

assets:
- assets/covid.png

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.

Expand Down

0 comments on commit 2b3ea1f

Please sign in to comment.