Skip to content

Commit 495f4e1

Browse files
committed
feat: setup Project sync table
1 parent 96c0b4f commit 495f4e1

3 files changed

Lines changed: 64 additions & 6 deletions

File tree

lib/data_providers/data/database_provider.dart

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'dart:io';
1717
import 'package:flutter/material.dart';
1818
import 'package:random_color/random_color.dart';
1919
import 'package:sqflite/sqflite.dart';
20+
import 'package:sqflite_common/sqflite_logger.dart';
2021
import 'package:timecop/data_providers/data/data_provider.dart';
2122
import 'package:timecop/models/timer_entry.dart';
2223
import 'package:timecop/models/project.dart';
@@ -65,6 +66,18 @@ class DatabaseProvider extends DataProvider {
6566
await db.execute('''
6667
create index if not exists timers_start_time on timers(start_time)
6768
''');
69+
await db.execute('''
70+
create table project_sync (
71+
sync_id text not null,
72+
internal_id int not null,
73+
status text default 'SYNCING',
74+
tasks_sha text default 'null',
75+
last_updated int,
76+
unique (internal_id),
77+
primary key (sync_id),
78+
foreign key (internal_id) references projects(id)
79+
)
80+
''');
6881
}
6982

7083
static void _onUpgrade(Database db, int version, int newVersion) async {
@@ -121,12 +134,16 @@ class DatabaseProvider extends DataProvider {
121134
}
122135

123136
static Future<DatabaseProvider> open(String path) async {
137+
var factoryWithLogs = SqfliteDatabaseFactoryLogger(databaseFactory,
138+
options:
139+
SqfliteLoggerOptions(type: SqfliteDatabaseFactoryLoggerType.all));
124140
// open the database
125-
Database db = await openDatabase(path,
126-
onConfigure: _onConfigure,
127-
onCreate: _onCreate,
128-
onUpgrade: _onUpgrade,
129-
version: _dbVersion);
141+
Database db = await factoryWithLogs.openDatabase(path,
142+
options: OpenDatabaseOptions(
143+
onConfigure: _onConfigure,
144+
onCreate: _onCreate,
145+
onUpgrade: _onUpgrade,
146+
version: _dbVersion));
130147
await db.execute("PRAGMA foreign_keys = ON");
131148
DatabaseProvider repo = DatabaseProvider(db);
132149

@@ -150,7 +167,7 @@ class DatabaseProvider extends DataProvider {
150167
@override
151168
Future<List<Project>> listProjects() async {
152169
List<Map<String, dynamic>> rawProjects = await _db.rawQuery('''
153-
select id, name, colour,
170+
select id, name, colour,
154171
case archived
155172
when 'false' then 0
156173
when 'true' then 1

lib/models/sync/project_sync.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'package:equatable/equatable.dart';
2+
import 'package:timecop/models/sync/sync_status.dart';
3+
4+
// this class represents the sync status of a project, if it is attached to a project
5+
// that means the user wants it synced.
6+
class ProjectSync extends Equatable {
7+
// Refers to the id stored in sqflite locally
8+
final int internalID;
9+
// refers to the id under which it is synced remotely (in that case it's an UUID)
10+
final String syncID;
11+
// Expresses the syncing status of the project
12+
final SyncStatus status;
13+
// a SHA fingerprint that when compared to the api will tell us
14+
// if the project's tasks are up to date
15+
final String tasksSHA;
16+
// timestamp in epoch milliseconds since the last update.
17+
final int lastUpdated;
18+
19+
const ProjectSync(
20+
{required this.internalID,
21+
required this.syncID,
22+
required this.status,
23+
required this.tasksSHA,
24+
required this.lastUpdated});
25+
26+
@override
27+
List<Object?> get props =>
28+
[internalID, syncID, status.value, tasksSHA, lastUpdated];
29+
}

lib/models/sync/sync_status.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
enum SyncStatus {
2+
// the object is NOT syncing
3+
synced("SYNCED"),
4+
// the object IS syncing
5+
syncing("SYNCING"),
6+
// last sync attempt failed
7+
syncFailed("FAILED");
8+
9+
final String value;
10+
11+
const SyncStatus(this.value);
12+
}

0 commit comments

Comments
 (0)