Skip to content

Commit

Permalink
refactor: Migrate from objectbox to drift.
Browse files Browse the repository at this point in the history
I hate riverpod.
  • Loading branch information
iphydf committed Jan 22, 2025
1 parent fcdcc88 commit 511599b
Show file tree
Hide file tree
Showing 49 changed files with 1,051 additions and 1,384 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ jobs:
name: tokhub-macos.tar.gz
path: tokhub-macos.tar.gz

web-build:
runs-on: windows-2022
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v4
- uses: subosito/flutter-action@v2
with:
channel: "stable"
flutter-version: ${{ env.FLUTTER_VERSION }}
cache: true
- run: ./tools/prepare-web
- run: flutter build web

windows-build:
runs-on: windows-2022
defaults:
Expand Down
25 changes: 5 additions & 20 deletions .metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This file should be version controlled and should not be manually edited.

version:
revision: "603104015dd692ea3403755b55d07813d5cf8965"
revision: "17025dd88227cd9532c33fa78f5250d548d87e9a"
channel: "stable"

project_type: app
Expand All @@ -13,26 +13,11 @@ project_type: app
migration:
platforms:
- platform: root
create_revision: 603104015dd692ea3403755b55d07813d5cf8965
base_revision: 603104015dd692ea3403755b55d07813d5cf8965
- platform: android
create_revision: 603104015dd692ea3403755b55d07813d5cf8965
base_revision: 603104015dd692ea3403755b55d07813d5cf8965
- platform: ios
create_revision: 603104015dd692ea3403755b55d07813d5cf8965
base_revision: 603104015dd692ea3403755b55d07813d5cf8965
- platform: linux
create_revision: 603104015dd692ea3403755b55d07813d5cf8965
base_revision: 603104015dd692ea3403755b55d07813d5cf8965
- platform: macos
create_revision: 603104015dd692ea3403755b55d07813d5cf8965
base_revision: 603104015dd692ea3403755b55d07813d5cf8965
create_revision: 17025dd88227cd9532c33fa78f5250d548d87e9a
base_revision: 17025dd88227cd9532c33fa78f5250d548d87e9a
- platform: web
create_revision: 603104015dd692ea3403755b55d07813d5cf8965
base_revision: 603104015dd692ea3403755b55d07813d5cf8965
- platform: windows
create_revision: 603104015dd692ea3403755b55d07813d5cf8965
base_revision: 603104015dd692ea3403755b55d07813d5cf8965
create_revision: 17025dd88227cd9532c33fa78f5250d548d87e9a
base_revision: 17025dd88227cd9532c33fa78f5250d548d87e9a

# User provided section

