mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-12-07 21:52:35 +00:00
Compare commits
2 Commits
731ac1567d
...
6524e625d8
| Author | SHA1 | Date | |
|---|---|---|---|
|
6524e625d8
|
|||
|
142359bf94
|
@@ -557,21 +557,24 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
|
|||||||
void _saveSupplement() async {
|
void _saveSupplement() async {
|
||||||
if (_formKey.currentState!.validate()) {
|
if (_formKey.currentState!.validate()) {
|
||||||
// Validate that we have at least one ingredient with name and amount
|
// Validate that we have at least one ingredient with name and amount
|
||||||
final validIngredients = _ingredientControllers.where((controller) =>
|
final validIngredients = _ingredientControllers
|
||||||
controller.nameController.text.trim().isNotEmpty &&
|
.where((controller) =>
|
||||||
(double.tryParse(controller.amountController.text) ?? 0) > 0
|
controller.nameController.text.trim().isNotEmpty &&
|
||||||
).map((controller) => Ingredient(
|
(double.tryParse(controller.amountController.text) ?? 0) > 0)
|
||||||
name: controller.nameController.text.trim(),
|
.map((controller) => Ingredient(
|
||||||
amount: double.tryParse(controller.amountController.text) ?? 0.0,
|
name: controller.nameController.text.trim(),
|
||||||
unit: controller.selectedUnit,
|
amount: double.tryParse(controller.amountController.text) ?? 0.0,
|
||||||
syncId: const Uuid().v4(),
|
unit: controller.selectedUnit,
|
||||||
lastModified: DateTime.now(),
|
syncId: const Uuid().v4(),
|
||||||
)).toList();
|
lastModified: DateTime.now(),
|
||||||
|
))
|
||||||
|
.toList();
|
||||||
|
|
||||||
if (validIngredients.isEmpty) {
|
if (validIngredients.isEmpty) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
const SnackBar(
|
const SnackBar(
|
||||||
content: Text('Please add at least one ingredient with name and amount'),
|
content:
|
||||||
|
Text('Please add at least one ingredient with name and amount'),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
@@ -580,14 +583,20 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
|
|||||||
final supplement = Supplement(
|
final supplement = Supplement(
|
||||||
id: widget.supplement?.id,
|
id: widget.supplement?.id,
|
||||||
name: _nameController.text.trim(),
|
name: _nameController.text.trim(),
|
||||||
brand: _brandController.text.trim().isNotEmpty ? _brandController.text.trim() : null,
|
brand: _brandController.text.trim().isNotEmpty
|
||||||
|
? _brandController.text.trim()
|
||||||
|
: null,
|
||||||
ingredients: validIngredients,
|
ingredients: validIngredients,
|
||||||
numberOfUnits: int.parse(_numberOfUnitsController.text),
|
numberOfUnits: int.parse(_numberOfUnitsController.text),
|
||||||
unitType: _selectedUnitType,
|
unitType: _selectedUnitType,
|
||||||
frequencyPerDay: _frequencyPerDay,
|
frequencyPerDay: _frequencyPerDay,
|
||||||
reminderTimes: _reminderTimes,
|
reminderTimes: _reminderTimes,
|
||||||
notes: _notesController.text.trim().isNotEmpty ? _notesController.text.trim() : null,
|
notes: _notesController.text.trim().isNotEmpty
|
||||||
|
? _notesController.text.trim()
|
||||||
|
: null,
|
||||||
createdAt: widget.supplement?.createdAt ?? DateTime.now(),
|
createdAt: widget.supplement?.createdAt ?? DateTime.now(),
|
||||||
|
syncId: widget.supplement?.syncId, // Preserve syncId on update
|
||||||
|
lastModified: DateTime.now(), // Always update lastModified on save
|
||||||
);
|
);
|
||||||
|
|
||||||
final provider = context.read<SupplementProvider>();
|
final provider = context.read<SupplementProvider>();
|
||||||
|
|||||||
@@ -27,40 +27,40 @@ enum RecordSyncStatus {
|
|||||||
|
|
||||||
class DatabaseSyncService {
|
class DatabaseSyncService {
|
||||||
static const String _remoteDbFileName = 'supplements.db';
|
static const String _remoteDbFileName = 'supplements.db';
|
||||||
|
|
||||||
// SharedPreferences keys for persistence
|
// SharedPreferences keys for persistence
|
||||||
static const String _keyServerUrl = 'sync_server_url';
|
static const String _keyServerUrl = 'sync_server_url';
|
||||||
static const String _keyUsername = 'sync_username';
|
static const String _keyUsername = 'sync_username';
|
||||||
static const String _keyPassword = 'sync_password';
|
static const String _keyPassword = 'sync_password';
|
||||||
static const String _keyRemotePath = 'sync_remote_path';
|
static const String _keyRemotePath = 'sync_remote_path';
|
||||||
|
|
||||||
Client? _client;
|
Client? _client;
|
||||||
String? _remotePath;
|
String? _remotePath;
|
||||||
|
|
||||||
// Store configuration values
|
// Store configuration values
|
||||||
String? _serverUrl;
|
String? _serverUrl;
|
||||||
String? _username;
|
String? _username;
|
||||||
String? _password;
|
String? _password;
|
||||||
String? _configuredRemotePath;
|
String? _configuredRemotePath;
|
||||||
|
|
||||||
final DatabaseHelper _databaseHelper = DatabaseHelper.instance;
|
final DatabaseHelper _databaseHelper = DatabaseHelper.instance;
|
||||||
|
|
||||||
SyncStatus _status = SyncStatus.idle;
|
SyncStatus _status = SyncStatus.idle;
|
||||||
String? _lastError;
|
String? _lastError;
|
||||||
DateTime? _lastSyncTime;
|
DateTime? _lastSyncTime;
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
SyncStatus get status => _status;
|
SyncStatus get status => _status;
|
||||||
String? get lastError => _lastError;
|
String? get lastError => _lastError;
|
||||||
DateTime? get lastSyncTime => _lastSyncTime;
|
DateTime? get lastSyncTime => _lastSyncTime;
|
||||||
bool get isConfigured => _client != null;
|
bool get isConfigured => _client != null;
|
||||||
|
|
||||||
// Configuration getters
|
// Configuration getters
|
||||||
String? get serverUrl => _serverUrl;
|
String? get serverUrl => _serverUrl;
|
||||||
String? get username => _username;
|
String? get username => _username;
|
||||||
String? get password => _password;
|
String? get password => _password;
|
||||||
String? get remotePath => _configuredRemotePath;
|
String? get remotePath => _configuredRemotePath;
|
||||||
|
|
||||||
// Callbacks for UI updates
|
// Callbacks for UI updates
|
||||||
Function(SyncStatus)? onStatusChanged;
|
Function(SyncStatus)? onStatusChanged;
|
||||||
Function(String)? onError;
|
Function(String)? onError;
|
||||||
@@ -78,11 +78,11 @@ class DatabaseSyncService {
|
|||||||
_username = prefs.getString(_keyUsername);
|
_username = prefs.getString(_keyUsername);
|
||||||
_password = prefs.getString(_keyPassword);
|
_password = prefs.getString(_keyPassword);
|
||||||
_configuredRemotePath = prefs.getString(_keyRemotePath);
|
_configuredRemotePath = prefs.getString(_keyRemotePath);
|
||||||
|
|
||||||
// If we have saved configuration, set up the client
|
// If we have saved configuration, set up the client
|
||||||
if (_serverUrl != null && _username != null && _password != null && _configuredRemotePath != null) {
|
if (_serverUrl != null && _username != null && _password != null && _configuredRemotePath != null) {
|
||||||
_remotePath = _configuredRemotePath!.endsWith('/') ? _configuredRemotePath : '$_configuredRemotePath/';
|
_remotePath = _configuredRemotePath!.endsWith('/') ? _configuredRemotePath : '$_configuredRemotePath/';
|
||||||
|
|
||||||
_client = newClient(
|
_client = newClient(
|
||||||
_serverUrl!,
|
_serverUrl!,
|
||||||
user: _username!,
|
user: _username!,
|
||||||
@@ -123,9 +123,9 @@ class DatabaseSyncService {
|
|||||||
_username = username;
|
_username = username;
|
||||||
_password = password;
|
_password = password;
|
||||||
_configuredRemotePath = remotePath;
|
_configuredRemotePath = remotePath;
|
||||||
|
|
||||||
_remotePath = remotePath.endsWith('/') ? remotePath : '$remotePath/';
|
_remotePath = remotePath.endsWith('/') ? remotePath : '$remotePath/';
|
||||||
|
|
||||||
_client = newClient(
|
_client = newClient(
|
||||||
serverUrl,
|
serverUrl,
|
||||||
user: username,
|
user: username,
|
||||||
@@ -139,7 +139,7 @@ class DatabaseSyncService {
|
|||||||
|
|
||||||
Future<bool> testConnection() async {
|
Future<bool> testConnection() async {
|
||||||
if (_client == null) return false;
|
if (_client == null) return false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await _client!.ping();
|
await _client!.ping();
|
||||||
return true;
|
return true;
|
||||||
@@ -157,26 +157,26 @@ class DatabaseSyncService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_setStatus(SyncStatus.downloading);
|
_setStatus(SyncStatus.downloading);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Step 1: Download remote database (if it exists)
|
// Step 1: Download remote database (if it exists)
|
||||||
final remoteDbPath = await _downloadRemoteDatabase();
|
final remoteDbPath = await _downloadRemoteDatabase();
|
||||||
|
|
||||||
// Step 2: Merge databases
|
// Step 2: Merge databases
|
||||||
_setStatus(SyncStatus.merging);
|
_setStatus(SyncStatus.merging);
|
||||||
await _mergeDatabases(remoteDbPath);
|
await _mergeDatabases(remoteDbPath);
|
||||||
|
|
||||||
// Step 3: Upload merged database
|
// Step 3: Upload merged database
|
||||||
_setStatus(SyncStatus.uploading);
|
_setStatus(SyncStatus.uploading);
|
||||||
await _uploadLocalDatabase();
|
await _uploadLocalDatabase();
|
||||||
|
|
||||||
// Step 4: Cleanup - for now we'll skip cleanup to avoid file issues
|
// Step 4: Cleanup - for now we'll skip cleanup to avoid file issues
|
||||||
// TODO: Implement proper cleanup once file operations are working
|
// TODO: Implement proper cleanup once file operations are working
|
||||||
|
|
||||||
_lastSyncTime = DateTime.now();
|
_lastSyncTime = DateTime.now();
|
||||||
_setStatus(SyncStatus.completed);
|
_setStatus(SyncStatus.completed);
|
||||||
onSyncCompleted?.call();
|
onSyncCompleted?.call();
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_lastError = e.toString();
|
_lastError = e.toString();
|
||||||
_setStatus(SyncStatus.error);
|
_setStatus(SyncStatus.error);
|
||||||
@@ -193,35 +193,35 @@ class DatabaseSyncService {
|
|||||||
// Check if remote database exists
|
// Check if remote database exists
|
||||||
final files = await _client!.readDir(_remotePath!);
|
final files = await _client!.readDir(_remotePath!);
|
||||||
final remoteDbExists = files.any((file) => file.name == _remoteDbFileName);
|
final remoteDbExists = files.any((file) => file.name == _remoteDbFileName);
|
||||||
|
|
||||||
if (!remoteDbExists) {
|
if (!remoteDbExists) {
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: No remote database found, will upload local database');
|
print('SupplementsLog: No remote database found, will upload local database');
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Remote database found, downloading...');
|
print('SupplementsLog: Remote database found, downloading...');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Download the remote database
|
// Download the remote database
|
||||||
final remoteDbBytes = await _client!.read('$_remotePath$_remoteDbFileName');
|
final remoteDbBytes = await _client!.read('$_remotePath$_remoteDbFileName');
|
||||||
|
|
||||||
// Create a temporary file path for the downloaded database
|
// Create a temporary file path for the downloaded database
|
||||||
final tempDir = await getDatabasesPath();
|
final tempDir = await getDatabasesPath();
|
||||||
final tempDbPath = join(tempDir, 'remote_supplements.db');
|
final tempDbPath = join(tempDir, 'remote_supplements.db');
|
||||||
|
|
||||||
// Write the downloaded database to a temporary file
|
// Write the downloaded database to a temporary file
|
||||||
final tempFile = io.File(tempDbPath);
|
final tempFile = io.File(tempDbPath);
|
||||||
await tempFile.writeAsBytes(remoteDbBytes);
|
await tempFile.writeAsBytes(remoteDbBytes);
|
||||||
|
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Downloaded remote database (${remoteDbBytes.length} bytes) to: $tempDbPath');
|
print('SupplementsLog: Downloaded remote database (${remoteDbBytes.length} bytes) to: $tempDbPath');
|
||||||
}
|
}
|
||||||
|
|
||||||
return tempDbPath;
|
return tempDbPath;
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Failed to download remote database: $e');
|
print('SupplementsLog: Failed to download remote database: $e');
|
||||||
@@ -237,20 +237,20 @@ class DatabaseSyncService {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Starting database merge from: $remoteDbPath');
|
print('SupplementsLog: Starting database merge from: $remoteDbPath');
|
||||||
}
|
}
|
||||||
|
|
||||||
final localDb = await _databaseHelper.database;
|
final localDb = await _databaseHelper.database;
|
||||||
final remoteDb = await openDatabase(remoteDbPath, readOnly: true);
|
final remoteDb = await openDatabase(remoteDbPath, readOnly: true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Check what tables exist in remote database
|
// Check what tables exist in remote database
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
final tables = await remoteDb.rawQuery("SELECT name FROM sqlite_master WHERE type='table'");
|
final tables = await remoteDb.rawQuery("SELECT name FROM sqlite_master WHERE type='table'");
|
||||||
print('SupplementsLog: Remote database tables: ${tables.map((t) => t['name']).toList()}');
|
print('SupplementsLog: Remote database tables: ${tables.map((t) => t['name']).toList()}');
|
||||||
|
|
||||||
// Count records in each table
|
// Count records in each table
|
||||||
try {
|
try {
|
||||||
final supplementCount = await remoteDb.rawQuery('SELECT COUNT(*) as count FROM supplements');
|
final supplementCount = await remoteDb.rawQuery('SELECT COUNT(*) as count FROM supplements');
|
||||||
@@ -258,7 +258,7 @@ class DatabaseSyncService {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('SupplementsLog: Error counting supplements: $e');
|
print('SupplementsLog: Error counting supplements: $e');
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final intakeCount = await remoteDb.rawQuery('SELECT COUNT(*) as count FROM supplement_intakes');
|
final intakeCount = await remoteDb.rawQuery('SELECT COUNT(*) as count FROM supplement_intakes');
|
||||||
print('SupplementsLog: Remote intakes count: ${intakeCount.first['count']}');
|
print('SupplementsLog: Remote intakes count: ${intakeCount.first['count']}');
|
||||||
@@ -266,17 +266,17 @@ class DatabaseSyncService {
|
|||||||
print('SupplementsLog: Error counting intakes: $e');
|
print('SupplementsLog: Error counting intakes: $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge supplements
|
// Merge supplements
|
||||||
await _mergeSupplements(localDb, remoteDb);
|
await _mergeSupplements(localDb, remoteDb);
|
||||||
|
|
||||||
// Merge intakes
|
// Merge intakes
|
||||||
await _mergeIntakes(localDb, remoteDb);
|
await _mergeIntakes(localDb, remoteDb);
|
||||||
|
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Database merge completed successfully');
|
print('SupplementsLog: Database merge completed successfully');
|
||||||
}
|
}
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
await remoteDb.close();
|
await remoteDb.close();
|
||||||
}
|
}
|
||||||
@@ -286,52 +286,62 @@ class DatabaseSyncService {
|
|||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Starting supplement merge...');
|
print('SupplementsLog: Starting supplement merge...');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all supplements from remote database
|
// Get all supplements from remote database
|
||||||
final remoteMaps = await remoteDb.query('supplements');
|
final remoteMaps = await remoteDb.query('supplements');
|
||||||
final remoteSupplements = remoteMaps.map((map) => Supplement.fromMap(map)).toList();
|
final remoteSupplements =
|
||||||
|
remoteMaps.map((map) => Supplement.fromMap(map)).toList();
|
||||||
|
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Found ${remoteSupplements.length} supplements in remote database');
|
print(
|
||||||
|
'SupplementsLog: Found ${remoteSupplements.length} supplements in remote database');
|
||||||
for (final supplement in remoteSupplements) {
|
for (final supplement in remoteSupplements) {
|
||||||
print('SupplementsLog: Remote supplement: ${supplement.name} (syncId: ${supplement.syncId}, deleted: ${supplement.isDeleted})');
|
print(
|
||||||
|
'SupplementsLog: Remote supplement: ${supplement.name} (syncId: ${supplement.syncId}, deleted: ${supplement.isDeleted})');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (final remoteSupplement in remoteSupplements) {
|
for (final remoteSupplement in remoteSupplements) {
|
||||||
if (remoteSupplement.syncId.isEmpty) {
|
if (remoteSupplement.syncId.isEmpty) {
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Skipping supplement ${remoteSupplement.name} - no syncId');
|
print(
|
||||||
|
'SupplementsLog: Skipping supplement ${remoteSupplement.name} - no syncId');
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find existing supplement by syncId
|
// Find existing supplement by syncId
|
||||||
final existingMaps = await localDb.query(
|
final existingMaps = await localDb.query(
|
||||||
'supplements',
|
'supplements',
|
||||||
where: 'syncId = ?',
|
where: 'syncId = ?',
|
||||||
whereArgs: [remoteSupplement.syncId],
|
whereArgs: [remoteSupplement.syncId],
|
||||||
);
|
);
|
||||||
|
|
||||||
if (existingMaps.isEmpty) {
|
if (existingMaps.isEmpty) {
|
||||||
// New supplement from remote - insert it
|
// New supplement from remote - insert it
|
||||||
if (!remoteSupplement.isDeleted) {
|
if (!remoteSupplement.isDeleted) {
|
||||||
final supplementToInsert = remoteSupplement.copyWith(id: null);
|
// Manually create a new map without the id to ensure it's null
|
||||||
await localDb.insert('supplements', supplementToInsert.toMap());
|
final mapToInsert = remoteSupplement.toMap();
|
||||||
|
mapToInsert.remove('id');
|
||||||
|
await localDb.insert('supplements', mapToInsert);
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: ✓ Inserted new supplement: ${remoteSupplement.name}');
|
print(
|
||||||
|
'SupplementsLog: ✓ Inserted new supplement: ${remoteSupplement.name}');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Skipping deleted supplement: ${remoteSupplement.name}');
|
print(
|
||||||
|
'SupplementsLog: Skipping deleted supplement: ${remoteSupplement.name}');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Existing supplement - update if remote is newer
|
// Existing supplement - update if remote is newer
|
||||||
final existingSupplement = Supplement.fromMap(existingMaps.first);
|
final existingSupplement = Supplement.fromMap(existingMaps.first);
|
||||||
|
|
||||||
if (remoteSupplement.lastModified.isAfter(existingSupplement.lastModified)) {
|
if (remoteSupplement.lastModified
|
||||||
final supplementToUpdate = remoteSupplement.copyWith(id: existingSupplement.id);
|
.isAfter(existingSupplement.lastModified)) {
|
||||||
|
final supplementToUpdate =
|
||||||
|
remoteSupplement.copyWith(id: existingSupplement.id);
|
||||||
await localDb.update(
|
await localDb.update(
|
||||||
'supplements',
|
'supplements',
|
||||||
supplementToUpdate.toMap(),
|
supplementToUpdate.toMap(),
|
||||||
@@ -339,16 +349,18 @@ class DatabaseSyncService {
|
|||||||
whereArgs: [existingSupplement.id],
|
whereArgs: [existingSupplement.id],
|
||||||
);
|
);
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: ✓ Updated supplement: ${remoteSupplement.name}');
|
print(
|
||||||
|
'SupplementsLog: ✓ Updated supplement: ${remoteSupplement.name}');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Local supplement ${remoteSupplement.name} is newer, keeping local version');
|
print(
|
||||||
|
'SupplementsLog: Local supplement ${remoteSupplement.name} is newer, keeping local version');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Supplement merge completed');
|
print('SupplementsLog: Supplement merge completed');
|
||||||
}
|
}
|
||||||
@@ -358,15 +370,15 @@ class DatabaseSyncService {
|
|||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Starting intake merge...');
|
print('SupplementsLog: Starting intake merge...');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all intakes from remote database
|
// Get all intakes from remote database
|
||||||
final remoteMaps = await remoteDb.query('supplement_intakes');
|
final remoteMaps = await remoteDb.query('supplement_intakes');
|
||||||
final remoteIntakes = remoteMaps.map((map) => SupplementIntake.fromMap(map)).toList();
|
final remoteIntakes = remoteMaps.map((map) => SupplementIntake.fromMap(map)).toList();
|
||||||
|
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Found ${remoteIntakes.length} intakes in remote database');
|
print('SupplementsLog: Found ${remoteIntakes.length} intakes in remote database');
|
||||||
}
|
}
|
||||||
|
|
||||||
for (final remoteIntake in remoteIntakes) {
|
for (final remoteIntake in remoteIntakes) {
|
||||||
if (remoteIntake.syncId.isEmpty) {
|
if (remoteIntake.syncId.isEmpty) {
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
@@ -374,14 +386,14 @@ class DatabaseSyncService {
|
|||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find existing intake by syncId
|
// Find existing intake by syncId
|
||||||
final existingMaps = await localDb.query(
|
final existingMaps = await localDb.query(
|
||||||
'supplement_intakes',
|
'supplement_intakes',
|
||||||
where: 'syncId = ?',
|
where: 'syncId = ?',
|
||||||
whereArgs: [remoteIntake.syncId],
|
whereArgs: [remoteIntake.syncId],
|
||||||
);
|
);
|
||||||
|
|
||||||
if (existingMaps.isEmpty) {
|
if (existingMaps.isEmpty) {
|
||||||
// New intake from remote - need to find local supplement ID
|
// New intake from remote - need to find local supplement ID
|
||||||
if (!remoteIntake.isDeleted) {
|
if (!remoteIntake.isDeleted) {
|
||||||
@@ -408,7 +420,7 @@ class DatabaseSyncService {
|
|||||||
} else {
|
} else {
|
||||||
// Existing intake - update if remote is newer
|
// Existing intake - update if remote is newer
|
||||||
final existingIntake = SupplementIntake.fromMap(existingMaps.first);
|
final existingIntake = SupplementIntake.fromMap(existingMaps.first);
|
||||||
|
|
||||||
if (remoteIntake.lastModified.isAfter(existingIntake.lastModified)) {
|
if (remoteIntake.lastModified.isAfter(existingIntake.lastModified)) {
|
||||||
final intakeToUpdate = remoteIntake.copyWith(id: existingIntake.id);
|
final intakeToUpdate = remoteIntake.copyWith(id: existingIntake.id);
|
||||||
await localDb.update(
|
await localDb.update(
|
||||||
@@ -427,7 +439,7 @@ class DatabaseSyncService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Intake merge completed');
|
print('SupplementsLog: Intake merge completed');
|
||||||
}
|
}
|
||||||
@@ -440,20 +452,20 @@ class DatabaseSyncService {
|
|||||||
where: 'id = ?',
|
where: 'id = ?',
|
||||||
whereArgs: [remoteSupplementId],
|
whereArgs: [remoteSupplementId],
|
||||||
);
|
);
|
||||||
|
|
||||||
if (remoteSupplementMaps.isEmpty) return null;
|
if (remoteSupplementMaps.isEmpty) return null;
|
||||||
|
|
||||||
final remoteSupplement = Supplement.fromMap(remoteSupplementMaps.first);
|
final remoteSupplement = Supplement.fromMap(remoteSupplementMaps.first);
|
||||||
|
|
||||||
// Find the local supplement with the same syncId
|
// Find the local supplement with the same syncId
|
||||||
final localSupplementMaps = await localDb.query(
|
final localSupplementMaps = await localDb.query(
|
||||||
'supplements',
|
'supplements',
|
||||||
where: 'syncId = ?',
|
where: 'syncId = ?',
|
||||||
whereArgs: [remoteSupplement.syncId],
|
whereArgs: [remoteSupplement.syncId],
|
||||||
);
|
);
|
||||||
|
|
||||||
if (localSupplementMaps.isEmpty) return null;
|
if (localSupplementMaps.isEmpty) return null;
|
||||||
|
|
||||||
return localSupplementMaps.first['id'] as int;
|
return localSupplementMaps.first['id'] as int;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -462,27 +474,27 @@ class DatabaseSyncService {
|
|||||||
// Get the local database path
|
// Get the local database path
|
||||||
final localDb = await _databaseHelper.database;
|
final localDb = await _databaseHelper.database;
|
||||||
final dbPath = localDb.path;
|
final dbPath = localDb.path;
|
||||||
|
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Reading database from: $dbPath');
|
print('SupplementsLog: Reading database from: $dbPath');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the database file
|
// Read the database file
|
||||||
final dbFile = io.File(dbPath);
|
final dbFile = io.File(dbPath);
|
||||||
if (!await dbFile.exists()) {
|
if (!await dbFile.exists()) {
|
||||||
throw Exception('Database file not found at: $dbPath');
|
throw Exception('Database file not found at: $dbPath');
|
||||||
}
|
}
|
||||||
|
|
||||||
final dbBytes = await dbFile.readAsBytes();
|
final dbBytes = await dbFile.readAsBytes();
|
||||||
|
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Database file size: ${dbBytes.length} bytes');
|
print('SupplementsLog: Database file size: ${dbBytes.length} bytes');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dbBytes.isEmpty) {
|
if (dbBytes.isEmpty) {
|
||||||
throw Exception('Database file is empty');
|
throw Exception('Database file is empty');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure remote directory exists
|
// Ensure remote directory exists
|
||||||
try {
|
try {
|
||||||
await _client!.readDir(_remotePath!);
|
await _client!.readDir(_remotePath!);
|
||||||
@@ -492,15 +504,15 @@ class DatabaseSyncService {
|
|||||||
}
|
}
|
||||||
await _client!.mkdir(_remotePath!);
|
await _client!.mkdir(_remotePath!);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upload the database file
|
// Upload the database file
|
||||||
final remoteUrl = '$_remotePath$_remoteDbFileName';
|
final remoteUrl = '$_remotePath$_remoteDbFileName';
|
||||||
await _client!.write(remoteUrl, dbBytes);
|
await _client!.write(remoteUrl, dbBytes);
|
||||||
|
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Successfully uploaded database (${dbBytes.length} bytes) to: $remoteUrl');
|
print('SupplementsLog: Successfully uploaded database (${dbBytes.length} bytes) to: $remoteUrl');
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Failed to upload database: $e');
|
print('SupplementsLog: Failed to upload database: $e');
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ void notificationTapBackground(NotificationResponse notificationResponse) {
|
|||||||
print('SupplementsLog: 📱 Payload: ${notificationResponse.payload}');
|
print('SupplementsLog: 📱 Payload: ${notificationResponse.payload}');
|
||||||
print('SupplementsLog: 📱 Notification ID: ${notificationResponse.id}');
|
print('SupplementsLog: 📱 Notification ID: ${notificationResponse.id}');
|
||||||
print('SupplementsLog: 📱 ==========================================');
|
print('SupplementsLog: 📱 ==========================================');
|
||||||
|
|
||||||
// For now, just log the action. The main app handler will process it.
|
// For now, just log the action. The main app handler will process it.
|
||||||
if (notificationResponse.actionId == 'take_supplement') {
|
if (notificationResponse.actionId == 'take_supplement') {
|
||||||
print('SupplementsLog: 📱 BACKGROUND: Take action detected');
|
print('SupplementsLog: 📱 BACKGROUND: Take action detected');
|
||||||
@@ -30,7 +30,7 @@ class NotificationService {
|
|||||||
bool _isInitialized = false;
|
bool _isInitialized = false;
|
||||||
static bool _engineInitialized = false;
|
static bool _engineInitialized = false;
|
||||||
bool _permissionsRequested = false;
|
bool _permissionsRequested = false;
|
||||||
|
|
||||||
// Callback for handling supplement intake from notifications
|
// Callback for handling supplement intake from notifications
|
||||||
Function(int supplementId, String supplementName, double units, String unitType)? _onTakeSupplementCallback;
|
Function(int supplementId, String supplementName, double units, String unitType)? _onTakeSupplementCallback;
|
||||||
|
|
||||||
@@ -45,11 +45,11 @@ class NotificationService {
|
|||||||
print('SupplementsLog: 📱 Already initialized');
|
print('SupplementsLog: 📱 Already initialized');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
print('SupplementsLog: 📱 Initializing timezones...');
|
print('SupplementsLog: 📱 Initializing timezones...');
|
||||||
print('SupplementsLog: 📱 Engine initialized flag: $_engineInitialized');
|
print('SupplementsLog: 📱 Engine initialized flag: $_engineInitialized');
|
||||||
|
|
||||||
if (!_engineInitialized) {
|
if (!_engineInitialized) {
|
||||||
tz.initializeTimeZones();
|
tz.initializeTimeZones();
|
||||||
_engineInitialized = true;
|
_engineInitialized = true;
|
||||||
@@ -61,15 +61,15 @@ class NotificationService {
|
|||||||
print('SupplementsLog: 📱 Warning: Timezone initialization issue (may already be initialized): $e');
|
print('SupplementsLog: 📱 Warning: Timezone initialization issue (may already be initialized): $e');
|
||||||
_engineInitialized = true; // Mark as initialized to prevent retry
|
_engineInitialized = true; // Mark as initialized to prevent retry
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to detect and set the local timezone more reliably
|
// Try to detect and set the local timezone more reliably
|
||||||
try {
|
try {
|
||||||
// First try using the system timezone name
|
// First try using the system timezone name
|
||||||
final String timeZoneName = DateTime.now().timeZoneName;
|
final String timeZoneName = DateTime.now().timeZoneName;
|
||||||
print('SupplementsLog: 📱 System timezone name: $timeZoneName');
|
print('SupplementsLog: 📱 System timezone name: $timeZoneName');
|
||||||
|
|
||||||
tz.Location? location;
|
tz.Location? location;
|
||||||
|
|
||||||
// Try common timezone mappings for your region
|
// Try common timezone mappings for your region
|
||||||
if (timeZoneName.contains('CET') || timeZoneName.contains('CEST')) {
|
if (timeZoneName.contains('CET') || timeZoneName.contains('CEST')) {
|
||||||
location = tz.getLocation('Europe/Amsterdam'); // Netherlands
|
location = tz.getLocation('Europe/Amsterdam'); // Netherlands
|
||||||
@@ -84,19 +84,19 @@ class NotificationService {
|
|||||||
location = tz.getLocation('Europe/Amsterdam');
|
location = tz.getLocation('Europe/Amsterdam');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tz.setLocalLocation(location);
|
tz.setLocalLocation(location);
|
||||||
print('SupplementsLog: 📱 Timezone set to: ${location.name}');
|
print('SupplementsLog: 📱 Timezone set to: ${location.name}');
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('SupplementsLog: 📱 Error setting timezone: $e, using default');
|
print('SupplementsLog: 📱 Error setting timezone: $e, using default');
|
||||||
// Fallback to a reasonable default for Netherlands
|
// Fallback to a reasonable default for Netherlands
|
||||||
tz.setLocalLocation(tz.getLocation('Europe/Amsterdam'));
|
tz.setLocalLocation(tz.getLocation('Europe/Amsterdam'));
|
||||||
}
|
}
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Current local time: ${tz.TZDateTime.now(tz.local)}');
|
print('SupplementsLog: 📱 Current local time: ${tz.TZDateTime.now(tz.local)}');
|
||||||
print('SupplementsLog: 📱 Current system time: ${DateTime.now()}');
|
print('SupplementsLog: 📱 Current system time: ${DateTime.now()}');
|
||||||
|
|
||||||
const AndroidInitializationSettings androidSettings = AndroidInitializationSettings('@mipmap/ic_launcher');
|
const AndroidInitializationSettings androidSettings = AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||||
const DarwinInitializationSettings iosSettings = DarwinInitializationSettings(
|
const DarwinInitializationSettings iosSettings = DarwinInitializationSettings(
|
||||||
requestAlertPermission: false, // We'll request these separately
|
requestAlertPermission: false, // We'll request these separately
|
||||||
@@ -119,10 +119,10 @@ class NotificationService {
|
|||||||
onDidReceiveNotificationResponse: _onNotificationResponse,
|
onDidReceiveNotificationResponse: _onNotificationResponse,
|
||||||
onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
|
onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Test if notification response callback is working
|
// Test if notification response callback is working
|
||||||
print('SupplementsLog: 📱 Callback function is set and ready');
|
print('SupplementsLog: 📱 Callback function is set and ready');
|
||||||
|
|
||||||
_isInitialized = true;
|
_isInitialized = true;
|
||||||
print('SupplementsLog: 📱 NotificationService initialization complete');
|
print('SupplementsLog: 📱 NotificationService initialization complete');
|
||||||
}
|
}
|
||||||
@@ -135,7 +135,7 @@ class NotificationService {
|
|||||||
print('SupplementsLog: 📱 Notification ID: ${response.id}');
|
print('SupplementsLog: 📱 Notification ID: ${response.id}');
|
||||||
print('SupplementsLog: 📱 Input: ${response.input}');
|
print('SupplementsLog: 📱 Input: ${response.input}');
|
||||||
print('SupplementsLog: 📱 ===============================');
|
print('SupplementsLog: 📱 ===============================');
|
||||||
|
|
||||||
if (response.actionId == 'take_supplement') {
|
if (response.actionId == 'take_supplement') {
|
||||||
print('SupplementsLog: 📱 Processing TAKE action...');
|
print('SupplementsLog: 📱 Processing TAKE action...');
|
||||||
_handleTakeAction(response.payload, response.id);
|
_handleTakeAction(response.payload, response.id);
|
||||||
@@ -151,43 +151,58 @@ class NotificationService {
|
|||||||
Future<void> _handleTakeAction(String? payload, int? notificationId) async {
|
Future<void> _handleTakeAction(String? payload, int? notificationId) async {
|
||||||
print('SupplementsLog: 📱 === HANDLING TAKE ACTION ===');
|
print('SupplementsLog: 📱 === HANDLING TAKE ACTION ===');
|
||||||
print('SupplementsLog: 📱 Payload received: $payload');
|
print('SupplementsLog: 📱 Payload received: $payload');
|
||||||
|
|
||||||
if (payload != null) {
|
if (payload != null) {
|
||||||
try {
|
try {
|
||||||
// Parse the payload to get supplement info
|
// Parse the payload to get supplement info
|
||||||
final parts = payload.split('|');
|
final parts = payload.split('|');
|
||||||
print('SupplementsLog: 📱 Payload parts: $parts (length: ${parts.length})');
|
print('SupplementsLog: 📱 Payload parts: $parts (length: ${parts.length})');
|
||||||
|
|
||||||
if (parts.length >= 4) {
|
if (parts.length >= 4) {
|
||||||
final supplementId = int.parse(parts[0]);
|
final supplementId = int.parse(parts[0]);
|
||||||
final supplementName = parts[1];
|
final supplementName = parts[1];
|
||||||
final units = double.parse(parts[2]);
|
final units = double.parse(parts[2]);
|
||||||
final unitType = parts[3];
|
final unitType = parts[3];
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Parsed data:');
|
print('SupplementsLog: 📱 Parsed data:');
|
||||||
print('SupplementsLog: 📱 - ID: $supplementId');
|
print('SupplementsLog: 📱 - ID: $supplementId');
|
||||||
print('SupplementsLog: 📱 - Name: $supplementName');
|
print('SupplementsLog: 📱 - Name: $supplementName');
|
||||||
print('SupplementsLog: 📱 - Units: $units');
|
print('SupplementsLog: 📱 - Units: $units');
|
||||||
print('SupplementsLog: 📱 - Type: $unitType');
|
print('SupplementsLog: 📱 - Type: $unitType');
|
||||||
|
|
||||||
// Call the callback to record the intake
|
// Call the callback to record the intake
|
||||||
if (_onTakeSupplementCallback != null) {
|
if (_onTakeSupplementCallback != null) {
|
||||||
print('SupplementsLog: 📱 Calling supplement callback...');
|
print('SupplementsLog: 📱 Calling supplement callback...');
|
||||||
_onTakeSupplementCallback!(supplementId, supplementName, units, unitType);
|
_onTakeSupplementCallback!(
|
||||||
|
supplementId, supplementName, units, unitType);
|
||||||
print('SupplementsLog: 📱 Callback completed');
|
print('SupplementsLog: 📱 Callback completed');
|
||||||
} else {
|
} else {
|
||||||
print('SupplementsLog: 📱 ERROR: No callback registered!');
|
print('SupplementsLog: 📱 ERROR: No callback registered!');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark notification as taken in database (this will cancel any pending retries)
|
// For retry notifications, the original notification ID is in the payload
|
||||||
if (notificationId != null) {
|
int originalNotificationId;
|
||||||
print('SupplementsLog: 📱 Marking notification $notificationId as taken');
|
if (parts.length > 4 && int.tryParse(parts[4]) != null) {
|
||||||
await DatabaseHelper.instance.markNotificationTaken(notificationId);
|
originalNotificationId = int.parse(parts[4]);
|
||||||
|
print(
|
||||||
// Cancel any pending retry notifications for this notification
|
'SupplementsLog: 📱 Retry notification detected. Original ID: $originalNotificationId');
|
||||||
_cancelRetryNotifications(notificationId);
|
} else if (notificationId != null) {
|
||||||
|
originalNotificationId = notificationId;
|
||||||
|
} else {
|
||||||
|
print(
|
||||||
|
'SupplementsLog: 📱 ERROR: Could not determine notification ID to cancel.');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mark notification as taken in database (this will cancel any pending retries)
|
||||||
|
print(
|
||||||
|
'SupplementsLog: 📱 Marking notification $originalNotificationId as taken');
|
||||||
|
await DatabaseHelper.instance
|
||||||
|
.markNotificationTaken(originalNotificationId);
|
||||||
|
|
||||||
|
// Cancel any pending retry notifications for this notification
|
||||||
|
_cancelRetryNotifications(originalNotificationId);
|
||||||
|
|
||||||
// Show a confirmation notification
|
// Show a confirmation notification
|
||||||
print('SupplementsLog: 📱 Showing confirmation notification...');
|
print('SupplementsLog: 📱 Showing confirmation notification...');
|
||||||
showInstantNotification(
|
showInstantNotification(
|
||||||
@@ -195,7 +210,8 @@ class NotificationService {
|
|||||||
'$supplementName has been recorded at ${DateTime.now().hour.toString().padLeft(2, '0')}:${DateTime.now().minute.toString().padLeft(2, '0')}',
|
'$supplementName has been recorded at ${DateTime.now().hour.toString().padLeft(2, '0')}:${DateTime.now().minute.toString().padLeft(2, '0')}',
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
print('SupplementsLog: 📱 ERROR: Invalid payload format - not enough parts');
|
print(
|
||||||
|
'SupplementsLog: 📱 ERROR: Invalid payload format - not enough parts');
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('SupplementsLog: 📱 ERROR in _handleTakeAction: $e');
|
print('SupplementsLog: 📱 ERROR in _handleTakeAction: $e');
|
||||||
@@ -218,26 +234,26 @@ class NotificationService {
|
|||||||
void _handleSnoozeAction(String? payload, int minutes, int? notificationId) {
|
void _handleSnoozeAction(String? payload, int minutes, int? notificationId) {
|
||||||
print('SupplementsLog: 📱 === HANDLING SNOOZE ACTION ===');
|
print('SupplementsLog: 📱 === HANDLING SNOOZE ACTION ===');
|
||||||
print('SupplementsLog: 📱 Payload: $payload, Minutes: $minutes');
|
print('SupplementsLog: 📱 Payload: $payload, Minutes: $minutes');
|
||||||
|
|
||||||
if (payload != null) {
|
if (payload != null) {
|
||||||
try {
|
try {
|
||||||
final parts = payload.split('|');
|
final parts = payload.split('|');
|
||||||
if (parts.length >= 2) {
|
if (parts.length >= 2) {
|
||||||
final supplementId = int.parse(parts[0]);
|
final supplementId = int.parse(parts[0]);
|
||||||
final supplementName = parts[1];
|
final supplementName = parts[1];
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Snoozing supplement for $minutes minutes: $supplementName');
|
print('SupplementsLog: 📱 Snoozing supplement for $minutes minutes: $supplementName');
|
||||||
|
|
||||||
// Mark notification as snoozed in database (increment retry count)
|
// Mark notification as snoozed in database (increment retry count)
|
||||||
if (notificationId != null) {
|
if (notificationId != null) {
|
||||||
print('SupplementsLog: 📱 Incrementing retry count for notification $notificationId');
|
print('SupplementsLog: 📱 Incrementing retry count for notification $notificationId');
|
||||||
DatabaseHelper.instance.incrementRetryCount(notificationId);
|
DatabaseHelper.instance.incrementRetryCount(notificationId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Schedule a new notification for the snooze time
|
// Schedule a new notification for the snooze time
|
||||||
final snoozeTime = tz.TZDateTime.now(tz.local).add(Duration(minutes: minutes));
|
final snoozeTime = tz.TZDateTime.now(tz.local).add(Duration(minutes: minutes));
|
||||||
print('SupplementsLog: 📱 Snooze time: $snoozeTime');
|
print('SupplementsLog: 📱 Snooze time: $snoozeTime');
|
||||||
|
|
||||||
_notifications.zonedSchedule(
|
_notifications.zonedSchedule(
|
||||||
supplementId * 1000 + minutes, // Unique ID for snooze notifications
|
supplementId * 1000 + minutes, // Unique ID for snooze notifications
|
||||||
'Reminder: $supplementName',
|
'Reminder: $supplementName',
|
||||||
@@ -266,7 +282,7 @@ class NotificationService {
|
|||||||
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
|
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
|
||||||
payload: payload,
|
payload: payload,
|
||||||
);
|
);
|
||||||
|
|
||||||
showInstantNotification(
|
showInstantNotification(
|
||||||
'Reminder Snoozed',
|
'Reminder Snoozed',
|
||||||
'$supplementName reminder snoozed for $minutes minutes',
|
'$supplementName reminder snoozed for $minutes minutes',
|
||||||
@@ -300,32 +316,32 @@ class NotificationService {
|
|||||||
required int maxRetryAttempts,
|
required int maxRetryAttempts,
|
||||||
}) async {
|
}) async {
|
||||||
print('SupplementsLog: 📱 Checking for pending notifications to retry...');
|
print('SupplementsLog: 📱 Checking for pending notifications to retry...');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!persistentReminders) {
|
if (!persistentReminders) {
|
||||||
print('SupplementsLog: 📱 Persistent reminders disabled');
|
print('SupplementsLog: 📱 Persistent reminders disabled');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Retry settings: interval=$reminderRetryInterval min, max=$maxRetryAttempts attempts');
|
print('SupplementsLog: 📱 Retry settings: interval=$reminderRetryInterval min, max=$maxRetryAttempts attempts');
|
||||||
|
|
||||||
// Get all pending notifications from database
|
// Get all pending notifications from database
|
||||||
final pendingNotifications = await DatabaseHelper.instance.getPendingNotifications();
|
final pendingNotifications = await DatabaseHelper.instance.getPendingNotifications();
|
||||||
print('SupplementsLog: 📱 Found ${pendingNotifications.length} pending notifications');
|
print('SupplementsLog: 📱 Found ${pendingNotifications.length} pending notifications');
|
||||||
|
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
|
|
||||||
for (final notification in pendingNotifications) {
|
for (final notification in pendingNotifications) {
|
||||||
final scheduledTime = DateTime.parse(notification['scheduledTime']).toLocal();
|
final scheduledTime = DateTime.parse(notification['scheduledTime']).toLocal();
|
||||||
final retryCount = notification['retryCount'] as int;
|
final retryCount = notification['retryCount'] as int;
|
||||||
final lastRetryTime = notification['lastRetryTime'] != null
|
final lastRetryTime = notification['lastRetryTime'] != null
|
||||||
? DateTime.parse(notification['lastRetryTime']).toLocal()
|
? DateTime.parse(notification['lastRetryTime']).toLocal()
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
// Check if notification is overdue
|
// Check if notification is overdue
|
||||||
final timeSinceScheduled = now.difference(scheduledTime).inMinutes;
|
final timeSinceScheduled = now.difference(scheduledTime).inMinutes;
|
||||||
final shouldRetry = timeSinceScheduled >= reminderRetryInterval;
|
final shouldRetry = timeSinceScheduled >= reminderRetryInterval;
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Checking notification ${notification['notificationId']}:');
|
print('SupplementsLog: 📱 Checking notification ${notification['notificationId']}:');
|
||||||
print('SupplementsLog: 📱 Scheduled: $scheduledTime (local)');
|
print('SupplementsLog: 📱 Scheduled: $scheduledTime (local)');
|
||||||
print('SupplementsLog: 📱 Now: $now');
|
print('SupplementsLog: 📱 Now: $now');
|
||||||
@@ -333,13 +349,13 @@ class NotificationService {
|
|||||||
print('SupplementsLog: 📱 Retry interval: $reminderRetryInterval minutes');
|
print('SupplementsLog: 📱 Retry interval: $reminderRetryInterval minutes');
|
||||||
print('SupplementsLog: 📱 Should retry: $shouldRetry');
|
print('SupplementsLog: 📱 Should retry: $shouldRetry');
|
||||||
print('SupplementsLog: 📱 Retry count: $retryCount / $maxRetryAttempts');
|
print('SupplementsLog: 📱 Retry count: $retryCount / $maxRetryAttempts');
|
||||||
|
|
||||||
// Check if we haven't exceeded max retry attempts
|
// Check if we haven't exceeded max retry attempts
|
||||||
if (retryCount >= maxRetryAttempts) {
|
if (retryCount >= maxRetryAttempts) {
|
||||||
print('SupplementsLog: 📱 Notification ${notification['notificationId']} exceeded max attempts ($maxRetryAttempts)');
|
print('SupplementsLog: 📱 Notification ${notification['notificationId']} exceeded max attempts ($maxRetryAttempts)');
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if enough time has passed since last retry
|
// Check if enough time has passed since last retry
|
||||||
if (lastRetryTime != null) {
|
if (lastRetryTime != null) {
|
||||||
final timeSinceLastRetry = now.difference(lastRetryTime).inMinutes;
|
final timeSinceLastRetry = now.difference(lastRetryTime).inMinutes;
|
||||||
@@ -348,7 +364,7 @@ class NotificationService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldRetry) {
|
if (shouldRetry) {
|
||||||
print('SupplementsLog: 📱 ⚡ SCHEDULING RETRY for notification ${notification['notificationId']}');
|
print('SupplementsLog: 📱 ⚡ SCHEDULING RETRY for notification ${notification['notificationId']}');
|
||||||
await _scheduleRetryNotification(notification, retryCount + 1);
|
await _scheduleRetryNotification(notification, retryCount + 1);
|
||||||
@@ -365,16 +381,16 @@ class NotificationService {
|
|||||||
try {
|
try {
|
||||||
final notificationId = notification['notificationId'] as int;
|
final notificationId = notification['notificationId'] as int;
|
||||||
final supplementId = notification['supplementId'] as int;
|
final supplementId = notification['supplementId'] as int;
|
||||||
|
|
||||||
// Generate a unique ID for this retry (200000 + original_id * 10 + retry_attempt)
|
// Generate a unique ID for this retry (200000 + original_id * 10 + retry_attempt)
|
||||||
final retryNotificationId = 200000 + (notificationId * 10) + retryAttempt;
|
final retryNotificationId = 200000 + (notificationId * 10) + retryAttempt;
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Scheduling retry notification $retryNotificationId for supplement $supplementId (attempt $retryAttempt)');
|
print('SupplementsLog: 📱 Scheduling retry notification $retryNotificationId for supplement $supplementId (attempt $retryAttempt)');
|
||||||
|
|
||||||
// Get supplement details from database
|
// Get supplement details from database
|
||||||
final supplements = await DatabaseHelper.instance.getAllSupplements();
|
final supplements = await DatabaseHelper.instance.getAllSupplements();
|
||||||
final supplement = supplements.firstWhere((s) => s.id == supplementId && s.isActive, orElse: () => throw Exception('Supplement not found'));
|
final supplement = supplements.firstWhere((s) => s.id == supplementId && s.isActive, orElse: () => throw Exception('Supplement not found'));
|
||||||
|
|
||||||
// Schedule the retry notification immediately
|
// Schedule the retry notification immediately
|
||||||
await _notifications.show(
|
await _notifications.show(
|
||||||
retryNotificationId,
|
retryNotificationId,
|
||||||
@@ -404,10 +420,10 @@ class NotificationService {
|
|||||||
),
|
),
|
||||||
payload: '${supplement.id}|${supplement.name}|${supplement.numberOfUnits}|${supplement.unitType}|$notificationId',
|
payload: '${supplement.id}|${supplement.name}|${supplement.numberOfUnits}|${supplement.unitType}|$notificationId',
|
||||||
);
|
);
|
||||||
|
|
||||||
// Update the retry count in database
|
// Update the retry count in database
|
||||||
await DatabaseHelper.instance.incrementRetryCount(notificationId);
|
await DatabaseHelper.instance.incrementRetryCount(notificationId);
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Retry notification scheduled successfully');
|
print('SupplementsLog: 📱 Retry notification scheduled successfully');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('SupplementsLog: 📱 Error scheduling retry notification: $e');
|
print('SupplementsLog: 📱 Error scheduling retry notification: $e');
|
||||||
@@ -420,10 +436,10 @@ class NotificationService {
|
|||||||
print('SupplementsLog: 📱 Permissions already requested');
|
print('SupplementsLog: 📱 Permissions already requested');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
_permissionsRequested = true;
|
_permissionsRequested = true;
|
||||||
|
|
||||||
final androidPlugin = _notifications.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
|
final androidPlugin = _notifications.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
|
||||||
if (androidPlugin != null) {
|
if (androidPlugin != null) {
|
||||||
print('SupplementsLog: 📱 Requesting Android permissions...');
|
print('SupplementsLog: 📱 Requesting Android permissions...');
|
||||||
@@ -434,7 +450,7 @@ class NotificationService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final iosPlugin = _notifications.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>();
|
final iosPlugin = _notifications.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>();
|
||||||
if (iosPlugin != null) {
|
if (iosPlugin != null) {
|
||||||
print('SupplementsLog: 📱 Requesting iOS permissions...');
|
print('SupplementsLog: 📱 Requesting iOS permissions...');
|
||||||
@@ -449,7 +465,7 @@ class NotificationService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
print('SupplementsLog: 📱 All permissions granted successfully');
|
print('SupplementsLog: 📱 All permissions granted successfully');
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -462,7 +478,7 @@ class NotificationService {
|
|||||||
Future<void> scheduleSupplementReminders(Supplement supplement) async {
|
Future<void> scheduleSupplementReminders(Supplement supplement) async {
|
||||||
print('SupplementsLog: 📱 Scheduling reminders for ${supplement.name}');
|
print('SupplementsLog: 📱 Scheduling reminders for ${supplement.name}');
|
||||||
print('SupplementsLog: 📱 Reminder times: ${supplement.reminderTimes}');
|
print('SupplementsLog: 📱 Reminder times: ${supplement.reminderTimes}');
|
||||||
|
|
||||||
// Cancel existing notifications for this supplement
|
// Cancel existing notifications for this supplement
|
||||||
await cancelSupplementReminders(supplement.id!);
|
await cancelSupplementReminders(supplement.id!);
|
||||||
|
|
||||||
@@ -474,7 +490,7 @@ class NotificationService {
|
|||||||
|
|
||||||
final notificationId = supplement.id! * 100 + i; // Unique ID for each reminder
|
final notificationId = supplement.id! * 100 + i; // Unique ID for each reminder
|
||||||
final scheduledTime = _nextInstanceOfTime(hour, minute);
|
final scheduledTime = _nextInstanceOfTime(hour, minute);
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Scheduling notification ID $notificationId for ${timeStr} -> ${scheduledTime}');
|
print('SupplementsLog: 📱 Scheduling notification ID $notificationId for ${timeStr} -> ${scheduledTime}');
|
||||||
|
|
||||||
// Track this notification in the database
|
// Track this notification in the database
|
||||||
@@ -505,7 +521,7 @@ class NotificationService {
|
|||||||
),
|
),
|
||||||
AndroidNotificationAction(
|
AndroidNotificationAction(
|
||||||
'snooze_10',
|
'snooze_10',
|
||||||
'Snooze 10min',
|
'Snooze 10min',
|
||||||
icon: DrawableResourceAndroidBitmap('@android:drawable/ic_menu_recent_history'),
|
icon: DrawableResourceAndroidBitmap('@android:drawable/ic_menu_recent_history'),
|
||||||
showsUserInterface: true, // Changed to true to open app
|
showsUserInterface: true, // Changed to true to open app
|
||||||
),
|
),
|
||||||
@@ -517,10 +533,10 @@ class NotificationService {
|
|||||||
matchDateTimeComponents: DateTimeComponents.time,
|
matchDateTimeComponents: DateTimeComponents.time,
|
||||||
payload: '${supplement.id}|${supplement.name}|${supplement.numberOfUnits}|${supplement.unitType}',
|
payload: '${supplement.id}|${supplement.name}|${supplement.numberOfUnits}|${supplement.unitType}',
|
||||||
);
|
);
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Successfully scheduled notification ID $notificationId');
|
print('SupplementsLog: 📱 Successfully scheduled notification ID $notificationId');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all pending notifications to verify
|
// Get all pending notifications to verify
|
||||||
final pendingNotifications = await _notifications.pendingNotificationRequests();
|
final pendingNotifications = await _notifications.pendingNotificationRequests();
|
||||||
print('SupplementsLog: 📱 Total pending notifications: ${pendingNotifications.length}');
|
print('SupplementsLog: 📱 Total pending notifications: ${pendingNotifications.length}');
|
||||||
@@ -535,7 +551,7 @@ class NotificationService {
|
|||||||
final notificationId = supplementId * 100 + i;
|
final notificationId = supplementId * 100 + i;
|
||||||
await _notifications.cancel(notificationId);
|
await _notifications.cancel(notificationId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also clean up database tracking records for this supplement
|
// Also clean up database tracking records for this supplement
|
||||||
await DatabaseHelper.instance.clearNotificationTracking(supplementId);
|
await DatabaseHelper.instance.clearNotificationTracking(supplementId);
|
||||||
}
|
}
|
||||||
@@ -547,18 +563,18 @@ class NotificationService {
|
|||||||
tz.TZDateTime _nextInstanceOfTime(int hour, int minute) {
|
tz.TZDateTime _nextInstanceOfTime(int hour, int minute) {
|
||||||
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
|
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
|
||||||
tz.TZDateTime scheduledDate = tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, minute);
|
tz.TZDateTime scheduledDate = tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, minute);
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Current time: $now (${now.timeZoneName})');
|
print('SupplementsLog: 📱 Current time: $now (${now.timeZoneName})');
|
||||||
print('SupplementsLog: 📱 Target time: ${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}');
|
print('SupplementsLog: 📱 Target time: ${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}');
|
||||||
print('SupplementsLog: 📱 Initial scheduled date: $scheduledDate (${scheduledDate.timeZoneName})');
|
print('SupplementsLog: 📱 Initial scheduled date: $scheduledDate (${scheduledDate.timeZoneName})');
|
||||||
|
|
||||||
if (scheduledDate.isBefore(now)) {
|
if (scheduledDate.isBefore(now)) {
|
||||||
scheduledDate = scheduledDate.add(const Duration(days: 1));
|
scheduledDate = scheduledDate.add(const Duration(days: 1));
|
||||||
print('SupplementsLog: 📱 Time has passed, scheduling for tomorrow: $scheduledDate (${scheduledDate.timeZoneName})');
|
print('SupplementsLog: 📱 Time has passed, scheduling for tomorrow: $scheduledDate (${scheduledDate.timeZoneName})');
|
||||||
} else {
|
} else {
|
||||||
print('SupplementsLog: 📱 Time is in the future, scheduling for today: $scheduledDate (${scheduledDate.timeZoneName})');
|
print('SupplementsLog: 📱 Time is in the future, scheduling for today: $scheduledDate (${scheduledDate.timeZoneName})');
|
||||||
}
|
}
|
||||||
|
|
||||||
return scheduledDate;
|
return scheduledDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -595,9 +611,9 @@ class NotificationService {
|
|||||||
print('SupplementsLog: 📱 Testing scheduled notification...');
|
print('SupplementsLog: 📱 Testing scheduled notification...');
|
||||||
final now = tz.TZDateTime.now(tz.local);
|
final now = tz.TZDateTime.now(tz.local);
|
||||||
final testTime = now.add(const Duration(minutes: 1));
|
final testTime = now.add(const Duration(minutes: 1));
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Scheduling test notification for: $testTime');
|
print('SupplementsLog: 📱 Scheduling test notification for: $testTime');
|
||||||
|
|
||||||
await _notifications.zonedSchedule(
|
await _notifications.zonedSchedule(
|
||||||
99999, // Special ID for test notifications
|
99999, // Special ID for test notifications
|
||||||
'Test Scheduled Notification',
|
'Test Scheduled Notification',
|
||||||
@@ -615,7 +631,7 @@ class NotificationService {
|
|||||||
),
|
),
|
||||||
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
|
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
|
||||||
);
|
);
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Test notification scheduled successfully');
|
print('SupplementsLog: 📱 Test notification scheduled successfully');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -627,7 +643,7 @@ class NotificationService {
|
|||||||
// Debug function to test notification actions
|
// Debug function to test notification actions
|
||||||
Future<void> testNotificationWithActions() async {
|
Future<void> testNotificationWithActions() async {
|
||||||
print('SupplementsLog: 📱 Creating test notification with actions...');
|
print('SupplementsLog: 📱 Creating test notification with actions...');
|
||||||
|
|
||||||
await _notifications.show(
|
await _notifications.show(
|
||||||
88888, // Special test ID
|
88888, // Special test ID
|
||||||
'Test Action Notification',
|
'Test Action Notification',
|
||||||
@@ -658,14 +674,14 @@ class NotificationService {
|
|||||||
),
|
),
|
||||||
payload: '999|Test Supplement|1.0|capsule',
|
payload: '999|Test Supplement|1.0|capsule',
|
||||||
);
|
);
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Test notification with actions created');
|
print('SupplementsLog: 📱 Test notification with actions created');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug function to test basic notification tap response
|
// Debug function to test basic notification tap response
|
||||||
Future<void> testBasicNotification() async {
|
Future<void> testBasicNotification() async {
|
||||||
print('SupplementsLog: 📱 Creating basic test notification...');
|
print('SupplementsLog: 📱 Creating basic test notification...');
|
||||||
|
|
||||||
await _notifications.show(
|
await _notifications.show(
|
||||||
77777, // Special test ID for basic notification
|
77777, // Special test ID for basic notification
|
||||||
'Basic Test Notification',
|
'Basic Test Notification',
|
||||||
@@ -682,7 +698,7 @@ class NotificationService {
|
|||||||
),
|
),
|
||||||
payload: 'basic_test',
|
payload: 'basic_test',
|
||||||
);
|
);
|
||||||
|
|
||||||
print('SupplementsLog: 📱 Basic test notification created');
|
print('SupplementsLog: 📱 Basic test notification created');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user