notification overhaul

This commit is contained in:
2025-08-30 00:12:29 +02:00
parent 9ae2bb5654
commit 6dccac6124
25 changed files with 1313 additions and 3947 deletions

View File

@@ -3,6 +3,7 @@ import 'package:flutter/foundation.dart';
import 'package:path/path.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sqflite/sqflite.dart';
import 'package:supplements/logging.dart';
import 'package:webdav_client/webdav_client.dart';
import '../models/supplement.dart';
@@ -92,7 +93,7 @@ class DatabaseSyncService {
}
} catch (e) {
if (kDebugMode) {
print('SupplementsLog: Error loading saved sync configuration: $e');
printLog('Error loading saved sync configuration: $e');
}
}
}
@@ -107,7 +108,7 @@ class DatabaseSyncService {
if (_configuredRemotePath != null) await prefs.setString(_keyRemotePath, _configuredRemotePath!);
} catch (e) {
if (kDebugMode) {
print('SupplementsLog: Error saving sync configuration: $e');
printLog('Error saving sync configuration: $e');
}
}
}
@@ -145,7 +146,7 @@ class DatabaseSyncService {
return true;
} catch (e) {
if (kDebugMode) {
print('SupplementsLog: Connection test failed: $e');
printLog('Connection test failed: $e');
}
return false;
}
@@ -182,7 +183,7 @@ class DatabaseSyncService {
_setStatus(SyncStatus.error);
onError?.call(_lastError!);
if (kDebugMode) {
print('SupplementsLog: Sync failed: $e');
printLog('Sync failed: $e');
}
rethrow;
}
@@ -196,13 +197,13 @@ class DatabaseSyncService {
if (!remoteDbExists) {
if (kDebugMode) {
print('SupplementsLog: No remote database found, will upload local database');
printLog('No remote database found, will upload local database');
}
return null;
}
if (kDebugMode) {
print('SupplementsLog: Remote database found, downloading...');
printLog('Remote database found, downloading...');
}
// Download the remote database
@@ -217,14 +218,14 @@ class DatabaseSyncService {
await tempFile.writeAsBytes(remoteDbBytes);
if (kDebugMode) {
print('SupplementsLog: Downloaded remote database (${remoteDbBytes.length} bytes) to: $tempDbPath');
printLog('Downloaded remote database (${remoteDbBytes.length} bytes) to: $tempDbPath');
}
return tempDbPath;
} catch (e) {
if (kDebugMode) {
print('SupplementsLog: Failed to download remote database: $e');
printLog('Failed to download remote database: $e');
}
return null;
}
@@ -233,13 +234,13 @@ class DatabaseSyncService {
Future<void> _mergeDatabases(String? remoteDbPath) async {
if (remoteDbPath == null) {
if (kDebugMode) {
print('SupplementsLog: No remote database to merge');
printLog('No remote database to merge');
}
return;
}
if (kDebugMode) {
print('SupplementsLog: Starting database merge from: $remoteDbPath');
printLog('Starting database merge from: $remoteDbPath');
}
final localDb = await _databaseHelper.database;
@@ -249,21 +250,21 @@ class DatabaseSyncService {
// Check what tables exist in remote database
if (kDebugMode) {
final tables = await remoteDb.rawQuery("SELECT name FROM sqlite_master WHERE type='table'");
print('SupplementsLog: Remote database tables: ${tables.map((t) => t['name']).toList()}');
printLog('Remote database tables: ${tables.map((t) => t['name']).toList()}');
// Count records in each table
try {
final supplementCount = await remoteDb.rawQuery('SELECT COUNT(*) as count FROM supplements');
print('SupplementsLog: Remote supplements count: ${supplementCount.first['count']}');
printLog('Remote supplements count: ${supplementCount.first['count']}');
} catch (e) {
print('SupplementsLog: Error counting supplements: $e');
printLog('Error counting supplements: $e');
}
try {
final intakeCount = await remoteDb.rawQuery('SELECT COUNT(*) as count FROM supplement_intakes');
print('SupplementsLog: Remote intakes count: ${intakeCount.first['count']}');
printLog('Remote intakes count: ${intakeCount.first['count']}');
} catch (e) {
print('SupplementsLog: Error counting intakes: $e');
printLog('Error counting intakes: $e');
}
}
@@ -274,7 +275,7 @@ class DatabaseSyncService {
await _mergeIntakes(localDb, remoteDb);
if (kDebugMode) {
print('SupplementsLog: Database merge completed successfully');
printLog('Database merge completed successfully');
}
} finally {
@@ -284,7 +285,7 @@ class DatabaseSyncService {
Future<void> _mergeSupplements(Database localDb, Database remoteDb) async {
if (kDebugMode) {
print('SupplementsLog: Starting supplement merge...');
printLog('Starting supplement merge...');
}
// Get all supplements from remote database
@@ -293,19 +294,19 @@ class DatabaseSyncService {
remoteMaps.map((map) => Supplement.fromMap(map)).toList();
if (kDebugMode) {
print(
'SupplementsLog: Found ${remoteSupplements.length} supplements in remote database');
printLog(
'Found ${remoteSupplements.length} supplements in remote database');
for (final supplement in remoteSupplements) {
print(
'SupplementsLog: Remote supplement: ${supplement.name} (syncId: ${supplement.syncId}, deleted: ${supplement.isDeleted})');
printLog(
'Remote supplement: ${supplement.name} (syncId: ${supplement.syncId}, deleted: ${supplement.isDeleted})');
}
}
for (final remoteSupplement in remoteSupplements) {
if (remoteSupplement.syncId.isEmpty) {
if (kDebugMode) {
print(
'SupplementsLog: Skipping supplement ${remoteSupplement.name} - no syncId');
printLog(
'Skipping supplement ${remoteSupplement.name} - no syncId');
}
continue;
}
@@ -325,13 +326,13 @@ class DatabaseSyncService {
mapToInsert.remove('id');
await localDb.insert('supplements', mapToInsert);
if (kDebugMode) {
print(
'SupplementsLog: ✓ Inserted new supplement: ${remoteSupplement.name}');
printLog(
'✓ Inserted new supplement: ${remoteSupplement.name}');
}
} else {
if (kDebugMode) {
print(
'SupplementsLog: Skipping deleted supplement: ${remoteSupplement.name}');
printLog(
'Skipping deleted supplement: ${remoteSupplement.name}');
}
}
} else {
@@ -349,26 +350,26 @@ class DatabaseSyncService {
whereArgs: [existingSupplement.id],
);
if (kDebugMode) {
print(
'SupplementsLog: ✓ Updated supplement: ${remoteSupplement.name}');
printLog(
'✓ Updated supplement: ${remoteSupplement.name}');
}
} else {
if (kDebugMode) {
print(
'SupplementsLog: Local supplement ${remoteSupplement.name} is newer, keeping local version');
printLog(
'Local supplement ${remoteSupplement.name} is newer, keeping local version');
}
}
}
}
if (kDebugMode) {
print('SupplementsLog: Supplement merge completed');
printLog('Supplement merge completed');
}
}
Future<void> _mergeIntakes(Database localDb, Database remoteDb) async {
if (kDebugMode) {
print('SupplementsLog: Starting intake merge...');
printLog('Starting intake merge...');
}
// Get all intakes from remote database
@@ -376,13 +377,13 @@ class DatabaseSyncService {
final remoteIntakes = remoteMaps.map((map) => SupplementIntake.fromMap(map)).toList();
if (kDebugMode) {
print('SupplementsLog: Found ${remoteIntakes.length} intakes in remote database');
printLog('Found ${remoteIntakes.length} intakes in remote database');
}
for (final remoteIntake in remoteIntakes) {
if (remoteIntake.syncId.isEmpty) {
if (kDebugMode) {
print('SupplementsLog: Skipping intake - no syncId');
printLog('Skipping intake - no syncId');
}
continue;
}
@@ -405,16 +406,16 @@ class DatabaseSyncService {
);
await localDb.insert('supplement_intakes', intakeToInsert.toMap());
if (kDebugMode) {
print('SupplementsLog: ✓ Inserted new intake: ${remoteIntake.syncId}');
printLog('✓ Inserted new intake: ${remoteIntake.syncId}');
}
} else {
if (kDebugMode) {
print('SupplementsLog: Could not find local supplement for intake ${remoteIntake.syncId}');
printLog('Could not find local supplement for intake ${remoteIntake.syncId}');
}
}
} else {
if (kDebugMode) {
print('SupplementsLog: Skipping deleted intake: ${remoteIntake.syncId}');
printLog('Skipping deleted intake: ${remoteIntake.syncId}');
}
}
} else {
@@ -430,18 +431,18 @@ class DatabaseSyncService {
whereArgs: [existingIntake.id],
);
if (kDebugMode) {
print('SupplementsLog: ✓ Updated intake: ${remoteIntake.syncId}');
printLog('✓ Updated intake: ${remoteIntake.syncId}');
}
} else {
if (kDebugMode) {
print('SupplementsLog: Local intake ${remoteIntake.syncId} is newer, keeping local version');
printLog('Local intake ${remoteIntake.syncId} is newer, keeping local version');
}
}
}
}
if (kDebugMode) {
print('SupplementsLog: Intake merge completed');
printLog('Intake merge completed');
}
}
@@ -476,7 +477,7 @@ class DatabaseSyncService {
final dbPath = localDb.path;
if (kDebugMode) {
print('SupplementsLog: Reading database from: $dbPath');
printLog('Reading database from: $dbPath');
}
// Read the database file
@@ -488,7 +489,7 @@ class DatabaseSyncService {
final dbBytes = await dbFile.readAsBytes();
if (kDebugMode) {
print('SupplementsLog: Database file size: ${dbBytes.length} bytes');
printLog('Database file size: ${dbBytes.length} bytes');
}
if (dbBytes.isEmpty) {
@@ -500,7 +501,7 @@ class DatabaseSyncService {
await _client!.readDir(_remotePath!);
} catch (e) {
if (kDebugMode) {
print('SupplementsLog: Creating remote directory: $_remotePath');
printLog('Creating remote directory: $_remotePath');
}
await _client!.mkdir(_remotePath!);
}
@@ -510,12 +511,12 @@ class DatabaseSyncService {
await _client!.write(remoteUrl, dbBytes);
if (kDebugMode) {
print('SupplementsLog: Successfully uploaded database (${dbBytes.length} bytes) to: $remoteUrl');
printLog('Successfully uploaded database (${dbBytes.length} bytes) to: $remoteUrl');
}
} catch (e) {
if (kDebugMode) {
print('SupplementsLog: Failed to upload database: $e');
printLog('Failed to upload database: $e');
}
rethrow;
}