Expand Down
8 changes: 8 additions & 0 deletions .restyled.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
exclude:
- "LICENSE"
# Generated files.
- "linux/**/*.cc"
- "linux/**/*.h"
- "windows/**/*.cc"
- "windows/**/*.h"
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
android {
namespace = "com.example.tokhub"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion
ndkVersion = "27.0.12077973"

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:label="tokhub"
android:label="TokHub"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
Expand Down
4 changes: 3 additions & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip
4 changes: 2 additions & 2 deletions android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pluginManagement {

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "8.1.0" apply false
id "org.jetbrains.kotlin.android" version "1.8.22" apply false
id "com.android.application" version "8.8.0" apply false
id "org.jetbrains.kotlin.android" version "2.1.0" apply false
}

include ":app"
13 changes: 7 additions & 6 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ ignoreWords: []
flagWords: []
import: []
ignorePaths:
- ".github/**"
- "build/**"
- "LICENSE*"
- .github/**
- build/**
- LICENSE*
- "*.json"
words:
- "iphydf"
- "robinlinden"
- "tokhub"
- antiope
- iphydf
- robinlinden
- tokhub
34 changes: 23 additions & 11 deletions lib/data_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,37 @@ import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:tokhub/logger.dart';

final _logger = Logger(['DataObject']);

Future<T?> loadDataObject<T extends DataObject<T>>(
T Function(Map<String, dynamic> json) fromJson) async {
final dir = await getApplicationDocumentsDirectory();
final file = File('${dir.path}/TokHub/${T.toString()}.json');
debugPrint('Loading data object from ${file.path}');
if (!await file.exists()) {
try {
final dir = await getApplicationDocumentsDirectory();
final file = File('${dir.path}/TokHub/${T.toString()}.json');
debugPrint('Loading data object from ${file.path}');
if (!await file.exists()) {
return null;
}
final text = await file.readAsString();
return fromJson(jsonDecode(text));
} catch (e, stackTrace) {
_logger.e('Error loading data object: $e', stackTrace);
return null;
}
final text = await file.readAsString();
return fromJson(jsonDecode(text));
}

Future<void> saveDataObject<T>(DataObject<T> object) async {
final dir = await getApplicationDocumentsDirectory();
final file = File('${dir.path}/TokHub/${T.toString()}.json');
debugPrint('Saving data object to ${file.path}');
await file.create(recursive: true);
await file.writeAsString(jsonEncode(object.toJson()));
try {
final dir = await getApplicationDocumentsDirectory();
final file = File('${dir.path}/TokHub/${T.toString()}.json');
debugPrint('Saving data object to ${file.path}');
await file.create(recursive: true);
await file.writeAsString(jsonEncode(object.toJson()));
} catch (e, stackTrace) {
_logger.e('Error saving data object: $e', stackTrace);
}
}

abstract class DataObject<T> {
Expand Down
30 changes: 30 additions & 0 deletions lib/db/database.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:drift/drift.dart';
import 'package:tokhub/models/check_status.dart';
import 'package:tokhub/models/github.dart';
import 'package:tokhub/models/json_enum.dart';

part 'database.g.dart';

@DriftDatabase(tables: [
MinimalCheckRun,
MinimalCommitStatus,
MinimalPullRequest,
MinimalRepository,
MinimalUser,
])
final class Database extends _$Database {
Database(super.e);

@override
int get schemaVersion => 1;

Future<void> clear() async {
await transaction(() async {
await delete(minimalCheckRun).go();
await delete(minimalCommitStatus).go();
await delete(minimalPullRequest).go();
await delete(minimalRepository).go();
await delete(minimalUser).go();
});
}
}
12 changes: 12 additions & 0 deletions lib/db/native.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'dart:io';

import 'package:tokhub/db/database.dart';
import 'package:drift/native.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';

Future<Database> constructDb() async {
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(path.join(dbFolder.path, 'TokHub', 'db.sqlite'));
return Database(NativeDatabase(file));
}
3 changes: 3 additions & 0 deletions lib/db/shared.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'unsupported.dart'
if (dart.library.html) 'web.dart'
if (dart.library.ffi) 'native.dart';
3 changes: 3 additions & 0 deletions lib/db/unsupported.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'package:tokhub/db/database.dart';

Future<Database> constructDb() => throw UnimplementedError();
11 changes: 11 additions & 0 deletions lib/db/web.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:tokhub/db/database.dart';
import 'package:drift/wasm.dart';

Future<Database> constructDb() async {
final db = await WasmDatabase.open(
databaseName: 'db',
sqlite3Uri: Uri(path: 'sqlite3.wasm'),
driftWorkerUri: Uri(path: 'drift_worker.js'),
);
return Database(db.resolvedExecutor);
}
21 changes: 20 additions & 1 deletion lib/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import 'package:clock/clock.dart';
import 'package:flutter/material.dart';
import 'package:circular_buffer/circular_buffer.dart';

String _callerFileLine() {
final stack = StackTrace.current.toString().split('\n');
final caller = stack.firstWhere((line) => !line.contains('logger.dart'));
return caller.split(' ').last;
}

final class Logger {
static const _bufferSize = 1000;
static final CircularBuffer<LogLine> _log = CircularBuffer(_bufferSize);
Expand Down Expand Up @@ -35,7 +41,8 @@ final class Logger {

void _logLine(LogLevel level, String text, [StackTrace? stackTrace]) {
if (level == LogLevel.verbose && !verbose) return;
final line = '${level.name[0].toUpperCase()} $tags $text';
final line =
'${_callerFileLine()}: ${level.name[0].toUpperCase()} $tags $text';
if (stackTrace != null) {
debugPrintStack(stackTrace: stackTrace, label: line);
_log.add(LogLine(clock.now(), level, tags, text, stackTrace));
Expand All @@ -44,6 +51,18 @@ final class Logger {
_log.add(LogLine(clock.now(), level, tags, text));
}
}

T Function(A) catching<T, A>(T Function(A) f) {
return (A a) {
try {
v('Calling $f with $a');
return f(a);
} catch (exn, stackTrace) {
e('Caught exception $exn', stackTrace);
rethrow;
}
};
}
}

enum LogLevel { verbose, debug, info, warning, error }
Expand Down
19 changes: 18 additions & 1 deletion lib/models/check_status.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import 'package:flutter/material.dart';
import 'package:tokhub/models/json_enum.dart';

enum CheckRunConclusion implements JsonEnum<CheckRunConclusion> {
success('success'),
failure('failure'),
neutral('neutral'),
cancelled('cancelled'),
timedOut('timed_out'),
skipped('skipped'),
actionRequired('action_required');

@override
final String string;

const CheckRunConclusion(this.string);
}

final class CheckStatus {
final String name;
Expand All @@ -14,7 +30,7 @@ final class CheckStatus {
});
}

enum CheckStatusConclusion {
enum CheckStatusConclusion implements JsonEnum<CheckStatusConclusion> {
unknown('unknown', 'Unknown', Icons.warning, Colors.red),
loading('loading', 'Loading', Icons.timer, Colors.blue),
success('success', 'Successful in', Icons.check_circle, Colors.green),
Expand All @@ -28,6 +44,7 @@ enum CheckStatusConclusion {
failure('failure', 'Failing after', Icons.error_outline, Colors.red),
empty('empty', 'Unknown after', Icons.help, Colors.grey);

@override
final String string;
final String title;
final IconData icon;
Expand Down
Loading

0 comments on commit 511599b

Please sign in to comment.