Skip to content

Commit

Permalink
#15: Adding Location DB WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
burhanrashid52 committed Apr 17, 2020
1 parent 741cf68 commit 0f73ba8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 34 deletions.
25 changes: 17 additions & 8 deletions lib/db/app_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_app/pages/labels/label.dart';
import 'package:flutter_app/pages/places/task_location.dart';
import 'package:flutter_app/pages/projects/project.dart';
import 'package:flutter_app/pages/tasks/models/task_labels.dart';
import 'package:path/path.dart';
Expand All @@ -12,11 +13,11 @@ import 'package:flutter_app/pages/tasks/models/tasks.dart';
/// This is the singleton database class which handlers all database transactions
/// All the task raw queries is handle here and return a Future<T> with result
class AppDatabase {
static final AppDatabase _appDatabase = AppDatabase._internal();

//private internal constructor to make it singleton
AppDatabase._internal();

static final AppDatabase _appDatabase = AppDatabase._internal();

Database _database;

static AppDatabase get() {
Expand All @@ -39,21 +40,25 @@ class AppDatabase {
_database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
// When creating the db, create the table
await _createProjectTable(db);
await _createTaskTable(db);
await _createLabelTable(db);
await _createTableInSequence(db);
}, onUpgrade: (Database db, int oldVersion, int newVersion) async {
await db.execute("DROP TABLE ${Tasks.tblTask}");
await db.execute("DROP TABLE ${Project.tblProject}");
await db.execute("DROP TABLE ${TaskLabels.tblTaskLabel}");
await db.execute("DROP TABLE ${Label.tblLabel}");
await _createProjectTable(db);
await _createTaskTable(db);
await _createLabelTable(db);
await db.execute(DROP_LOCATION_TABLE_QUERY);
await _createTableInSequence(db);
});
didInit = true;
}

Future _createTableInSequence(Database db) async {
await _createLocationTable(db);
await _createProjectTable(db);
await _createTaskTable(db);
await _createLabelTable(db);
}

Future _createProjectTable(Database db) {
return db.transaction((Transaction txn) async {
txn.execute("CREATE TABLE ${Project.tblProject} ("
Expand Down Expand Up @@ -83,6 +88,10 @@ class AppDatabase {
});
}

Future _createLocationTable(Database db) {
return db.execute(CREATE_LOCATION_TABLE_QUERY);
}

Future _createTaskTable(Database db) {
return db.execute("CREATE TABLE ${Tasks.tblTask} ("
"${Tasks.dbId} INTEGER PRIMARY KEY AUTOINCREMENT,"
Expand Down
16 changes: 9 additions & 7 deletions lib/pages/home/home.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_app/bloc/bloc_provider.dart';
import 'package:flutter_app/pages/labels/label_db.dart';
import 'package:flutter_app/pages/places/task_location_db.dart';
import 'package:flutter_app/pages/projects/project_db.dart';
import 'package:flutter_app/pages/tasks/bloc/add_task_bloc.dart';
import 'package:flutter_app/pages/tasks/bloc/task_bloc.dart';
Expand Down Expand Up @@ -38,7 +39,8 @@ class HomePage extends StatelessWidget {
backgroundColor: Colors.orange,
onPressed: () async {
var blocProviderAddTask = BlocProvider(
bloc: AddTaskBloc(TaskDB.get(), ProjectDB.get(), LabelDB.get()),
bloc: AddTaskBloc(
TaskDB.get(), ProjectDB.get(), LabelDB.get(), LocationDB.get()),
child: AddTaskScreen(),
);
await Navigator.push(
Expand All @@ -55,7 +57,7 @@ class HomePage extends StatelessWidget {
),
);
}

Widget buildPopupMenu(BuildContext context) {
return PopupMenuButton<MenuItem>(
icon: Icon(Icons.more_vert),
Expand All @@ -72,11 +74,11 @@ class HomePage extends StatelessWidget {
}
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<MenuItem>>[
const PopupMenuItem<MenuItem>(
value: MenuItem.taskCompleted,
child: Text('Completed Tasks'),
)
],
const PopupMenuItem<MenuItem>(
value: MenuItem.taskCompleted,
child: Text('Completed Tasks'),
)
],
);
}
}
Expand Down
11 changes: 10 additions & 1 deletion lib/pages/places/task_location.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';

class TaskLocation {

TaskLocation.create({
@required this.id,
@required this.location_name,
Expand Down Expand Up @@ -44,3 +43,13 @@ class TaskLocation {
@override
bool operator ==(o) => o is TaskLocation && o.id == id;
}

final CREATE_LOCATION_TABLE_QUERY =
"CREATE TABLE ${TaskLocation.tblTaskLocation} ("
"${TaskLocation.dbId} INTEGER PRIMARY KEY AUTOINCREMENT,"
"${TaskLocation.dbLocationName} TEXT,"
"${TaskLocation.dbAddress} TEXT,"
"${TaskLocation.dbLat} DOUBLE,"
"${TaskLocation.dbLng} DOUBLE);";

final DROP_LOCATION_TABLE_QUERY = "DROP TABLE ${TaskLocation.tblTaskLocation}";
12 changes: 11 additions & 1 deletion lib/pages/places/task_location_db.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter_app/db/app_db.dart';
import 'package:flutter_app/pages/places/places_models.dart';
import 'package:flutter_app/pages/places/task_location.dart';

class LocationDB {
//private internal constructor to make it singleton
Expand All @@ -11,4 +13,12 @@ class LocationDB {
static LocationDB get() {
return _locationDb;
}
}

Future createLocation(LocationInfo locationInfo) async {
final db = await _appDatabase.getDb();
await db.execute(
"INSERT INTO ${TaskLocation.tblTaskLocation} (${TaskLocation.dbLocationName},${TaskLocation.dbAddress},${TaskLocation.dbLat},${TaskLocation.dbLng})"
" VALUES(${locationInfo.locationName},${locationInfo.address},${locationInfo.lat},${locationInfo.lng});");
return 1;
}
}
29 changes: 18 additions & 11 deletions lib/pages/tasks/bloc/add_task_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_app/models/priority.dart';
import 'package:flutter_app/pages/labels/label.dart';
import 'package:flutter_app/pages/labels/label_db.dart';
import 'package:flutter_app/pages/places/places_models.dart';
import 'package:flutter_app/pages/places/task_location_db.dart';
import 'package:flutter_app/pages/projects/project.dart';
import 'package:flutter_app/pages/projects/project_db.dart';
import 'package:flutter_app/pages/tasks/models/tasks.dart';
Expand All @@ -13,19 +14,20 @@ import 'package:rxdart/rxdart.dart';
import 'package:rxdart/subjects.dart';

class AddTaskBloc implements BlocBase {
final TaskDB _taskDB;
final ProjectDB _projectDB;
final LabelDB _labelDB;
Status lastPrioritySelection = Status.PRIORITY_4;

AddTaskBloc(this._taskDB, this._projectDB, this._labelDB) {
AddTaskBloc(this._taskDB, this._projectDB, this._labelDB, this._locationDB) {
_loadProjects();
_loadLabels();
updateDueDate(DateTime.now().millisecondsSinceEpoch);
_projectSelection.add(Project.getInbox());
_prioritySelected.add(lastPrioritySelection);
}

final TaskDB _taskDB;
final ProjectDB _projectDB;
final LabelDB _labelDB;
final LocationDB _locationDB;
Status lastPrioritySelection = Status.PRIORITY_4;

BehaviorSubject<List<Project>> _projectController =
BehaviorSubject<List<Project>>();

Expand Down Expand Up @@ -104,7 +106,7 @@ class AddTaskBloc implements BlocBase {
});
String labelJoinString = selectedLabelNameList.join(" ");
String displayLabels =
labelJoinString.length == 0 ? "No Labels" : labelJoinString;
labelJoinString.isEmpty ? "No Labels" : labelJoinString;
_labelSelected.add(displayLabels);
}

Expand All @@ -114,8 +116,10 @@ class AddTaskBloc implements BlocBase {
}

Observable<String> createTask() {
return Observable.zip3(selectedProject, dueDateSelected, prioritySelected,
(Project project, int dueDateSelected, Status status) {
return Observable.zip4(
selectedProject, dueDateSelected, prioritySelected, locationInfo,
(Project project, int dueDateSelected, Status status,
LocationInfo locationInfo) {
List<int> labelIds = List();
_selectedLabelList.forEach((label) {
labelIds.add(label.id);
Expand All @@ -127,8 +131,11 @@ class AddTaskBloc implements BlocBase {
priority: status,
projectId: project.id,
);
_taskDB.updateTask(task, labelIDs: labelIds).then((task) {
Notification.onDone();

_locationDB.createLocation(locationInfo).then((value) {
_taskDB.updateTask(task, labelIDs: labelIds).then((task) {
Notification.onDone();
});
});
});
}
Expand Down
10 changes: 4 additions & 6 deletions lib/pages/tasks/task_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import 'package:flutter_app/pages/tasks/models/task_labels.dart';
import 'package:sqflite/sqflite.dart';

class TaskDB {
static final TaskDB _taskDb = TaskDB._internal(AppDatabase.get());

AppDatabase _appDatabase;

//private internal constructor to make it singleton
TaskDB._internal(this._appDatabase);

//static TaskDB get taskDb => _taskDb;
static final TaskDB _taskDb = TaskDB._internal(AppDatabase.get());

AppDatabase _appDatabase;

static TaskDB get() {
return _taskDb;
Expand Down Expand Up @@ -110,7 +108,7 @@ class TaskDB {
int id = await txn.rawInsert('INSERT OR REPLACE INTO '
'${Tasks.tblTask}(${Tasks.dbId},${Tasks.dbTitle},${Tasks.dbProjectID},${Tasks.dbComment},${Tasks.dbDueDate},${Tasks.dbPriority},${Tasks.dbStatus})'
' VALUES(${task.id}, "${task.title}", ${task.projectId},"${task.comment}", ${task.dueDate},${task.priority.index},${task.tasksStatus.index})');
if (id > 0 && labelIDs != null && labelIDs.length > 0) {
if (id > 0 && labelIDs != null && labelIDs.isNotEmpty) {
labelIDs.forEach((labelId) {
txn.rawInsert('INSERT OR REPLACE INTO '
'${TaskLabels.tblTaskLabel}(${TaskLabels.dbId},${TaskLabels.dbTaskId},${TaskLabels.dbLabelId})'
Expand Down

0 comments on commit 0f73ba8

Please sign in to comment.