9 Commits

Author SHA1 Message Date
99711d56ec chore: bump version to 1.0.9+31082025 2025-08-31 20:12:39 +02:00
6bd80780e6 feat: Enhance HistoryScreen with month picker and improved intake display 2025-08-31 20:12:29 +02:00
7c63eb473b feat: Update theme colors and add Today Schedule screen
- Changed color scheme from ShadBlueColorScheme to ShadZincColorScheme in main.dart
- Added getMostRecentIntake method in SupplementProvider to retrieve the latest intake for a supplement
- Integrated TodayScheduleScreen into HomeScreen with a new BottomNavigationBar item
- Updated SupplementsListScreen title and adjusted layout for better UX
- Enhanced SupplementCard to support undoing last taken action and improved popover menu options
- Added popover package for better UI interactions
2025-08-31 20:00:32 +02:00
666008f05d Refactor and enhance UI components across multiple screens
- Updated date and time formatting in debug notifications screen for clarity.
- Wrapped context-dependent state updates in post-frame callbacks in history screen to ensure proper context usage.
- Improved layout and styling in settings screen by reordering radio list tiles.
- Enhanced logging in auto sync service for better error tracking.
- Added context mounted checks in notification router to prevent errors during navigation.
- Updated bulk take dialog to use new UI components from shadcn_ui package.
- Refactored take supplement dialog to utilize shadcn_ui for a more modern look and feel.
- Adjusted info chip and supplement card widgets to use updated color schemes and layouts.
- Updated pubspec.yaml and pubspec.lock to include new dependencies and versions.
2025-08-31 19:15:32 +02:00
6a2085f4e6 refactor: update color opacity methods to use withValues for consistency 2025-08-31 18:32:57 +02:00
62debb6a7c chore: bump version to 1.0.8+30082025 2025-08-30 01:53:58 +02:00
f7966ce587 feat: Implement snooze functionality for notifications
- Added snooze duration setting in SettingsScreen.
- Created DebugNotificationsScreen to view pending notifications and logs.
- Integrated notification logging with NotificationDebugStore.
- Enhanced SimpleNotificationService to handle snooze actions and log notifications.
- Removed ProfileSetupScreen as it is no longer needed.
- Updated NotificationRouter to manage snooze actions without UI.
- Refactored settings provider to include snooze duration management.
2025-08-30 01:51:38 +02:00
811c1f3d6a chore: bump version to 1.0.7+30082025 2025-08-30 01:03:22 +02:00
6dccac6124 notification overhaul 2025-08-30 00:12:29 +02:00
34 changed files with 3326 additions and 5377 deletions

14
.kilocode/mcp.json Normal file
View File

@@ -0,0 +1,14 @@
{
"mcpServers": {
"context7": {
"command": "npx",
"args": [
"-y",
"@upstash/context7-mcp"
],
"env": {
"DEFAULT_MINIMUM_TOKENS": ""
}
}
}
}

9
.zed/debug.json Normal file
View File

@@ -0,0 +1,9 @@
[
{
"label": "Debug Flutter App",
"adapter": "Dart",
"type": "flutter",
"program": "lib/main.dart",
"args": ["--web-port=9090"]
}
]

View File

@@ -4,12 +4,20 @@
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" /> <uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" /> <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"/>
<application <application
android:label="supplements" android:label="My Supplements"
android:name="${applicationName}" android:name="${applicationName}"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:enableOnBackInvokedCallback="true"> android:enableOnBackInvokedCallback="true">
<service android:name="com.gdelataillade.alarm.services.NotificationOnKillService" />
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:exported="true" android:exported="true"

View File

@@ -1,896 +0,0 @@
{
"source": "Health Canada Dietary Reference Intakes (vitamins & elements)",
"note": "Values come from Health Canada DRI tables. rda_type denotes whether the value is an RDA or an AI as published.",
"nutrients": {
"vitamin_a": {
"unit": "µg/day (RAE)",
"rda_type": "RDA/AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 400, "ul": 600 },
{ "age_range": "7-12 mo", "sex": "infant", "value": 500, "ul": 600 },
{ "age_range": "1-3 y", "sex": "both", "value": 300, "ul": 600 },
{ "age_range": "4-8 y", "sex": "both", "value": 400, "ul": 900 },
{ "age_range": "9-13 y", "sex": "male", "value": 600, "ul": 1700 },
{ "age_range": "9-13 y", "sex": "female", "value": 600, "ul": 1700 },
{ "age_range": "14-18 y", "sex": "male", "value": 900, "ul": 2800 },
{ "age_range": "14-18 y", "sex": "female", "value": 700, "ul": 2800 },
{ "age_range": "19-30 y", "sex": "male", "value": 900, "ul": 3000 },
{ "age_range": "19-30 y", "sex": "female", "value": 700, "ul": 3000 },
{ "age_range": "31-50 y", "sex": "male", "value": 900, "ul": 3000 },
{ "age_range": "31-50 y", "sex": "female", "value": 700, "ul": 3000 },
{ "age_range": "51-70 y", "sex": "male", "value": 900, "ul": 3000 },
{ "age_range": "51-70 y", "sex": "female", "value": 700, "ul": 3000 },
{ "age_range": ">70 y", "sex": "male", "value": 900, "ul": 3000 },
{ "age_range": ">70 y", "sex": "female", "value": 700, "ul": 3000 },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 750,
"ul": 2800
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 770,
"ul": 3000
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 1200,
"ul": 2800
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 1300,
"ul": 3000
}
]
},
"vitamin_c": {
"unit": "mg/day",
"rda_type": "RDA/AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 40, "ul": null },
{ "age_range": "7-12 mo", "sex": "infant", "value": 50, "ul": null },
{ "age_range": "1-3 y", "sex": "both", "value": 15, "ul": 400 },
{ "age_range": "4-8 y", "sex": "both", "value": 25, "ul": 650 },
{ "age_range": "9-13 y", "sex": "male", "value": 45, "ul": 1200 },
{ "age_range": "9-13 y", "sex": "female", "value": 45, "ul": 1200 },
{ "age_range": "14-18 y", "sex": "male", "value": 75, "ul": 1800 },
{ "age_range": "14-18 y", "sex": "female", "value": 65, "ul": 1800 },
{ "age_range": "19-30 y", "sex": "male", "value": 75, "ul": 1800 },
{ "age_range": "19-30 y", "sex": "female", "value": 90, "ul": 2000 },
{ "age_range": "31-50 y", "sex": "male", "value": 75, "ul": 2000 },
{ "age_range": "31-50 y", "sex": "female", "value": 75, "ul": 2000 },
{ "age_range": "51-70 y", "sex": "both", "value": 75, "ul": 2000 },
{ "age_range": ">70 y", "sex": "both", "value": 75, "ul": 2000 },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 80,
"ul": 1800
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 85,
"ul": 2000
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 115,
"ul": 1800
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 120,
"ul": 2000
}
]
},
"vitamin_d": {
"unit": "µg/day",
"rda_type": "RDA/AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 10, "ul": 25 },
{ "age_range": "7-12 mo", "sex": "infant", "value": 15, "ul": 25 },
{ "age_range": "1-3 y", "sex": "both", "value": 15, "ul": 50 },
{ "age_range": "4-8 y", "sex": "both", "value": 15, "ul": 50 },
{ "age_range": "9-13 y", "sex": "both", "value": 15, "ul": 100 },
{ "age_range": "14-18 y", "sex": "both", "value": 15, "ul": 100 },
{ "age_range": "19-30 y", "sex": "both", "value": 15, "ul": 100 },
{ "age_range": "31-50 y", "sex": "both", "value": 15, "ul": 100 },
{ "age_range": "51-70 y", "sex": "both", "value": 20, "ul": 100 },
{ "age_range": ">70 y", "sex": "both", "value": 20, "ul": 100 },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 15,
"ul": 100
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 15,
"ul": 100
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 15,
"ul": 100
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 15,
"ul": 100
}
]
},
"vitamin_e": {
"unit": "mg/day (alpha-tocopherol)",
"rda_type": "RDA/AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 4, "ul": null },
{ "age_range": "7-12 mo", "sex": "infant", "value": 5, "ul": null },
{ "age_range": "1-3 y", "sex": "both", "value": 6, "ul": 200 },
{ "age_range": "4-8 y", "sex": "both", "value": 7, "ul": 300 },
{ "age_range": "9-13 y", "sex": "both", "value": 11, "ul": 600 },
{ "age_range": "14-18 y", "sex": "both", "value": 15, "ul": 800 },
{ "age_range": "19-30 y", "sex": "both", "value": 15, "ul": 800 },
{ "age_range": "31-50 y", "sex": "both", "value": 15, "ul": 800 },
{ "age_range": "51-70 y", "sex": "both", "value": 15, "ul": 800 },
{ "age_range": ">70 y", "sex": "both", "value": 15, "ul": 800 },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 19,
"ul": 800
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 19,
"ul": 800
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 19,
"ul": 800
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 19,
"ul": 800
}
]
},
"vitamin_k": {
"unit": "µg/day",
"rda_type": "AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 2, "ul": null },
{ "age_range": "7-12 mo", "sex": "infant", "value": 2.5, "ul": null },
{ "age_range": "1-3 y", "sex": "both", "value": 30, "ul": null },
{ "age_range": "4-8 y", "sex": "both", "value": 55, "ul": null },
{ "age_range": "9-13 y", "sex": "both", "value": 60, "ul": null },
{ "age_range": "14-18 y", "sex": "male", "value": 75, "ul": null },
{ "age_range": "14-18 y", "sex": "female", "value": 75, "ul": null },
{ "age_range": "19-30 y", "sex": "male", "value": 75, "ul": null },
{ "age_range": "19-30 y", "sex": "female", "value": 75, "ul": null },
{ "age_range": "31-50 y", "sex": "male", "value": 75, "ul": null },
{ "age_range": "31-50 y", "sex": "female", "value": 75, "ul": null },
{ "age_range": "51-70 y", "sex": "male", "value": 75, "ul": null },
{ "age_range": "51-70 y", "sex": "female", "value": 75, "ul": null },
{ "age_range": ">70 y", "sex": "male", "value": 75, "ul": null },
{ "age_range": ">70 y", "sex": "female", "value": 75, "ul": null },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 75,
"ul": null
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 75,
"ul": null
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 75,
"ul": null
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 75,
"ul": null
}
]
},
"vitamin_b1": {
"unit": "mg/day",
"rda_type": "RDA/AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 0.2, "ul": null },
{ "age_range": "7-12 mo", "sex": "infant", "value": 0.3, "ul": null },
{ "age_range": "1-3 y", "sex": "both", "value": 0.5, "ul": null },
{ "age_range": "4-8 y", "sex": "both", "value": 0.6, "ul": null },
{ "age_range": "9-13 y", "sex": "male", "value": 0.9, "ul": null },
{ "age_range": "9-13 y", "sex": "female", "value": 0.9, "ul": null },
{ "age_range": "14-18 y", "sex": "male", "value": 1.2, "ul": null },
{ "age_range": "14-18 y", "sex": "female", "value": 1.0, "ul": null },
{ "age_range": "19-30 y", "sex": "male", "value": 1.2, "ul": null },
{ "age_range": "19-30 y", "sex": "female", "value": 1.1, "ul": null },
{ "age_range": "31-50 y", "sex": "male", "value": 1.2, "ul": null },
{ "age_range": "31-50 y", "sex": "female", "value": 1.1, "ul": null },
{ "age_range": "51-70 y", "sex": "male", "value": 1.2, "ul": null },
{ "age_range": "51-70 y", "sex": "female", "value": 1.1, "ul": null },
{ "age_range": ">70 y", "sex": "male", "value": 1.2, "ul": null },
{ "age_range": ">70 y", "sex": "female", "value": 1.1, "ul": null },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 1.4,
"ul": null
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 1.4,
"ul": null
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 1.5,
"ul": null
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 1.5,
"ul": null
}
]
},
"vitamin_b2": {
"unit": "mg/day",
"rda_type": "RDA/AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 0.3, "ul": null },
{ "age_range": "7-12 mo", "sex": "infant", "value": 0.4, "ul": null },
{ "age_range": "1-3 y", "sex": "both", "value": 0.5, "ul": null },
{ "age_range": "4-8 y", "sex": "both", "value": 0.6, "ul": null },
{ "age_range": "9-13 y", "sex": "male", "value": 0.9, "ul": null },
{ "age_range": "9-13 y", "sex": "female", "value": 0.9, "ul": null },
{ "age_range": "14-18 y", "sex": "male", "value": 1.3, "ul": null },
{ "age_range": "14-18 y", "sex": "female", "value": 1.0, "ul": null },
{ "age_range": "19-30 y", "sex": "male", "value": 1.3, "ul": null },
{ "age_range": "19-30 y", "sex": "female", "value": 1.1, "ul": null },
{ "age_range": "31-50 y", "sex": "male", "value": 1.3, "ul": null },
{ "age_range": "31-50 y", "sex": "female", "value": 1.1, "ul": null },
{ "age_range": "51-70 y", "sex": "male", "value": 1.3, "ul": null },
{ "age_range": "51-70 y", "sex": "female", "value": 1.1, "ul": null },
{ "age_range": ">70 y", "sex": "male", "value": 1.3, "ul": null },
{ "age_range": ">70 y", "sex": "female", "value": 1.1, "ul": null },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 1.4,
"ul": null
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 1.4,
"ul": null
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 1.6,
"ul": null
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 1.6,
"ul": null
}
]
},
"vitamin_b3": {
"unit": "mg/day (niacin equivalents, NE)",
"rda_type": "RDA/AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 2, "ul": null },
{ "age_range": "7-12 mo", "sex": "infant", "value": 4, "ul": null },
{ "age_range": "1-3 y", "sex": "both", "value": 6, "ul": 10 },
{ "age_range": "4-8 y", "sex": "both", "value": 8, "ul": 15 },
{ "age_range": "9-13 y", "sex": "both", "value": 12, "ul": 20 },
{ "age_range": "14-18 y", "sex": "male", "value": 16, "ul": 30 },
{ "age_range": "14-18 y", "sex": "female", "value": 14, "ul": 30 },
{ "age_range": "19-30 y", "sex": "male", "value": 16, "ul": 35 },
{ "age_range": "19-30 y", "sex": "female", "value": 14, "ul": 35 },
{ "age_range": "31-50 y", "sex": "male", "value": 16, "ul": 35 },
{ "age_range": "31-50 y", "sex": "female", "value": 14, "ul": 35 },
{ "age_range": "51-70 y", "sex": "male", "value": 16, "ul": 35 },
{ "age_range": "51-70 y", "sex": "female", "value": 14, "ul": 35 },
{ "age_range": ">70 y", "sex": "male", "value": 16, "ul": 35 },
{ "age_range": ">70 y", "sex": "female", "value": 14, "ul": 35 },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 18,
"ul": 35
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 18,
"ul": 35
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 17,
"ul": 35
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 17,
"ul": 35
}
]
},
"vitamin_b5": {
"unit": "mg/day",
"rda_type": "AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 1.7, "ul": null },
{ "age_range": "7-12 mo", "sex": "infant", "value": 1.8, "ul": null },
{ "age_range": "1-3 y", "sex": "both", "value": 2.0, "ul": null },
{ "age_range": "4-8 y", "sex": "both", "value": 3.0, "ul": null },
{ "age_range": "9-13 y", "sex": "both", "value": 4.0, "ul": null },
{ "age_range": "14-18 y", "sex": "both", "value": 5.0, "ul": null },
{ "age_range": "19-30 y", "sex": "both", "value": 5.0, "ul": null },
{ "age_range": "31-50 y", "sex": "both", "value": 5.0, "ul": null },
{ "age_range": "51-70 y", "sex": "both", "value": 5.0, "ul": null },
{ "age_range": ">70 y", "sex": "both", "value": 5.0, "ul": null },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 6.0,
"ul": null
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 6.0,
"ul": null
},
{
"age_range": "pregnancy 31-50 y",
"sex": "female",
"value": 6.0,
"ul": null
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 7.0,
"ul": null
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 7.0,
"ul": null
},
{
"age_range": "lactation 31-50 y",
"sex": "female",
"value": 7.0,
"ul": null
}
]
},
"vitamin_b6": {
"unit": "mg/day",
"rda_type": "RDA/AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 0.1, "ul": null },
{ "age_range": "7-12 mo", "sex": "infant", "value": 0.3, "ul": null },
{ "age_range": "1-3 y", "sex": "both", "value": 0.5, "ul": 30 },
{ "age_range": "4-8 y", "sex": "both", "value": 0.6, "ul": 40 },
{ "age_range": "9-13 y", "sex": "both", "value": 1.0, "ul": 60 },
{ "age_range": "14-18 y", "sex": "male", "value": 1.3, "ul": 80 },
{ "age_range": "14-18 y", "sex": "female", "value": 1.2, "ul": 80 },
{ "age_range": "19-50 y", "sex": "male", "value": 1.3, "ul": 100 },
{ "age_range": "19-50 y", "sex": "female", "value": 1.3, "ul": 100 },
{ "age_range": "51-70 y", "sex": "male", "value": 1.7, "ul": 100 },
{ "age_range": "51-70 y", "sex": "female", "value": 1.5, "ul": 100 },
{ "age_range": ">70 y", "sex": "male", "value": 1.7, "ul": 100 },
{ "age_range": ">70 y", "sex": "female", "value": 1.5, "ul": 100 },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 1.9,
"ul": 80
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 1.9,
"ul": 80
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 2.0,
"ul": 100
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 2.0,
"ul": 100
}
]
},
"vitamin_b12": {
"unit": "µg/day",
"rda_type": "RDA/AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 0.4, "ul": null },
{ "age_range": "7-12 mo", "sex": "infant", "value": 0.5, "ul": null },
{ "age_range": "1-3 y", "sex": "both", "value": 0.9, "ul": null },
{ "age_range": "4-8 y", "sex": "both", "value": 1.2, "ul": null },
{ "age_range": "9-13 y", "sex": "both", "value": 1.8, "ul": null },
{ "age_range": "14-18 y", "sex": "both", "value": 2.4, "ul": null },
{ "age_range": "19-30 y", "sex": "both", "value": 2.4, "ul": null },
{ "age_range": "31-50 y", "sex": "both", "value": 2.4, "ul": null },
{ "age_range": "51-70 y", "sex": "both", "value": 2.4, "ul": null },
{ "age_range": ">70 y", "sex": "both", "value": 2.4, "ul": null },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 2.6,
"ul": null
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 2.6,
"ul": null
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 2.8,
"ul": null
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 2.8,
"ul": null
}
]
},
"iron": {
"unit": "mg/day",
"rda_type": "RDA/AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 0.27, "ul": 40 },
{ "age_range": "7-12 mo", "sex": "infant", "value": 11, "ul": 40 },
{ "age_range": "1-3 y", "sex": "both", "value": 7, "ul": 40 },
{ "age_range": "4-8 y", "sex": "both", "value": 10, "ul": 40 },
{ "age_range": "9-13 y", "sex": "male", "value": 8, "ul": 45 },
{ "age_range": "9-13 y", "sex": "female", "value": 8, "ul": 45 },
{ "age_range": "14-18 y", "sex": "male", "value": 11, "ul": 45 },
{ "age_range": "14-18 y", "sex": "female", "value": 15, "ul": 45 },
{ "age_range": "19-30 y", "sex": "male", "value": 8, "ul": 45 },
{ "age_range": "19-30 y", "sex": "female", "value": 18, "ul": 45 },
{ "age_range": "31-50 y", "sex": "male", "value": 8, "ul": 45 },
{ "age_range": "31-50 y", "sex": "female", "value": 18, "ul": 45 },
{ "age_range": "51-70 y", "sex": "male", "value": 8, "ul": 45 },
{ "age_range": "51-70 y", "sex": "female", "value": 8, "ul": 45 },
{ "age_range": ">70 y", "sex": "male", "value": 8, "ul": 45 },
{ "age_range": ">70 y", "sex": "female", "value": 8, "ul": 45 },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 27,
"ul": 45
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 27,
"ul": 45
},
{
"age_range": "pregnancy 31-50 y",
"sex": "female",
"value": 27,
"ul": 45
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 10,
"ul": 45
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 9,
"ul": 45
},
{
"age_range": "lactation 31-50 y",
"sex": "female",
"value": 9,
"ul": 45
}
]
},
"magnesium": {
"unit": "mg/day",
"rda_type": "RDA/AI",
"note": "UL represents intake from pharmacological agents only, not food or water.",
"life_stages": [
{
"age_range": "0-6 mo",
"sex": "infant",
"value": 30,
"rda_type": "AI",
"ul": 40
},
{
"age_range": "7-12 mo",
"sex": "infant",
"value": 75,
"rda_type": "AI",
"ul": 40
},
{
"age_range": "1-3 y",
"sex": "both",
"value": 80,
"rda_type": "RDA",
"ul": 65
},
{
"age_range": "4-8 y",
"sex": "both",
"value": 130,
"rda_type": "RDA",
"ul": 110
},
{
"age_range": "9-13 y",
"sex": "male",
"value": 240,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "9-13 y",
"sex": "female",
"value": 240,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "14-18 y",
"sex": "male",
"value": 410,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "14-18 y",
"sex": "female",
"value": 360,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "19-30 y",
"sex": "male",
"value": 400,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "19-30 y",
"sex": "female",
"value": 310,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "31-50 y",
"sex": "male",
"value": 420,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "31-50 y",
"sex": "female",
"value": 320,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "51-70 y",
"sex": "male",
"value": 420,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "51-70 y",
"sex": "female",
"value": 320,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": ">70 y",
"sex": "male",
"value": 420,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": ">70 y",
"sex": "female",
"value": 320,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 400,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 350,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "pregnancy 31-50 y",
"sex": "female",
"value": 360,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 360,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 320,
"rda_type": "RDA",
"ul": 350
},
{
"age_range": "lactation 31-50 y",
"sex": "female",
"value": 320,
"rda_type": "RDA",
"ul": 350
}
]
},
"zinc": {
"unit": "mg/day",
"rda_type": "RDA/AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 2, "ul": 4 },
{ "age_range": "7-12 mo", "sex": "infant", "value": 3, "ul": 5 },
{ "age_range": "1-3 y", "sex": "both", "value": 3, "ul": 7 },
{ "age_range": "4-8 y", "sex": "both", "value": 5, "ul": 12 },
{ "age_range": "9-13 y", "sex": "male", "value": 8, "ul": 23 },
{ "age_range": "9-13 y", "sex": "female", "value": 8, "ul": 23 },
{ "age_range": "14-18 y", "sex": "male", "value": 11, "ul": 34 },
{ "age_range": "14-18 y", "sex": "female", "value": 9, "ul": 34 },
{ "age_range": "19-30 y", "sex": "male", "value": 11, "ul": 40 },
{ "age_range": "19-30 y", "sex": "female", "value": 8, "ul": 40 },
{ "age_range": "31-50 y", "sex": "male", "value": 11, "ul": 40 },
{ "age_range": "31-50 y", "sex": "female", "value": 8, "ul": 40 },
{ "age_range": "51-70 y", "sex": "male", "value": 11, "ul": 40 },
{ "age_range": "51-70 y", "sex": "female", "value": 8, "ul": 40 },
{ "age_range": ">70 y", "sex": "male", "value": 11, "ul": 40 },
{ "age_range": ">70 y", "sex": "female", "value": 8, "ul": 40 },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 12,
"ul": 40
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 11,
"ul": 40
},
{
"age_range": "pregnancy 31-50 y",
"sex": "female",
"value": 11,
"ul": 40
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 13,
"ul": 40
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 12,
"ul": 40
},
{
"age_range": "lactation 31-50 y",
"sex": "female",
"value": 12,
"ul": 40
}
]
},
"creatine": {
"unit": "g/day",
"rda_type": "Recommended Dosage",
"note": "Creatine monohydrate supplementation typically involves a loading phase of 20-25 g/day divided into 4-5 g doses for 5-7 days, followed by a maintenance dose of 3-5 g/day. The UL is not a strict daily maximum but based on safe dosages studied for various durations.",
"life_stages": [
{
"age_range": "adult",
"sex": "both",
"value_min": 3,
"value_max": 5,
"description": "Maintenance dose (typical daily intake)"
},
{
"age_range": "adult",
"sex": "both",
"value": 20,
"description": "Loading phase daily dose (divided into 4-5 g servings for 5-7 days)"
}
],
"ul": {
"value": 25,
"unit": "g/day",
"duration": "up to 14 days",
"note": "Doses up to 25 g/day for up to 14 days have been safely used. Long-term doses of 3-5 g/day are considered safe."
}
},
"manganese": {
"unit": "mg/day",
"rda_type": "AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 0.003, "ul": null },
{ "age_range": "7-12 mo", "sex": "infant", "value": 0.6, "ul": null },
{ "age_range": "1-3 y", "sex": "both", "value": 1.2, "ul": 2 },
{ "age_range": "4-8 y", "sex": "both", "value": 1.5, "ul": 3 },
{ "age_range": "9-13 y", "sex": "male", "value": 1.9, "ul": 6 },
{ "age_range": "9-13 y", "sex": "female", "value": 1.6, "ul": 6 },
{ "age_range": "14-18 y", "sex": "male", "value": 2.2, "ul": 9 },
{ "age_range": "14-18 y", "sex": "female", "value": 1.6, "ul": 9 },
{ "age_range": "19-30 y", "sex": "male", "value": 2.3, "ul": 11 },
{ "age_range": "19-30 y", "sex": "female", "value": 1.8, "ul": 11 },
{ "age_range": "31-50 y", "sex": "male", "value": 2.3, "ul": 11 },
{ "age_range": "31-50 y", "sex": "female", "value": 1.8, "ul": 11 },
{ "age_range": "51-70 y", "sex": "male", "value": 2.3, "ul": 11 },
{ "age_range": "51-70 y", "sex": "female", "value": 1.8, "ul": 11 },
{ "age_range": ">70 y", "sex": "male", "value": 2.3, "ul": 11 },
{ "age_range": ">70 y", "sex": "female", "value": 1.8, "ul": 11 },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 1.9,
"ul": 11
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 1.8,
"ul": 11
},
{
"age_range": "pregnancy 31-50 y",
"sex": "female",
"value": 1.8,
"ul": 11
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 2.6,
"ul": 11
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 2.3,
"ul": 11
},
{
"age_range": "lactation 31-50 y",
"sex": "female",
"value": 2.3,
"ul": 11
}
]
},
"folate_dfe": {
"unit": "µg/day (DFE)",
"rda_type": "RDA/AI",
"life_stages": [
{ "age_range": "0-6 mo", "sex": "infant", "value": 65, "ul": null },
{ "age_range": "7-12 mo", "sex": "infant", "value": 80, "ul": null },
{ "age_range": "1-3 y", "sex": "both", "value": 150, "ul": 300 },
{ "age_range": "4-8 y", "sex": "both", "value": 200, "ul": 400 },
{ "age_range": "9-13 y", "sex": "male", "value": 300, "ul": 600 },
{ "age_range": "9-13 y", "sex": "female", "value": 300, "ul": 600 },
{ "age_range": "14-18 y", "sex": "both", "value": 400, "ul": 800 },
{ "age_range": "19-30 y", "sex": "female", "value": 400, "ul": 1000 },
{ "age_range": "19-30 y", "sex": "male", "value": 400, "ul": 1000 },
{ "age_range": "31-50 y", "sex": "both", "value": 400, "ul": 1000 },
{ "age_range": "51-70 y", "sex": "both", "value": 400, "ul": 1000 },
{ "age_range": ">70 y", "sex": "both", "value": 400, "ul": 1000 },
{
"age_range": "pregnancy ≤18 y",
"sex": "female",
"value": 600,
"ul": 800
},
{
"age_range": "pregnancy 19-30 y",
"sex": "female",
"value": 600,
"ul": 1000
},
{
"age_range": "lactation ≤18 y",
"sex": "female",
"value": 500,
"ul": 800
},
{
"age_range": "lactation 19-30 y",
"sex": "female",
"value": 500,
"ul": 1000
}
]
}
}
}

8
lib/logging.dart Normal file
View File

@@ -0,0 +1,8 @@
// A simple logging function that prints a message to the console if [kDebugMode] is enabled.
import 'package:flutter/foundation.dart';
void printLog(String message) {
if (kDebugMode) {
print('SupplementsLog: $message');
}
}

View File

@@ -1,15 +1,35 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; // Import this
import 'package:supplements/logging.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'providers/settings_provider.dart'; import 'providers/settings_provider.dart';
import 'providers/simple_sync_provider.dart'; import 'providers/simple_sync_provider.dart';
import 'providers/supplement_provider.dart'; import 'providers/supplement_provider.dart';
import 'screens/home_screen.dart'; import 'screens/home_screen.dart';
import 'screens/profile_setup_screen.dart'; import 'services/notification_router.dart';
import 'services/simple_notification_service.dart';
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
// Top-level function to handle notification responses in the background
@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse notificationResponse) {
// handle action here
printLog('Background notification action tapped: ${notificationResponse.actionId}');
NotificationRouter.instance.handleNotificationResponse(notificationResponse);
}
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
// Initialize SimpleNotificationService early
await SimpleNotificationService.instance.initialize(
onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
);
final settingsProvider = SettingsProvider(); final settingsProvider = SettingsProvider();
await settingsProvider.initialize(); await settingsProvider.initialize();
@@ -42,15 +62,25 @@ class MyApp extends StatelessWidget {
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
final supplementProvider = context.read<SupplementProvider>(); final supplementProvider = context.read<SupplementProvider>();
// Initialize notification router with the app's navigator
// This is done here because navigatorKey is only available after MaterialApp is built
NotificationRouter.instance.initialize(navigatorKey);
// If the app was launched via a notification, route to the proper dialog
// This needs to be called after the router is initialized with the navigatorKey
SimpleNotificationService.instance.getLaunchDetails().then((details) {
NotificationRouter.instance.handleAppLaunchDetails(details);
});
// Set up sync completion callback // Set up sync completion callback
syncProvider.setOnSyncCompleteCallback(() async { syncProvider.setOnSyncCompleteCallback(() async {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Sync completed, refreshing UI data...'); printLog('Sync completed, refreshing UI data...');
} }
await supplementProvider.loadSupplements(); await supplementProvider.loadSupplements();
await supplementProvider.loadTodayIntakes(); await supplementProvider.loadTodayIntakes();
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: UI data refreshed after sync'); printLog('UI data refreshed after sync');
} }
}); });
@@ -63,26 +93,19 @@ class MyApp extends StatelessWidget {
}); });
}); });
return MaterialApp( return ShadApp(
navigatorKey: navigatorKey,
title: 'Supplements Tracker', title: 'Supplements Tracker',
theme: ThemeData( theme: ShadThemeData(
colorScheme: ColorScheme.fromSeed( brightness: Brightness.light,
seedColor: Colors.blue, colorScheme: const ShadZincColorScheme.light(),
brightness: Brightness.light,
),
useMaterial3: true,
), ),
darkTheme: ThemeData( darkTheme: ShadThemeData(
colorScheme: ColorScheme.fromSeed( brightness: Brightness.dark,
seedColor: Colors.blue, colorScheme: const ShadZincColorScheme.dark(),
brightness: Brightness.dark,
),
useMaterial3: true,
), ),
themeMode: settingsProvider.themeMode, themeMode: settingsProvider.themeMode,
home: (settingsProvider.age == null || settingsProvider.gender == null) home: const HomeScreen(),
? const ProfileSetupScreen()
: const HomeScreen(),
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
); );
}, },

View File

@@ -1,87 +0,0 @@
class Nutrient {
final String name;
final String unit;
final String rdaType;
final String? note;
final UpperLimit? ul; // nutrient-level UL (optional)
final List<LifeStage> lifeStages;
Nutrient({
required this.name,
required this.unit,
required this.rdaType,
this.note,
this.ul,
required this.lifeStages,
});
factory Nutrient.fromJson(String name, Map<String, dynamic> json) {
return Nutrient(
name: name,
unit: json['unit'],
rdaType: json['rda_type'],
note: json['note'],
ul: (json['ul'] is Map<String, dynamic>) ? UpperLimit.fromJson(json['ul'] as Map<String, dynamic>) : null,
lifeStages: (json['life_stages'] as List)
.map((stage) => LifeStage.fromJson(stage))
.toList(),
);
}
}
class LifeStage {
final String ageRange;
final String sex;
final double value;
final double? valueMin;
final double? valueMax;
final double? ul;
final String? description;
LifeStage({
required this.ageRange,
required this.sex,
required this.value,
this.valueMin,
this.valueMax,
this.ul,
this.description,
});
factory LifeStage.fromJson(Map<String, dynamic> json) {
return LifeStage(
ageRange: json['age_range'],
sex: json['sex'],
value: (json['value'] as num?)?.toDouble() ?? 0.0,
valueMin: json['value_min'] != null ? (json['value_min'] as num).toDouble() : null,
valueMax: json['value_max'] != null ? (json['value_max'] as num).toDouble() : null,
ul: json['ul'] != null ? (json['ul'] as num).toDouble() : null,
description: json['description'],
);
}
}
class UpperLimit {
final double value;
final String unit;
final String? duration;
final String? note;
const UpperLimit({
required this.value,
required this.unit,
this.duration,
this.note,
});
factory UpperLimit.fromJson(Map<String, dynamic> json) {
return UpperLimit(
value: (json['value'] as num).toDouble(),
unit: json['unit'] ?? '',
duration: json['duration'],
note: json['note'],
);
}
}

View File

@@ -10,10 +10,6 @@ enum ThemeOption {
class SettingsProvider extends ChangeNotifier { class SettingsProvider extends ChangeNotifier {
ThemeOption _themeOption = ThemeOption.system; ThemeOption _themeOption = ThemeOption.system;
// Profile fields
DateTime? _dateOfBirth;
String? _gender;
// Time range settings (stored as hours, 0-23) // Time range settings (stored as hours, 0-23)
int _morningStart = 5; int _morningStart = 5;
int _morningEnd = 10; int _morningEnd = 10;
@@ -24,10 +20,8 @@ class SettingsProvider extends ChangeNotifier {
int _nightStart = 23; int _nightStart = 23;
int _nightEnd = 4; int _nightEnd = 4;
// Persistent reminder settings // Notifications
bool _persistentReminders = true; int _snoozeMinutes = 10;
int _reminderRetryInterval = 5; // minutes
int _maxRetryAttempts = 3;
// Auto-sync settings // Auto-sync settings
bool _autoSyncEnabled = false; bool _autoSyncEnabled = false;
@@ -35,19 +29,6 @@ class SettingsProvider extends ChangeNotifier {
ThemeOption get themeOption => _themeOption; ThemeOption get themeOption => _themeOption;
// Profile getters
DateTime? get dateOfBirth => _dateOfBirth;
String? get gender => _gender;
int? get age {
if (_dateOfBirth == null) return null;
final now = DateTime.now();
int years = now.year - _dateOfBirth!.year;
final hasHadBirthday = (now.month > _dateOfBirth!.month) ||
(now.month == _dateOfBirth!.month && now.day >= _dateOfBirth!.day);
if (!hasHadBirthday) years--;
return years;
}
// Time range getters // Time range getters
int get morningStart => _morningStart; int get morningStart => _morningStart;
int get morningEnd => _morningEnd; int get morningEnd => _morningEnd;
@@ -58,10 +39,8 @@ class SettingsProvider extends ChangeNotifier {
int get nightStart => _nightStart; int get nightStart => _nightStart;
int get nightEnd => _nightEnd; int get nightEnd => _nightEnd;
// Persistent reminder getters // Notifications
bool get persistentReminders => _persistentReminders; int get snoozeMinutes => _snoozeMinutes;
int get reminderRetryInterval => _reminderRetryInterval;
int get maxRetryAttempts => _maxRetryAttempts;
// Auto-sync getters // Auto-sync getters
bool get autoSyncEnabled => _autoSyncEnabled; bool get autoSyncEnabled => _autoSyncEnabled;
@@ -93,13 +72,6 @@ class SettingsProvider extends ChangeNotifier {
final themeIndex = prefs.getInt('theme_option') ?? 0; final themeIndex = prefs.getInt('theme_option') ?? 0;
_themeOption = ThemeOption.values[themeIndex]; _themeOption = ThemeOption.values[themeIndex];
// Load profile fields
final dobString = prefs.getString('date_of_birth');
if (dobString != null) {
_dateOfBirth = DateTime.tryParse(dobString);
}
_gender = prefs.getString('gender');
// Load time range settings // Load time range settings
_morningStart = prefs.getInt('morning_start') ?? 5; _morningStart = prefs.getInt('morning_start') ?? 5;
_morningEnd = prefs.getInt('morning_end') ?? 10; _morningEnd = prefs.getInt('morning_end') ?? 10;
@@ -110,10 +82,8 @@ class SettingsProvider extends ChangeNotifier {
_nightStart = prefs.getInt('night_start') ?? 23; _nightStart = prefs.getInt('night_start') ?? 23;
_nightEnd = prefs.getInt('night_end') ?? 4; _nightEnd = prefs.getInt('night_end') ?? 4;
// Load persistent reminder settings // Load snooze setting
_persistentReminders = prefs.getBool('persistent_reminders') ?? true; _snoozeMinutes = prefs.getInt('snooze_minutes') ?? 10;
_reminderRetryInterval = prefs.getInt('reminder_retry_interval') ?? 5;
_maxRetryAttempts = prefs.getInt('max_retry_attempts') ?? 3;
// Load auto-sync settings // Load auto-sync settings
_autoSyncEnabled = prefs.getBool('auto_sync_enabled') ?? false; _autoSyncEnabled = prefs.getBool('auto_sync_enabled') ?? false;
@@ -131,8 +101,6 @@ class SettingsProvider extends ChangeNotifier {
} }
Future<void> setDateOfBirthAndGender(DateTime dateOfBirth, String gender) async { Future<void> setDateOfBirthAndGender(DateTime dateOfBirth, String gender) async {
_dateOfBirth = dateOfBirth;
_gender = gender;
notifyListeners(); notifyListeners();
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
@@ -278,29 +246,17 @@ class SettingsProvider extends ChangeNotifier {
} }
} }
// Persistent reminder setters // Notifications setters
Future<void> setPersistentReminders(bool enabled) async { Future<void> setSnoozeMinutes(int minutes) async {
_persistentReminders = enabled; const allowed = [5, 10, 15, 20];
if (!allowed.contains(minutes)) {
throw ArgumentError('Snooze minutes must be one of ${allowed.join(", ")}');
}
_snoozeMinutes = minutes;
notifyListeners(); notifyListeners();
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
await prefs.setBool('persistent_reminders', enabled); await prefs.setInt('snooze_minutes', minutes);
}
Future<void> setReminderRetryInterval(int minutes) async {
_reminderRetryInterval = minutes;
notifyListeners();
final prefs = await SharedPreferences.getInstance();
await prefs.setInt('reminder_retry_interval', minutes);
}
Future<void> setMaxRetryAttempts(int attempts) async {
_maxRetryAttempts = attempts;
notifyListeners();
final prefs = await SharedPreferences.getInstance();
await prefs.setInt('max_retry_attempts', attempts);
} }
// Auto-sync setters // Auto-sync setters

View File

@@ -1,4 +1,5 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:supplements/logging.dart';
import '../services/database_sync_service.dart'; import '../services/database_sync_service.dart';
import '../services/auto_sync_service.dart'; import '../services/auto_sync_service.dart';
@@ -66,7 +67,7 @@ class SimpleSyncProvider with ChangeNotifier {
); );
if (kDebugMode) { if (kDebugMode) {
print('SimpleSyncProvider: Auto-sync service initialized'); printLog('SimpleSyncProvider: Auto-sync service initialized');
} }
} }
@@ -111,7 +112,7 @@ class SimpleSyncProvider with ChangeNotifier {
await _syncService.syncDatabase(); await _syncService.syncDatabase();
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Sync failed in provider: $e'); printLog('Sync failed in provider: $e');
} }
rethrow; rethrow;
} finally { } finally {

View File

@@ -3,22 +3,24 @@ import 'dart:async';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:supplements/logging.dart';
import '../models/supplement.dart'; import '../models/supplement.dart';
import '../models/supplement_intake.dart'; import '../models/supplement_intake.dart';
import '../services/database_helper.dart'; import '../services/database_helper.dart';
import '../services/database_sync_service.dart'; import '../services/database_sync_service.dart';
import '../services/notification_service.dart'; import '../services/simple_notification_service.dart';
class SupplementProvider with ChangeNotifier, WidgetsBindingObserver { class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
final DatabaseHelper _databaseHelper = DatabaseHelper.instance; final DatabaseHelper _databaseHelper = DatabaseHelper.instance;
final NotificationService _notificationService = NotificationService(); final SimpleNotificationService _notificationService = SimpleNotificationService.instance;
bool _initialized = false;
List<Supplement> _supplements = []; List<Supplement> _supplements = [];
List<Map<String, dynamic>> _todayIntakes = []; List<Map<String, dynamic>> _todayIntakes = [];
List<Map<String, dynamic>> _monthlyIntakes = []; List<Map<String, dynamic>> _monthlyIntakes = [];
bool _isLoading = false; bool _isLoading = false;
Timer? _persistentReminderTimer;
Timer? _dateChangeTimer; Timer? _dateChangeTimer;
DateTime _lastDateCheck = DateTime.now(); DateTime _lastDateCheck = DateTime.now();
@@ -41,37 +43,22 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
} }
Future<void> initialize() async { Future<void> initialize() async {
if (_initialized) {
return;
}
_initialized = true;
// Add this provider as an observer for app lifecycle changes // Add this provider as an observer for app lifecycle changes
WidgetsBinding.instance.addObserver(this); WidgetsBinding.instance.addObserver(this);
await _notificationService.initialize(); await _notificationService.initialize();
// Set up the callback for handling supplement intake from notifications
print('SupplementsLog: 📱 Setting up notification callback...');
_notificationService.setTakeSupplementCallback((supplementId, supplementName, units, unitType) {
print('SupplementsLog: 📱 === NOTIFICATION CALLBACK TRIGGERED ===');
print('SupplementsLog: 📱 Supplement ID: $supplementId');
print('SupplementsLog: 📱 Supplement Name: $supplementName');
print('SupplementsLog: 📱 Units: $units');
print('SupplementsLog: 📱 Unit Type: $unitType');
// Record the intake when user taps "Take" on notification
recordIntake(supplementId, 0.0, unitsTaken: units);
print('SupplementsLog: 📱 Intake recorded successfully');
print('SupplementsLog: 📱 === CALLBACK COMPLETE ===');
if (kDebugMode) {
print('SupplementsLog: 📱 Recorded intake from notification: $supplementName ($units $unitType)');
}
});
print('SupplementsLog: 📱 Notification callback setup complete');
// Request permissions with error handling // Request permissions with error handling
try { try {
await _notificationService.requestPermissions(); await _notificationService.requestPermissions();
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error requesting notification permissions: $e'); printLog('Error requesting notification permissions: $e');
} }
// Continue without notifications rather than crashing // Continue without notifications rather than crashing
} }
@@ -79,35 +66,14 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
await loadSupplements(); await loadSupplements();
await loadTodayIntakes(); await loadTodayIntakes();
// Reschedule notifications for all active supplements to ensure persistence // Schedule notifications for all active supplements
await _rescheduleAllNotifications(); await _rescheduleAllNotifications();
// Start periodic checking for persistent reminders (every 5 minutes)
_startPersistentReminderCheck();
// Start date change monitoring to reset daily intake status // Start date change monitoring to reset daily intake status
_startDateChangeMonitoring(); _startDateChangeMonitoring();
} }
void _startPersistentReminderCheck() {
// Cancel any existing timer
_persistentReminderTimer?.cancel();
// Check every 5 minutes for persistent reminders
_persistentReminderTimer = Timer.periodic(const Duration(minutes: 5), (timer) async {
try {
// This will be called from settings provider context, so we need to import it
await _checkPersistentReminders();
} catch (e) {
if (kDebugMode) {
print('SupplementsLog: Error checking persistent reminders: $e');
}
}
});
// Also check immediately
_checkPersistentReminders();
}
void _startDateChangeMonitoring() { void _startDateChangeMonitoring() {
// Cancel any existing timer // Cancel any existing timer
@@ -121,8 +87,8 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
if (currentDate != lastCheckDate) { if (currentDate != lastCheckDate) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Date changed detected: ${lastCheckDate} -> ${currentDate}'); printLog('Date changed detected: $lastCheckDate -> $currentDate');
print('SupplementsLog: Refreshing today\'s intakes for new day...'); printLog('Refreshing today\'s intakes for new day...');
} }
// Date has changed, refresh today's intakes // Date has changed, refresh today's intakes
@@ -130,49 +96,22 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
await loadTodayIntakes(); await loadTodayIntakes();
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Today\'s intakes refreshed for new day'); printLog('Today\'s intakes refreshed for new day');
} }
} }
}); });
} }
Future<void> _checkPersistentReminders() async {
// This method will be enhanced to accept settings from the UI layer
// For now, we'll check with default settings
// In practice, the UI should call checkPersistentRemindersWithSettings
if (kDebugMode) {
print('SupplementsLog: 📱 Checking persistent reminders with default settings');
}
}
// Method to be called from UI with actual settings
Future<void> checkPersistentRemindersWithSettings({
required bool persistentReminders,
required int reminderRetryInterval,
required int maxRetryAttempts,
}) async {
print('SupplementsLog: 📱 🔄 MANUAL CHECK: Persistent reminders called from UI');
await _notificationService.checkPersistentReminders(
persistentReminders,
reminderRetryInterval,
maxRetryAttempts,
);
}
// Add a manual trigger method for testing
Future<void> triggerRetryCheck() async {
print('SupplementsLog: 📱 🚨 MANUAL TRIGGER: Forcing retry check...');
await checkPersistentRemindersWithSettings(
persistentReminders: true,
reminderRetryInterval: 5, // Force 5 minute interval for testing
maxRetryAttempts: 3,
);
}
@override @override
void dispose() { void dispose() {
WidgetsBinding.instance.removeObserver(this); WidgetsBinding.instance.removeObserver(this);
_persistentReminderTimer?.cancel();
_dateChangeTimer?.cancel(); _dateChangeTimer?.cancel();
super.dispose(); super.dispose();
} }
@@ -184,7 +123,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
if (state == AppLifecycleState.resumed) { if (state == AppLifecycleState.resumed) {
// App came back to foreground, check if date changed // App came back to foreground, check if date changed
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: App resumed, checking for date change...'); printLog('App resumed, checking for date change...');
} }
forceCheckDateChange(); forceCheckDateChange();
} }
@@ -192,23 +131,20 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
Future<void> _rescheduleAllNotifications() async { Future<void> _rescheduleAllNotifications() async {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: 📱 Rescheduling notifications for all active supplements...'); printLog('📱 Rescheduling notifications for all active supplements...');
} }
for (final supplement in _supplements) { try {
if (supplement.reminderTimes.isNotEmpty) { await _notificationService.scheduleDailyGroupedRemindersSafe(_supplements);
try { await _notificationService.getPendingNotifications();
await _notificationService.scheduleSupplementReminders(supplement); } catch (e) {
} catch (e) { if (kDebugMode) {
if (kDebugMode) { printLog('📱 Error scheduling grouped notifications: $e');
print('SupplementsLog: 📱 Error rescheduling notifications for ${supplement.name}: $e');
}
}
} }
} }
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: 📱 Finished rescheduling notifications'); printLog('📱 Finished rescheduling notifications');
} }
} }
@@ -217,16 +153,16 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
notifyListeners(); notifyListeners();
try { try {
print('SupplementsLog: Loading supplements from database...'); printLog('Loading supplements from database...');
_supplements = await _databaseHelper.getAllSupplements(); _supplements = await _databaseHelper.getAllSupplements();
print('SupplementsLog: Loaded ${_supplements.length} supplements'); printLog('Loaded ${_supplements.length} supplements');
for (var supplement in _supplements) { for (var supplement in _supplements) {
print('SupplementsLog: Supplement: ${supplement.name}'); printLog('Supplement: ${supplement.name}');
} }
} catch (e) { } catch (e) {
print('SupplementsLog: Error loading supplements: $e'); printLog('Error loading supplements: $e');
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error loading supplements: $e'); printLog('Error loading supplements: $e');
} }
} finally { } finally {
_isLoading = false; _isLoading = false;
@@ -236,28 +172,20 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
Future<void> addSupplement(Supplement supplement) async { Future<void> addSupplement(Supplement supplement) async {
try { try {
print('SupplementsLog: Adding supplement: ${supplement.name}'); printLog('Adding supplement: ${supplement.name}');
final id = await _databaseHelper.insertSupplement(supplement); final id = await _databaseHelper.insertSupplement(supplement);
print('SupplementsLog: Supplement inserted with ID: $id'); printLog('Supplement inserted with ID: $id');
final newSupplement = supplement.copyWith(id: id);
// Schedule notifications (skip if there's an error)
try {
await _notificationService.scheduleSupplementReminders(newSupplement);
print('SupplementsLog: Notifications scheduled');
} catch (notificationError) {
print('SupplementsLog: Warning: Could not schedule notifications: $notificationError');
}
await loadSupplements(); await loadSupplements();
print('SupplementsLog: Supplements reloaded, count: ${_supplements.length}'); printLog('Supplements reloaded, count: ${_supplements.length}');
await _rescheduleAllNotifications();
// Trigger sync after adding supplement // Trigger sync after adding supplement
_triggerSyncIfEnabled(); _triggerSyncIfEnabled();
} catch (e) { } catch (e) {
print('SupplementsLog: Error adding supplement: $e'); printLog('Error adding supplement: $e');
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error adding supplement: $e'); printLog('Error adding supplement: $e');
} }
rethrow; rethrow;
} }
@@ -267,16 +195,14 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
try { try {
await _databaseHelper.updateSupplement(supplement); await _databaseHelper.updateSupplement(supplement);
// Reschedule notifications
await _notificationService.scheduleSupplementReminders(supplement);
await loadSupplements(); await loadSupplements();
await _rescheduleAllNotifications();
// Trigger sync after updating supplement // Trigger sync after updating supplement
_triggerSyncIfEnabled(); _triggerSyncIfEnabled();
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error updating supplement: $e'); printLog('Error updating supplement: $e');
} }
} }
} }
@@ -298,7 +224,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
} }
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error duplicating supplement: $e'); printLog('Error duplicating supplement: $e');
} }
} }
} }
@@ -307,16 +233,14 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
try { try {
await _databaseHelper.deleteSupplement(id); await _databaseHelper.deleteSupplement(id);
// Cancel notifications
await _notificationService.cancelSupplementReminders(id);
await loadSupplements(); await loadSupplements();
await _rescheduleAllNotifications();
// Trigger sync after deleting supplement // Trigger sync after deleting supplement
_triggerSyncIfEnabled(); _triggerSyncIfEnabled();
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error deleting supplement: $e'); printLog('Error deleting supplement: $e');
} }
} }
} }
@@ -340,13 +264,13 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
// Show confirmation notification // Show confirmation notification
final supplement = _supplements.firstWhere((s) => s.id == supplementId); final supplement = _supplements.firstWhere((s) => s.id == supplementId);
final unitsText = unitsTaken != null && unitsTaken != 1 ? '${unitsTaken.toStringAsFixed(unitsTaken % 1 == 0 ? 0 : 1)} ${supplement.unitType}' : ''; final unitsText = unitsTaken != null && unitsTaken != 1 ? '${unitsTaken.toStringAsFixed(unitsTaken % 1 == 0 ? 0 : 1)} ${supplement.unitType}' : '';
await _notificationService.showInstantNotification( await _notificationService.showInstant(
'Supplement Taken', title: 'Supplement Taken',
'Recorded ${supplement.name}${unitsText.isNotEmpty ? ' - $unitsText' : ''} (${supplement.ingredientsDisplay})', body: 'Recorded ${supplement.name}${unitsText.isNotEmpty ? ' - $unitsText' : ''} (${supplement.ingredientsDisplay})',
); );
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error recording intake: $e'); printLog('Error recording intake: $e');
} }
} }
} }
@@ -355,22 +279,22 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
try { try {
final today = DateTime.now(); final today = DateTime.now();
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Loading intakes for date: ${today.year}-${today.month}-${today.day}'); printLog('Loading intakes for date: ${today.year}-${today.month}-${today.day}');
} }
_todayIntakes = await _databaseHelper.getIntakesWithSupplementsForDate(today); _todayIntakes = await _databaseHelper.getIntakesWithSupplementsForDate(today);
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Loaded ${_todayIntakes.length} intakes for today'); printLog('Loaded ${_todayIntakes.length} intakes for today');
for (var intake in _todayIntakes) { for (var intake in _todayIntakes) {
print('SupplementsLog: - Supplement ID: ${intake['supplement_id']}, taken at: ${intake['takenAt']}'); printLog(' - Supplement ID: ${intake['supplement_id']}, taken at: ${intake['takenAt']}');
} }
} }
notifyListeners(); notifyListeners();
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error loading today\'s intakes: $e'); printLog('Error loading today\'s intakes: $e');
} }
} }
} }
@@ -381,7 +305,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
notifyListeners(); notifyListeners();
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error loading monthly intakes: $e'); printLog('Error loading monthly intakes: $e');
} }
} }
} }
@@ -391,7 +315,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
return await _databaseHelper.getIntakesWithSupplementsForDate(date); return await _databaseHelper.getIntakesWithSupplementsForDate(date);
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error loading intakes for date: $e'); printLog('Error loading intakes for date: $e');
} }
return []; return [];
} }
@@ -411,7 +335,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
_triggerSyncIfEnabled(); _triggerSyncIfEnabled();
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error deleting intake: $e'); printLog('Error deleting intake: $e');
} }
} }
} }
@@ -430,7 +354,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
_triggerSyncIfEnabled(); _triggerSyncIfEnabled();
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error permanently deleting intake: $e'); printLog('Error permanently deleting intake: $e');
} }
} }
} }
@@ -443,6 +367,24 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
return _todayIntakes.where((intake) => intake['supplement_id'] == supplementId).length; return _todayIntakes.where((intake) => intake['supplement_id'] == supplementId).length;
} }
/// Get the most recent intake for a supplement today
Map<String, dynamic>? getMostRecentIntake(int supplementId) {
final supplementIntakes = _todayIntakes
.where((intake) => intake['supplement_id'] == supplementId)
.toList();
if (supplementIntakes.isEmpty) return null;
// Sort by takenAt time (most recent first)
supplementIntakes.sort((a, b) {
final aTime = DateTime.parse(a['takenAt']);
final bTime = DateTime.parse(b['takenAt']);
return bTime.compareTo(aTime); // Descending order
});
return supplementIntakes.first;
}
Map<String, double> get dailyIngredientIntake { Map<String, double> get dailyIngredientIntake {
final Map<String, double> ingredientIntake = {}; final Map<String, double> ingredientIntake = {};
@@ -462,7 +404,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
// Method to manually refresh daily status (useful for testing or manual refresh) // Method to manually refresh daily status (useful for testing or manual refresh)
Future<void> refreshDailyStatus() async { Future<void> refreshDailyStatus() async {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Manually refreshing daily status...'); printLog('Manually refreshing daily status...');
} }
_lastDateCheck = DateTime.now(); _lastDateCheck = DateTime.now();
await loadTodayIntakes(); await loadTodayIntakes();
@@ -475,20 +417,20 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
final lastCheckDate = DateTime(_lastDateCheck.year, _lastDateCheck.month, _lastDateCheck.day); final lastCheckDate = DateTime(_lastDateCheck.year, _lastDateCheck.month, _lastDateCheck.day);
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Force checking date change...'); printLog('Force checking date change...');
print('SupplementsLog: Current date: $currentDate'); printLog('Current date: $currentDate');
print('SupplementsLog: Last check date: $lastCheckDate'); printLog('Last check date: $lastCheckDate');
} }
if (currentDate != lastCheckDate) { if (currentDate != lastCheckDate) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Date change detected, refreshing intakes...'); printLog('Date change detected, refreshing intakes...');
} }
_lastDateCheck = now; _lastDateCheck = now;
await loadTodayIntakes(); await loadTodayIntakes();
} else { } else {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: No date change detected'); printLog('No date change detected');
} }
} }
} }
@@ -503,7 +445,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
notifyListeners(); notifyListeners();
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error loading archived supplements: $e'); printLog('Error loading archived supplements: $e');
} }
} }
} }
@@ -518,7 +460,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
_triggerSyncIfEnabled(); _triggerSyncIfEnabled();
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error archiving supplement: $e'); printLog('Error archiving supplement: $e');
} }
} }
} }
@@ -533,7 +475,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
_triggerSyncIfEnabled(); _triggerSyncIfEnabled();
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error unarchiving supplement: $e'); printLog('Error unarchiving supplement: $e');
} }
} }
} }
@@ -547,32 +489,39 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
_triggerSyncIfEnabled(); _triggerSyncIfEnabled();
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('SupplementsLog: Error permanently deleting archived supplement: $e'); printLog('Error permanently deleting archived supplement: $e');
} }
} }
} }
// Debug methods for notification testing // Debug methods for notification testing
Future<void> testNotifications() async { Future<void> testNotifications() async {
await _notificationService.testNotification(); await _notificationService.showInstant(
title: 'Test Notification',
body: 'This is a test notification to verify the system is working.',
);
} }
Future<void> testScheduledNotification() async { Future<void> testScheduledNotification() async {
await _notificationService.testScheduledNotification(); await _notificationService.showInstant(
title: 'Test Scheduled Notification',
body: 'This is a simple test notification.',
);
} }
Future<void> testNotificationActions() async { Future<void> testNotificationActions() async {
await _notificationService.testNotificationWithActions(); await _notificationService.showInstant(
title: 'Test Action Notification',
body: 'Actions are not available in the simple notification service.',
);
} }
Future<List<PendingNotificationRequest>> getPendingNotifications() async { Future<List<PendingNotificationRequest>> getPendingNotifications() async {
return await _notificationService.getPendingNotifications(); // Not supported in simple service; return empty list for compatibility.
return [];
} }
// Get pending notifications with retry information from database
Future<List<Map<String, dynamic>>> getTrackedNotifications() async {
return await DatabaseHelper.instance.getPendingNotifications();
}
// Debug method to test notification persistence // Debug method to test notification persistence
Future<void> rescheduleAllNotifications() async { Future<void> rescheduleAllNotifications() async {
@@ -581,6 +530,6 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
// Debug method to cancel all notifications // Debug method to cancel all notifications
Future<void> cancelAllNotifications() async { Future<void> cancelAllNotifications() async {
await _notificationService.cancelAllReminders(); await _notificationService.cancelAll();
} }
} }

View File

@@ -3,10 +3,8 @@ import 'package:provider/provider.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
import '../models/ingredient.dart'; import '../models/ingredient.dart';
import '../models/nutrient.dart';
import '../models/supplement.dart'; import '../models/supplement.dart';
import '../providers/supplement_provider.dart'; import '../providers/supplement_provider.dart';
import '../services/nutrient_data_service.dart';
// Helper class to manage ingredient text controllers // Helper class to manage ingredient text controllers
class IngredientController { class IngredientController {
@@ -53,12 +51,8 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
final _numberOfUnitsController = TextEditingController(); final _numberOfUnitsController = TextEditingController();
final _notesController = TextEditingController(); final _notesController = TextEditingController();
// Nutrient data for autocomplete
final NutrientDataService _nutrientDataService = NutrientDataService();
List<Nutrient> _nutrients = [];
// Multi-ingredient support with persistent controllers // Multi-ingredient support with persistent controllers
List<IngredientController> _ingredientControllers = []; final _ingredientControllers = [];
String _selectedUnitType = 'capsules'; String _selectedUnitType = 'capsules';
int _frequencyPerDay = 1; int _frequencyPerDay = 1;
@@ -172,7 +166,7 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
Expanded( Expanded(
flex: 1, flex: 1,
child: DropdownButtonFormField<String>( child: DropdownButtonFormField<String>(
value: controller.selectedUnit, initialValue: controller.selectedUnit,
decoration: const InputDecoration( decoration: const InputDecoration(
labelText: 'Unit', labelText: 'Unit',
border: OutlineInputBorder(), border: OutlineInputBorder(),
@@ -323,7 +317,7 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
Expanded( Expanded(
flex: 1, flex: 1,
child: DropdownButtonFormField<String>( child: DropdownButtonFormField<String>(
value: _selectedUnitType, initialValue: _selectedUnitType,
decoration: const InputDecoration( decoration: const InputDecoration(
labelText: 'Type', labelText: 'Type',
border: OutlineInputBorder(), border: OutlineInputBorder(),
@@ -348,7 +342,7 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
// Show ingredients summary // Show ingredients summary
if (_ingredientControllers.isNotEmpty && _ingredientControllers.any((c) => c.nameController.text.isNotEmpty && (double.tryParse(c.amountController.text) ?? 0) > 0)) if (_ingredientControllers.isNotEmpty && _ingredientControllers.any((c) => c.nameController.text.isNotEmpty && (double.tryParse(c.amountController.text) ?? 0) > 0))
Card( Card(
color: Theme.of(context).colorScheme.primaryContainer.withOpacity(0.3), color: Theme.of(context).colorScheme.primaryContainer.withValues(alpha: 0.3),
child: Padding( child: Padding(
padding: const EdgeInsets.all(12), padding: const EdgeInsets.all(12),
child: Column( child: Column(

View File

@@ -164,9 +164,9 @@ class _ArchivedSupplementCard extends StatelessWidget {
child: Container( child: Container(
decoration: BoxDecoration( decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16), borderRadius: BorderRadius.circular(16),
color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.3), color: Theme.of(context).colorScheme.surfaceContainerHighest.withValues(alpha: 0.3),
border: Border.all( border: Border.all(
color: Theme.of(context).colorScheme.outline.withOpacity(0.2), color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.2),
width: 1, width: 1,
), ),
), ),
@@ -180,7 +180,7 @@ class _ArchivedSupplementCard extends StatelessWidget {
Container( Container(
padding: const EdgeInsets.all(8), padding: const EdgeInsets.all(8),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Theme.of(context).colorScheme.outline.withOpacity(0.1), color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.1),
shape: BoxShape.circle, shape: BoxShape.circle,
), ),
child: Icon( child: Icon(
@@ -262,7 +262,7 @@ class _ArchivedSupplementCard extends StatelessWidget {
Container( Container(
padding: const EdgeInsets.all(12), padding: const EdgeInsets.all(12),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.3), color: Theme.of(context).colorScheme.surfaceContainerHighest.withValues(alpha: 0.3),
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),
), ),
child: Column( child: Column(
@@ -284,7 +284,7 @@ class _ArchivedSupplementCard extends StatelessWidget {
return Container( return Container(
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2), padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Theme.of(context).colorScheme.outline.withOpacity(0.1), color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(6), borderRadius: BorderRadius.circular(6),
), ),
child: Text( child: Text(

View File

@@ -0,0 +1,415 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:provider/provider.dart';
import 'package:supplements/providers/supplement_provider.dart';
import 'package:supplements/services/notification_debug_store.dart';
import 'package:supplements/services/simple_notification_service.dart';
class DebugNotificationsScreen extends StatefulWidget {
const DebugNotificationsScreen({super.key});
@override
State<DebugNotificationsScreen> createState() => _DebugNotificationsScreenState();
}
class _DebugNotificationsScreenState extends State<DebugNotificationsScreen> {
bool _loading = true;
List<PendingNotificationRequest> _pending = const [];
List<NotificationLogEntry> _logEntries = const [];
final Map<int, String> _supplementNameCache = {};
@override
void initState() {
super.initState();
_load();
}
Future<void> _load() async {
setState(() {
_loading = true;
});
// Fetch pending from plugin
final pending = await SimpleNotificationService.instance.getPendingNotifications();
// Fetch log from local store
final logs = await NotificationDebugStore.instance.getAll();
// Optionally resolve supplement names for single payloads
await _resolveSupplementNames(pending, logs);
if (!mounted) return;
setState(() {
_pending = pending;
_logEntries = logs.reversed.toList(); // newest first
_loading = false;
});
}
Future<void> _resolveSupplementNames(
List<PendingNotificationRequest> pending,
List<NotificationLogEntry> logs,
) async {
final ctx = context;
if (!mounted) return;
final provider = Provider.of<SupplementProvider>(ctx, listen: false);
// Use existing list; attempt load if empty
if (provider.supplements.isEmpty) {
try {
await provider.loadSupplements();
} catch (_) {
// ignore
}
}
// Collect potential IDs
final Set<int> ids = {};
for (final p in pending) {
final id = _extractSingleIdFromPayload(p.payload);
if (id != null) ids.add(id);
}
for (final e in logs) {
if (e.singleId != null) ids.add(e.singleId!);
}
// Build cache
for (final id in ids) {
try {
final s = provider.supplements.firstWhere((el) => el.id == id);
_supplementNameCache[id] = s.name;
} catch (_) {
// leave missing
}
}
}
int? _extractSingleIdFromPayload(String? payload) {
if (payload == null || payload.isEmpty) return null;
try {
final map = jsonDecode(payload);
if (map is Map && map['type'] == 'single') {
final v = map['id'];
if (v is int) return v;
}
} catch (_) {
// ignore
}
return null;
}
String? _extractGroupTimeFromPayload(String? payload) {
if (payload == null || payload.isEmpty) return null;
try {
final map = jsonDecode(payload);
if (map is Map && map['type'] == 'group') {
final v = map['time'];
if (v is String) return v;
}
} catch (_) {
// ignore
}
return null;
}
String _extractKindFromPayload(String? payload) {
if (payload == null || payload.isEmpty) return 'unknown';
try {
final map = jsonDecode(payload);
if (map is Map) {
final meta = map['meta'];
if (meta is Map && meta['kind'] is String) return meta['kind'] as String;
}
} catch (_) {}
return 'unknown';
}
DateTime? _estimateScheduledAtFromPayload(String? payload) {
// For snooze with meta.createdAt/delayMin we can compute when.
if (payload == null || payload.isEmpty) return null;
try {
final map = jsonDecode(payload);
if (map is Map) {
final meta = map['meta'];
if (meta is Map) {
final kind = meta['kind'];
if (kind == 'snooze') {
final createdAt = meta['createdAt'];
final delayMin = meta['delayMin'];
if (createdAt is int && delayMin is int) {
return DateTime.fromMillisecondsSinceEpoch(createdAt)
.add(Duration(minutes: delayMin));
}
}
}
// For daily group we can compute next HH:mm
if (map['type'] == 'group' && map['time'] is String) {
final timeKey = map['time'] as String;
final parts = timeKey.split(':');
if (parts.length == 2) {
final hour = int.tryParse(parts[0]) ?? 0;
final minute = int.tryParse(parts[1]) ?? 0;
final now = DateTime.now();
DateTime sched = DateTime(now.year, now.month, now.day, hour, minute);
if (!sched.isAfter(now)) {
sched = sched.add(const Duration(days: 1));
}
return sched;
}
}
}
} catch (_) {}
return null;
}
Future<void> _cancelId(int id) async {
await SimpleNotificationService.instance.cancelById(id);
await _load();
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Canceled notification $id')),
);
}
Future<void> _cancelAll() async {
await SimpleNotificationService.instance.cancelAll();
await _load();
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Canceled all notifications')),
);
}
Future<void> _clearLog() async {
await NotificationDebugStore.instance.clear();
await _load();
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Cleared debug log')),
);
}
void _copyToClipboard(String text) {
Clipboard.setData(ClipboardData(text: text));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Copied to clipboard')),
);
}
@override
Widget build(BuildContext context) {
final pendingIds = _pending.map((e) => e.id).toSet();
return Scaffold(
appBar: AppBar(
title: const Text('Debug Notifications'),
actions: [
IconButton(
tooltip: 'Refresh',
onPressed: _load,
icon: const Icon(Icons.refresh),
),
IconButton(
tooltip: 'Cancel All',
onPressed: _cancelAll,
icon: const Icon(Icons.cancel_schedule_send),
),
IconButton(
tooltip: 'Clear Log',
onPressed: _clearLog,
icon: const Icon(Icons.delete_sweep),
),
],
),
body: _loading
? const Center(child: CircularProgressIndicator())
: ListView(
padding: const EdgeInsets.all(12),
children: [
_buildPendingSection(),
const SizedBox(height: 16),
_buildTestSnoozeSection(), // Add the test snooze section
const SizedBox(height: 16),
_buildLogSection(pendingIds),
],
),
);
}
Widget _buildPendingSection() {
return Card(
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Pending (${_pending.length})', style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 8),
if (_pending.isEmpty)
const Text('No pending notifications'),
for (final p in _pending) _buildPendingTile(p),
],
),
),
);
}
Widget _buildTestSnoozeSection() {
return Card(
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Test Snooze', style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () async {
// Trigger a test snooze notification with snooze actions
await SimpleNotificationService.instance.showInstant(
title: 'Test Snooze Notification',
body: 'This is a test notification for snooze.',
payload: jsonEncode({
"type": "single",
"id": 1, // Use a dummy ID for testing
"meta": {"kind": "daily"} // Simulate a daily notification
}),
includeSnoozeActions: true, // Include snooze actions
isSingle: true, // This is a single notification
);
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Test snooze notification sent!')),
);
},
child: const Text('Send Test Snooze Notification'),
),
],
),
),
);
}
Widget _buildPendingTile(PendingNotificationRequest p) {
final kind = _extractKindFromPayload(p.payload);
final singleId = _extractSingleIdFromPayload(p.payload);
final groupTime = _extractGroupTimeFromPayload(p.payload);
final schedAt = _estimateScheduledAtFromPayload(p.payload);
final forStr = singleId != null
? (_supplementNameCache[singleId] != null
? '${_supplementNameCache[singleId]} (id=$singleId)'
: 'Supplement id=$singleId')
: (groupTime != null ? 'Time $groupTime' : 'unknown');
// Build a clearer subtitle with scheduled time prominently displayed
String subtitle = 'Kind: $kind • For: $forStr';
if (schedAt != null) {
final now = DateTime.now();
final diff = schedAt.difference(now);
String whenStr;
if (diff.isNegative) {
whenStr = 'Overdue (${schedAt.toLocal().toString().substring(0, 16)})';
} else if (diff.inDays > 0) {
whenStr = 'In ${diff.inDays}d ${diff.inHours % 24}h (${schedAt.toLocal().toString().substring(0, 16)})';
} else if (diff.inHours > 0) {
whenStr = 'In ${diff.inHours}h ${diff.inMinutes % 60}m (${schedAt.toLocal().toString().substring(11, 16)})';
} else if (diff.inMinutes > 0) {
whenStr = 'In ${diff.inMinutes}m (${schedAt.toLocal().toString().substring(11, 16)})';
} else {
whenStr = 'Very soon (${schedAt.toLocal().toString().substring(11, 16)})';
}
subtitle = '$subtitle\n🕒 $whenStr';
}
// Removed the "else" block that displayed "Schedule time unknown"
return ListTile(
dense: true,
title: Text('ID ${p.id}${p.title ?? "(no title)"}'),
subtitle: Text(subtitle),
trailing: Wrap(
spacing: 4,
children: [
IconButton(
tooltip: 'Copy payload',
icon: const Icon(Icons.copy),
onPressed: () => _copyToClipboard(p.payload ?? ''),
),
IconButton(
tooltip: 'Cancel',
icon: const Icon(Icons.cancel),
onPressed: () => _cancelId(p.id),
),
],
),
);
}
Widget _buildLogSection(Set<int> pendingIds) {
return Card(
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Schedule Log (${_logEntries.length})', style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 8),
if (_logEntries.isEmpty)
const Text('No log entries'),
for (final e in _logEntries) _buildLogTile(e, pendingIds),
],
),
),
);
}
Widget _buildLogTile(NotificationLogEntry e, Set<int> pendingIds) {
final inQueue = pendingIds.contains(e.id);
String forStr = 'unknown';
if (e.type == 'single') {
if (e.singleId != null) {
final name = _supplementNameCache[e.singleId!];
forStr = name != null ? '$name (id=${e.singleId})' : 'id=${e.singleId}';
}
} else if (e.type == 'group') {
if (e.timeKey != null) {
forStr = 'Time ${e.timeKey}';
}
}
final scheduledAt = DateTime.fromMillisecondsSinceEpoch(e.whenEpochMs).toLocal();
final createdAt = DateTime.fromMillisecondsSinceEpoch(e.createdAtEpochMs).toLocal();
// Format times more clearly
final scheduledStr = scheduledAt.toString().substring(0, 16);
final createdStr = createdAt.toString().substring(0, 16);
// Show status and timing info
final statusStr = inQueue ? '🟡 Pending' : '✅ Completed/Canceled';
return ListTile(
dense: true,
leading: Icon(inQueue ? Icons.pending : Icons.check, color: inQueue ? Colors.amber : Colors.green),
title: Text('[${e.kind}] ${e.title} (ID ${e.id})'),
subtitle: Text('$statusStr • Type: ${e.type} • For: $forStr\n🕒 Scheduled: $scheduledStr\n📝 Created: $createdStr'),
trailing: Wrap(
spacing: 4,
children: [
IconButton(
tooltip: 'Copy payload',
icon: const Icon(Icons.copy),
onPressed: () => _copyToClipboard(e.payload),
),
if (inQueue)
IconButton(
tooltip: 'Cancel',
icon: const Icon(Icons.cancel),
onPressed: () => _cancelId(e.id),
),
],
),
);
}
}

View File

@@ -15,12 +15,10 @@ class HistoryScreen extends StatefulWidget {
class _HistoryScreenState extends State<HistoryScreen> { class _HistoryScreenState extends State<HistoryScreen> {
int _selectedMonth = DateTime.now().month; int _selectedMonth = DateTime.now().month;
int _selectedYear = DateTime.now().year; int _selectedYear = DateTime.now().year;
DateTime? _selectedDay;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_selectedDay = DateTime.now(); // Set today as the default selected day
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth); context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
}); });
@@ -32,16 +30,33 @@ class _HistoryScreenState extends State<HistoryScreen> {
appBar: AppBar( appBar: AppBar(
title: const Text('Intake History'), title: const Text('Intake History'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary, backgroundColor: Theme.of(context).colorScheme.inversePrimary,
actions: [
IconButton(
icon: const Icon(Icons.calendar_today),
onPressed: _showMonthPicker,
tooltip: 'Select Month',
),
],
), ),
body: _buildCalendarView(), body: _buildCompactHistoryView(),
); );
} }
Widget _buildCalendarView() { Widget _buildCompactHistoryView() {
return Column( return Column(
children: [ children: [
// Month selector header
Container( Container(
padding: const EdgeInsets.all(16), padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
border: Border(
bottom: BorderSide(
color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.2),
width: 1,
),
),
),
child: Row( child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
@@ -55,22 +70,19 @@ class _HistoryScreenState extends State<HistoryScreen> {
_selectedMonth--; _selectedMonth--;
} }
}); });
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth); WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
});
}, },
icon: const Icon(Icons.chevron_left), icon: const Icon(Icons.chevron_left),
iconSize: 20,
), ),
InkWell( Text(
onTap: _showMonthPicker, DateFormat('MMMM yyyy').format(DateTime(_selectedYear, _selectedMonth)),
borderRadius: BorderRadius.circular(8), style: TextStyle(
child: Padding( fontSize: 16,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), fontWeight: FontWeight.w600,
child: Text( color: Theme.of(context).colorScheme.onSurface,
DateFormat('MMMM yyyy').format(DateTime(_selectedYear, _selectedMonth)),
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
), ),
), ),
IconButton( IconButton(
@@ -85,76 +97,191 @@ class _HistoryScreenState extends State<HistoryScreen> {
_selectedMonth++; _selectedMonth++;
} }
}); });
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth); WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
});
} }
}, },
icon: const Icon(Icons.chevron_right), icon: const Icon(Icons.chevron_right),
iconSize: 20,
), ),
], ],
), ),
), ),
// History list
Expanded( Expanded(
child: Consumer<SupplementProvider>( child: Consumer<SupplementProvider>(
builder: (context, provider, child) { builder: (context, provider, child) {
// Group intakes by date if (provider.monthlyIntakes.isEmpty) {
final groupedIntakes = <String, List<Map<String, dynamic>>>{}; return Center(
for (final intake in provider.monthlyIntakes) { child: Column(
final date = DateTime.parse(intake['takenAt']); mainAxisAlignment: MainAxisAlignment.center,
final dateKey = DateFormat('yyyy-MM-dd').format(date); children: [
groupedIntakes.putIfAbsent(dateKey, () => []); Icon(
groupedIntakes[dateKey]!.add(intake); Icons.history,
size: 48,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(height: 16),
Text(
'No intake history for this month',
style: TextStyle(
fontSize: 16,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
],
),
);
} }
return LayoutBuilder( // Sort intakes by date (most recent first)
builder: (context, constraints) { final sortedIntakes = List<Map<String, dynamic>>.from(provider.monthlyIntakes)
final isWideScreen = constraints.maxWidth > 800; ..sort((a, b) => DateTime.parse(b['takenAt']).compareTo(DateTime.parse(a['takenAt'])));
if (isWideScreen) { return ListView.builder(
// Desktop/tablet layout: side-by-side padding: const EdgeInsets.symmetric(vertical: 8),
return Row( itemCount: sortedIntakes.length,
crossAxisAlignment: CrossAxisAlignment.start, itemBuilder: (context, index) {
children: [ final intake = sortedIntakes[index];
// Calendar on the left final takenAt = DateTime.parse(intake['takenAt']);
Expanded( final units = (intake['unitsTaken'] as num?)?.toDouble() ?? 1.0;
flex: 2,
child: Container( // Check if we need a date header
margin: const EdgeInsets.all(16), final previousIntake = index > 0 ? sortedIntakes[index - 1] : null;
child: _buildCalendar(groupedIntakes), final showDateHeader = previousIntake == null ||
), DateFormat('yyyy-MM-dd').format(DateTime.parse(previousIntake['takenAt'])) !=
), DateFormat('yyyy-MM-dd').format(takenAt);
// Selected day details on the right
Expanded( return Column(
flex: 3, crossAxisAlignment: CrossAxisAlignment.start,
child: Container( children: [
// add a bit more horizontal spacing between calendar and card if (showDateHeader) ...[
margin: const EdgeInsets.fromLTRB(8, 16, 16, 16), Container(
child: SingleChildScrollView( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: _buildSelectedDayDetails(groupedIntakes), child: Text(
DateFormat('EEEE, MMMM d').format(takenAt),
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: Theme.of(context).colorScheme.primary,
), ),
), ),
), ),
], ],
); Dismissible(
} else { key: Key('intake_${intake['id']}'),
// Mobile layout: vertical stack direction: DismissDirection.endToStart,
return SingleChildScrollView( background: Container(
child: Column( alignment: Alignment.centerRight,
children: [ padding: const EdgeInsets.only(right: 20),
// Calendar color: Colors.red.shade400,
Container( child: const Icon(
margin: const EdgeInsets.symmetric(horizontal: 16), Icons.delete,
child: _buildCalendar(groupedIntakes), color: Colors.white,
), ),
const SizedBox(height: 16), ),
// Selected day details confirmDismiss: (direction) async {
Container( return await showDialog(
margin: const EdgeInsets.symmetric(horizontal: 16), context: context,
child: _buildSelectedDayDetails(groupedIntakes), builder: (context) => AlertDialog(
title: const Text('Delete Intake'),
content: Text('Delete ${intake['supplementName']} taken at ${DateFormat('HH:mm').format(takenAt)}?'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
style: TextButton.styleFrom(foregroundColor: Colors.red),
child: const Text('Delete'),
),
],
),
);
},
onDismissed: (direction) {
context.read<SupplementProvider>().deleteIntake(intake['id']);
},
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 2),
child: Card(
margin: EdgeInsets.zero,
elevation: 2,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
child: InkWell(
onTap: () => _deleteIntake(context, intake['id'], intake['supplementName']),
borderRadius: BorderRadius.circular(12),
splashColor: Theme.of(context).colorScheme.primary.withValues(alpha: 0.4),
highlightColor: Theme.of(context).colorScheme.primary.withValues(alpha: 0.3),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(6),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary.withValues(alpha: 0.1),
shape: BoxShape.circle,
),
child: Icon(
Icons.medication,
color: Theme.of(context).colorScheme.primary,
size: 18,
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
intake['supplementName'],
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onSurface,
),
),
const SizedBox(height: 2),
Text(
'${DateFormat('HH:mm').format(takenAt)}${units.toStringAsFixed(units % 1 == 0 ? 0 : 1)} ${intake['supplementUnitType'] ?? 'units'}',
style: TextStyle(
fontSize: 11,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
if (intake['notes'] != null && intake['notes'].toString().isNotEmpty) ...[
const SizedBox(height: 2),
Text(
intake['notes'],
style: TextStyle(
fontSize: 11,
fontStyle: FontStyle.italic,
color: Theme.of(context).colorScheme.onSurfaceVariant.withValues(alpha: 0.8),
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
],
],
),
),
Icon(
Icons.delete_outline,
size: 18,
color: Theme.of(context).colorScheme.onSurfaceVariant.withValues(alpha: 0.5),
),
],
),
),
), ),
], ),
), ),
); ),
} ],
);
}, },
); );
}, },
@@ -187,12 +314,14 @@ class _HistoryScreenState extends State<HistoryScreen> {
); );
if (picked != null) { if (picked != null) {
if (!context.mounted) return;
setState(() { setState(() {
_selectedMonth = picked.month; _selectedMonth = picked.month;
_selectedYear = picked.year; _selectedYear = picked.year;
_selectedDay = picked; // Set the selected day to the picked date
}); });
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth); WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
});
} }
} }
@@ -210,13 +339,16 @@ class _HistoryScreenState extends State<HistoryScreen> {
ElevatedButton( ElevatedButton(
onPressed: () async { onPressed: () async {
await context.read<SupplementProvider>().deleteIntake(intakeId); await context.read<SupplementProvider>().deleteIntake(intakeId);
if (!context.mounted) return;
Navigator.of(context).pop(); Navigator.of(context).pop();
// Force refresh of the UI // Force refresh of the UI
setState(() {}); setState(() {});
// Force refresh of the current view data // Force refresh of the current view data
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth); WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<SupplementProvider>().loadMonthlyIntakes(_selectedYear, _selectedMonth);
});
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
@@ -233,471 +365,7 @@ class _HistoryScreenState extends State<HistoryScreen> {
); );
} }
Widget _buildCalendar(Map<String, List<Map<String, dynamic>>> groupedIntakes) {
final firstDayOfMonth = DateTime(_selectedYear, _selectedMonth, 1);
final lastDayOfMonth = DateTime(_selectedYear, _selectedMonth + 1, 0);
final firstWeekday = firstDayOfMonth.weekday;
final daysInMonth = lastDayOfMonth.day;
// Calculate how many cells we need (including empty ones for alignment)
final totalCells = ((daysInMonth + firstWeekday - 1) / 7).ceil() * 7;
final weeks = (totalCells / 7).ceil();
return LayoutBuilder(
builder: (context, constraints) {
final isWideScreen = constraints.maxWidth > 800;
// Calculate calendar height based on number of weeks needed
final cellHeight = isWideScreen ? 56.0 : 48.0;
final calendarContentHeight = (weeks * cellHeight) + 60; // +60 for headers and padding
final calendarHeight = isWideScreen ? 400.0 : calendarContentHeight;
return Card(
child: Container(
height: calendarHeight,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
// Calendar header (weekdays)
Row(
children: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
.map((day) => Expanded(
child: Center(
child: Text(
day,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onSurfaceVariant,
fontSize: isWideScreen ? 14 : 12,
),
),
),
))
.toList(),
),
const SizedBox(height: 8),
// Calendar grid
Expanded(
child: GridView.builder(
shrinkWrap: true,
physics: const ClampingScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 7,
childAspectRatio: 1.0,
mainAxisSpacing: 4,
crossAxisSpacing: 4,
),
itemCount: totalCells,
itemBuilder: (context, index) {
final dayNumber = index - firstWeekday + 2;
if (dayNumber < 1 || dayNumber > daysInMonth) {
return const SizedBox(); // Empty cell
}
final date = DateTime(_selectedYear, _selectedMonth, dayNumber);
final dateKey = DateFormat('yyyy-MM-dd').format(date);
final hasIntakes = groupedIntakes.containsKey(dateKey);
final intakeCount = hasIntakes ? groupedIntakes[dateKey]!.length : 0;
final isSelected = _selectedDay != null &&
DateFormat('yyyy-MM-dd').format(_selectedDay!) == dateKey;
final isToday = DateFormat('yyyy-MM-dd').format(DateTime.now()) == dateKey;
return GestureDetector(
onTap: () {
setState(() {
_selectedDay = date;
});
},
child: Container(
margin: const EdgeInsets.all(1),
decoration: BoxDecoration(
color: isSelected
? Theme.of(context).colorScheme.primary
: hasIntakes
? Theme.of(context).colorScheme.primaryContainer
: null,
border: isToday
? Border.all(color: Theme.of(context).colorScheme.secondary, width: 2)
: null,
borderRadius: BorderRadius.circular(8),
),
child: Stack(
children: [
Center(
child: Text(
'$dayNumber',
style: TextStyle(
color: isSelected
? Theme.of(context).colorScheme.onPrimary
: hasIntakes
? Theme.of(context).colorScheme.onPrimaryContainer
: Theme.of(context).colorScheme.onSurface,
fontWeight: isToday ? FontWeight.bold : FontWeight.normal,
fontSize: isWideScreen ? 16 : 14,
),
),
),
if (hasIntakes)
Positioned(
top: 2,
right: 2,
child: Container(
padding: EdgeInsets.all(isWideScreen ? 3 : 2),
decoration: BoxDecoration(
color: isSelected
? Theme.of(context).colorScheme.onPrimary
: Theme.of(context).colorScheme.primary,
borderRadius: BorderRadius.circular(8),
),
constraints: BoxConstraints(
minWidth: isWideScreen ? 18 : 16,
minHeight: isWideScreen ? 18 : 16,
),
child: Text(
'$intakeCount',
style: TextStyle(
color: isSelected
? Theme.of(context).colorScheme.primary
: Theme.of(context).colorScheme.onPrimary,
fontSize: isWideScreen ? 11 : 10,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
),
),
],
),
),
);
},
),
),
],
),
),
),
);
},
);
}
Widget _buildSelectedDayDetails(Map<String, List<Map<String, dynamic>>> groupedIntakes) {
return LayoutBuilder(
builder: (context, constraints) {
final isWideScreen = constraints.maxWidth > 600;
if (_selectedDay == null) {
return Card(
child: Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.touch_app,
size: isWideScreen ? 64 : 48,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(height: 16),
Text(
'Tap a date on the calendar to see details',
style: TextStyle(
fontSize: isWideScreen ? 18 : 16,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
],
),
),
),
);
}
final dateKey = DateFormat('yyyy-MM-dd').format(_selectedDay!);
final dayIntakes = groupedIntakes[dateKey] ?? [];
if (dayIntakes.isEmpty) {
return Card(
child: Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.event_available,
size: isWideScreen ? 64 : 48,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(height: 16),
Text(
'No supplements taken on ${DateFormat('MMM d, yyyy').format(_selectedDay!)}',
style: TextStyle(
fontSize: isWideScreen ? 18 : 16,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
],
),
),
),
);
}
return Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: double.infinity,
padding: EdgeInsets.all(isWideScreen ? 20 : 16),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primaryContainer,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
DateFormat('EEEE, MMM d, yyyy').format(_selectedDay!),
style: TextStyle(
fontSize: isWideScreen ? 20 : 18,
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onPrimaryContainer,
),
),
const SizedBox(height: 4),
Text(
'${dayIntakes.length} supplement${dayIntakes.length != 1 ? 's' : ''} taken',
style: TextStyle(
fontSize: isWideScreen ? 16 : 14,
color: Theme.of(context).colorScheme.onPrimaryContainer.withOpacity(0.8),
),
),
],
),
),
Padding(
padding: EdgeInsets.all(isWideScreen ? 20 : 16),
child: Builder(
builder: (context) {
final settingsProvider = Provider.of<SettingsProvider>(context, listen: false);
// Sort once per render
final sortedDayIntakes = List<Map<String, dynamic>>.from(dayIntakes)
..sort((a, b) => DateTime.parse(a['takenAt']).compareTo(DateTime.parse(b['takenAt'])));
// Helpers
String timeCategory(DateTime dt) {
final h = dt.hour;
if (h >= settingsProvider.morningStart && h <= settingsProvider.morningEnd) return 'morning';
if (h >= settingsProvider.afternoonStart && h <= settingsProvider.afternoonEnd) return 'afternoon';
if (h >= settingsProvider.eveningStart && h <= settingsProvider.eveningEnd) return 'evening';
final ns = settingsProvider.nightStart;
final ne = settingsProvider.nightEnd;
final inNight = ns <= ne ? (h >= ns && h <= ne) : (h >= ns || h <= ne);
return inNight ? 'night' : 'anytime';
}
String? sectionRange(String cat) {
switch (cat) {
case 'morning':
return settingsProvider.morningRange;
case 'afternoon':
return settingsProvider.afternoonRange;
case 'evening':
return settingsProvider.eveningRange;
case 'night':
return settingsProvider.nightRange;
default:
return null;
}
}
Widget headerFor(String cat) {
late final IconData icon;
late final Color color;
late final String title;
switch (cat) {
case 'morning':
icon = Icons.wb_sunny;
color = Colors.orange;
title = 'Morning';
break;
case 'afternoon':
icon = Icons.light_mode;
color = Colors.blue;
title = 'Afternoon';
break;
case 'evening':
icon = Icons.nightlight_round;
color = Colors.indigo;
title = 'Evening';
break;
case 'night':
icon = Icons.bedtime;
color = Colors.purple;
title = 'Night';
break;
default:
icon = Icons.schedule;
color = Colors.grey;
title = 'Anytime';
}
final range = sectionRange(cat);
return Container(
margin: const EdgeInsets.only(bottom: 12),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: color.withOpacity(0.1),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: color.withOpacity(0.3),
width: 1,
),
),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: color.withOpacity(0.2),
shape: BoxShape.circle,
),
child: Icon(
icon,
size: 20,
color: color,
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: color,
),
),
if (range != null) ...[
Text(
'($range)',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500,
color: color.withOpacity(0.8),
),
),
],
],
),
),
],
),
);
}
// Build a non-scrollable list so the card auto-expands to fit content
final List<Widget> children = [];
for (int index = 0; index < sortedDayIntakes.length; index++) {
final intake = sortedDayIntakes[index];
final takenAt = DateTime.parse(intake['takenAt']);
final units = (intake['unitsTaken'] as num?)?.toDouble() ?? 1.0;
final currentCategory = timeCategory(takenAt);
final needsHeader = index == 0
? true
: currentCategory != timeCategory(DateTime.parse(sortedDayIntakes[index - 1]['takenAt']));
if (needsHeader) {
children.add(headerFor(currentCategory));
}
children.add(
Card(
margin: const EdgeInsets.only(bottom: 12),
elevation: 2,
child: Padding(
padding: EdgeInsets.all(isWideScreen ? 16 : 12),
child: Row(
children: [
CircleAvatar(
backgroundColor: Theme.of(context).colorScheme.primary,
radius: isWideScreen ? 24 : 20,
child: Icon(
Icons.medication,
color: Theme.of(context).colorScheme.onPrimary,
size: isWideScreen ? 24 : 20,
),
),
SizedBox(width: isWideScreen ? 16 : 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
intake['supplementName'],
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: isWideScreen ? 16 : 14,
),
),
const SizedBox(height: 4),
Text(
'${units.toStringAsFixed(units % 1 == 0 ? 0 : 1)} ${intake['supplementUnitType'] ?? 'units'} at ${DateFormat('HH:mm').format(takenAt)}',
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
fontWeight: FontWeight.w500,
fontSize: isWideScreen ? 14 : 12,
),
),
if (intake['notes'] != null && intake['notes'].toString().isNotEmpty) ...[
const SizedBox(height: 4),
Text(
intake['notes'],
style: TextStyle(
fontSize: isWideScreen ? 13 : 12,
fontStyle: FontStyle.italic,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
],
],
),
),
IconButton(
icon: Icon(
Icons.delete_outline,
color: Colors.red.shade400,
size: isWideScreen ? 24 : 20,
),
onPressed: () => _deleteIntake(context, intake['id'], intake['supplementName']),
tooltip: 'Delete intake',
),
],
),
),
),
);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
);
},
),
),
],
),
);
},
);
}
@override @override
void dispose() { void dispose() {

View File

@@ -1,11 +1,12 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import '../providers/supplement_provider.dart'; import '../providers/supplement_provider.dart';
import '../providers/settings_provider.dart';
import 'supplements_list_screen.dart';
import 'history_screen.dart';
import 'add_supplement_screen.dart'; import 'add_supplement_screen.dart';
import 'history_screen.dart';
import 'settings_screen.dart'; import 'settings_screen.dart';
import 'supplements_list_screen.dart';
import 'today_schedule_screen.dart';
class HomeScreen extends StatefulWidget { class HomeScreen extends StatefulWidget {
const HomeScreen({super.key}); const HomeScreen({super.key});
@@ -18,6 +19,7 @@ class _HomeScreenState extends State<HomeScreen> {
int _currentIndex = 0; int _currentIndex = 0;
final List<Widget> _screens = [ final List<Widget> _screens = [
const TodayScheduleScreen(),
const SupplementsListScreen(), const SupplementsListScreen(),
const HistoryScreen(), const HistoryScreen(),
const SettingsScreen(), const SettingsScreen(),
@@ -28,45 +30,10 @@ class _HomeScreenState extends State<HomeScreen> {
super.initState(); super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<SupplementProvider>().initialize(); context.read<SupplementProvider>().initialize();
_startPersistentReminderCheck();
}); });
} }
void _startPersistentReminderCheck() { // Persistent reminder checks removed
// Check immediately and then every 3 minutes (faster than any retry interval)
_checkPersistentReminders();
// Set up periodic checking every 3 minutes to ensure we catch all retry intervals
Future.doWhile(() async {
await Future.delayed(const Duration(minutes: 3));
if (mounted) {
await _checkPersistentReminders();
return true;
}
return false;
});
}
Future<void> _checkPersistentReminders() async {
if (!mounted) return;
try {
print('SupplementsLog: 📱 === HOME SCREEN: Checking persistent reminders ===');
final supplementProvider = context.read<SupplementProvider>();
final settingsProvider = context.read<SettingsProvider>();
print('SupplementsLog: 📱 Settings: persistent=${settingsProvider.persistentReminders}, interval=${settingsProvider.reminderRetryInterval}, max=${settingsProvider.maxRetryAttempts}');
await supplementProvider.checkPersistentRemindersWithSettings(
persistentReminders: settingsProvider.persistentReminders,
reminderRetryInterval: settingsProvider.reminderRetryInterval,
maxRetryAttempts: settingsProvider.maxRetryAttempts,
);
print('SupplementsLog: 📱 === HOME SCREEN: Persistent reminder check complete ===');
} catch (e) {
print('SupplementsLog: Error checking persistent reminders: $e');
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -80,11 +47,18 @@ class _HomeScreenState extends State<HomeScreen> {
}); });
}, },
type: BottomNavigationBarType.fixed, type: BottomNavigationBarType.fixed,
selectedItemColor: Theme.of(context).colorScheme.primary,
unselectedItemColor: Theme.of(context).colorScheme.onSurface.withValues(alpha: 0.4),
backgroundColor: Theme.of(context).colorScheme.surface,
items: const [ items: const [
BottomNavigationBarItem( BottomNavigationBarItem(
icon: Icon(Icons.medication), icon: Icon(Icons.medication_liquid_sharp),
label: 'Supplements', label: 'Supplements',
), ),
BottomNavigationBarItem(
icon: Icon(Icons.edit_calendar_rounded),
label: 'Edit',
),
BottomNavigationBarItem( BottomNavigationBarItem(
icon: Icon(Icons.history), icon: Icon(Icons.history),
label: 'History', label: 'History',
@@ -95,7 +69,7 @@ class _HomeScreenState extends State<HomeScreen> {
), ),
], ],
), ),
floatingActionButton: _currentIndex == 0 floatingActionButton: _currentIndex == 1
? FloatingActionButton( ? FloatingActionButton(
onPressed: () async { onPressed: () async {
await Navigator.of(context).push( await Navigator.of(context).push(

View File

@@ -1,823 +1,58 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:provider/provider.dart';
import '../services/notification_service.dart';
import '../services/database_helper.dart';
import '../providers/settings_provider.dart';
import '../providers/supplement_provider.dart';
class PendingNotificationsScreen extends StatefulWidget { /// Simple placeholder screen for pending notifications.
/// In the simplified notification setup, we no longer track or retry notifications,
/// so there is no "pending notifications" list to display.
///
/// Keep this screen minimal to avoid heavy logic and dependencies.
class PendingNotificationsScreen extends StatelessWidget {
const PendingNotificationsScreen({super.key}); const PendingNotificationsScreen({super.key});
@override
State<PendingNotificationsScreen> createState() => _PendingNotificationsScreenState();
}
class _PendingNotificationsScreenState extends State<PendingNotificationsScreen> {
List<PendingNotificationRequest> _pendingNotifications = [];
List<Map<String, dynamic>> _trackedNotifications = [];
bool _isLoading = true;
@override
void initState() {
super.initState();
_loadNotifications();
}
Future<void> _loadNotifications() async {
setState(() {
_isLoading = true;
});
try {
// Get settings for retry interval calculation
final settingsProvider = Provider.of<SettingsProvider>(context, listen: false);
final reminderRetryInterval = settingsProvider.reminderRetryInterval;
final maxRetryAttempts = settingsProvider.maxRetryAttempts;
// Get system pending notifications
final notificationService = NotificationService();
final systemPending = await notificationService.getPendingNotifications();
// Get tracked notifications from database (including retries)
final trackedNotifications = await DatabaseHelper.instance.getPendingNotifications();
// Create a more intelligent matching system
final allNotifications = <Map<String, dynamic>>[];
final matchedSystemIds = <int>{};
// First, try to match tracked notifications with system notifications
for (final trackedNotification in trackedNotifications) {
final scheduledTime = DateTime.parse(trackedNotification['scheduledTime']).toLocal();
final lastRetryTime = trackedNotification['lastRetryTime'] != null
? DateTime.parse(trackedNotification['lastRetryTime']).toLocal()
: null;
final retryCount = trackedNotification['retryCount'] ?? 0;
final isRetrying = trackedNotification['status'] == 'retrying';
final notificationId = trackedNotification['notificationId'] as int;
// Try to find matching system notification(s)
final matchingSystemNotifications = systemPending.where((systemNotification) {
return _isMatchingNotification(systemNotification, trackedNotification, retryCount);
}).toList();
// Calculate next retry time if this is a retry notification
DateTime? nextRetryTime;
bool hasReachedMaxRetries = retryCount >= maxRetryAttempts;
if (isRetrying && !hasReachedMaxRetries) {
if (lastRetryTime != null) {
// Next retry is based on last retry time + interval
nextRetryTime = lastRetryTime.add(Duration(minutes: reminderRetryInterval));
} else {
// First retry is based on original scheduled time + interval
nextRetryTime = scheduledTime.add(Duration(minutes: reminderRetryInterval));
}
}
// Create the notification entry
final notificationEntry = {
'id': notificationId,
'title': 'Time for ${trackedNotification['supplementName'] ?? 'Supplement'}',
'body': 'Take your supplement',
'scheduledTime': scheduledTime,
'nextRetryTime': nextRetryTime,
'lastRetryTime': lastRetryTime,
'type': matchingSystemNotifications.isNotEmpty ? 'matched' : 'tracked_only',
'status': trackedNotification['status'],
'isRetry': isRetrying,
'retryCount': retryCount,
'hasReachedMaxRetries': hasReachedMaxRetries,
'maxRetryAttempts': maxRetryAttempts,
'supplementName': trackedNotification['supplementName'],
'supplementId': trackedNotification['supplementId'],
'systemNotificationCount': matchingSystemNotifications.length,
};
allNotifications.add(notificationEntry);
// Mark these system notifications as matched
for (final systemNotification in matchingSystemNotifications) {
matchedSystemIds.add(systemNotification.id);
}
}
// Add unmatched system notifications
for (final systemNotification in systemPending) {
if (!matchedSystemIds.contains(systemNotification.id)) {
allNotifications.add({
'id': systemNotification.id,
'title': systemNotification.title ?? 'System Notification',
'body': systemNotification.body ?? '',
'scheduledTime': DateTime.now(), // PendingNotificationRequest doesn't have scheduledTime
'type': 'system_only',
'isRetry': false,
'retryCount': 0,
'hasReachedMaxRetries': false,
'maxRetryAttempts': maxRetryAttempts,
});
}
}
// Sort by scheduled time (soonest first)
allNotifications.sort((a, b) {
final timeA = a['scheduledTime'] as DateTime;
final timeB = b['scheduledTime'] as DateTime;
return timeA.compareTo(timeB);
});
setState(() {
_pendingNotifications = systemPending;
_trackedNotifications = trackedNotifications;
_isLoading = false;
});
} catch (e) {
print('SupplementsLog: Error loading notifications: $e');
setState(() {
_isLoading = false;
});
}
}
Future<void> _showCleanupDialog() async {
final result = await showDialog<String>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Cleanup Old Notifications'),
content: const Text(
'This will help clean up old or duplicate notifications:\n\n'
'• Clear stale system notifications\n'
'• Remove very old tracked notifications (>24h overdue)\n'
'• Reschedule fresh notifications for active supplements\n\n'
'Choose an option:',
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop('cancel'),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.of(context).pop('clear_old'),
child: const Text('Clear Old Only'),
),
TextButton(
onPressed: () => Navigator.of(context).pop('clear_all'),
child: const Text('Clear All & Reschedule'),
),
],
);
},
);
if (result != null && result != 'cancel') {
await _performCleanup(result);
}
}
Future<void> _performCleanup(String action) async {
try {
// Show loading indicator
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Cleaning up notifications...'),
duration: Duration(seconds: 2),
),
);
}
final notificationService = NotificationService();
if (action == 'clear_all') {
// Clear all notifications and reschedule fresh ones
await notificationService.cancelAllReminders();
await DatabaseHelper.instance.cleanupOldNotificationTracking();
// Reschedule notifications for all active supplements
final supplementProvider = Provider.of<SupplementProvider>(context, listen: false);
await supplementProvider.rescheduleAllNotifications();
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('All notifications cleared and rescheduled!'),
backgroundColor: Colors.green,
),
);
}
} else if (action == 'clear_old') {
// Clear only very old notifications (>24 hours overdue)
await _clearOldNotifications();
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Old notifications cleared!'),
backgroundColor: Colors.orange,
),
);
}
}
// Refresh the list
await _loadNotifications();
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error during cleanup: $e'),
backgroundColor: Colors.red,
),
);
}
}
}
Future<void> _clearOldNotifications() async {
final now = DateTime.now();
final cutoff = now.subtract(const Duration(hours: 24));
// Clear very old tracked notifications
final db = await DatabaseHelper.instance.database;
await db.delete(
'notification_tracking',
where: 'scheduledTime < ? AND status IN (?, ?)',
whereArgs: [cutoff.toIso8601String(), 'pending', 'retrying'],
);
// Note: We can't selectively clear system notifications as we don't have their scheduled times
// This is a limitation of the flutter_local_notifications plugin
}
bool _isMatchingNotification(PendingNotificationRequest systemNotification, Map<String, dynamic> trackedNotification, int retryCount) {
final trackedId = trackedNotification['notificationId'] as int;
final supplementId = trackedNotification['supplementId'] as int;
// Check for exact ID match (original notification)
if (systemNotification.id == trackedId) {
return true;
}
// Check for retry notification IDs (200000 + original_id * 10 + retry_attempt)
for (int attempt = 1; attempt <= retryCount; attempt++) {
final retryId = 200000 + (trackedId * 10) + attempt;
if (systemNotification.id == retryId) {
return true;
}
}
// Check for snooze notification IDs (supplementId * 1000 + minutes)
// Common snooze intervals: 5, 10, 15, 30 minutes
final snoozeIds = [5, 10, 15, 30].map((minutes) => supplementId * 1000 + minutes);
if (snoozeIds.contains(systemNotification.id)) {
return true;
}
// Check if it's within the supplement's notification ID range (supplementId * 100 + reminderIndex)
final baseId = supplementId * 100;
if (systemNotification.id >= baseId && systemNotification.id < baseId + 10) {
return true;
}
return false;
}
Color _getTypeColor(String type) {
switch (type) {
case 'matched':
return Colors.green;
case 'tracked_only':
return Colors.orange;
case 'system_only':
return Colors.blue;
default:
return Colors.grey;
}
}
Color _getTypeDarkColor(String type) {
switch (type) {
case 'matched':
return Colors.green.shade700;
case 'tracked_only':
return Colors.orange.shade700;
case 'system_only':
return Colors.blue.shade700;
default:
return Colors.grey.shade700;
}
}
String _getTypeLabel(String type, Map<String, dynamic> notification) {
final systemCount = notification['systemNotificationCount'] as int? ?? 0;
switch (type) {
case 'matched':
return systemCount > 1 ? 'Matched' : 'Synced';
case 'tracked_only':
return 'Tracking Only';
case 'system_only':
return 'System Only';
default:
return 'Unknown';
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text('Pending Notifications'), title: const Text('Pending Notifications'),
actions: [
IconButton(
icon: const Icon(Icons.cleaning_services),
onPressed: _showCleanupDialog,
tooltip: 'Cleanup old notifications',
),
IconButton(
icon: const Icon(Icons.refresh),
onPressed: _loadNotifications,
tooltip: 'Refresh',
),
],
), ),
body: _isLoading body: Center(
? const Center(child: CircularProgressIndicator()) child: Padding(
: RefreshIndicator( padding: const EdgeInsets.all(24),
onRefresh: _loadNotifications, child: Column(
child: _buildNotificationsList(), mainAxisSize: MainAxisSize.min,
), children: [
); const Icon(
} Icons.notifications_none,
size: 72,
Widget _buildNotificationsList() {
if (_pendingNotifications.isEmpty && _trackedNotifications.isEmpty) {
return const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.notifications_off,
size: 64,
color: Colors.grey,
),
SizedBox(height: 16),
Text(
'No pending notifications',
style: TextStyle(
fontSize: 18,
color: Colors.grey, color: Colors.grey,
), ),
), const SizedBox(height: 16),
SizedBox(height: 8), Text(
Text( 'No pending notifications UI in simple mode',
'All caught up!', textAlign: TextAlign.center,
style: TextStyle( style: Theme.of(context).textTheme.titleMedium?.copyWith(
color: Colors.grey, color: Colors.grey.shade800,
fontWeight: FontWeight.w600,
),
), ),
),
],
),
);
}
// Get settings for retry interval calculation
final settingsProvider = Provider.of<SettingsProvider>(context, listen: false);
final reminderRetryInterval = settingsProvider.reminderRetryInterval;
final maxRetryAttempts = settingsProvider.maxRetryAttempts;
// Create a more intelligent matching system
final allNotifications = <Map<String, dynamic>>[];
final matchedSystemIds = <int>{};
// First, try to match tracked notifications with system notifications
for (final trackedNotification in _trackedNotifications) {
final scheduledTime = DateTime.parse(trackedNotification['scheduledTime']).toLocal();
final lastRetryTime = trackedNotification['lastRetryTime'] != null
? DateTime.parse(trackedNotification['lastRetryTime']).toLocal()
: null;
final retryCount = trackedNotification['retryCount'] ?? 0;
final isRetrying = trackedNotification['status'] == 'retrying';
final notificationId = trackedNotification['notificationId'] as int;
// Try to find matching system notification(s)
final matchingSystemNotifications = _pendingNotifications.where((systemNotification) {
return _isMatchingNotification(systemNotification, trackedNotification, retryCount);
}).toList();
// Calculate next retry time if this is a retry notification
DateTime? nextRetryTime;
bool hasReachedMaxRetries = retryCount >= maxRetryAttempts;
if (isRetrying && !hasReachedMaxRetries) {
if (lastRetryTime != null) {
// Next retry is based on last retry time + interval
nextRetryTime = lastRetryTime.add(Duration(minutes: reminderRetryInterval));
} else {
// First retry is based on original scheduled time + interval
nextRetryTime = scheduledTime.add(Duration(minutes: reminderRetryInterval));
}
}
// Create the notification entry
final notificationEntry = {
'id': notificationId,
'title': 'Time for ${trackedNotification['supplementName'] ?? 'Supplement'}',
'body': 'Take your supplement',
'scheduledTime': scheduledTime,
'nextRetryTime': nextRetryTime,
'lastRetryTime': lastRetryTime,
'type': matchingSystemNotifications.isNotEmpty ? 'matched' : 'tracked_only',
'status': trackedNotification['status'],
'isRetry': isRetrying,
'retryCount': retryCount,
'hasReachedMaxRetries': hasReachedMaxRetries,
'maxRetryAttempts': maxRetryAttempts,
'supplementName': trackedNotification['supplementName'],
'supplementId': trackedNotification['supplementId'],
'systemNotificationCount': matchingSystemNotifications.length,
};
allNotifications.add(notificationEntry);
// Mark these system notifications as matched
for (final systemNotification in matchingSystemNotifications) {
matchedSystemIds.add(systemNotification.id);
}
}
// Add unmatched system notifications
for (final systemNotification in _pendingNotifications) {
if (!matchedSystemIds.contains(systemNotification.id)) {
allNotifications.add({
'id': systemNotification.id,
'title': systemNotification.title ?? 'System Notification',
'body': systemNotification.body ?? '',
'scheduledTime': DateTime.now(), // PendingNotificationRequest doesn't have scheduledTime
'type': 'system_only',
'isRetry': false,
'retryCount': 0,
'hasReachedMaxRetries': false,
'maxRetryAttempts': maxRetryAttempts,
});
}
}
// Sort by scheduled time (soonest first)
allNotifications.sort((a, b) {
final timeA = a['scheduledTime'] as DateTime;
final timeB = b['scheduledTime'] as DateTime;
return timeA.compareTo(timeB);
});
return ListView.builder(
padding: const EdgeInsets.all(16),
itemCount: allNotifications.length,
itemBuilder: (context, index) {
final notification = allNotifications[index];
return _buildNotificationCard(notification);
},
);
}
Widget _buildNotificationCard(Map<String, dynamic> notification) {
final scheduledTime = notification['scheduledTime'] as DateTime;
final nextRetryTime = notification['nextRetryTime'] as DateTime?;
final lastRetryTime = notification['lastRetryTime'] as DateTime?;
final now = DateTime.now();
final isOverdue = scheduledTime.isBefore(now);
final isRetry = notification['isRetry'] as bool;
final retryCount = notification['retryCount'] as int;
final hasReachedMaxRetries = notification['hasReachedMaxRetries'] as bool? ?? false;
final maxRetryAttempts = notification['maxRetryAttempts'] as int? ?? 3;
final type = notification['type'] as String;
// Determine which time to show and calculate time text
DateTime displayTime = scheduledTime;
String timePrefix = '';
bool showAsNextRetry = false;
String timeText = '';
if (isRetry && nextRetryTime != null && !hasReachedMaxRetries) {
// For retry notifications that haven't reached max retries, show next retry time
final timeDifference = nextRetryTime.difference(now);
if (timeDifference.inSeconds.abs() < 30) {
// If within 30 seconds, show "due now"
displayTime = nextRetryTime;
timePrefix = 'Next retry: ';
showAsNextRetry = true;
timeText = 'due now';
} else if (nextRetryTime.isAfter(now)) {
displayTime = nextRetryTime;
timePrefix = 'Next retry: ';
showAsNextRetry = true;
} else {
// If next retry time has passed, show it's overdue for retry
displayTime = nextRetryTime;
timePrefix = 'Retry overdue: ';
showAsNextRetry = true;
}
} else if (isRetry && hasReachedMaxRetries) {
// For notifications that have reached max retries, don't show next retry time
showAsNextRetry = false;
}
// Calculate time text if not already set
if (timeText.isEmpty) {
final isDisplayTimeOverdue = displayTime.isBefore(now);
final timeUntil = displayTime.difference(now);
if (isDisplayTimeOverdue) {
final overdue = now.difference(displayTime);
if (overdue.inDays > 0) {
timeText = 'Overdue by ${overdue.inDays}d ${overdue.inHours % 24}h';
} else if (overdue.inHours > 0) {
timeText = 'Overdue by ${overdue.inHours}h ${overdue.inMinutes % 60}m';
} else if (overdue.inMinutes > 0) {
timeText = 'Overdue by ${overdue.inMinutes}m';
} else {
timeText = 'Overdue by ${overdue.inSeconds}s';
}
} else {
if (timeUntil.inDays > 0) {
timeText = 'In ${timeUntil.inDays}d ${timeUntil.inHours % 24}h';
} else if (timeUntil.inHours > 0) {
timeText = 'In ${timeUntil.inHours}h ${timeUntil.inMinutes % 60}m';
} else if (timeUntil.inMinutes > 0) {
timeText = 'In ${timeUntil.inMinutes}m';
} else {
timeText = 'In ${timeUntil.inSeconds}s';
}
}
}
// Determine if display time is overdue (needed for icon colors later)
final isDisplayTimeOverdue = displayTime.isBefore(now);
return Card(
margin: const EdgeInsets.only(bottom: 12),
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
notification['title'] ?? 'Notification',
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
),
),
if (notification['body'] != null) ...[
const SizedBox(height: 4),
Text(
notification['body'],
style: Theme.of(context).textTheme.bodyMedium,
),
],
],
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
if (isRetry) ...[
Container(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 4,
),
decoration: BoxDecoration(
color: hasReachedMaxRetries
? Colors.red.withOpacity(0.2)
: Colors.orange.withOpacity(0.2),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: hasReachedMaxRetries
? Colors.red.withOpacity(0.5)
: Colors.orange.withOpacity(0.5),
),
),
child: Text(
hasReachedMaxRetries
? 'Max retries ($retryCount/$maxRetryAttempts)'
: 'Retry #$retryCount',
style: TextStyle(
fontSize: 12,
color: hasReachedMaxRetries
? Colors.red.shade700
: Colors.orange.shade700,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(height: 4),
],
Container(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 4,
),
decoration: BoxDecoration(
color: _getTypeColor(type).withOpacity(0.2),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: _getTypeColor(type).withOpacity(0.5),
),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
_getTypeLabel(type, notification),
style: TextStyle(
fontSize: 12,
color: _getTypeDarkColor(type),
fontWeight: FontWeight.bold,
),
),
if (type == 'matched' && (notification['systemNotificationCount'] as int? ?? 0) > 1) ...[
const SizedBox(width: 4),
Container(
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 1),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.8),
borderRadius: BorderRadius.circular(8),
),
child: Text(
'${notification['systemNotificationCount']}',
style: TextStyle(
fontSize: 10,
color: _getTypeDarkColor(type),
fontWeight: FontWeight.bold,
),
),
),
],
],
),
),
],
),
],
),
const SizedBox(height: 12),
// Show original scheduled time
Row(
children: [
Icon(
isOverdue ? Icons.schedule_outlined : Icons.access_time,
size: 16,
color: isOverdue ? Colors.red : Colors.grey,
),
const SizedBox(width: 8),
Text(
'Scheduled: ${_formatTime(scheduledTime)}',
style: TextStyle(
color: isOverdue ? Colors.red : Colors.grey.shade700,
fontWeight: isOverdue ? FontWeight.bold : FontWeight.normal,
),
),
if (isOverdue) ...[
const SizedBox(width: 8),
Text(
'(${_formatOverdueTime(scheduledTime)})',
style: TextStyle(
color: Colors.red,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
],
],
),
// Show next retry time if applicable
if (showAsNextRetry && nextRetryTime != null) ...[
const SizedBox(height: 6),
Row(
children: [
Icon(
isDisplayTimeOverdue ? Icons.warning_outlined : Icons.refresh,
size: 16,
color: isDisplayTimeOverdue ? Colors.red : Colors.orange,
),
const SizedBox(width: 8),
Text(
'${timePrefix}${_formatTime(displayTime)}$timeText',
style: TextStyle(
color: isDisplayTimeOverdue ? Colors.red : Colors.orange.shade700,
fontWeight: FontWeight.bold,
fontSize: 13,
),
),
],
),
],
// Show max retries reached message
if (isRetry && hasReachedMaxRetries) ...[
const SizedBox(height: 6),
Row(
children: [
const Icon(
Icons.block,
size: 16,
color: Colors.red,
),
const SizedBox(width: 8),
Expanded(
child: Text(
'Maximum retry attempts reached. No more reminders will be sent.',
style: TextStyle(
color: Colors.red.shade700,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
),
],
),
],
// Show last retry time if available
if (isRetry && lastRetryTime != null) ...[
const SizedBox(height: 6),
Row(
children: [
const Icon(
Icons.history,
size: 16,
color: Colors.grey,
),
const SizedBox(width: 8),
Text(
'Last retry: ${_formatTime(lastRetryTime)}',
style: TextStyle(
color: Colors.grey.shade600,
fontSize: 12,
fontStyle: FontStyle.italic,
),
),
],
),
],
if (type == 'tracked' && notification['supplementName'] != null) ...[
const SizedBox(height: 8), const SizedBox(height: 8),
Row( Text(
children: [ 'Notifications are now scheduled daily at the selected times '
const Icon( 'without retries or tracking.',
Icons.medication, textAlign: TextAlign.center,
size: 16, style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Colors.grey, color: Colors.grey.shade600,
),
const SizedBox(width: 8),
Text(
notification['supplementName'],
style: TextStyle(
color: Colors.grey.shade700,
fontStyle: FontStyle.italic,
), ),
), ),
], const SizedBox(height: 24),
ElevatedButton.icon(
onPressed: () => Navigator.of(context).maybePop(),
icon: const Icon(Icons.close),
label: const Text('Close'),
), ),
], ],
], ),
), ),
), ),
); );
} }
String _formatTime(DateTime dateTime) {
final now = DateTime.now();
final today = DateTime(now.year, now.month, now.day);
final tomorrow = today.add(const Duration(days: 1));
final notificationDate = DateTime(dateTime.year, dateTime.month, dateTime.day);
String timeStr = '${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}';
if (notificationDate == today) {
return 'Today $timeStr';
} else if (notificationDate == tomorrow) {
return 'Tomorrow $timeStr';
} else {
return '${dateTime.day}/${dateTime.month} $timeStr';
}
}
String _formatOverdueTime(DateTime scheduledTime) {
final now = DateTime.now();
final overdue = now.difference(scheduledTime);
if (overdue.inDays > 0) {
return '${overdue.inDays}d ${overdue.inHours % 24}h overdue';
} else if (overdue.inHours > 0) {
return '${overdue.inHours}h ${overdue.inMinutes % 60}m overdue';
} else {
return '${overdue.inMinutes}m overdue';
}
}
} }

View File

@@ -1,129 +0,0 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:supplements/screens/home_screen.dart';
import '../providers/settings_provider.dart';
// Profile setup screen
class ProfileSetupScreen extends StatefulWidget {
const ProfileSetupScreen({super.key});
@override
State<ProfileSetupScreen> createState() => _ProfileSetupScreenState();
}
class _ProfileSetupScreenState extends State<ProfileSetupScreen> {
final _formKey = GlobalKey<FormState>();
DateTime? _dateOfBirth;
String? _gender;
final List<String> _genders = ['Male', 'Female', 'Other', 'Prefer not to say'];
@override
void initState() {
super.initState();
final settingsProvider = Provider.of<SettingsProvider>(context, listen: false);
_dateOfBirth = settingsProvider.dateOfBirth;
_gender = settingsProvider.gender;
}
void _saveProfile() {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
Provider.of<SettingsProvider>(context, listen: false).setDateOfBirthAndGender(_dateOfBirth!, _gender!);
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => HomeScreen()));
}
}
Future<void> _selectDate(BuildContext context) async {
final DateTime? picked = await showDatePicker(
context: context,
initialDate: _dateOfBirth ?? DateTime.now(),
firstDate: DateTime(1900),
lastDate: DateTime.now(),
);
if (picked != null && picked != _dateOfBirth) {
setState(() {
_dateOfBirth = picked;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Set Up Your Profile'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: [
Text(
'To provide you with personalized ingredient insights, please provide your date of birth and gender.',
style: Theme.of(context).textTheme.titleMedium,
textAlign: TextAlign.center,
),
const SizedBox(height: 24),
TextFormField(
decoration: const InputDecoration(
labelText: 'Date of Birth',
border: OutlineInputBorder(),
suffixIcon: Icon(Icons.calendar_today),
),
readOnly: true,
controller: TextEditingController(
text: _dateOfBirth == null
? ''
: '${_dateOfBirth!.toLocal()}'.split(' ')[0],
),
onTap: () => _selectDate(context),
validator: (value) {
if (_dateOfBirth == null) {
return 'Please select your date of birth';
}
return null;
},
),
const SizedBox(height: 16),
DropdownButtonFormField<String>(
decoration: const InputDecoration(
labelText: 'Gender',
border: OutlineInputBorder(),
),
value: _gender,
items: _genders.map((String gender) {
return DropdownMenuItem<String>(
value: gender,
child: Text(gender),
);
}).toList(),
onChanged: (value) {
setState(() {
_gender = value;
});
},
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please select your gender';
}
return null;
},
onSaved: (value) {
_gender = value;
},
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: _saveProfile,
child: const Text('Save and Continue'),
),
],
),
),
),
);
}
}

View File

@@ -1,9 +1,9 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import '../providers/settings_provider.dart'; import '../providers/settings_provider.dart';
import 'pending_notifications_screen.dart'; import 'debug_notifications_screen.dart';
import 'profile_setup_screen.dart';
import 'simple_sync_settings_screen.dart'; import 'simple_sync_settings_screen.dart';
class SettingsScreen extends StatelessWidget { class SettingsScreen extends StatelessWidget {
@@ -20,22 +20,25 @@ class SettingsScreen extends StatelessWidget {
return ListView( return ListView(
padding: const EdgeInsets.all(16.0), padding: const EdgeInsets.all(16.0),
children: [ children: [
Card( // Debug section (only in debug builds)
child: ListTile( if (kDebugMode) ...[
leading: const Icon(Icons.person), Card(
title: const Text('Profile'), child: ListTile(
subtitle: Text('Date of Birth: ${settingsProvider.dateOfBirth != null ? '${settingsProvider.dateOfBirth!.toLocal()}'.split(' ')[0] : 'Not set'}, Gender: ${settingsProvider.gender ?? 'Not set'}'), leading: const Icon(Icons.bug_report),
trailing: const Icon(Icons.chevron_right), title: const Text('Debug Notifications'),
onTap: () { subtitle: const Text('View scheduled notifications and debug log'),
Navigator.of(context).push( trailing: const Icon(Icons.chevron_right),
MaterialPageRoute( onTap: () {
builder: (context) => const ProfileSetupScreen(), Navigator.of(context).push(
), MaterialPageRoute(
); builder: (context) => const DebugNotificationsScreen(),
}, ),
);
},
),
), ),
), const SizedBox(height: 16),
const SizedBox(height: 16), ],
Card( Card(
child: ListTile( child: ListTile(
leading: const Icon(Icons.cloud_sync), leading: const Icon(Icons.cloud_sync),
@@ -64,8 +67,6 @@ class SettingsScreen extends StatelessWidget {
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
RadioListTile<ThemeOption>( RadioListTile<ThemeOption>(
title: const Text('Follow System'),
subtitle: const Text('Use system theme setting'),
value: ThemeOption.system, value: ThemeOption.system,
groupValue: settingsProvider.themeOption, groupValue: settingsProvider.themeOption,
onChanged: (value) { onChanged: (value) {
@@ -73,10 +74,10 @@ class SettingsScreen extends StatelessWidget {
settingsProvider.setThemeOption(value); settingsProvider.setThemeOption(value);
} }
}, },
title: const Text('Follow System'),
subtitle: const Text('Use system theme setting'),
), ),
RadioListTile<ThemeOption>( RadioListTile<ThemeOption>(
title: const Text('Light Theme'),
subtitle: const Text('Always use light theme'),
value: ThemeOption.light, value: ThemeOption.light,
groupValue: settingsProvider.themeOption, groupValue: settingsProvider.themeOption,
onChanged: (value) { onChanged: (value) {
@@ -84,10 +85,10 @@ class SettingsScreen extends StatelessWidget {
settingsProvider.setThemeOption(value); settingsProvider.setThemeOption(value);
} }
}, },
title: const Text('Light Theme'),
subtitle: const Text('Always use light theme'),
), ),
RadioListTile<ThemeOption>( RadioListTile<ThemeOption>(
title: const Text('Dark Theme'),
subtitle: const Text('Always use dark theme'),
value: ThemeOption.dark, value: ThemeOption.dark,
groupValue: settingsProvider.themeOption, groupValue: settingsProvider.themeOption,
onChanged: (value) { onChanged: (value) {
@@ -95,104 +96,33 @@ class SettingsScreen extends StatelessWidget {
settingsProvider.setThemeOption(value); settingsProvider.setThemeOption(value);
} }
}, },
title: const Text('Dark Theme'),
subtitle: const Text('Always use dark theme'),
), ),
], ],
), ),
), ),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
// Notifications
Card( Card(
child: Padding( child: ListTile(
padding: const EdgeInsets.all(16.0), leading: const Icon(Icons.snooze),
child: Column( title: const Text('Snooze duration'),
crossAxisAlignment: CrossAxisAlignment.start, subtitle: const Text('Delay for Snooze action'),
children: [ trailing: DropdownButton<int>(
Row( value: settingsProvider.snoozeMinutes,
children: [ items: const [
Icon(Icons.notifications_active, color: Colors.blue), DropdownMenuItem(value: 5, child: Text('5 min')),
const SizedBox(width: 8), DropdownMenuItem(value: 10, child: Text('10 min')),
Text( DropdownMenuItem(value: 15, child: Text('15 min')),
'Reminders', DropdownMenuItem(value: 20, child: Text('20 min')),
style: Theme.of(context).textTheme.titleMedium,
),
],
),
const SizedBox(height: 8),
Text(
'Configure reminders and how often they are retried when ignored',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 16),
SwitchListTile(
title: const Text('Enable Persistent Reminders'),
subtitle: const Text('Resend notifications if ignored after a specific time'),
value: settingsProvider.persistentReminders,
onChanged: (value) {
settingsProvider.setPersistentReminders(value);
},
),
if (settingsProvider.persistentReminders) ...[
const SizedBox(height: 16),
Text(
'Retry Interval',
style: Theme.of(context).textTheme.titleSmall,
),
const SizedBox(height: 8),
SegmentedButton<int>(
segments: const [
ButtonSegment(value: 5, label: Text('5 min')),
ButtonSegment(value: 10, label: Text('10 min')),
ButtonSegment(value: 15, label: Text('15 min')),
ButtonSegment(value: 30, label: Text('30 min')),
],
selected: {settingsProvider.reminderRetryInterval},
onSelectionChanged: (values) {
settingsProvider.setReminderRetryInterval(values.first);
},
),
const SizedBox(height: 16),
Text(
'Maximum Retry Attempts',
style: Theme.of(context).textTheme.titleSmall,
),
const SizedBox(height: 8),
SegmentedButton<int>(
segments: const [
ButtonSegment(value: 1, label: Text('1')),
ButtonSegment(value: 2, label: Text('2')),
ButtonSegment(value: 3, label: Text('3')),
ButtonSegment(value: 4, label: Text('4')),
ButtonSegment(value: 5, label: Text('5')),
],
selected: {settingsProvider.maxRetryAttempts},
onSelectionChanged: (values) {
settingsProvider.setMaxRetryAttempts(values.first);
},
),
const SizedBox(height: 16),
Text(
'Notification Actions',
style: Theme.of(context).textTheme.titleSmall,
),
const SizedBox(height: 8),
SizedBox(
width: 320,
child: ElevatedButton.icon(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const PendingNotificationsScreen(),
),
);
},
icon: const Icon(Icons.list),
label: const Text('View Pending Notifications'),
),
),
],
], ],
onChanged: (value) {
if (value != null) {
settingsProvider.setSnoozeMinutes(value);
}
},
), ),
), ),
), ),
@@ -331,9 +261,9 @@ class _TimeRangeSelector extends StatelessWidget {
return Container( return Container(
padding: const EdgeInsets.all(12), padding: const EdgeInsets.all(12),
decoration: BoxDecoration( decoration: BoxDecoration(
color: color.withOpacity(0.1), color: color.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),
border: Border.all(color: color.withOpacity(0.3)), border: Border.all(color: color.withValues(alpha: 0.3)),
), ),
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,

View File

@@ -1,16 +1,13 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:supplements/widgets/info_chip.dart';
import 'package:url_launcher/url_launcher.dart';
import '../models/ingredient.dart';
import '../models/supplement.dart'; import '../models/supplement.dart';
import '../providers/settings_provider.dart'; import '../providers/settings_provider.dart';
import '../providers/simple_sync_provider.dart'; import '../providers/simple_sync_provider.dart';
import '../providers/supplement_provider.dart'; import '../providers/supplement_provider.dart';
import '../services/database_sync_service.dart'; import '../services/database_sync_service.dart';
import '../services/rda_service.dart';
import '../widgets/supplement_card.dart'; import '../widgets/supplement_card.dart';
import '../widgets/dialogs/take_supplement_dialog.dart';
import 'add_supplement_screen.dart'; import 'add_supplement_screen.dart';
import 'archived_supplements_screen.dart'; import 'archived_supplements_screen.dart';
@@ -21,7 +18,7 @@ class SupplementsListScreen extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text('My Supplements'), title: const Text('Edit Supplements'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary, backgroundColor: Theme.of(context).colorScheme.inversePrimary,
actions: [ actions: [
Consumer<SimpleSyncProvider>( Consumer<SimpleSyncProvider>(
@@ -103,180 +100,30 @@ class SupplementsListScreen extends StatelessWidget {
await provider.loadSupplements(); await provider.loadSupplements();
await provider.refreshDailyStatus(); await provider.refreshDailyStatus();
}, },
child: _buildGroupedSupplementsList(context, provider.supplements, settingsProvider), child: _buildGroupedSupplementsList(context, provider.supplements, settingsProvider, provider),
); );
}, },
), ),
); );
} }
Widget _buildGroupedSupplementsList(BuildContext context, List<Supplement> supplements, SettingsProvider settingsProvider) { Widget _buildGroupedSupplementsList(BuildContext context, List<Supplement> supplements, SettingsProvider settingsProvider, SupplementProvider provider) {
final provider = Provider.of<SupplementProvider>(context, listen: false);
final groupedSupplements = _groupSupplementsByTimeOfDay(supplements, settingsProvider); final groupedSupplements = _groupSupplementsByTimeOfDay(supplements, settingsProvider);
return ListView( return ListView(
padding: const EdgeInsets.all(16), padding: const EdgeInsets.fromLTRB(16, 16, 16, 80),
children: [ children: [
// Daily RDA overview header
FutureBuilder<Map<String, Map<String, dynamic>>>(
future: (() async {
if (provider.todayIntakes.isEmpty) return <String, Map<String, dynamic>>{};
final dailyItems = <Ingredient>[];
for (final intake in provider.todayIntakes) {
final supId = intake['supplement_id'] as int;
final unitsRaw = intake['unitsTaken'];
final double units = unitsRaw is int ? unitsRaw.toDouble() : (unitsRaw as double? ?? 1.0);
final matching = provider.supplements.where((s) => s.id == supId);
if (matching.isEmpty) continue;
final sup = matching.first;
for (final ing in sup.ingredients) {
dailyItems.add(ing.copyWith(amount: ing.amount * units));
}
}
if (dailyItems.isEmpty) return <String, Map<String, dynamic>>{};
final service = RdaService();
final agg = await service.aggregateDailyIntake(
dailyItems,
dateOfBirth: settingsProvider.dateOfBirth,
gender: settingsProvider.gender,
);
// Convert to plain map for UI without depending on service types
final result = <String, Map<String, dynamic>>{};
agg.forEach((key, value) {
final v = value; // dynamic
result[key] = {
'unitLabel': v.unitLabel,
'rdaValue': v.rdaValue,
'rdaValueMin': v.rdaValueMin,
'rdaValueMax': v.rdaValueMax,
'ulValue': v.ulValue,
'total': v.totalAmountInRdaUnit,
'pctRda': v.percentOfRda,
'pctUl': v.percentOfUl,
'lifeStage': v.matchedLifeStageLabel,
'lifeStageDescription': v.matchedLifeStageDescription,
'rdaType': v.rdaType,
'note': v.note,
'nutrientUl': v.nutrientUl != null
? {
'value': v.nutrientUl!.value,
'unit': v.nutrientUl!.unit,
'duration': v.nutrientUl!.duration,
'note': v.nutrientUl!.note,
}
: null,
};
});
return result;
})(),
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const SizedBox.shrink();
}
final data = snapshot.data;
if (data == null || data.isEmpty) {
return const SizedBox.shrink();
}
return Card(
margin: const EdgeInsets.only(bottom: 16),
color: Theme.of(context).colorScheme.primaryContainer.withOpacity(0.25),
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(Icons.health_and_safety, size: 18, color: Theme.of(context).colorScheme.primary),
const SizedBox(width: 8),
Text(
"Today's intake ",
style: Theme.of(context).textTheme.titleSmall,
),
Text(
"(Vs Recommended Dietary Allowance)",
style: Theme.of(context).textTheme.labelSmall,
),
const Spacer(),
IconButton(
tooltip: 'Sources & disclaimer',
icon: const Icon(Icons.info_outline, size: 18),
onPressed: () => _showRdaSourcesSheet(context),
),
],
),
const SizedBox(height: 8),
Wrap(
spacing: 8,
runSpacing: 8,
children: data.entries.map((e) {
final v = e.value;
final double pct = (v['pctRda'] as double?) ?? 0.0;
final double? pctUl = v['pctUl'] as double?;
final pretty = e.key.split('_').map((w) => w.isEmpty ? w : '${w[0].toUpperCase()}${w.substring(1)}').join(' ');
Color bg = Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.5);
if (pctUl != null && pctUl > 100.0) {
bg = (pctUl <= 110.0) ? Colors.amber.withOpacity(0.25) : Colors.red.withOpacity(0.25);
} else if (pct >= 100.0) {
bg = Colors.green.withOpacity(0.25);
}
final color = Theme.of(context).colorScheme.onSurfaceVariant;
return InkWell(
onTap: () {
_showRdaDetailsSheet(context, pretty, v);
},
borderRadius: BorderRadius.circular(8),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
decoration: BoxDecoration(
color: bg,
borderRadius: BorderRadius.circular(8),
border: Border.all(color: Theme.of(context).colorScheme.outline.withOpacity(0.3)),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
pretty,
style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: color),
),
const SizedBox(width: 6),
Text(
'${pct.toStringAsFixed(pct >= 10 ? 0 : 1)}%',
style: TextStyle(fontSize: 12, color: color),
),
if (pctUl != null) ...[
const SizedBox(width: 6),
Icon(
pctUl > 100.0 ? Icons.warning_amber : Icons.shield_outlined,
size: 14,
color: pctUl > 100.0
? (pctUl <= 110.0 ? Colors.amber : Colors.red)
: color,
),
],
],
),
),
);
}).toList(),
),
],
),
),
);
},
),
if (groupedSupplements['morning']!.isNotEmpty) ...[ if (groupedSupplements['morning']!.isNotEmpty) ...[
_buildSectionHeader('Morning (${settingsProvider.morningRange})', Icons.wb_sunny, Colors.orange, groupedSupplements['morning']!.length), _buildSectionHeader('Morning (${settingsProvider.morningRange})', Icons.wb_sunny, Colors.orange, groupedSupplements['morning']!.length),
...groupedSupplements['morning']!.map((supplement) => ...groupedSupplements['morning']!.map((supplement) =>
SupplementCard( SupplementCard(
supplement: supplement, supplement: supplement,
onTake: () => _showTakeDialog(context, supplement), onTake: () => showTakeSupplementDialog(context, supplement),
onEdit: () => _editSupplement(context, supplement), onEdit: () => _editSupplement(context, supplement),
onDelete: () => _deleteSupplement(context, supplement), onDelete: () => _deleteSupplement(context, supplement),
onArchive: () => _archiveSupplement(context, supplement), onArchive: () => _archiveSupplement(context, supplement),
onDuplicate: () => context.read<SupplementProvider>().duplicateSupplement(supplement.id!), onDuplicate: () => context.read<SupplementProvider>().duplicateSupplement(supplement.id!),
showCompletionStatus: false,
), ),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
@@ -287,11 +134,12 @@ class SupplementsListScreen extends StatelessWidget {
...groupedSupplements['afternoon']!.map((supplement) => ...groupedSupplements['afternoon']!.map((supplement) =>
SupplementCard( SupplementCard(
supplement: supplement, supplement: supplement,
onTake: () => _showTakeDialog(context, supplement), onTake: () => showTakeSupplementDialog(context, supplement),
onEdit: () => _editSupplement(context, supplement), onEdit: () => _editSupplement(context, supplement),
onDelete: () => _deleteSupplement(context, supplement), onDelete: () => _deleteSupplement(context, supplement),
onArchive: () => _archiveSupplement(context, supplement), onArchive: () => _archiveSupplement(context, supplement),
onDuplicate: () => context.read<SupplementProvider>().duplicateSupplement(supplement.id!), onDuplicate: () => context.read<SupplementProvider>().duplicateSupplement(supplement.id!),
showCompletionStatus: false,
), ),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
@@ -302,11 +150,12 @@ class SupplementsListScreen extends StatelessWidget {
...groupedSupplements['evening']!.map((supplement) => ...groupedSupplements['evening']!.map((supplement) =>
SupplementCard( SupplementCard(
supplement: supplement, supplement: supplement,
onTake: () => _showTakeDialog(context, supplement), onTake: () => showTakeSupplementDialog(context, supplement),
onEdit: () => _editSupplement(context, supplement), onEdit: () => _editSupplement(context, supplement),
onDelete: () => _deleteSupplement(context, supplement), onDelete: () => _deleteSupplement(context, supplement),
onArchive: () => _archiveSupplement(context, supplement), onArchive: () => _archiveSupplement(context, supplement),
onDuplicate: () => context.read<SupplementProvider>().duplicateSupplement(supplement.id!), onDuplicate: () => context.read<SupplementProvider>().duplicateSupplement(supplement.id!),
showCompletionStatus: false,
), ),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
@@ -317,11 +166,12 @@ class SupplementsListScreen extends StatelessWidget {
...groupedSupplements['night']!.map((supplement) => ...groupedSupplements['night']!.map((supplement) =>
SupplementCard( SupplementCard(
supplement: supplement, supplement: supplement,
onTake: () => _showTakeDialog(context, supplement), onTake: () => showTakeSupplementDialog(context, supplement),
onEdit: () => _editSupplement(context, supplement), onEdit: () => _editSupplement(context, supplement),
onDelete: () => _deleteSupplement(context, supplement), onDelete: () => _deleteSupplement(context, supplement),
onArchive: () => _archiveSupplement(context, supplement), onArchive: () => _archiveSupplement(context, supplement),
onDuplicate: () => context.read<SupplementProvider>().duplicateSupplement(supplement.id!), onDuplicate: () => context.read<SupplementProvider>().duplicateSupplement(supplement.id!),
showCompletionStatus: false,
), ),
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
@@ -332,14 +182,16 @@ class SupplementsListScreen extends StatelessWidget {
...groupedSupplements['anytime']!.map((supplement) => ...groupedSupplements['anytime']!.map((supplement) =>
SupplementCard( SupplementCard(
supplement: supplement, supplement: supplement,
onTake: () => _showTakeDialog(context, supplement), onTake: () => showTakeSupplementDialog(context, supplement),
onEdit: () => _editSupplement(context, supplement), onEdit: () => _editSupplement(context, supplement),
onDelete: () => _deleteSupplement(context, supplement), onDelete: () => _deleteSupplement(context, supplement),
onArchive: () => _archiveSupplement(context, supplement), onArchive: () => _archiveSupplement(context, supplement),
onDuplicate: () => context.read<SupplementProvider>().duplicateSupplement(supplement.id!), onDuplicate: () => context.read<SupplementProvider>().duplicateSupplement(supplement.id!),
showCompletionStatus: false,
), ),
), ),
], ],
], ],
); );
} }
@@ -349,10 +201,10 @@ class SupplementsListScreen extends StatelessWidget {
margin: const EdgeInsets.only(bottom: 12), margin: const EdgeInsets.only(bottom: 12),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration( decoration: BoxDecoration(
color: color.withOpacity(0.1), color: color.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(12), borderRadius: BorderRadius.circular(12),
border: Border.all( border: Border.all(
color: color.withOpacity(0.3), color: color.withValues(alpha: 0.3),
width: 1, width: 1,
), ),
), ),
@@ -361,7 +213,7 @@ class SupplementsListScreen extends StatelessWidget {
Container( Container(
padding: const EdgeInsets.all(8), padding: const EdgeInsets.all(8),
decoration: BoxDecoration( decoration: BoxDecoration(
color: color.withOpacity(0.2), color: color.withValues(alpha: 0.2),
shape: BoxShape.circle, shape: BoxShape.circle,
), ),
child: Icon( child: Icon(
@@ -389,7 +241,7 @@ class SupplementsListScreen extends StatelessWidget {
style: TextStyle( style: TextStyle(
fontSize: 12, fontSize: 12,
fontWeight: FontWeight.w500, fontWeight: FontWeight.w500,
color: color.withOpacity(0.8), color: color.withValues(alpha: 0.8),
), ),
), ),
], ],
@@ -399,7 +251,7 @@ class SupplementsListScreen extends StatelessWidget {
Container( Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4), padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
decoration: BoxDecoration( decoration: BoxDecoration(
color: color.withOpacity(0.15), color: color.withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(12), borderRadius: BorderRadius.circular(12),
), ),
child: Text( child: Text(
@@ -433,509 +285,6 @@ class SupplementsListScreen extends StatelessWidget {
return grouped; return grouped;
} }
void _showRdaSourcesSheet(BuildContext context) {
showModalBottomSheet(
context: context,
showDragHandle: true,
builder: (context) {
return Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Dietary reference sources',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
Text(
'Source: Health Canada Dietary Reference Intakes. Values are matched by your age and sex. Some ULs (e.g., magnesium) apply to supplemental intake only.',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 12),
ListTile(
dense: true,
contentPadding: EdgeInsets.zero,
leading: const Icon(Icons.open_in_new, size: 18),
title: const Text(
'Vitamins reference values',
style: TextStyle(fontSize: 13),
),
subtitle: const Text(
'canada.ca • reference-values-vitamins',
style: TextStyle(fontSize: 11),
),
onTap: () => _launchUrl('https://www.canada.ca/en/health-canada/services/food-nutrition/healthy-eating/dietary-reference-intakes/tables/reference-values-vitamins.html'),
),
ListTile(
dense: true,
contentPadding: EdgeInsets.zero,
leading: const Icon(Icons.open_in_new, size: 18),
title: const Text(
'Elements (minerals) reference values',
style: TextStyle(fontSize: 13),
),
subtitle: const Text(
'canada.ca • reference-values-elements',
style: TextStyle(fontSize: 11),
),
onTap: () => _launchUrl('https://www.canada.ca/en/health-canada/services/food-nutrition/healthy-eating/dietary-reference-intakes/tables/reference-values-elements.html'),
),
const SizedBox(height: 8),
Text(
'Disclaimer: Informational only, some of the details in this app are parsed using AI, and may not be accurate. Always consult a healthcare professional for personalized advice.',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
],
),
);
},
);
}
Future<void> _launchUrl(String url) async {
final uri = Uri.parse(url);
await launchUrl(uri, mode: LaunchMode.externalApplication);
}
void _showRdaDetailsSheet(BuildContext context, String nutrientPretty, Map<String, dynamic> data) {
showModalBottomSheet(
context: context,
showDragHandle: true,
isScrollControlled: false,
builder: (context) {
final unitLabel = (data['unitLabel'] as String?) ?? '';
final rdaValue = data['rdaValue'] as double?;
final ulValue = data['ulValue'] as double?;
final total = data['total'] as double?;
final pctRda = data['pctRda'] as double?;
final pctUl = data['pctUl'] as double?;
final rdaType = data['rdaType'] as String? ?? '';
final lifeStage = data['lifeStage'] as String? ?? '';
final note = data['note'] as String?;
final lifeStageDesc = data['lifeStageDescription'] as String?;
final rdaValueMin = data['rdaValueMin'] as double?;
final rdaValueMax = data['rdaValueMax'] as double?;
final nutrientUl = (data['nutrientUl'] as Map?)?.cast<String, dynamic>();
return Padding(
padding: const EdgeInsets.all(16),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
// Header row: title and sources button
Row(
children: [
Expanded(
child: Text(
nutrientPretty,
style: Theme.of(context).textTheme.titleLarge,
overflow: TextOverflow.ellipsis,
),
),
IconButton(
tooltip: 'Sources & disclaimer',
icon: const Icon(Icons.info_outline),
onPressed: () => _showRdaSourcesSheet(context),
),
],
),
if (lifeStage.isNotEmpty || (lifeStageDesc != null && lifeStageDesc.isNotEmpty)) ...[
const SizedBox(height: 4),
Wrap(
spacing: 8,
runSpacing: 4,
children: [
if (lifeStage.isNotEmpty)
InfoChip(
icon: Icons.person_outline,
label: 'Life stage: $lifeStage',
context: context,
),
if (lifeStageDesc != null && lifeStageDesc.isNotEmpty)
InfoChip(
icon: Icons.info_outline,
label: lifeStageDesc!,
context: context,
),
],
),
],
const SizedBox(height: 8),
// Intake vs RDA chips
Wrap(
spacing: 8,
runSpacing: 8,
children: [
if (total != null)
InfoChip(
icon: Icons.local_drink,
label: 'Intake: ${total.toStringAsFixed(total % 1 == 0 ? 0 : 1)} $unitLabel',
context: context,
),
if (rdaValueMin != null && rdaValueMax != null)
InfoChip(
icon: Icons.rule,
label: 'RDA: ${rdaValueMin!.toStringAsFixed(rdaValueMin! % 1 == 0 ? 0 : 1)}${rdaValueMax!.toStringAsFixed(rdaValueMax! % 1 == 0 ? 0 : 1)} $unitLabel',
context: context,
)
else if (rdaValue != null)
InfoChip(
icon: Icons.rule,
label: 'RDA: ${rdaValue!.toStringAsFixed(rdaValue! % 1 == 0 ? 0 : 1)} $unitLabel',
context: context,
),
if (pctRda != null)
InfoChip(
icon: Icons.percent,
label: '%RDA: ${pctRda.toStringAsFixed(pctRda >= 10 ? 0 : 1)}%',
context: context,
),
if (ulValue != null)
InfoChip(
icon: Icons.shield_outlined,
label: 'UL: ${ulValue.toStringAsFixed(ulValue % 1 == 0 ? 0 : 1)} $unitLabel',
context: context,
),
if (pctUl != null)
InfoChip(
icon: Icons.warning_amber,
label: '%UL: ${pctUl.toStringAsFixed(pctUl >= 10 ? 0 : 1)}%',
context: context,
),
],
),
if (rdaType.isNotEmpty || (note != null && note!.isNotEmpty) || nutrientUl != null) ...[
const SizedBox(height: 12),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (rdaType.isNotEmpty)
Text(
'Basis: $rdaType',
style: Theme.of(context).textTheme.bodySmall,
),
if (nutrientUl != null) ...[
const SizedBox(height: 6),
Text(
'Upper limit guidance',
style: Theme.of(context).textTheme.titleSmall,
),
const SizedBox(height: 4),
Wrap(
spacing: 8,
runSpacing: 4,
children: [
InfoChip(
icon: Icons.shield_moon_outlined,
label: 'UL: ${nutrientUl['value']} ${nutrientUl['unit']}',
context: context,
),
if ((nutrientUl['duration'] as String?)?.isNotEmpty ?? false)
InfoChip(
icon: Icons.schedule,
label: 'Duration: ${nutrientUl['duration']}',
context: context,
),
],
),
if ((nutrientUl['note'] as String?)?.isNotEmpty ?? false) ...[
const SizedBox(height: 4),
Text(
nutrientUl['note'],
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
],
],
if (note != null && note!.isNotEmpty) ...[
const SizedBox(height: 8),
Text(
note!,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
],
],
),
],
],
),
),
);
},
);
}
void _showTakeDialog(BuildContext context, Supplement supplement) {
final unitsController = TextEditingController(text: supplement.numberOfUnits.toString());
final notesController = TextEditingController();
DateTime selectedDateTime = DateTime.now();
bool useCustomTime = false;
showDialog(
context: context,
builder: (context) => StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text('Take ${supplement.name}'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Expanded(
child: TextField(
controller: unitsController,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
decoration: InputDecoration(
labelText: 'Number of ${supplement.unitType}',
border: const OutlineInputBorder(),
suffixText: supplement.unitType,
),
onChanged: (value) => setState(() {}),
),
),
],
),
const SizedBox(height: 8),
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceVariant,
borderRadius: BorderRadius.circular(4),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Total dosage:',
style: TextStyle(
fontSize: 12,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
Text(
supplement.ingredientsDisplay,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: Theme.of(context).colorScheme.onSurface,
),
),
],
),
),
const SizedBox(height: 16),
// Time selection section
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primaryContainer.withOpacity(0.3),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Theme.of(context).colorScheme.primary.withOpacity(0.3),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(
Icons.access_time,
size: 16,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(width: 6),
Text(
'When did you take it?',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: Theme.of(context).colorScheme.primary,
),
),
],
),
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: RadioListTile<bool>(
dense: true,
contentPadding: EdgeInsets.zero,
title: const Text('Just now', style: TextStyle(fontSize: 12)),
value: false,
groupValue: useCustomTime,
onChanged: (value) => setState(() => useCustomTime = value!),
),
),
Expanded(
child: RadioListTile<bool>(
dense: true,
contentPadding: EdgeInsets.zero,
title: const Text('Custom time', style: TextStyle(fontSize: 12)),
value: true,
groupValue: useCustomTime,
onChanged: (value) => setState(() => useCustomTime = value!),
),
),
],
),
if (useCustomTime) ...[
const SizedBox(height: 8),
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
borderRadius: BorderRadius.circular(6),
border: Border.all(
color: Theme.of(context).colorScheme.outline.withOpacity(0.5),
),
),
child: Column(
children: [
// Date picker
Row(
children: [
Icon(
Icons.calendar_today,
size: 14,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(width: 8),
Expanded(
child: Text(
'Date: ${selectedDateTime.day}/${selectedDateTime.month}/${selectedDateTime.year}',
style: const TextStyle(fontSize: 12),
),
),
TextButton(
onPressed: () async {
final date = await showDatePicker(
context: context,
initialDate: selectedDateTime,
firstDate: DateTime.now().subtract(const Duration(days: 7)),
lastDate: DateTime.now(),
);
if (date != null) {
setState(() {
selectedDateTime = DateTime(
date.year,
date.month,
date.day,
selectedDateTime.hour,
selectedDateTime.minute,
);
});
}
},
child: const Text('Change', style: TextStyle(fontSize: 10)),
),
],
),
const SizedBox(height: 4),
// Time picker
Row(
children: [
Icon(
Icons.access_time,
size: 14,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(width: 8),
Expanded(
child: Text(
'Time: ${selectedDateTime.hour.toString().padLeft(2, '0')}:${selectedDateTime.minute.toString().padLeft(2, '0')}',
style: const TextStyle(fontSize: 12),
),
),
TextButton(
onPressed: () async {
final time = await showTimePicker(
context: context,
initialTime: TimeOfDay.fromDateTime(selectedDateTime),
);
if (time != null) {
setState(() {
selectedDateTime = DateTime(
selectedDateTime.year,
selectedDateTime.month,
selectedDateTime.day,
time.hour,
time.minute,
);
});
}
},
child: const Text('Change', style: TextStyle(fontSize: 10)),
),
],
),
],
),
),
],
],
),
),
const SizedBox(height: 16),
TextField(
controller: notesController,
decoration: const InputDecoration(
labelText: 'Notes (optional)',
border: OutlineInputBorder(),
),
maxLines: 2,
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
ElevatedButton(
onPressed: () {
final unitsTaken = double.tryParse(unitsController.text) ?? supplement.numberOfUnits.toDouble();
// For now, we'll record 0 as total dosage since we're transitioning to ingredients
// This will be properly implemented when we add the full ingredient tracking
final totalDosageTaken = 0.0;
context.read<SupplementProvider>().recordIntake(
supplement.id!,
totalDosageTaken,
unitsTaken: unitsTaken,
notes: notesController.text.isNotEmpty ? notesController.text : null,
takenAt: useCustomTime ? selectedDateTime : null,
);
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('${supplement.name} recorded!'),
backgroundColor: Colors.green,
),
);
},
child: const Text('Record'),
),
],
);
},
),
);
}
void _editSupplement(BuildContext context, Supplement supplement) { void _editSupplement(BuildContext context, Supplement supplement) {
Navigator.of(context).push( Navigator.of(context).push(
MaterialPageRoute( MaterialPageRoute(

View File

@@ -0,0 +1,456 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:popover/popover.dart';
import '../models/supplement.dart';
import '../providers/settings_provider.dart';
import '../providers/simple_sync_provider.dart';
import '../providers/supplement_provider.dart';
import '../services/database_sync_service.dart';
import '../widgets/dialogs/take_supplement_dialog.dart';
class TodayScheduleScreen extends StatelessWidget {
const TodayScheduleScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("My Supplements"),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
actions: [
Consumer<SimpleSyncProvider>(
builder: (context, syncProvider, child) {
if (!syncProvider.isConfigured) {
return const SizedBox.shrink();
}
return IconButton(
icon: syncProvider.isSyncing
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: syncProvider.status == SyncStatus.completed &&
syncProvider.lastSyncTime != null &&
DateTime.now().difference(syncProvider.lastSyncTime!).inSeconds < 5
? const Icon(Icons.check, color: Colors.green)
: const Icon(Icons.sync),
onPressed: syncProvider.isSyncing ? null : () {
syncProvider.syncDatabase();
},
tooltip: syncProvider.isSyncing ? 'Syncing...' : 'Force Sync',
);
},
),
],
),
body: Consumer2<SupplementProvider, SettingsProvider>(
builder: (context, provider, settingsProvider, child) {
if (provider.isLoading) {
return const Center(child: CircularProgressIndicator());
}
if (provider.todayIntakes.isEmpty && provider.supplements.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.schedule,
size: 64,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
const SizedBox(height: 16),
Text(
'No schedule for today',
style: TextStyle(
fontSize: 18,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 8),
Text(
'Add supplements with reminder times to see your schedule',
style: TextStyle(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
],
),
);
}
return RefreshIndicator(
onRefresh: () async {
await provider.loadSupplements();
await provider.refreshDailyStatus();
},
child: _buildTimelineView(context, provider, settingsProvider),
);
},
),
);
}
Widget _buildTimelineView(BuildContext context, SupplementProvider provider, SettingsProvider settingsProvider) {
final scheduledItems = _getScheduledItems(provider.supplements, provider.todayIntakes);
final groupedItems = _groupScheduledItemsByTimeOfDay(scheduledItems);
return ListView(
padding: const EdgeInsets.all(16),
children: [
if (groupedItems['morning']!.isNotEmpty) ...[
_buildTimeSectionHeader('Morning', Icons.wb_sunny, Colors.orange),
...groupedItems['morning']!.map((item) => _buildScheduledItem(context, item)),
const SizedBox(height: 12),
],
if (groupedItems['afternoon']!.isNotEmpty) ...[
_buildTimeSectionHeader('Afternoon', Icons.light_mode, Colors.blue),
...groupedItems['afternoon']!.map((item) => _buildScheduledItem(context, item)),
const SizedBox(height: 12),
],
if (groupedItems['evening']!.isNotEmpty) ...[
_buildTimeSectionHeader('Evening', Icons.nightlight_round, Colors.indigo),
...groupedItems['evening']!.map((item) => _buildScheduledItem(context, item)),
const SizedBox(height: 12),
],
if (groupedItems['night']!.isNotEmpty) ...[
_buildTimeSectionHeader('Night', Icons.bedtime, Colors.purple),
...groupedItems['night']!.map((item) => _buildScheduledItem(context, item)),
],
],
);
}
Widget _buildTimeSectionHeader(String title, IconData icon, Color color) {
return Container(
margin: const EdgeInsets.only(bottom: 8, top: 8),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
decoration: BoxDecoration(
color: color.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
Icon(icon, size: 16, color: color),
const SizedBox(width: 8),
Text(
title,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: color,
),
),
],
),
);
}
Widget _buildScheduledItem(BuildContext context, Map<String, dynamic> item) {
final time = item['time'] as String;
final supplement = item['supplement'] as Supplement;
final status = item['status'] as String;
final actualTime = item['actualTime'] as String?;
final isCompleted = status == 'on_time' || status == 'off_time';
final isOnTime = status == 'on_time';
return Builder(
builder: (context) => InkWell(
onTap: () {
showPopover(
context: context,
bodyBuilder: (context) => Container(
constraints: const BoxConstraints(maxWidth: 150),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.3),
width: 1,
),
boxShadow: [
BoxShadow(
color: Theme.of(context).colorScheme.shadow.withValues(alpha: 0.1),
blurRadius: 8,
offset: const Offset(0, 2),
),
],
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (isCompleted)
_buildPopoverItem(
context: context,
icon: Icons.undo,
label: 'Undo Last Taken',
onTap: () async {
Navigator.of(context).pop();
final provider = context.read<SupplementProvider>();
final mostRecentIntake = provider.getMostRecentIntake(supplement.id!);
if (mostRecentIntake != null) {
await provider.deleteIntake(mostRecentIntake['id']);
// Refresh the schedule after undoing
if (context.mounted) {
provider.refreshDailyStatus();
}
}
},
color: Colors.orange,
),
_buildPopoverItem(
context: context,
icon: isCompleted ? Icons.add_circle_outline : Icons.medication,
label: isCompleted ? 'Take Again' : 'Take',
onTap: () async {
Navigator.of(context).pop();
await showTakeSupplementDialog(context, supplement);
// Refresh the schedule after taking
if (context.mounted) {
context.read<SupplementProvider>().refreshDailyStatus();
}
},
),
],
),
),
direction: PopoverDirection.top,
width: 180,
height: null,
arrowHeight: 0,
arrowWidth: 0,
backgroundColor: Colors.transparent,
shadow: const [],
);
},
borderRadius: BorderRadius.circular(8),
child: AnimatedContainer(
duration: const Duration(milliseconds: 150),
margin: const EdgeInsets.only(bottom: 6),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
decoration: BoxDecoration(
color: isCompleted
? Theme.of(context).colorScheme.surface
: Theme.of(context).colorScheme.primary.withValues(alpha: 0.05),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: isCompleted
? Theme.of(context).colorScheme.outline.withValues(alpha: 0.3)
: Theme.of(context).colorScheme.primary.withValues(alpha: 0.3),
),
),
child: Row(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
time,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500,
color: isCompleted
? Theme.of(context).colorScheme.onSurfaceVariant
: Theme.of(context).colorScheme.primary,
),
),
if (actualTime != null && actualTime != time)
Text(
'Taken: $actualTime',
style: TextStyle(
fontSize: 10,
color: Theme.of(context).colorScheme.onSurfaceVariant.withValues(alpha: 0.6),
),
),
],
),
const SizedBox(width: 12),
Expanded(
child: Text(
supplement.name,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
color: isCompleted
? Theme.of(context).colorScheme.onSurface.withValues(alpha: 0.7)
: Theme.of(context).colorScheme.onSurface,
decoration: isCompleted ? TextDecoration.lineThrough : null,
),
),
),
if (isCompleted)
Row(
children: [
if (!isOnTime)
Icon(
Icons.schedule,
size: 14,
color: Colors.orange,
),
const SizedBox(width: 4),
Icon(
Icons.check_circle,
size: 16,
color: isOnTime ? Colors.green : Colors.orange,
),
],
)
else
Container(
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary,
borderRadius: BorderRadius.circular(12),
),
child: Text(
'Pending',
style: TextStyle(
fontSize: 10,
color: Theme.of(context).colorScheme.onPrimary,
fontWeight: FontWeight.w500,
),
),
),
],
),
),
),
);
}
Widget _buildPopoverItem({
required BuildContext context,
required IconData icon,
required String label,
required VoidCallback onTap,
Color? color,
}) {
return InkWell(
onTap: onTap,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
child: Row(
children: [
Icon(
icon,
size: 18,
color: color ?? Theme.of(context).colorScheme.onSurface,
),
const SizedBox(width: 12),
Text(
label,
style: TextStyle(
fontSize: 14,
color: color ?? Theme.of(context).colorScheme.onSurface,
fontWeight: FontWeight.w500,
),
),
],
),
),
);
}
List<Map<String, dynamic>> _getScheduledItems(List<Supplement> supplements, List<Map<String, dynamic>> todayIntakes) {
final List<Map<String, dynamic>> scheduledItems = [];
for (final supplement in supplements) {
for (final reminderTime in supplement.reminderTimes) {
// Parse reminder time (format: "HH:MM")
final parts = reminderTime.split(':');
final hour = int.parse(parts[0]);
final minute = int.parse(parts[1]);
// Check if this reminder has been taken today
String status = 'pending';
String? actualTime;
for (final intake in todayIntakes) {
if (intake['supplement_id'] == supplement.id) {
final takenAt = DateTime.parse(intake['takenAt']);
final reminderDateTime = DateTime(
takenAt.year,
takenAt.month,
takenAt.day,
hour,
minute,
);
// Check if taken within 1 hour of reminder time
final timeDiff = (takenAt.difference(reminderDateTime)).inMinutes.abs();
if (timeDiff <= 60) {
status = 'on_time';
actualTime = '${takenAt.hour.toString().padLeft(2, '0')}:${takenAt.minute.toString().padLeft(2, '0')}';
} else {
status = 'off_time';
actualTime = '${takenAt.hour.toString().padLeft(2, '0')}:${takenAt.minute.toString().padLeft(2, '0')}';
}
break;
}
}
scheduledItems.add({
'time': reminderTime,
'supplement': supplement,
'status': status,
'actualTime': actualTime,
});
}
}
// Sort by time
scheduledItems.sort((a, b) {
final timeA = a['time'] as String;
final timeB = b['time'] as String;
return timeA.compareTo(timeB);
});
return scheduledItems;
}
Map<String, List<Map<String, dynamic>>> _groupScheduledItemsByTimeOfDay(List<Map<String, dynamic>> scheduledItems) {
final Map<String, List<Map<String, dynamic>>> grouped = {
'morning': <Map<String, dynamic>>[],
'afternoon': <Map<String, dynamic>>[],
'evening': <Map<String, dynamic>>[],
'night': <Map<String, dynamic>>[],
};
for (final item in scheduledItems) {
final time = item['time'] as String;
final parts = time.split(':');
final hour = int.parse(parts[0]);
String category;
if (hour >= 6 && hour < 12) {
category = 'morning';
} else if (hour >= 12 && hour < 18) {
category = 'afternoon';
} else if (hour >= 18 && hour < 22) {
category = 'evening';
} else {
category = 'night';
}
grouped[category]!.add(item);
}
// Sort items by time within each category
for (final category in grouped.keys) {
grouped[category]!.sort((a, b) {
final timeA = a['time'] as String;
final timeB = b['time'] as String;
return timeA.compareTo(timeB);
});
}
return grouped;
}
}

View File

@@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'dart:math'; import 'dart:math';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:supplements/logging.dart';
import '../providers/settings_provider.dart'; import '../providers/settings_provider.dart';
import '../providers/simple_sync_provider.dart'; import '../providers/simple_sync_provider.dart';
@@ -69,7 +70,7 @@ class AutoSyncService {
// Check if auto-sync is enabled // Check if auto-sync is enabled
if (!_settingsProvider.autoSyncEnabled) { if (!_settingsProvider.autoSyncEnabled) {
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Auto-sync is disabled, skipping trigger'); printLog('AutoSyncService: Auto-sync is disabled, skipping trigger');
} }
return; return;
} }
@@ -77,7 +78,7 @@ class AutoSyncService {
// Check if auto-sync was disabled due to persistent errors // Check if auto-sync was disabled due to persistent errors
if (_autoDisabledDueToErrors) { if (_autoDisabledDueToErrors) {
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Auto-sync disabled due to persistent errors, skipping trigger'); printLog('AutoSyncService: Auto-sync disabled due to persistent errors, skipping trigger');
} }
return; return;
} }
@@ -90,7 +91,7 @@ class AutoSyncService {
timestamp: DateTime.now(), timestamp: DateTime.now(),
)); ));
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Sync not configured, skipping auto-sync'); printLog('AutoSyncService: Sync not configured, skipping auto-sync');
} }
return; return;
} }
@@ -99,7 +100,7 @@ class AutoSyncService {
if (_syncInProgress || _syncProvider.isSyncing) { if (_syncInProgress || _syncProvider.isSyncing) {
_hasPendingSync = true; _hasPendingSync = true;
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Sync in progress, marking pending sync'); printLog('AutoSyncService: Sync in progress, marking pending sync');
} }
return; return;
} }
@@ -111,7 +112,7 @@ class AutoSyncService {
final backoffDelay = _calculateBackoffDelay(); final backoffDelay = _calculateBackoffDelay();
if (backoffDelay > 0) { if (backoffDelay > 0) {
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Applying backoff delay of ${backoffDelay}s due to recent failures'); printLog('AutoSyncService: Applying backoff delay of ${backoffDelay}s due to recent failures');
} }
_debounceTimer = Timer(Duration(seconds: backoffDelay), () { _debounceTimer = Timer(Duration(seconds: backoffDelay), () {
_executePendingSync(); _executePendingSync();
@@ -126,7 +127,7 @@ class AutoSyncService {
}); });
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Auto-sync scheduled in ${debounceSeconds}s'); printLog('AutoSyncService: Auto-sync scheduled in ${debounceSeconds}s');
} }
} }
@@ -135,14 +136,14 @@ class AutoSyncService {
// Double-check conditions before executing // Double-check conditions before executing
if (!_settingsProvider.autoSyncEnabled) { if (!_settingsProvider.autoSyncEnabled) {
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Auto-sync disabled during execution, aborting'); printLog('AutoSyncService: Auto-sync disabled during execution, aborting');
} }
return; return;
} }
if (_autoDisabledDueToErrors) { if (_autoDisabledDueToErrors) {
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Auto-sync disabled due to errors during execution, aborting'); printLog('AutoSyncService: Auto-sync disabled due to errors during execution, aborting');
} }
return; return;
} }
@@ -154,14 +155,14 @@ class AutoSyncService {
timestamp: DateTime.now(), timestamp: DateTime.now(),
)); ));
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Sync not configured during execution, aborting'); printLog('AutoSyncService: Sync not configured during execution, aborting');
} }
return; return;
} }
if (_syncInProgress || _syncProvider.isSyncing) { if (_syncInProgress || _syncProvider.isSyncing) {
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Sync already in progress during execution, aborting'); printLog('AutoSyncService: Sync already in progress during execution, aborting');
} }
return; return;
} }
@@ -171,7 +172,7 @@ class AutoSyncService {
try { try {
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Executing auto-sync (attempt ${_consecutiveFailures + 1})'); printLog('AutoSyncService: Executing auto-sync (attempt ${_consecutiveFailures + 1})');
} }
// Check network connectivity before attempting sync // Check network connectivity before attempting sync
@@ -191,12 +192,12 @@ class AutoSyncService {
_autoDisabledDueToErrors = false; _autoDisabledDueToErrors = false;
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Auto-sync completed successfully'); printLog('AutoSyncService: Auto-sync completed successfully');
} }
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Auto-sync failed: $e'); printLog('AutoSyncService: Auto-sync failed: $e');
} }
// Handle specific error types // Handle specific error types
@@ -208,7 +209,7 @@ class AutoSyncService {
// If there was a pending sync request while we were syncing, trigger it // If there was a pending sync request while we were syncing, trigger it
if (_hasPendingSync && !_autoDisabledDueToErrors) { if (_hasPendingSync && !_autoDisabledDueToErrors) {
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Processing queued sync request'); printLog('AutoSyncService: Processing queued sync request');
} }
_hasPendingSync = false; _hasPendingSync = false;
// Use a small delay to avoid immediate re-triggering // Use a small delay to avoid immediate re-triggering
@@ -231,14 +232,14 @@ class AutoSyncService {
if (_consecutiveFailures >= _autoDisableThreshold) { if (_consecutiveFailures >= _autoDisableThreshold) {
_autoDisabledDueToErrors = true; _autoDisabledDueToErrors = true;
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Auto-sync disabled due to ${_consecutiveFailures} consecutive failures'); printLog('AutoSyncService: Auto-sync disabled due to $_consecutiveFailures consecutive failures');
} }
// For configuration errors, disable immediately // For configuration errors, disable immediately
if (autoSyncError.type == AutoSyncErrorType.configuration || if (autoSyncError.type == AutoSyncErrorType.configuration ||
autoSyncError.type == AutoSyncErrorType.authentication) { autoSyncError.type == AutoSyncErrorType.authentication) {
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Auto-sync disabled due to configuration/authentication error'); printLog('AutoSyncService: Auto-sync disabled due to configuration/authentication error');
} }
} }
} }
@@ -328,7 +329,7 @@ class AutoSyncService {
} }
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Recorded error: $error'); printLog('AutoSyncService: Recorded error: $error');
} }
} }
@@ -375,13 +376,13 @@ class AutoSyncService {
_retryTimer?.cancel(); _retryTimer?.cancel();
_retryTimer = Timer(Duration(seconds: retryDelay), () { _retryTimer = Timer(Duration(seconds: retryDelay), () {
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Retrying auto-sync after backoff delay'); printLog('AutoSyncService: Retrying auto-sync after backoff delay');
} }
triggerAutoSync(); triggerAutoSync();
}); });
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Scheduled retry in ${retryDelay}s'); printLog('AutoSyncService: Scheduled retry in ${retryDelay}s');
} }
} }
@@ -392,7 +393,7 @@ class AutoSyncService {
return result.isNotEmpty && result[0].rawAddress.isNotEmpty; return result.isNotEmpty && result[0].rawAddress.isNotEmpty;
} catch (e) { } catch (e) {
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Network check failed: $e'); printLog('AutoSyncService: Network check failed: $e');
} }
return false; return false;
} }
@@ -406,7 +407,7 @@ class AutoSyncService {
_hasPendingSync = false; _hasPendingSync = false;
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Cancelled pending sync and retry timer'); printLog('AutoSyncService: Cancelled pending sync and retry timer');
} }
} }
@@ -426,7 +427,7 @@ class AutoSyncService {
_retryTimer = null; _retryTimer = null;
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Error state reset, auto-sync re-enabled'); printLog('AutoSyncService: Error state reset, auto-sync re-enabled');
} }
} }
@@ -440,7 +441,7 @@ class AutoSyncService {
_recentErrors.clear(); _recentErrors.clear();
if (kDebugMode) { if (kDebugMode) {
print('AutoSyncService: Disposed'); printLog('AutoSyncService: Disposed');
} }
} }

View File

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

View File

@@ -0,0 +1,92 @@
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
class NotificationLogEntry {
final int id;
final String kind; // 'daily' | 'snooze'
final String type; // 'single' | 'group'
final int whenEpochMs; // exact scheduled time (epoch ms)
final int createdAtEpochMs; // when we created the schedule (epoch ms)
final String title;
final String payload;
final int? singleId; // supplement id for single
final String? timeKey; // HH:mm for group
const NotificationLogEntry({
required this.id,
required this.kind,
required this.type,
required this.whenEpochMs,
required this.createdAtEpochMs,
required this.title,
required this.payload,
this.singleId,
this.timeKey,
});
Map<String, dynamic> toJson() => {
'id': id,
'kind': kind,
'type': type,
'when': whenEpochMs,
'createdAt': createdAtEpochMs,
'title': title,
'payload': payload,
'singleId': singleId,
'timeKey': timeKey,
};
static NotificationLogEntry fromJson(Map<String, dynamic> map) {
return NotificationLogEntry(
id: map['id'] is int ? map['id'] as int : int.tryParse('${map['id']}') ?? 0,
kind: map['kind'] ?? 'unknown',
type: map['type'] ?? 'unknown',
whenEpochMs: map['when'] is int ? map['when'] as int : int.tryParse('${map['when']}') ?? 0,
createdAtEpochMs: map['createdAt'] is int ? map['createdAt'] as int : int.tryParse('${map['createdAt']}') ?? 0,
title: map['title'] ?? '',
payload: map['payload'] ?? '',
singleId: map['singleId'],
timeKey: map['timeKey'],
);
}
}
class NotificationDebugStore {
NotificationDebugStore._internal();
static final NotificationDebugStore instance = NotificationDebugStore._internal();
static const String _prefsKey = 'notification_log';
static const int _maxEntries = 200;
Future<List<NotificationLogEntry>> getAll() async {
final prefs = await SharedPreferences.getInstance();
final raw = prefs.getString(_prefsKey);
if (raw == null || raw.isEmpty) return [];
try {
final list = jsonDecode(raw) as List;
return list
.map((e) => NotificationLogEntry.fromJson(e as Map<String, dynamic>))
.toList();
} catch (_) {
return [];
}
}
Future<void> add(NotificationLogEntry entry) async {
final prefs = await SharedPreferences.getInstance();
final current = await getAll();
current.add(entry);
// Cap size
final trimmed = current.length > _maxEntries
? current.sublist(current.length - _maxEntries)
: current;
final serialized = jsonEncode(trimmed.map((e) => e.toJson()).toList());
await prefs.setString(_prefsKey, serialized);
}
Future<void> clear() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(_prefsKey);
}
}

View File

@@ -0,0 +1,298 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:provider/provider.dart';
import 'package:supplements/logging.dart';
import '../models/supplement.dart';
import '../providers/supplement_provider.dart';
import '../widgets/dialogs/bulk_take_dialog.dart';
import '../widgets/dialogs/take_supplement_dialog.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:supplements/services/simple_notification_service.dart';
/// Centralizes routing from notification actions/taps to in-app UI.
/// Handles both foreground/background taps and terminated-launch scenarios.
class NotificationRouter {
NotificationRouter._internal();
static final NotificationRouter instance = NotificationRouter._internal();
GlobalKey<NavigatorState>? _navigatorKey;
void initialize(GlobalKey<NavigatorState> navigatorKey) {
_navigatorKey = navigatorKey;
}
Future<void> handleNotificationResponse(NotificationResponse response) async {
final payloadMap = _decodePayload(response.payload);
final actionId = response.actionId;
printLog('🔔 handleNotificationResponse: actionId=$actionId payload=${response.payload} map=$payloadMap');
printLog('🔔 handleNotificationResponse: Received actionId: $actionId');
printLog('🔔 handleNotificationResponse: Decoded payloadMap: $payloadMap');
// Handle Snooze actions without surfacing UI
if (actionId == 'snooze_single' || actionId == 'snooze_group') {
try {
final prefs = await SharedPreferences.getInstance();
final minutes = prefs.getInt('snooze_minutes') ?? 10;
await _scheduleSnoozeFromPayload(payloadMap, Duration(minutes: minutes));
} catch (e) {
printLog('⚠️ Failed to handle snooze action: $e');
}
return;
}
// Default: route to in-app UI for Take actions and normal taps
await _routeFromPayload(payloadMap);
}
Future<void> handleAppLaunchDetails(NotificationAppLaunchDetails? details) async {
if (details == null) return;
if (!details.didNotificationLaunchApp) return;
final resp = details.notificationResponse;
final payloadMap = _decodePayload(resp?.payload);
printLog('🚀 App launched from notification: payload=${resp?.payload} map=$payloadMap');
await _routeFromPayload(payloadMap);
}
Map<String, dynamic>? _decodePayload(String? payload) {
if (payload == null || payload.isEmpty) return null;
// Try JSON first
try {
final map = jsonDecode(payload);
if (map is Map<String, dynamic>) return map;
} catch (_) {
// Ignore and try fallback
}
// Fallback: previous implementation used HH:mm as raw payload
final hhmm = RegExp(r'^\d{2}:\d{2}$');
if (hhmm.hasMatch(payload)) {
return { 'type': 'group', 'time': payload };
}
return null;
}
Future<void> _routeFromPayload(Map<String, dynamic>? payload) async {
if (_navigatorKey == null) {
printLog('⚠️ NotificationRouter not initialized with navigatorKey');
return;
}
// Wait until navigator is ready and providers have loaded
final ready = await _waitUntilReady(timeout: const Duration(seconds: 5));
if (!ready) {
printLog('⚠️ Timeout waiting for app to be ready for routing');
return;
}
final context = _navigatorKey!.currentContext!;
if (!context.mounted) return;
final provider = context.read<SupplementProvider>();
if (payload == null) {
printLog('⚠️ No payload to route');
return;
}
final type = payload['type'];
if (type == 'single') {
final id = payload['id'];
if (id is int) {
Supplement? s;
try {
s = provider.supplements.firstWhere((el) => el.id == id);
} catch (_) {
s = null;
}
if (s == null) {
// Attempt reload once
await provider.loadSupplements();
if (!context.mounted) return;
try {
s = provider.supplements.firstWhere((el) => el.id == id);
} catch (_) {
s = null;
}
}
if (s != null) {
// For single: use the regular dialog (with time selection)
// Ensure we close any existing dialog first
_popAnyDialog(context);
await showTakeSupplementDialog(context, s, hideTime: false);
if (!context.mounted) return;
} else {
printLog('⚠️ Supplement id=$id not found for single-take routing');
_showSnack(context, 'Supplement not found');
}
}
} else if (type == 'group') {
final timeKey = payload['time'];
if (timeKey is String) {
// Build list of supplements scheduled at this timeKey
final List<Supplement> list = provider.supplements.where((s) {
return s.isActive && s.reminderTimes.contains(timeKey);
}).toList();
if (list.isEmpty) {
printLog('⚠️ No supplements found for group time=$timeKey');
_showSnack(context, 'No supplements for $timeKey');
return;
}
_popAnyDialog(context);
await showBulkTakeDialog(context, list);
if (!context.mounted) return;
}
} else {
printLog('⚠️ Unknown payload type: $type');
}
}
Future<void> _scheduleSnoozeFromPayload(Map<String, dynamic>? payload, Duration delay) async {
if (payload == null) {
printLog('⚠️ Snooze requested but payload was null');
return;
}
// Try to wait for providers to be ready to build rich content.
BuildContext? ctx = _navigatorKey?.currentContext;
final ready = await _waitUntilReady(timeout: const Duration(seconds: 5));
if (ctx != null && !ctx.mounted) ctx = null;
SupplementProvider? provider;
if (ready && ctx != null) {
provider = Provider.of<SupplementProvider>(ctx, listen: false);
}
String title = 'Supplement reminder';
String body = 'Tap to see details';
bool isSingle = false;
// Start with a mutable copy of the payload to add meta information
final Map<String, dynamic> mutablePayload = Map.from(payload);
final type = mutablePayload['type'];
if (type == 'single') {
final id = payload['id'];
isSingle = true;
// Ensure the payload for single snooze is correctly formatted
mutablePayload['type'] = 'single';
mutablePayload['id'] = id;
if (id is int && provider != null) {
Supplement? s;
try {
s = provider.supplements.firstWhere((el) => el.id == id);
} catch (_) {
s = null;
}
if (s != null) {
title = 'Time for ${s.name}';
body =
'${s.name}${s.numberOfUnits} ${s.unitType} (${s.ingredientsPerUnit})';
} else {
body = 'Tap to take supplement';
}
}
} else if (type == 'group') {
final timeKey = mutablePayload['time'];
if (timeKey is String) {
if (provider != null) {
final list = provider.supplements
.where((s) =>
s.isActive && s.reminderTimes.contains(timeKey))
.toList();
if (list.length == 1) {
final s = list.first;
isSingle = true;
title = 'Time for ${s.name}';
body =
'${s.name}${s.numberOfUnits} ${s.unitType} (${s.ingredientsPerUnit})';
// If a group becomes a single, update the payload type
mutablePayload['type'] = 'single';
mutablePayload['id'] = s.id;
mutablePayload.remove('time'); // Remove time key for single
} else if (list.isNotEmpty) {
isSingle = false;
title = 'Time for ${list.length} supplements';
final lines = list
.map((s) =>
'${s.name}${s.numberOfUnits} ${s.unitType} (${s.ingredientsPerUnit})')
.toList();
body = lines.join('\n');
// Ensure payload type is group
mutablePayload['type'] = 'group';
mutablePayload['time'] = timeKey;
} else {
// Fallback generic group
isSingle = false;
title = 'Supplement reminder';
body = 'Tap to see details';
mutablePayload['type'] = 'group';
mutablePayload['time'] = timeKey;
}
} else {
// Provider not ready; schedule generic group payload
isSingle = false;
mutablePayload['type'] = 'group';
mutablePayload['time'] = timeKey;
}
}
}
// Ensure the payload always has the correct type and ID/time for logging
// and re-scheduling. The SimpleNotificationService will add the 'meta' field.
final payloadStr = jsonEncode(mutablePayload);
await SimpleNotificationService.instance.scheduleOneOffReminder(
title: title,
body: body,
payload: payloadStr,
isSingle: isSingle,
delay: delay,
);
}
Future<bool> _waitUntilReady({required Duration timeout}) async {
final start = DateTime.now();
while (DateTime.now().difference(start) < timeout) {
final key = _navigatorKey;
final ctx = key?.currentContext;
if (ctx != null) {
if (!ctx.mounted) continue;
try {
final provider = Provider.of<SupplementProvider>(ctx, listen: false);
if (!provider.isLoading) {
return true;
}
} catch (_) {
// Provider not available yet
}
}
await Future.delayed(const Duration(milliseconds: 100));
}
return false;
}
void _popAnyDialog(BuildContext context) {
if (Navigator.of(context, rootNavigator: true).canPop()) {
Navigator.of(context, rootNavigator: true).pop();
}
}
void _showSnack(BuildContext context, String message) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(message)),
);
});
}
}

View File

@@ -1,704 +1,11 @@
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; /*
import 'package:timezone/timezone.dart' as tz; Deprecated/removed: notification_service.dart
import 'package:timezone/data/latest.dart' as tz;
import '../models/supplement.dart';
import 'database_helper.dart';
// Top-level function to handle notification responses when app is running This legacy notification service has been intentionally removed.
@pragma('vm:entry-point') The app now uses a minimal scheduler in:
void notificationTapBackground(NotificationResponse notificationResponse) { services/simple_notification_service.dart
print('SupplementsLog: 📱 === BACKGROUND NOTIFICATION RESPONSE ===');
print('SupplementsLog: 📱 Action ID: ${notificationResponse.actionId}');
print('SupplementsLog: 📱 Payload: ${notificationResponse.payload}');
print('SupplementsLog: 📱 Notification ID: ${notificationResponse.id}');
print('SupplementsLog: 📱 ==========================================');
// For now, just log the action. The main app handler will process it. All retry/snooze/database-tracking logic has been dropped to keep things simple.
if (notificationResponse.actionId == 'take_supplement') { This file is left empty to ensure any lingering references fail at compile time,
print('SupplementsLog: 📱 BACKGROUND: Take action detected'); prompting migration to the new SimpleNotificationService.
} else if (notificationResponse.actionId == 'snooze_10') { */
print('SupplementsLog: 📱 BACKGROUND: Snooze action detected');
}
}
class NotificationService {
static final NotificationService _instance = NotificationService._internal();
factory NotificationService() => _instance;
NotificationService._internal();
final FlutterLocalNotificationsPlugin _notifications = FlutterLocalNotificationsPlugin();
bool _isInitialized = false;
static bool _engineInitialized = false;
bool _permissionsRequested = false;
// Callback for handling supplement intake from notifications
Function(int supplementId, String supplementName, double units, String unitType)? _onTakeSupplementCallback;
// Set callback for handling supplement intake from notifications
void setTakeSupplementCallback(Function(int supplementId, String supplementName, double units, String unitType) callback) {
_onTakeSupplementCallback = callback;
}
Future<void> initialize() async {
print('SupplementsLog: 📱 Initializing NotificationService...');
if (_isInitialized) {
print('SupplementsLog: 📱 Already initialized');
return;
}
try {
print('SupplementsLog: 📱 Initializing timezones...');
print('SupplementsLog: 📱 Engine initialized flag: $_engineInitialized');
if (!_engineInitialized) {
tz.initializeTimeZones();
_engineInitialized = true;
print('SupplementsLog: 📱 Timezones initialized successfully');
} else {
print('SupplementsLog: 📱 Timezones already initialized, skipping');
}
} catch (e) {
print('SupplementsLog: 📱 Warning: Timezone initialization issue (may already be initialized): $e');
_engineInitialized = true; // Mark as initialized to prevent retry
}
// Try to detect and set the local timezone more reliably
try {
// First try using the system timezone name
final String timeZoneName = DateTime.now().timeZoneName;
print('SupplementsLog: 📱 System timezone name: $timeZoneName');
tz.Location? location;
// Try common timezone mappings for your region
if (timeZoneName.contains('CET') || timeZoneName.contains('CEST')) {
location = tz.getLocation('Europe/Amsterdam'); // Netherlands
} else if (timeZoneName.contains('UTC') || timeZoneName.contains('GMT')) {
location = tz.getLocation('UTC');
} else {
// Fallback: try to use the timezone name directly
try {
location = tz.getLocation(timeZoneName);
} catch (e) {
print('SupplementsLog: 📱 Could not find timezone $timeZoneName, using Europe/Amsterdam as default');
location = tz.getLocation('Europe/Amsterdam');
}
}
tz.setLocalLocation(location);
print('SupplementsLog: 📱 Timezone set to: ${location.name}');
} catch (e) {
print('SupplementsLog: 📱 Error setting timezone: $e, using default');
// Fallback to a reasonable default for Netherlands
tz.setLocalLocation(tz.getLocation('Europe/Amsterdam'));
}
print('SupplementsLog: 📱 Current local time: ${tz.TZDateTime.now(tz.local)}');
print('SupplementsLog: 📱 Current system time: ${DateTime.now()}');
const AndroidInitializationSettings androidSettings = AndroidInitializationSettings('@mipmap/ic_launcher');
const DarwinInitializationSettings iosSettings = DarwinInitializationSettings(
requestAlertPermission: false, // We'll request these separately
requestBadgePermission: false,
requestSoundPermission: false,
);
const LinuxInitializationSettings linuxSettings = LinuxInitializationSettings(
defaultActionName: 'Open notification',
);
const InitializationSettings initSettings = InitializationSettings(
android: androidSettings,
iOS: iosSettings,
linux: linuxSettings,
);
print('SupplementsLog: 📱 Initializing flutter_local_notifications...');
await _notifications.initialize(
initSettings,
onDidReceiveNotificationResponse: _onNotificationResponse,
onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
);
// Test if notification response callback is working
print('SupplementsLog: 📱 Callback function is set and ready');
_isInitialized = true;
print('SupplementsLog: 📱 NotificationService initialization complete');
}
// Handle notification responses (when user taps on notification or action)
void _onNotificationResponse(NotificationResponse response) {
print('SupplementsLog: 📱 === NOTIFICATION RESPONSE ===');
print('SupplementsLog: 📱 Action ID: ${response.actionId}');
print('SupplementsLog: 📱 Payload: ${response.payload}');
print('SupplementsLog: 📱 Notification ID: ${response.id}');
print('SupplementsLog: 📱 Input: ${response.input}');
print('SupplementsLog: 📱 ===============================');
if (response.actionId == 'take_supplement') {
print('SupplementsLog: 📱 Processing TAKE action...');
_handleTakeAction(response.payload, response.id);
} else if (response.actionId == 'snooze_10') {
print('SupplementsLog: 📱 Processing SNOOZE action...');
_handleSnoozeAction(response.payload, 10, response.id);
} else {
print('SupplementsLog: 📱 Default notification tap (no specific action)');
// Default tap (no actionId) opens the app normally
}
}
Future<void> _handleTakeAction(String? payload, int? notificationId) async {
print('SupplementsLog: 📱 === HANDLING TAKE ACTION ===');
print('SupplementsLog: 📱 Payload received: $payload');
if (payload != null) {
try {
// Parse the payload to get supplement info
final parts = payload.split('|');
print('SupplementsLog: 📱 Payload parts: $parts (length: ${parts.length})');
if (parts.length >= 4) {
final supplementId = int.parse(parts[0]);
final supplementName = parts[1];
final units = double.parse(parts[2]);
final unitType = parts[3];
print('SupplementsLog: 📱 Parsed data:');
print('SupplementsLog: 📱 - ID: $supplementId');
print('SupplementsLog: 📱 - Name: $supplementName');
print('SupplementsLog: 📱 - Units: $units');
print('SupplementsLog: 📱 - Type: $unitType');
// Call the callback to record the intake
if (_onTakeSupplementCallback != null) {
print('SupplementsLog: 📱 Calling supplement callback...');
_onTakeSupplementCallback!(
supplementId, supplementName, units, unitType);
print('SupplementsLog: 📱 Callback completed');
} else {
print('SupplementsLog: 📱 ERROR: No callback registered!');
}
// For retry notifications, the original notification ID is in the payload
int originalNotificationId;
if (parts.length > 4 && int.tryParse(parts[4]) != null) {
originalNotificationId = int.parse(parts[4]);
print(
'SupplementsLog: 📱 Retry notification detected. Original ID: $originalNotificationId');
} 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
print('SupplementsLog: 📱 Showing confirmation notification...');
showInstantNotification(
'Supplement Taken!',
'$supplementName has been recorded at ${DateTime.now().hour.toString().padLeft(2, '0')}:${DateTime.now().minute.toString().padLeft(2, '0')}',
);
} else {
print(
'SupplementsLog: 📱 ERROR: Invalid payload format - not enough parts');
}
} catch (e) {
print('SupplementsLog: 📱 ERROR in _handleTakeAction: $e');
}
} else {
print('SupplementsLog: 📱 ERROR: Payload is null');
}
print('SupplementsLog: 📱 === TAKE ACTION COMPLETE ===');
}
void _cancelRetryNotifications(int notificationId) {
// Retry notifications use ID range starting from 200000
for (int i = 0; i < 10; i++) { // Cancel up to 10 potential retries
int retryId = 200000 + (notificationId * 10) + i;
_notifications.cancel(retryId);
print('SupplementsLog: 📱 Cancelled retry notification ID: $retryId');
}
}
void _handleSnoozeAction(String? payload, int minutes, int? notificationId) {
print('SupplementsLog: 📱 === HANDLING SNOOZE ACTION ===');
print('SupplementsLog: 📱 Payload: $payload, Minutes: $minutes');
if (payload != null) {
try {
final parts = payload.split('|');
if (parts.length >= 2) {
final supplementId = int.parse(parts[0]);
final supplementName = parts[1];
print('SupplementsLog: 📱 Snoozing supplement for $minutes minutes: $supplementName');
// Mark notification as snoozed in database (increment retry count)
if (notificationId != null) {
print('SupplementsLog: 📱 Incrementing retry count for notification $notificationId');
DatabaseHelper.instance.incrementRetryCount(notificationId);
}
// Schedule a new notification for the snooze time
final snoozeTime = tz.TZDateTime.now(tz.local).add(Duration(minutes: minutes));
print('SupplementsLog: 📱 Snooze time: $snoozeTime');
_notifications.zonedSchedule(
supplementId * 1000 + minutes, // Unique ID for snooze notifications
'Reminder: $supplementName',
'Snoozed reminder - Take your $supplementName now',
snoozeTime,
NotificationDetails(
android: AndroidNotificationDetails(
'supplement_reminders',
'Supplement Reminders',
channelDescription: 'Notifications for supplement intake reminders',
importance: Importance.high,
priority: Priority.high,
actions: [
AndroidNotificationAction(
'take_supplement',
'Take',
),
AndroidNotificationAction(
'snooze_10',
'Snooze 10min',
),
],
),
iOS: const DarwinNotificationDetails(),
),
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
payload: payload,
);
showInstantNotification(
'Reminder Snoozed',
'$supplementName reminder snoozed for $minutes minutes',
);
print('SupplementsLog: 📱 Snooze scheduled successfully');
}
} catch (e) {
print('SupplementsLog: 📱 Error handling snooze action: $e');
}
}
print('SupplementsLog: 📱 === SNOOZE ACTION COMPLETE ===');
}
/// Check for persistent reminders from app context with settings
Future<void> checkPersistentReminders(
bool persistentReminders,
int reminderRetryInterval,
int maxRetryAttempts,
) async {
await schedulePersistentReminders(
persistentReminders: persistentReminders,
reminderRetryInterval: reminderRetryInterval,
maxRetryAttempts: maxRetryAttempts,
);
}
/// Check for pending notifications that need retry and schedule them
Future<void> schedulePersistentReminders({
required bool persistentReminders,
required int reminderRetryInterval,
required int maxRetryAttempts,
}) async {
print('SupplementsLog: 📱 Checking for pending notifications to retry...');
try {
if (!persistentReminders) {
print('SupplementsLog: 📱 Persistent reminders disabled');
return;
}
print('SupplementsLog: 📱 Retry settings: interval=$reminderRetryInterval min, max=$maxRetryAttempts attempts');
// Get all pending notifications from database
final pendingNotifications = await DatabaseHelper.instance.getPendingNotifications();
print('SupplementsLog: 📱 Found ${pendingNotifications.length} pending notifications');
final now = DateTime.now();
for (final notification in pendingNotifications) {
final scheduledTime = DateTime.parse(notification['scheduledTime']).toLocal();
final retryCount = notification['retryCount'] as int;
final lastRetryTime = notification['lastRetryTime'] != null
? DateTime.parse(notification['lastRetryTime']).toLocal()
: null;
// Check if notification is overdue
final timeSinceScheduled = now.difference(scheduledTime).inMinutes;
final shouldRetry = timeSinceScheduled >= reminderRetryInterval;
print('SupplementsLog: 📱 Checking notification ${notification['notificationId']}:');
print('SupplementsLog: 📱 Scheduled: $scheduledTime (local)');
print('SupplementsLog: 📱 Now: $now');
print('SupplementsLog: 📱 Time since scheduled: $timeSinceScheduled minutes');
print('SupplementsLog: 📱 Retry interval: $reminderRetryInterval minutes');
print('SupplementsLog: 📱 Should retry: $shouldRetry');
print('SupplementsLog: 📱 Retry count: $retryCount / $maxRetryAttempts');
// Check if we haven't exceeded max retry attempts
if (retryCount >= maxRetryAttempts) {
print('SupplementsLog: 📱 Notification ${notification['notificationId']} exceeded max attempts ($maxRetryAttempts)');
continue;
}
// Check if enough time has passed since last retry
if (lastRetryTime != null) {
final timeSinceLastRetry = now.difference(lastRetryTime).inMinutes;
if (timeSinceLastRetry < reminderRetryInterval) {
print('SupplementsLog: 📱 Notification ${notification['notificationId']} not ready for retry yet');
continue;
}
}
if (shouldRetry) {
print('SupplementsLog: 📱 ⚡ SCHEDULING RETRY for notification ${notification['notificationId']}');
await _scheduleRetryNotification(notification, retryCount + 1);
} else {
print('SupplementsLog: 📱 ⏸️ NOT READY FOR RETRY: ${notification['notificationId']}');
}
}
} catch (e) {
print('SupplementsLog: 📱 Error scheduling persistent reminders: $e');
}
}
Future<void> _scheduleRetryNotification(Map<String, dynamic> notification, int retryAttempt) async {
try {
final notificationId = notification['notificationId'] as int;
final supplementId = notification['supplementId'] as int;
// Generate a unique ID for this retry (200000 + original_id * 10 + retry_attempt)
final retryNotificationId = 200000 + (notificationId * 10) + retryAttempt;
print('SupplementsLog: 📱 Scheduling retry notification $retryNotificationId for supplement $supplementId (attempt $retryAttempt)');
// Get supplement details from database
final supplements = await DatabaseHelper.instance.getAllSupplements();
final supplement = supplements.firstWhere((s) => s.id == supplementId && s.isActive, orElse: () => throw Exception('Supplement not found'));
// Schedule the retry notification immediately
await _notifications.show(
retryNotificationId,
'Reminder: ${supplement.name}',
'Don\'t forget to take your ${supplement.name}! (Retry #$retryAttempt)',
NotificationDetails(
android: AndroidNotificationDetails(
'supplement_reminders',
'Supplement Reminders',
channelDescription: 'Notifications for supplement intake reminders',
importance: Importance.high,
priority: Priority.high,
actions: [
AndroidNotificationAction(
'take_supplement',
'Take',
showsUserInterface: true,
),
AndroidNotificationAction(
'snooze_10',
'Snooze 10min',
showsUserInterface: true,
),
],
),
iOS: const DarwinNotificationDetails(),
),
payload: '${supplement.id}|${supplement.name}|${supplement.numberOfUnits}|${supplement.unitType}|$notificationId',
);
// Update the retry count in database
await DatabaseHelper.instance.incrementRetryCount(notificationId);
print('SupplementsLog: 📱 Retry notification scheduled successfully');
} catch (e) {
print('SupplementsLog: 📱 Error scheduling retry notification: $e');
}
}
Future<bool> requestPermissions() async {
print('SupplementsLog: 📱 Requesting notification permissions...');
if (_permissionsRequested) {
print('SupplementsLog: 📱 Permissions already requested');
return true;
}
try {
_permissionsRequested = true;
final androidPlugin = _notifications.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
if (androidPlugin != null) {
print('SupplementsLog: 📱 Requesting Android permissions...');
final granted = await androidPlugin.requestNotificationsPermission();
print('SupplementsLog: 📱 Android permissions granted: $granted');
if (granted != true) {
_permissionsRequested = false;
return false;
}
}
final iosPlugin = _notifications.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>();
if (iosPlugin != null) {
print('SupplementsLog: 📱 Requesting iOS permissions...');
final granted = await iosPlugin.requestPermissions(
alert: true,
badge: true,
sound: true,
);
print('SupplementsLog: 📱 iOS permissions granted: $granted');
if (granted != true) {
_permissionsRequested = false;
return false;
}
}
print('SupplementsLog: 📱 All permissions granted successfully');
return true;
} catch (e) {
_permissionsRequested = false;
print('SupplementsLog: 📱 Error requesting permissions: $e');
return false;
}
}
Future<void> scheduleSupplementReminders(Supplement supplement) async {
print('SupplementsLog: 📱 Scheduling reminders for ${supplement.name}');
print('SupplementsLog: 📱 Reminder times: ${supplement.reminderTimes}');
// Cancel existing notifications for this supplement
await cancelSupplementReminders(supplement.id!);
for (int i = 0; i < supplement.reminderTimes.length; i++) {
final timeStr = supplement.reminderTimes[i];
final timeParts = timeStr.split(':');
final hour = int.parse(timeParts[0]);
final minute = int.parse(timeParts[1]);
final notificationId = supplement.id! * 100 + i; // Unique ID for each reminder
final scheduledTime = _nextInstanceOfTime(hour, minute);
print('SupplementsLog: 📱 Scheduling notification ID $notificationId for ${timeStr} -> ${scheduledTime}');
// Track this notification in the database
await DatabaseHelper.instance.trackNotification(
notificationId: notificationId,
supplementId: supplement.id!,
scheduledTime: scheduledTime.toLocal(),
);
await _notifications.zonedSchedule(
notificationId,
'Time for ${supplement.name}',
'Take ${supplement.numberOfUnits} ${supplement.unitType} (${supplement.ingredientsPerUnit})',
scheduledTime,
NotificationDetails(
android: AndroidNotificationDetails(
'supplement_reminders',
'Supplement Reminders',
channelDescription: 'Notifications for supplement intake reminders',
importance: Importance.high,
priority: Priority.high,
actions: [
AndroidNotificationAction(
'take_supplement',
'Take',
icon: DrawableResourceAndroidBitmap('@android:drawable/ic_menu_save'),
showsUserInterface: true, // Changed to true to open app
),
AndroidNotificationAction(
'snooze_10',
'Snooze 10min',
icon: DrawableResourceAndroidBitmap('@android:drawable/ic_menu_recent_history'),
showsUserInterface: true, // Changed to true to open app
),
],
),
iOS: const DarwinNotificationDetails(),
),
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
matchDateTimeComponents: DateTimeComponents.time,
payload: '${supplement.id}|${supplement.name}|${supplement.numberOfUnits}|${supplement.unitType}',
);
print('SupplementsLog: 📱 Successfully scheduled notification ID $notificationId');
}
// Get all pending notifications to verify
final pendingNotifications = await _notifications.pendingNotificationRequests();
print('SupplementsLog: 📱 Total pending notifications: ${pendingNotifications.length}');
for (final notification in pendingNotifications) {
print('SupplementsLog: 📱 Pending: ID=${notification.id}, Title=${notification.title}');
}
}
Future<void> cancelSupplementReminders(int supplementId) async {
// Cancel all notifications for this supplement (up to 10 possible reminders)
for (int i = 0; i < 10; i++) {
final notificationId = supplementId * 100 + i;
await _notifications.cancel(notificationId);
}
// Also clean up database tracking records for this supplement
await DatabaseHelper.instance.clearNotificationTracking(supplementId);
}
Future<void> cancelAllReminders() async {
await _notifications.cancelAll();
}
tz.TZDateTime _nextInstanceOfTime(int hour, int minute) {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduledDate = tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, minute);
print('SupplementsLog: 📱 Current time: $now (${now.timeZoneName})');
print('SupplementsLog: 📱 Target time: ${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}');
print('SupplementsLog: 📱 Initial scheduled date: $scheduledDate (${scheduledDate.timeZoneName})');
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
print('SupplementsLog: 📱 Time has passed, scheduling for tomorrow: $scheduledDate (${scheduledDate.timeZoneName})');
} else {
print('SupplementsLog: 📱 Time is in the future, scheduling for today: $scheduledDate (${scheduledDate.timeZoneName})');
}
return scheduledDate;
}
Future<void> showInstantNotification(String title, String body) async {
print('SupplementsLog: 📱 Showing instant notification: $title - $body');
const NotificationDetails notificationDetails = NotificationDetails(
android: AndroidNotificationDetails(
'instant_notifications',
'Instant Notifications',
channelDescription: 'Instant notifications for supplement app',
importance: Importance.high,
priority: Priority.high,
),
iOS: DarwinNotificationDetails(),
);
await _notifications.show(
DateTime.now().millisecondsSinceEpoch ~/ 1000,
title,
body,
notificationDetails,
);
print('SupplementsLog: 📱 Instant notification sent');
}
// Debug function to test notifications
Future<void> testNotification() async {
print('SupplementsLog: 📱 Testing notification system...');
await showInstantNotification('Test Notification', 'This is a test notification to verify the system is working.');
}
// Debug function to schedule a test notification 1 minute from now
Future<void> testScheduledNotification() async {
print('SupplementsLog: 📱 Testing scheduled notification...');
final now = tz.TZDateTime.now(tz.local);
final testTime = now.add(const Duration(minutes: 1));
print('SupplementsLog: 📱 Scheduling test notification for: $testTime');
await _notifications.zonedSchedule(
99999, // Special ID for test notifications
'Test Scheduled Notification',
'This notification was scheduled 1 minute ago at ${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}',
testTime,
const NotificationDetails(
android: AndroidNotificationDetails(
'test_notifications',
'Test Notifications',
channelDescription: 'Test notifications for debugging',
importance: Importance.high,
priority: Priority.high,
),
iOS: DarwinNotificationDetails(),
),
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
);
print('SupplementsLog: 📱 Test notification scheduled successfully');
}
// Debug function to get all pending notifications
Future<List<PendingNotificationRequest>> getPendingNotifications() async {
return await _notifications.pendingNotificationRequests();
}
// Debug function to test notification actions
Future<void> testNotificationWithActions() async {
print('SupplementsLog: 📱 Creating test notification with actions...');
await _notifications.show(
88888, // Special test ID
'Test Action Notification',
'Tap Take or Snooze to test notification actions',
NotificationDetails(
android: AndroidNotificationDetails(
'test_notifications',
'Test Notifications',
channelDescription: 'Test notifications for debugging actions',
importance: Importance.high,
priority: Priority.high,
actions: [
AndroidNotificationAction(
'take_supplement',
'Take',
icon: DrawableResourceAndroidBitmap('@android:drawable/ic_menu_save'),
showsUserInterface: true,
),
AndroidNotificationAction(
'snooze_10',
'Snooze 10min',
icon: DrawableResourceAndroidBitmap('@android:drawable/ic_menu_recent_history'),
showsUserInterface: true,
),
],
),
iOS: const DarwinNotificationDetails(),
),
payload: '999|Test Supplement|1.0|capsule',
);
print('SupplementsLog: 📱 Test notification with actions created');
}
// Debug function to test basic notification tap response
Future<void> testBasicNotification() async {
print('SupplementsLog: 📱 Creating basic test notification...');
await _notifications.show(
77777, // Special test ID for basic notification
'Basic Test Notification',
'Tap this notification to test basic callback',
NotificationDetails(
android: AndroidNotificationDetails(
'test_notifications',
'Test Notifications',
channelDescription: 'Test notifications for debugging',
importance: Importance.high,
priority: Priority.high,
),
iOS: const DarwinNotificationDetails(),
),
payload: 'basic_test',
);
print('SupplementsLog: 📱 Basic test notification created');
}
}

View File

@@ -1,40 +0,0 @@
import 'dart:convert';
import 'package:flutter/services.dart';
import '../models/nutrient.dart';
class NutrientDataService {
static final NutrientDataService _instance = NutrientDataService._internal();
factory NutrientDataService() {
return _instance;
}
NutrientDataService._internal();
List<Nutrient>? _nutrients;
Future<List<Nutrient>> get nutrients async {
if (_nutrients != null) {
return _nutrients!;
}
await _loadNutrientData();
return _nutrients!;
}
Future<void> _loadNutrientData() async {
try {
final String response = await rootBundle.loadString('assets/canada_health.json');
final data = await json.decode(response);
final nutrientsData = data['nutrients'] as Map<String, dynamic>;
_nutrients = nutrientsData.entries.map((entry) {
return Nutrient.fromJson(entry.key, entry.value);
}).toList();
} catch (e) {
print('Error loading nutrient data: $e');
_nutrients = [];
}
}
}

View File

@@ -1,617 +0,0 @@
import 'dart:async';
import 'dart:math';
import '../models/ingredient.dart';
import '../models/nutrient.dart';
import 'nutrient_data_service.dart';
/// Represents an RDA/AI match for a user and nutrient
class RdaResult {
final String nutrientKey;
final String unitLabel; // e.g., "mg/day", "µg/day (RAE)"
final String rdaType; // "RDA/AI" or "AI"
final double value; // RDA/AI value in the units of unitLabel
final double? valueMin; // Optional minimum recommended value
final double? valueMax; // Optional maximum recommended value
final double? ul; // Upper limit (if provided) in the same base units as unitLabel (life-stage)
final String matchedLifeStageLabel; // e.g., "19-30 y", "51-70 y"
final String? lifeStageDescription; // Optional description for the life stage (e.g., maintenance/loading)
final UpperLimit? nutrientUl; // Nutrient-level UL (object with unit/duration/note), if available
final String? note; // Optional dataset note (e.g., magnesium UL is supplemental only)
const RdaResult({
required this.nutrientKey,
required this.unitLabel,
required this.rdaType,
required this.value,
this.valueMin,
this.valueMax,
required this.ul,
required this.matchedLifeStageLabel,
this.lifeStageDescription,
this.nutrientUl,
this.note,
});
}
/// Aggregated daily overview by nutrient
class RdaAggregate {
final String nutrientKey;
final String unitLabel; // RDA unit label
final double rdaValue; // Midpoint of range when available
final double? rdaValueMin; // Optional minimum recommended value
final double? rdaValueMax; // Optional maximum recommended value
final double? ulValue;
final double totalAmountInRdaUnit; // Total intake converted to RDA units
final double percentOfRda; // 0..100+ (may exceed 100)
final double? percentOfUl; // 0..100+ (may exceed 100)
final String? matchedLifeStageLabel; // e.g., "19-30 y"
final String? matchedLifeStageDescription; // Optional description for the life stage
final String? rdaType; // e.g., "RDA/AI" or "AI"
final UpperLimit? nutrientUl; // Nutrient-level UL object (unit/duration/note)
final String? note; // Optional dataset note
const RdaAggregate({
required this.nutrientKey,
required this.unitLabel,
required this.rdaValue,
this.rdaValueMin,
this.rdaValueMax,
required this.ulValue,
required this.totalAmountInRdaUnit,
required this.percentOfRda,
required this.percentOfUl,
this.matchedLifeStageLabel,
this.matchedLifeStageDescription,
this.rdaType,
this.nutrientUl,
this.note,
});
}
/// Service for working with Health Canada DRIs (RDA/AI and UL)
/// - Maps app ingredient names to nutrient keys in canada_health.json
/// - Computes user-specific RDA/AI and UL values based on age and gender
/// - Converts units and calculates % of RDA/AI and % of UL
class RdaService {
RdaService._internal();
static final RdaService _instance = RdaService._internal();
factory RdaService() => _instance;
final NutrientDataService _nutrientDataService = NutrientDataService();
// Cache nutrients by key: e.g., "vitamin_d"
Map<String, Nutrient>? _nutrientsByKey;
// Known alias mapping for common ingredient names to nutrient keys
// Keys must be lowercase for matching
static const Map<String, String> _aliasToNutrientKey = {
// Vitamin C
'vitamin c': 'vitamin_c',
'ascorbic acid': 'vitamin_c',
// Vitamin D
'vitamin d': 'vitamin_d',
'vitamin d3': 'vitamin_d',
'cholecalciferol': 'vitamin_d',
'vitamin d2': 'vitamin_d', // ergocalciferol - treat same RDA
// Vitamin A (RAE)
'vitamin a': 'vitamin_a',
'retinol': 'vitamin_a',
'beta-carotene': 'vitamin_a',
// Vitamin E (alpha-tocopherol)
'vitamin e': 'vitamin_e',
'alpha tocopherol': 'vitamin_e',
'alpha-tocopherol': 'vitamin_e',
// Vitamin K (K1/K2 common mapping to total Vitamin K AI)
'vitamin k': 'vitamin_k',
'vitamin k1': 'vitamin_k',
'phylloquinone': 'vitamin_k',
'vitamin k2': 'vitamin_k',
'menaquinone': 'vitamin_k',
// B1 (Thiamine)
'vitamin b1': 'vitamin_b1',
'thiamine': 'vitamin_b1',
'thiamin': 'vitamin_b1',
// B2 (Riboflavin)
'vitamin b2': 'vitamin_b2',
'riboflavin': 'vitamin_b2',
// Folate
'folate': 'folate_dfe',
'folic acid': 'folate_dfe',
'folate (dfe)': 'folate_dfe',
'dfe': 'folate_dfe',
};
// RDA result and aggregate types moved to top-level (Dart doesn't support nested classes)
/// Get a user-specific RDA result for a given ingredient name.
/// - Resolves the ingredient to a nutrient key using aliases and simple heuristics.
/// - Computes the appropriate life-stage record based on age and gender.
///
/// If the ingredient doesn't map to a known nutrient or no life stage matches,
/// returns null.
Future<RdaResult?> getUserRdaForIngredient(
String ingredientName, {
DateTime? dateOfBirth,
String? gender, // expected values similar to ['Male','Female','Other','Prefer not to say']
}) async {
final key = await mapIngredientToNutrientKey(ingredientName);
if (key == null) return null;
return getUserRdaForNutrientKey(
key,
dateOfBirth: dateOfBirth,
gender: gender,
);
}
/// Get a user-specific RDA result for a known nutrient key
/// e.g., "vitamin_d", "vitamin_c".
Future<RdaResult?> getUserRdaForNutrientKey(
String nutrientKey, {
DateTime? dateOfBirth,
String? gender,
}) async {
final nutrient = await _getNutrientByKey(nutrientKey);
if (nutrient == null) return null;
final _UserProfile profile = _UserProfile.from(dateOfBirth: dateOfBirth, gender: gender);
final LifeStage? stage = _matchLifeStageForProfile(nutrient.lifeStages, profile);
if (stage == null) return null;
return RdaResult(
nutrientKey: nutrientKey,
unitLabel: nutrient.unit,
rdaType: nutrient.rdaType,
value: stage.value,
valueMin: stage.valueMin,
valueMax: stage.valueMax,
ul: stage.ul,
matchedLifeStageLabel: stage.ageRange,
lifeStageDescription: stage.description,
nutrientUl: nutrient.ul,
note: nutrient.note,
);
}
/// Compute % of RDA and % of UL for a single ingredient dose.
/// - Resolves ingredient to nutrient key
/// - Converts the amount+unit to the RDA unit base for that nutrient
/// - Calculates percent of RDA and UL
///
/// Returns null if the ingredient cannot be mapped or units cannot be converted.
Future<RdaAggregate?> computePercentForDose(
String ingredientName,
double amount,
String unit, {
DateTime? dateOfBirth,
String? gender,
}) async {
final rda = await getUserRdaForIngredient(
ingredientName,
dateOfBirth: dateOfBirth,
gender: gender,
);
if (rda == null) return null;
final String rdaUnitSymbol = _unitSymbolFromLabel(rda.unitLabel); // "mg" or "ug"
final String normalizedInputUnit = _normalizeUnit(unit);
final double? amountInRdaUnit = _convertAmountToTargetUnit(
ingredientName: ingredientName,
amount: amount,
fromUnit: normalizedInputUnit,
toUnit: rdaUnitSymbol,
);
if (amountInRdaUnit == null) return null;
final double rdaForCalc = (rda.valueMin != null && rda.valueMax != null)
? ((rda.valueMin! + rda.valueMax!) / 2.0)
: rda.value;
final double percentOfRda = (amountInRdaUnit / rdaForCalc) * 100.0;
final double? percentOfUl =
rda.ul != null && rda.ul! > 0 ? (amountInRdaUnit / rda.ul!) * 100.0 : null;
return RdaAggregate(
nutrientKey: rda.nutrientKey,
unitLabel: rda.unitLabel,
rdaValue: rdaForCalc,
rdaValueMin: rda.valueMin,
rdaValueMax: rda.valueMax,
ulValue: rda.ul,
totalAmountInRdaUnit: amountInRdaUnit,
percentOfRda: percentOfRda,
percentOfUl: percentOfUl,
matchedLifeStageLabel: rda.matchedLifeStageLabel,
matchedLifeStageDescription: rda.lifeStageDescription,
rdaType: rda.rdaType,
nutrientUl: rda.nutrientUl,
note: rda.note,
);
}
/// Aggregate multiple ingredients (e.g., full-day intake) into user-specific RDA overview.
/// - Sums all ingredients mapped to the same nutrient
/// - Converts units to the RDA base unit
/// - Returns map keyed by nutrientKey
Future<Map<String, RdaAggregate>> aggregateDailyIntake(
List<Ingredient> ingredients, {
DateTime? dateOfBirth,
String? gender,
}) async {
final Map<String, double> totalsByNutrient = {};
final Map<String, RdaResult> rdaByNutrient = {};
for (final ing in ingredients) {
final key = await mapIngredientToNutrientKey(ing.name);
if (key == null) continue;
// Ensure RDA is loaded for the nutrient
rdaByNutrient[key] = rdaByNutrient[key] ??
(await getUserRdaForNutrientKey(key, dateOfBirth: dateOfBirth, gender: gender))!;
final rda = rdaByNutrient[key];
if (rda == null) continue; // no match for this nutrient
final String rdaUnitSymbol = _unitSymbolFromLabel(rda.unitLabel);
final double? converted = _convertAmountToTargetUnit(
ingredientName: ing.name,
amount: ing.amount,
fromUnit: _normalizeUnit(ing.unit),
toUnit: rdaUnitSymbol,
);
if (converted == null) continue;
totalsByNutrient[key] = (totalsByNutrient[key] ?? 0.0) + converted;
}
final Map<String, RdaAggregate> result = {};
for (final entry in totalsByNutrient.entries) {
final key = entry.key;
final total = entry.value;
final rda = rdaByNutrient[key];
if (rda == null) continue;
final double rdaForCalc = (rda.valueMin != null && rda.valueMax != null)
? ((rda.valueMin! + rda.valueMax!) / 2.0)
: rda.value;
final double percentOfRda = (total / rdaForCalc) * 100.0;
final double? percentOfUl = rda.ul != null && rda.ul! > 0 ? (total / rda.ul!) * 100.0 : null;
result[key] = RdaAggregate(
nutrientKey: key,
unitLabel: rda.unitLabel,
rdaValue: rdaForCalc,
rdaValueMin: rda.valueMin,
rdaValueMax: rda.valueMax,
ulValue: rda.ul,
totalAmountInRdaUnit: total,
percentOfRda: percentOfRda,
percentOfUl: percentOfUl,
matchedLifeStageLabel: rda.matchedLifeStageLabel,
matchedLifeStageDescription: rda.lifeStageDescription,
rdaType: rda.rdaType,
nutrientUl: rda.nutrientUl,
note: rda.note,
);
}
return result;
}
/// Map an ingredient name (e.g., "Vitamin D3") to a nutrient key (e.g., "vitamin_d") used in canada_health.json
/// Returns null if no mapping is found.
Future<String?> mapIngredientToNutrientKey(String ingredientName) async {
await _ensureNutrientsLoaded();
final String cleaned = _normalizeIngredientName(ingredientName);
// Direct alias mapping
final direct = _aliasToNutrientKey[cleaned];
if (direct != null && _nutrientsByKey!.containsKey(direct)) return direct;
// Heuristic contains-based mapping
if (cleaned.contains('vitamin d')) return _nutrientsByKey!.containsKey('vitamin_d') ? 'vitamin_d' : null;
if (cleaned.contains('vitamin c')) return _nutrientsByKey!.containsKey('vitamin_c') ? 'vitamin_c' : null;
if (cleaned.contains('vitamin a') || cleaned.contains('retinol') || cleaned.contains('beta carotene')) {
return _nutrientsByKey!.containsKey('vitamin_a') ? 'vitamin_a' : null;
}
if (cleaned.contains('vitamin e') || cleaned.contains('alpha tocopherol') || cleaned.contains('alpha-tocopherol')) {
return _nutrientsByKey!.containsKey('vitamin_e') ? 'vitamin_e' : null;
}
if (cleaned.contains('vitamin k') || cleaned.contains('phylloquinone') || cleaned.contains('menaquinone')) {
return _nutrientsByKey!.containsKey('vitamin_k') ? 'vitamin_k' : null;
}
if (cleaned.contains('b1') || cleaned.contains('thiamin') || cleaned.contains('thiamine')) {
return _nutrientsByKey!.containsKey('vitamin_b1') ? 'vitamin_b1' : null;
}
if (cleaned.contains('b2') || cleaned.contains('riboflavin')) {
return _nutrientsByKey!.containsKey('vitamin_b2') ? 'vitamin_b2' : null;
}
if (cleaned.contains('folate') || cleaned.contains('folic')) {
return _nutrientsByKey!.containsKey('folate_dfe') ? 'folate_dfe' : null;
}
if (cleaned.contains('vitamin b3') || cleaned.contains('niacin') || cleaned.contains('nicotinic acid') || cleaned.contains('niacinamide')) {
return _nutrientsByKey!.containsKey('vitamin_b3') ? 'vitamin_b3' : null;
}
if (cleaned.contains('vitamin b5') || cleaned.contains('pantothenic')) {
return _nutrientsByKey!.containsKey('vitamin_b5') ? 'vitamin_b5' : null;
}
if (cleaned.contains('vitamin b6') || cleaned.contains('pyridoxine')) {
return _nutrientsByKey!.containsKey('vitamin_b6') ? 'vitamin_b6' : null;
}
if (cleaned.contains('vitamin b12') || cleaned.contains('cobalamin') || cleaned.contains('cyanocobalamin') || cleaned.contains('methylcobalamin')) {
return _nutrientsByKey!.containsKey('vitamin_b12') ? 'vitamin_b12' : null;
}
if (cleaned.contains('magnesium')) {
return _nutrientsByKey!.containsKey('magnesium') ? 'magnesium' : null;
}
if (cleaned.contains('zinc') || cleaned == 'zn') {
return _nutrientsByKey!.containsKey('zinc') ? 'zinc' : null;
}
if (cleaned.contains('iron') || cleaned.contains('ferrous') || cleaned.contains('ferric')) {
return _nutrientsByKey!.containsKey('iron') ? 'iron' : null;
}
if (cleaned.contains('creatine') || cleaned.contains('creapure') || cleaned.contains('creatine monohydrate')) {
return _nutrientsByKey!.containsKey('creatine') ? 'creatine' : null;
}
return null;
}
// -----------------------
// Internal helpers
// -----------------------
Future<void> _ensureNutrientsLoaded() async {
if (_nutrientsByKey != null) return;
final list = await _nutrientDataService.nutrients;
_nutrientsByKey = {for (final n in list) n.name: n};
}
Future<Nutrient?> _getNutrientByKey(String key) async {
await _ensureNutrientsLoaded();
return _nutrientsByKey![key];
}
// Normalize units (user input and stored ingredients)
// Supported return values: "mg", "ug", "g", "iu", others returned as lowercased original (e.g., "ml")
String _normalizeUnit(String unit) {
final u = unit.trim().toLowerCase();
if (u == 'mg') return 'mg';
if (u == 'g' || u == 'gram' || u == 'grams') return 'g';
if (u == 'µg' || u == 'μg' || u == 'mcg' || u == 'ug' || u == 'microgram' || u == 'micrograms') return 'ug';
if (u == 'iu') return 'iu';
return u; // e.g., "ml", "drops" etc. (unhandled for RDA calc)
}
// Extract the base unit symbol ("mg" or "ug") from the dataset unit label (e.g., "µg/day (RAE)")
String _unitSymbolFromLabel(String label) {
final lower = label.toLowerCase();
if (lower.startsWith('mg')) return 'mg';
if (lower.startsWith('g')) return 'g';
if (lower.startsWith('µg') || lower.startsWith('μg') || lower.startsWith('mcg')) return 'ug';
// Fallback: assume microgram if unknown
return 'ug';
}
// Convert an amount from one unit to another.
// Supported:
// - mg <-> ug <-> g
// - IU->ug for Vitamin D only (1 µg = 40 IU)
// Returns null if conversion cannot be performed.
double? _convertAmountToTargetUnit({
required String ingredientName,
required double amount,
required String fromUnit,
required String toUnit,
}) {
if (amount.isNaN || amount.isInfinite) return null;
// Handle IU conversions only for Vitamin D
final name = _normalizeIngredientName(ingredientName);
final isVitaminD = name.contains('vitamin d') || name.contains('cholecalciferol') || name.contains('ergocalciferol');
// If fromUnit equals toUnit and it's one of our supported numeric units
if ((fromUnit == toUnit) && (fromUnit == 'mg' || fromUnit == 'ug' || fromUnit == 'g')) {
return amount;
}
// IU -> ug for Vitamin D
if (fromUnit == 'iu' && isVitaminD) {
// 1 µg = 40 IU => ug = IU / 40
final ug = amount / 40.0;
if (toUnit == 'ug') return ug;
if (toUnit == 'mg') return ug / 1000.0;
if (toUnit == 'g') return ug / 1e6;
return null;
}
// Mass conversions
double? inUg;
if (fromUnit == 'ug') {
inUg = amount;
} else if (fromUnit == 'mg') {
inUg = amount * 1000.0;
} else if (fromUnit == 'g') {
inUg = amount * 1e6;
} else {
// Unsupported unit (e.g., ml, drops)
return null;
}
if (toUnit == 'ug') return inUg;
if (toUnit == 'mg') return inUg / 1000.0;
if (toUnit == 'g') return inUg / 1e6;
return null;
}
// Normalize an ingredient name for alias matching
String _normalizeIngredientName(String name) {
final lower = name.trim().toLowerCase();
// Replace common punctuation with spaces, then condense
final replaced = lower
.replaceAll(RegExp(r'[\(\)\[\]\{\},;:+/_-]+'), ' ')
.replaceAll(RegExp(r'\s+'), ' ')
.trim();
return replaced;
}
// -----------------------
// Life stage matching
// -----------------------
LifeStage? _matchLifeStageForProfile(List<LifeStage> stages, _UserProfile profile) {
// Exclude pregnancy/lactation when we don't track that state yet
final filtered = stages.where((s) {
final l = s.ageRange.toLowerCase();
return !(l.contains('pregnancy') || l.contains('lactation'));
}).toList();
// Try in order:
// 1) Exact age match + exact sex
final exactSexMatch = filtered.where((s) => _matchesAge(s.ageRange, profile) && _matchesSex(s.sex, profile)).toList();
if (exactSexMatch.isNotEmpty) return exactSexMatch.first;
// 2) Age match + sex == 'both'
final bothMatch = filtered.where((s) => _matchesAge(s.ageRange, profile) && s.sex.toLowerCase() == 'both').toList();
if (bothMatch.isNotEmpty) return bothMatch.first;
// 3) Age match ignoring sex (fallback)
final ageOnly = filtered.where((s) => _matchesAge(s.ageRange, profile)).toList();
if (ageOnly.isNotEmpty) return ageOnly.first;
// 4) If nothing matches, try 'adult-like' fallback: pick a reasonable adult range
final adultFallback = filtered.where((s) => s.ageRange.contains('19-30') || s.ageRange.contains('31-50')).toList();
if (adultFallback.isNotEmpty) return adultFallback.first;
// 5) Any entry as last resort
return filtered.isNotEmpty ? filtered.first : null;
}
bool _matchesSex(String stageSex, _UserProfile profile) {
final s = stageSex.toLowerCase();
if (s == 'both') return true;
if (profile.isInfant && s == 'infant') return true;
if (profile.sex == _Sex.male && s == 'male') return true;
if (profile.sex == _Sex.female && s == 'female') return true;
// For 'Other' or 'Prefer not to say', accept 'both'
if (profile.sex == _Sex.unknown && s == 'both') return true;
return false;
}
bool _matchesAge(String ageRange, _UserProfile profile) {
final ar = ageRange.toLowerCase().trim();
// Common shorthand: "adult" (assume >= 18 years)
if (ar == 'adult' || ar == 'adults') {
return profile.ageYears >= 18;
}
// Months range: e.g., "0-6 mo" or "7-12 mo"
final moMatch = RegExp(r'^(\d+)\s*-\s*(\d+)\s*mo$').firstMatch(ar);
if (moMatch != null) {
if (!profile.isInfant) return false;
final minMo = int.parse(moMatch.group(1)!);
final maxMo = int.parse(moMatch.group(2)!);
return profile.ageMonths >= minMo && profile.ageMonths <= maxMo;
}
// Years range: e.g., "1-3 y", "4-8 y", "9-13 y", "14-18 y", "19-30 y", "31-50 y", "51-70 y"
final yearRange = RegExp(r'^(\d+)\s*-\s*(\d+)\s*y$').firstMatch(ar);
if (yearRange != null) {
final minY = int.parse(yearRange.group(1)!);
final maxY = int.parse(yearRange.group(2)!);
return profile.ageYears >= minY && profile.ageYears <= maxY;
}
// Greater than: e.g., ">70 y"
final gtYear = RegExp(r'^>\s*(\d+)\s*y$').firstMatch(ar);
if (gtYear != null) {
final minExclusive = int.parse(gtYear.group(1)!);
return profile.ageYears > minExclusive;
}
// "infant" buckets handled via months range above
// Any unknown format: do a best-effort fallback
if (profile.isInfant) {
// If the stage mentions "infant", accept
if (ar.contains('infant')) return true;
// Else, if it starts at 0-? y, let's accept if upper bound >= 0 (rare)
if (ar.contains('0-') && ar.contains('y')) return true;
return false;
}
// If we are adult and stage is one of the adult ranges not following the patterns,
// just return false to avoid false positives.
return false;
}
}
/// Internal representation of user profile for matching
class _UserProfile {
final int ageYears; // rounded down, e.g., 29
final int ageMonths; // total months (for infants). If >= 12, consider non-infant.
final _Sex sex;
bool get isInfant => ageYears < 1;
_UserProfile({
required this.ageYears,
required this.ageMonths,
required this.sex,
});
factory _UserProfile.from({DateTime? dateOfBirth, String? gender}) {
final now = DateTime.now();
int years;
int monthsTotal;
if (dateOfBirth == null) {
// Default to adult 30 years old when unknown
years = 30;
monthsTotal = 30 * 12;
} else {
years = now.year - dateOfBirth.year;
final beforeBirthday = (now.month < dateOfBirth.month) ||
(now.month == dateOfBirth.month && now.day < dateOfBirth.day);
if (beforeBirthday) years = max(0, years - 1);
// Calculate total months difference
int months = (now.year - dateOfBirth.year) * 12 + (now.month - dateOfBirth.month);
if (now.day < dateOfBirth.day) {
months = max(0, months - 1);
}
monthsTotal = max(0, months);
}
final s = _parseSex(gender);
return _UserProfile(
ageYears: years,
ageMonths: monthsTotal,
sex: s,
);
}
}
enum _Sex { male, female, unknown }
_Sex _parseSex(String? gender) {
if (gender == null) return _Sex.unknown;
final g = gender.trim().toLowerCase();
if (g == 'male') return _Sex.male;
if (g == 'female') return _Sex.female;
return _Sex.unknown; // 'Other', 'Prefer not to say' -> unknown
}

View File

@@ -0,0 +1,568 @@
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:supplements/logging.dart';
import 'package:timezone/data/latest.dart' as tzdata;
import 'package:timezone/timezone.dart' as tz;
import 'dart:convert';
import 'package:supplements/services/notification_router.dart';
import 'package:supplements/services/notification_debug_store.dart';
import '../models/supplement.dart';
/// A minimal notification scheduler focused purely on:
/// - Initialization
/// - Permission requests
/// - Scheduling daily notifications for supplements
/// - Canceling scheduled notifications
///
/// No retries, no snooze, no database logic.
class SimpleNotificationService {
SimpleNotificationService._internal();
static final SimpleNotificationService instance =
SimpleNotificationService._internal();
final FlutterLocalNotificationsPlugin _plugin =
FlutterLocalNotificationsPlugin();
bool _initialized = false;
// Channel IDs
static const String _channelDailyId = 'supplement_reminders';
static const String _channelDailyName = 'Supplement Reminders';
static const String _channelDailyDescription = 'Daily supplement intake reminders';
/// Initialize timezone data and the notifications plugin.
///
/// Note: This does not request runtime permissions. Call [requestPermissions]
/// to prompt the user for notification permissions.
Future<void> initialize({
DidReceiveBackgroundNotificationResponseCallback? onDidReceiveBackgroundNotificationResponse,
}) async {
if (_initialized) return;
// Initialize timezone database and set a sane default.
// If you prefer, replace 'Europe/Amsterdam' with your preferred default,
// or integrate a platform timezone resolver.
tzdata.initializeTimeZones();
tz.setLocalLocation(tz.getLocation('Europe/Amsterdam'));
const androidSettings = AndroidInitializationSettings('@mipmap/ic_launcher');
final iosSettings = DarwinInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
notificationCategories: [
DarwinNotificationCategory(
'single',
actions: [
DarwinNotificationAction.plain('take_single', 'Take'),
DarwinNotificationAction.plain('snooze_single', 'Snooze'),
],
),
DarwinNotificationCategory(
'group',
actions: [
DarwinNotificationAction.plain('take_group', 'Take All'),
DarwinNotificationAction.plain('snooze_group', 'Snooze'),
],
),
],
);
const linuxSettings = LinuxInitializationSettings(
defaultActionName: 'Open notification',
);
final initSettings = InitializationSettings(
android: androidSettings,
iOS: iosSettings,
linux: linuxSettings,
);
await _plugin.initialize(
initSettings,
onDidReceiveNotificationResponse: (response) {
NotificationRouter.instance.handleNotificationResponse(response);
},
onDidReceiveBackgroundNotificationResponse: onDidReceiveBackgroundNotificationResponse,
);
_initialized = true;
}
/// Request runtime notification permissions.
///
/// On Android 13+, this will prompt for POST_NOTIFICATIONS. On older Android,
/// this is a no-op. On iOS, it requests alert/badge/sound.
Future<bool> requestPermissions() async {
// Ensure the plugin is ready before requesting permissions.
if (!_initialized) {
await initialize();
}
bool granted = true;
final androidPlugin = _plugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();
if (androidPlugin != null) {
final ok = await androidPlugin.requestNotificationsPermission();
granted = granted && (ok == true);
}
final iosPlugin = _plugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>();
if (iosPlugin != null) {
final ok = await iosPlugin.requestPermissions(
alert: true,
badge: true,
sound: true,
);
granted = granted && (ok == true);
}
return granted;
}
/// Schedule grouped daily reminders for a list of supplements.
///
/// - Groups supplements by HH:mm and schedules one notification per time slot.
/// - Uses daily recurrence via matchDateTimeComponents: DateTimeComponents.time.
/// - Keeps iOS pending notifications well below the 64 limit.
///
/// IDs:
/// - Group ID per time slot: 40000 + hour*60 + minute.
/// - Stable and predictable for cancel/update operations.
Future<void> scheduleDailyGroupedReminders(List<Supplement> supplements) async {
if (!_initialized) {
await initialize();
}
printLog('🛠 scheduleDailyGroupedReminders -> ${supplements.length} supplements');
// Clear everything first to avoid duplicates or stale schedules
await cancelAll();
printLog('🧹 Cleared all existing notifications before scheduling groups');
// Build groups: HH:mm -> list<Supplement>
final Map<String, List<Supplement>> groups = {};
for (final s in supplements.where((s) => s.isActive && s.reminderTimes.isNotEmpty && s.id != null)) {
for (final timeStr in s.reminderTimes) {
final parts = timeStr.split(':');
if (parts.length != 2) continue;
final hour = int.tryParse(parts[0]);
final minute = int.tryParse(parts[1]);
if (hour == null || minute == null) continue;
final key = '${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}';
groups.putIfAbsent(key, () => []).add(s);
}
}
printLog('⏱ Found ${groups.length} time group(s): ${groups.keys.toList()}');
if (groups.isEmpty) {
printLog('⚠️ No groups to schedule (no active supplements with reminder times)');
return;
}
// Schedule one notification per time group
for (final entry in groups.entries) {
final timeKey = entry.key; // HH:mm
final items = entry.value;
final parts = timeKey.split(':');
final hour = int.parse(parts[0]);
final minute = int.parse(parts[1]);
final when = _nextInstanceOfTime(hour, minute);
final id = 40000 + (hour * 60) + minute;
final count = items.length;
final title = count == 1
? 'Time for ${items.first.name}'
: 'Time for $count supplements';
// Build body that lists each supplement concisely
final bodyLines = items.map((s) {
final units = s.numberOfUnits;
final unitType = s.unitType;
final perUnit = s.ingredientsPerUnit;
return '${s.name}$units $unitType ($perUnit)';
}).toList();
final body = bodyLines.join('\n');
printLog('📅 Scheduling group $timeKey (count=$count) id=$id');
printLog('🕒 Now=${tz.TZDateTime.now(tz.local)} | When=$when');
// Use BigTextStyle/InboxStyle for Android to show multiple lines
final bool isSingle = count == 1;
// Tag payload with origin meta for debug/inspection
final Map<String, dynamic> payloadMap = isSingle
? {"type": "single", "id": items.first.id}
: {"type": "group", "time": timeKey};
payloadMap["meta"] = {"kind": "daily"};
final String payloadStr = jsonEncode(payloadMap);
final androidDetails = AndroidNotificationDetails(
_channelDailyId,
_channelDailyName,
channelDescription: _channelDailyDescription,
importance: Importance.high,
priority: Priority.high,
styleInformation: BigTextStyleInformation(
body,
contentTitle: title,
htmlFormatContentTitle: false,
),
actions: [
if (isSingle)
AndroidNotificationAction(
'take_single',
'Take',
showsUserInterface: true,
cancelNotification: true,
)
else
AndroidNotificationAction(
'take_group',
'Take All',
showsUserInterface: true,
cancelNotification: true,
),
if (isSingle)
AndroidNotificationAction(
'snooze_single',
'Snooze',
showsUserInterface: false,
// Removed cancelNotification: true for debugging
)
else
AndroidNotificationAction(
'snooze_group',
'Snooze',
showsUserInterface: false,
// Removed cancelNotification: true for debugging
),
],
);
final iosDetails = DarwinNotificationDetails(
categoryIdentifier: isSingle ? 'single' : 'group',
);
await _plugin.zonedSchedule(
id,
title,
isSingle ? body : 'Tap to see details',
when,
NotificationDetails(
android: androidDetails,
iOS: iosDetails,
),
androidScheduleMode: AndroidScheduleMode.inexactAllowWhileIdle,
matchDateTimeComponents: DateTimeComponents.time,
payload: payloadStr,
);
// Log to debug store
final createdAtMs = DateTime.now().millisecondsSinceEpoch;
await NotificationDebugStore.instance.add(
NotificationLogEntry(
id: id,
kind: 'daily',
type: isSingle ? 'single' : 'group',
whenEpochMs: when.millisecondsSinceEpoch,
createdAtEpochMs: createdAtMs,
title: title,
payload: payloadStr,
singleId: isSingle ? items.first.id : null,
timeKey: isSingle ? null : timeKey,
),
);
printLog('✅ Scheduled group $timeKey with ID $id');
}
// Log what the system reports as pending
try {
final pending = await _plugin.pendingNotificationRequests();
printLog('📋 Pending notifications after scheduling: ${pending.length}');
for (final p in pending) {
printLog(' - ID=${p.id}, Title=${p.title}, BodyLen=${p.body?.length ?? 0}');
}
} catch (e) {
printLog('⚠️ Could not fetch pending notifications: $e');
}
}
/// Convenience to schedule grouped reminders for today and tomorrow.
///
/// For iOSs 64 limit, we stick to one day for recurring (matchDateTimeComponents)
/// which already repeats every day without needing to schedule future dates.
/// If you want an extra safety net, you could schedule tomorrows one-offs,
/// but with daily components this is unnecessary and risks hitting iOS limits.
Future<void> scheduleDailyGroupedRemindersSafe(List<Supplement> supplements) async {
// For now, just schedule todays recurring groups.
await scheduleDailyGroupedReminders(supplements);
}
/// Cancel all scheduled reminders for a given [supplementId].
///
/// We assume up to 100 slots per supplement (00-99). This is simple and safe.
Future<void> cancelSupplementReminders(int supplementId) async {
if (!_initialized) {
await initialize();
}
for (int i = 0; i < 100; i++) {
await _plugin.cancel(supplementId * 100 + i);
}
}
/// Cancel all scheduled notifications.
Future<void> cancelAll() async {
if (!_initialized) {
await initialize();
}
await _plugin.cancelAll();
}
/// Cancel a specific notification by ID.
Future<void> cancelById(int id) async {
if (!_initialized) {
await initialize();
}
await _plugin.cancel(id);
}
/// Show an immediate notification. Useful for quick diagnostics.
Future<void> showInstant({
required String title,
required String body,
String? payload,
bool includeSnoozeActions = false, // New parameter
bool isSingle = true, // New parameter, defaults to single for instant
}) async {
if (!_initialized) {
await initialize();
}
final androidDetails = AndroidNotificationDetails(
'instant_notifications',
'Instant Notifications',
channelDescription: 'One-off or immediate notifications',
importance: Importance.high,
priority: Priority.high,
actions: includeSnoozeActions
? [
if (isSingle)
AndroidNotificationAction(
'take_single',
'Take',
showsUserInterface: true,
cancelNotification: true,
)
else
AndroidNotificationAction(
'take_group',
'Take All',
showsUserInterface: true,
cancelNotification: true,
),
if (isSingle)
AndroidNotificationAction(
'snooze_single',
'Snooze',
showsUserInterface: false,
cancelNotification: true,
)
else
AndroidNotificationAction(
'snooze_group',
'Snooze',
showsUserInterface: false,
cancelNotification: true,
),
]
: [], // No actions by default
);
final iosDetails = DarwinNotificationDetails(
categoryIdentifier: includeSnoozeActions
? (isSingle ? 'single' : 'group')
: null, // Use category for actions
);
await _plugin.show(
DateTime.now().millisecondsSinceEpoch ~/ 1000,
title,
body,
NotificationDetails(
android: androidDetails,
iOS: iosDetails,
),
payload: payload,
);
}
/// Schedule a one-off (non-repeating) reminder, typically used for Snooze.
Future<void> scheduleOneOffReminder({
required String title,
required String body,
required String payload,
required bool isSingle,
required Duration delay,
}) async {
if (!_initialized) {
await initialize();
}
final when = tz.TZDateTime.now(tz.local).add(delay);
final id = DateTime.now().millisecondsSinceEpoch ~/ 1000;
final androidDetails = AndroidNotificationDetails(
_channelDailyId,
_channelDailyName,
channelDescription: _channelDailyDescription,
importance: Importance.high,
priority: Priority.high,
styleInformation: BigTextStyleInformation(
body,
contentTitle: title,
htmlFormatContentTitle: false,
),
actions: [
if (isSingle)
AndroidNotificationAction(
'take_single',
'Take',
showsUserInterface: true,
cancelNotification: true,
)
else
AndroidNotificationAction(
'take_group',
'Take All',
showsUserInterface: true,
cancelNotification: true,
),
if (isSingle)
AndroidNotificationAction(
'snooze_single',
'Snooze',
showsUserInterface: false,
// Removed cancelNotification: true for debugging
)
else
AndroidNotificationAction(
'snooze_group',
'Snooze',
showsUserInterface: false,
// Removed cancelNotification: true for debugging
),
],
);
final iosDetails = DarwinNotificationDetails(
categoryIdentifier: isSingle ? 'single' : 'group',
);
// Enrich payload with meta for snooze; also capture linkage for logging
Map<String, dynamic>? pmap;
try {
pmap = jsonDecode(payload) as Map<String, dynamic>;
} catch (_) {
pmap = null;
}
final createdAtMs = DateTime.now().millisecondsSinceEpoch;
String payloadFinal = payload;
int? logSingleId;
String? logTimeKey;
if (pmap != null) {
final meta = {
'kind': 'snooze',
'createdAt': createdAtMs,
'delayMin': delay.inMinutes,
};
pmap['meta'] = meta;
if (pmap['type'] == 'single') {
final v = pmap['id'];
logSingleId = v is int ? v : null;
} else if (pmap['type'] == 'group') {
logTimeKey = pmap['time'] as String?;
}
payloadFinal = jsonEncode(pmap);
}
await _plugin.zonedSchedule(
id,
title,
isSingle ? body : 'Tap to see details',
when,
NotificationDetails(
android: androidDetails,
iOS: iosDetails,
),
androidScheduleMode: AndroidScheduleMode.inexactAllowWhileIdle,
payload: payloadFinal,
);
// Log to debug store
await NotificationDebugStore.instance.add(
NotificationLogEntry(
id: id,
kind: 'snooze',
type: isSingle ? 'single' : 'group',
whenEpochMs: when.millisecondsSinceEpoch,
createdAtEpochMs: createdAtMs,
title: title,
payload: payloadFinal,
singleId: logSingleId,
timeKey: logTimeKey,
),
);
printLog('⏰ Scheduled one-off reminder (id=$id) at $when, isSingle=$isSingle');
}
Future<NotificationAppLaunchDetails?> getLaunchDetails() async {
if (!_initialized) {
await initialize();
}
try {
final details = await _plugin.getNotificationAppLaunchDetails();
return details;
} catch (e) {
printLog('⚠️ getLaunchDetails error: $e');
return null;
}
}
/// Helper to compute the next instance of [hour]:[minute] in the local tz.
tz.TZDateTime _nextInstanceOfTime(int hour, int minute) {
final now = tz.TZDateTime.now(tz.local);
var scheduled =
tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, minute);
if (scheduled.isBefore(now)) {
scheduled = scheduled.add(const Duration(days: 1));
printLog('⏭ Scheduling for tomorrow at ${scheduled.toString()} (${scheduled.timeZoneName})');
} else {
printLog('⏲ Scheduling for today at ${scheduled.toString()} (${scheduled.timeZoneName})');
}
return scheduled;
}
/// Debug helper to fetch and log all pending notifications.
Future<List<PendingNotificationRequest>> getPendingNotifications() async {
if (!_initialized) {
await initialize();
}
final list = await _plugin.pendingNotificationRequests();
printLog('🧾 getPendingNotifications -> ${list.length} pending');
for (final p in list) {
printLog(' • ID=${p.id}, Title=${p.title}, Payload=${p.payload}');
}
return list;
}
}

View File

@@ -0,0 +1,206 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../models/supplement.dart';
import '../../providers/supplement_provider.dart';
/// Shows a bulk "Take supplements" dialog for a list of supplements.
/// - No time selection (records as now)
/// - Allows editing units per supplement
/// - Optional shared notes (applies to all)
Future<void> showBulkTakeDialog(
BuildContext context,
List<Supplement> supplements,
) async {
if (supplements.isEmpty) {
return;
}
// Controllers for each supplement's units
final Map<int, TextEditingController> unitControllers = {
for (final s in supplements.where((s) => s.id != null))
s.id!: TextEditingController(text: s.numberOfUnits.toString()),
};
final notesController = TextEditingController();
await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Take ${supplements.length} supplements'),
content: SizedBox(
width: double.maxFinite,
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 500),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
// List of supplements with editable units
// Use a scroll view with explicit max height to avoid intrinsic dimension issues.
ConstrainedBox(
constraints: const BoxConstraints(maxHeight: 320),
child: SingleChildScrollView(
child: Column(
children: [
for (int index = 0; index < supplements.length; index++) ...[
() {
final s = supplements[index];
final controller = unitControllers[s.id] ??
TextEditingController(text: s.numberOfUnits.toString());
return Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainerHighest.withValues(alpha: 0.5),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.3),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Name and per-unit info
Row(
children: [
Expanded(
child: Text(
s.name,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
const SizedBox(width: 8),
Text(
s.unitType,
style: TextStyle(
fontSize: 12,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
],
),
const SizedBox(height: 8),
// Units editor
Row(
children: [
Expanded(
child: TextField(
controller: controller,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
decoration: InputDecoration(
labelText: 'Units',
border: const OutlineInputBorder(),
suffixText: s.unitType,
),
),
),
],
),
const SizedBox(height: 8),
// Dosage line
Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
borderRadius: BorderRadius.circular(6),
border: Border.all(
color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.3),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Per unit:',
style: TextStyle(
fontSize: 12,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
Text(
s.ingredientsDisplay,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: Theme.of(context).colorScheme.onSurface,
),
),
],
),
),
],
),
);
}(),
if (index != supplements.length - 1) const SizedBox(height: 8),
],
],
),
),
),
const SizedBox(height: 12),
// Shared notes
TextField(
controller: notesController,
decoration: const InputDecoration(
labelText: 'Notes for all (optional)',
border: OutlineInputBorder(),
),
maxLines: 2,
),
],
),
)),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
ElevatedButton(
onPressed: () {
final provider = context.read<SupplementProvider>();
int recorded = 0;
for (final s in supplements) {
if (s.id == null) continue;
final controller = unitControllers[s.id]!;
final units = double.tryParse(controller.text) ?? s.numberOfUnits.toDouble();
// totalDosageTaken stays 0.0 for now (ingredients-based tracking later)
provider.recordIntake(
s.id!,
0.0,
unitsTaken: units,
notes: notesController.text.isNotEmpty ? notesController.text : null,
takenAt: null, // "now"
);
recorded++;
}
Navigator.of(context).pop();
if (recorded > 0) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Recorded $recorded supplement${recorded == 1 ? '' : 's'}'),
backgroundColor: Colors.green,
),
);
}
},
child: const Text('Record All'),
),
],
);
},
);
// Dispose controllers
for (final c in unitControllers.values) {
c.dispose();
}
notesController.dispose();
}

View File

@@ -0,0 +1,245 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import '../../models/supplement.dart';
import '../../providers/supplement_provider.dart';
/// Shows the "Take supplement" dialog.
/// - If [hideTime] is true, the time selection UI is hidden and intake is recorded as "now".
Future<void> showTakeSupplementDialog(
BuildContext context,
Supplement supplement, {
bool hideTime = false,
}) async {
final unitsController = TextEditingController(text: supplement.numberOfUnits.toString());
final notesController = TextEditingController();
DateTime selectedDateTime = DateTime.now();
bool useCustomTime = false;
await showShadDialog(
context: context,
builder: (context) => StatefulBuilder(
builder: (context, setState) {
return ShadDialog(
title: Text('Take ${supplement.name}'),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Expanded(
child: ShadInput(
controller: unitsController,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
placeholder: Text('Number of ${supplement.unitType}'),
onChanged: (value) => setState(() {}),
),
),
],
),
const SizedBox(height: 8),
ShadCard(
child: Padding(
padding: const EdgeInsets.all(8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Total dosage:',
style: TextStyle(
fontSize: 12,
color: ShadTheme.of(context).colorScheme.foreground,
),
),
Text(
supplement.ingredientsDisplay,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: ShadTheme.of(context).colorScheme.foreground,
),
),
],
),
),
),
const SizedBox(height: 16),
if (!hideTime) ...[
ShadCard(
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(
Icons.access_time,
size: 16,
color: ShadTheme.of(context).colorScheme.primary,
),
const SizedBox(width: 6),
Text(
'When did you take it?',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: ShadTheme.of(context).colorScheme.primary,
),
),
],
),
const SizedBox(height: 8),
ShadRadioGroupFormField<bool>(
initialValue: useCustomTime,
onChanged: (value) => setState(() => useCustomTime = value!),
items: [
ShadRadio(
value: false,
label: const Text('Just now', style: TextStyle(fontSize: 12)),
),
ShadRadio(
value: true,
label: const Text('Custom time', style: TextStyle(fontSize: 12)),
),
],
),
if (useCustomTime) ...[
const SizedBox(height: 8),
ShadCard(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
child: Column(
children: [
Row(
children: [
Icon(
Icons.calendar_today,
size: 14,
color: ShadTheme.of(context).colorScheme.foreground,
),
const SizedBox(width: 8),
Expanded(
child: Text(
'Date: ${selectedDateTime.day}/${selectedDateTime.month}/${selectedDateTime.year}',
style: const TextStyle(fontSize: 12),
),
),
ShadButton.outline(
onPressed: () async {
final date = await showDatePicker(
context: context,
initialDate: selectedDateTime,
firstDate: DateTime.now().subtract(const Duration(days: 7)),
lastDate: DateTime.now(),
);
if (date != null) {
setState(() {
selectedDateTime = DateTime(
date.year,
date.month,
date.day,
selectedDateTime.hour,
selectedDateTime.minute,
);
});
}
},
child: const Text('Change', style: TextStyle(fontSize: 10)),
),
],
),
const SizedBox(height: 4),
Row(
children: [
Icon(
Icons.access_time,
size: 14,
color: ShadTheme.of(context).colorScheme.foreground,
),
const SizedBox(width: 8),
Expanded(
child: Text(
'Time: ${selectedDateTime.hour.toString().padLeft(2, '0')}:${selectedDateTime.minute.toString().padLeft(2, '0')}',
style: const TextStyle(fontSize: 12),
),
),
ShadButton.outline(
onPressed: () async {
final time = await showTimePicker(
context: context,
initialTime: TimeOfDay.fromDateTime(selectedDateTime),
);
if (time != null) {
setState(() {
selectedDateTime = DateTime(
selectedDateTime.year,
selectedDateTime.month,
selectedDateTime.day,
time.hour,
time.minute,
);
});
}
},
child: const Text('Change', style: TextStyle(fontSize: 10)),
),
],
),
],
),
),
),
],
],
),
),
),
const SizedBox(height: 16),
],
ShadInput(
controller: notesController,
placeholder: const Text('Notes (optional)'),
maxLines: 2,
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
ShadButton.outline(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
const SizedBox(width: 8),
ShadButton(
onPressed: () {
final unitsTaken = double.tryParse(unitsController.text) ?? supplement.numberOfUnits.toDouble();
final totalDosageTaken = 0.0;
context.read<SupplementProvider>().recordIntake(
supplement.id!,
totalDosageTaken,
unitsTaken: unitsTaken,
notes: notesController.text.isNotEmpty ? notesController.text : null,
takenAt: hideTime ? null : (useCustomTime ? selectedDateTime : null),
);
Navigator.of(context).pop();
ShadToaster.of(context).show(
ShadToast(
title: Text('${supplement.name} recorded!'),
),
);
},
child: const Text('Record'),
),
],
),
],
),
),
);
},
),
);
}

View File

@@ -7,6 +7,7 @@ class InfoChip extends StatelessWidget {
final bool fullWidth; final bool fullWidth;
const InfoChip({ const InfoChip({
super.key,
required this.icon, required this.icon,
required this.label, required this.label,
required this.context, required this.context,
@@ -19,7 +20,7 @@ class InfoChip extends StatelessWidget {
width: fullWidth ? double.infinity : null, width: fullWidth ? double.infinity : null,
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6), padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.4), color: Theme.of(context).colorScheme.surfaceContainerHighest.withValues(alpha: 0.4),
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),
), ),
child: Row( child: Row(

View File

@@ -1,5 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:popover/popover.dart';
import '../models/supplement.dart'; import '../models/supplement.dart';
import '../providers/supplement_provider.dart'; import '../providers/supplement_provider.dart';
@@ -10,6 +12,8 @@ class SupplementCard extends StatefulWidget {
final VoidCallback onDelete; final VoidCallback onDelete;
final VoidCallback onArchive; final VoidCallback onArchive;
final VoidCallback onDuplicate; final VoidCallback onDuplicate;
final VoidCallback? onUndoLastTaken;
final bool showCompletionStatus;
const SupplementCard({ const SupplementCard({
super.key, super.key,
@@ -19,6 +23,8 @@ class SupplementCard extends StatefulWidget {
required this.onDelete, required this.onDelete,
required this.onArchive, required this.onArchive,
required this.onDuplicate, required this.onDuplicate,
this.onUndoLastTaken,
this.showCompletionStatus = true,
}); });
@override @override
@@ -28,16 +34,48 @@ class SupplementCard extends StatefulWidget {
class _SupplementCardState extends State<SupplementCard> { class _SupplementCardState extends State<SupplementCard> {
bool _isExpanded = false; bool _isExpanded = false;
Widget _buildPopoverItem({
required IconData icon,
required String label,
required VoidCallback onTap,
Color? color,
}) {
return InkWell(
onTap: onTap,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
child: Row(
children: [
Icon(
icon,
size: 18,
color: color ?? Theme.of(context).colorScheme.onSurface,
),
const SizedBox(width: 12),
Text(
label,
style: TextStyle(
fontSize: 14,
color: color ?? Theme.of(context).colorScheme.onSurface,
fontWeight: FontWeight.w500,
),
),
],
),
),
);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Consumer<SupplementProvider>( return Consumer<SupplementProvider>(
builder: (context, provider, child) { builder: (context, provider, child) {
final bool isTakenToday = provider.hasBeenTakenToday(widget.supplement.id!); final bool isTakenToday = widget.showCompletionStatus ? provider.hasBeenTakenToday(widget.supplement.id!) : false;
final int todayIntakeCount = provider.getTodayIntakeCount(widget.supplement.id!); final int todayIntakeCount = widget.showCompletionStatus ? provider.getTodayIntakeCount(widget.supplement.id!) : 0;
final bool isCompletelyTaken = todayIntakeCount >= widget.supplement.frequencyPerDay; final bool isCompletelyTaken = widget.showCompletionStatus ? todayIntakeCount >= widget.supplement.frequencyPerDay : false;
// Get today's intake times for this supplement // Get today's intake times for this supplement (only if showing completion status)
final todayIntakes = provider.todayIntakes final todayIntakes = widget.showCompletionStatus ? provider.todayIntakes
.where((intake) => intake['supplement_id'] == widget.supplement.id) .where((intake) => intake['supplement_id'] == widget.supplement.id)
.map((intake) { .map((intake) {
final takenAt = DateTime.parse(intake['takenAt']); final takenAt = DateTime.parse(intake['takenAt']);
@@ -46,447 +84,227 @@ class _SupplementCardState extends State<SupplementCard> {
'time': '${takenAt.hour.toString().padLeft(2, '0')}:${takenAt.minute.toString().padLeft(2, '0')}', 'time': '${takenAt.hour.toString().padLeft(2, '0')}:${takenAt.minute.toString().padLeft(2, '0')}',
'units': unitsTaken is int ? unitsTaken.toDouble() : unitsTaken as double, 'units': unitsTaken is int ? unitsTaken.toDouble() : unitsTaken as double,
}; };
}).toList(); }).toList() : [];
return Card( return Card(
margin: const EdgeInsets.only(bottom: 16), margin: const EdgeInsets.only(bottom: 8),
elevation: 3, elevation: 2,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
child: Container( child: InkWell(
decoration: BoxDecoration( onTap: () {
borderRadius: BorderRadius.circular(16), showPopover(
color: isCompletelyTaken context: context,
? Theme.of(context).colorScheme.surface bodyBuilder: (context) => Container(
: isTakenToday constraints: const BoxConstraints(maxWidth: 200),
? Theme.of(context).colorScheme.secondaryContainer
: Theme.of(context).colorScheme.surface,
border: Border.all(
color: isCompletelyTaken
? Colors.green.shade600
: isTakenToday
? Theme.of(context).colorScheme.secondary
: Theme.of(context).colorScheme.outline.withOpacity(0.2),
width: 1.5,
),
),
child: Theme(
data: Theme.of(context).copyWith(
dividerColor: Colors.transparent,
),
child: ExpansionTile(
initiallyExpanded: _isExpanded,
onExpansionChanged: (expanded) {
setState(() {
_isExpanded = expanded;
});
},
tilePadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
childrenPadding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
leading: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration( decoration: BoxDecoration(
color: isCompletelyTaken color: Theme.of(context).colorScheme.surface,
? Colors.green.shade500 borderRadius: BorderRadius.circular(12),
: isTakenToday border: Border.all(
? Theme.of(context).colorScheme.secondary color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.3),
: Theme.of(context).colorScheme.primary.withOpacity(0.1), width: 1,
shape: BoxShape.circle,
),
child: Icon(
isCompletelyTaken
? Icons.check_circle
: isTakenToday
? Icons.schedule
: Icons.medication,
color: isCompletelyTaken
? Theme.of(context).colorScheme.inverseSurface
: isTakenToday
? Theme.of(context).colorScheme.onSecondary
: Theme.of(context).colorScheme.primary,
size: 20,
),
),
title: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.supplement.name,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: isCompletelyTaken
? Theme.of(context).colorScheme.inverseSurface
: isTakenToday
? Theme.of(context).colorScheme.onSecondaryContainer
: Theme.of(context).colorScheme.onSurface,
),
),
if (widget.supplement.brand != null && widget.supplement.brand!.isNotEmpty)
Text(
widget.supplement.brand!,
style: TextStyle(
fontSize: 12,
color: isCompletelyTaken
? Colors.green.shade200
: isTakenToday
? Theme.of(context).colorScheme.secondary
: Theme.of(context).colorScheme.primary,
fontWeight: FontWeight.w500,
),
),
],
),
), ),
// Status badge and take button in collapsed view boxShadow: [
if (!_isExpanded) ...[ BoxShadow(
if (isCompletelyTaken) color: Theme.of(context).colorScheme.shadow.withValues(alpha: 0.1),
Container( blurRadius: 8,
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4), offset: const Offset(0, 2),
decoration: BoxDecoration( ),
color: Colors.green.shade500,
borderRadius: BorderRadius.circular(12),
),
child: Text(
'Complete',
style: TextStyle(
color: Theme.of(context).colorScheme.inverseSurface,
fontSize: 10,
fontWeight: FontWeight.bold,
),
),
)
else ...[
if (isTakenToday)
Container(
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
margin: const EdgeInsets.only(right: 8),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.secondary,
borderRadius: BorderRadius.circular(12),
),
child: Text(
'$todayIntakeCount/${widget.supplement.frequencyPerDay}',
style: TextStyle(
color: Theme.of(context).colorScheme.onSecondary,
fontSize: 10,
fontWeight: FontWeight.bold,
),
),
),
ElevatedButton(
onPressed: widget.onTake,
style: ElevatedButton.styleFrom(
backgroundColor: isCompletelyTaken
? Colors.green.shade500
: Theme.of(context).colorScheme.primary,
foregroundColor: Theme.of(context).colorScheme.inverseSurface,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
minimumSize: const Size(60, 32),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text(
isCompletelyTaken ? '' : 'Take',
style: const TextStyle(fontSize: 12, fontWeight: FontWeight.w600),
),
),
],
], ],
],
),
trailing: PopupMenuButton(
padding: EdgeInsets.zero,
icon: Icon(
Icons.more_vert,
color: isCompletelyTaken
? Theme.of(context).colorScheme.inverseSurface
: Theme.of(context).colorScheme.onSurfaceVariant,
), ),
onSelected: (value) { child: Column(
switch (value) { mainAxisSize: MainAxisSize.min,
case 'edit': children: [
widget.onEdit(); if (widget.onUndoLastTaken != null && isTakenToday)
break; _buildPopoverItem(
case 'duplicate': icon: Icons.undo,
widget.onDuplicate(); label: 'Undo Last Taken',
break; onTap: () {
case 'archive': Navigator.of(context).pop();
widget.onArchive(); widget.onUndoLastTaken!();
break; },
case 'delete': color: Colors.orange,
widget.onDelete();
break;
}
},
itemBuilder: (context) => [
if (isTakenToday)
PopupMenuItem(
value: 'take',
onTap: widget.onTake,
child: const Row(
children: [
Icon(Icons.add_circle_outline),
SizedBox(width: 8),
Text('Take Again'),
],
), ),
_buildPopoverItem(
icon: Icons.edit,
label: 'Edit',
onTap: () {
Navigator.of(context).pop();
widget.onEdit();
},
), ),
const PopupMenuItem( _buildPopoverItem(
value: 'edit', icon: Icons.copy,
child: Row( label: 'Duplicate',
children: [ onTap: () {
Icon(Icons.edit), Navigator.of(context).pop();
SizedBox(width: 8), widget.onDuplicate();
Text('Edit'), },
],
), ),
), _buildPopoverItem(
const PopupMenuItem( icon: Icons.archive,
value: 'duplicate', label: 'Archive',
child: Row( color: Colors.orange,
children: [ onTap: () {
Icon(Icons.copy), Navigator.of(context).pop();
SizedBox(width: 8), widget.onArchive();
Text('Duplicate'), },
],
), ),
), _buildPopoverItem(
const PopupMenuItem( icon: Icons.delete,
value: 'archive', label: 'Delete',
child: Row( color: Colors.red,
children: [ onTap: () {
Icon(Icons.archive, color: Colors.orange), Navigator.of(context).pop();
SizedBox(width: 8), widget.onDelete();
Text('Archive'), },
],
), ),
), ],
const PopupMenuItem( ),
value: 'delete',
child: Row(
children: [
Icon(Icons.delete, color: Colors.red),
SizedBox(width: 8),
Text('Delete', style: TextStyle(color: Colors.red)),
],
),
),
],
), ),
direction: PopoverDirection.bottom,
width: 180,
height: null,
arrowHeight: 0,
arrowWidth: 0,
backgroundColor: Colors.transparent,
shadow: const [],
);
},
borderRadius: BorderRadius.circular(12),
splashColor: Theme.of(context).colorScheme.primary.withValues(alpha: 0.4),
highlightColor: Theme.of(context).colorScheme.primary.withValues(alpha: 0.3),
child: AnimatedContainer(
duration: const Duration(milliseconds: 150),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
color: widget.showCompletionStatus ? (isCompletelyTaken
? Theme.of(context).colorScheme.surface
: isTakenToday
? Theme.of(context).colorScheme.secondaryContainer
: Theme.of(context).colorScheme.surface) : Theme.of(context).colorScheme.surface,
border: Border.all(
color: widget.showCompletionStatus ? (isCompletelyTaken
? Colors.green.shade600
: isTakenToday
? Theme.of(context).colorScheme.secondary
: Theme.of(context).colorScheme.outline.withValues(alpha: 0.2)) : Theme.of(context).colorScheme.outline.withValues(alpha: 0.2),
width: 1,
),
),
child: Row(
children: [ children: [
// Today's intake times (if any) - only show in expanded view
if (todayIntakes.isNotEmpty) ...[
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: isCompletelyTaken
? Colors.green.shade700.withOpacity(0.8)
: Theme.of(context).colorScheme.secondaryContainer.withOpacity(0.7),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: isCompletelyTaken
? Colors.green.shade500
: Theme.of(context).colorScheme.secondary,
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(
Icons.check_circle_outline,
size: 16,
color: isCompletelyTaken
? Colors.green.shade200
: Theme.of(context).colorScheme.onSecondaryContainer,
),
const SizedBox(width: 6),
Text(
'Taken today:',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: isCompletelyTaken
? Colors.green.shade200
: Theme.of(context).colorScheme.onSecondaryContainer,
),
),
],
),
const SizedBox(height: 6),
Wrap(
spacing: 8,
runSpacing: 4,
children: todayIntakes.map((intake) {
final units = intake['units'] as double;
final unitsText = units == 1.0
? '${widget.supplement.unitType}'
: '${units.toStringAsFixed(units % 1 == 0 ? 0 : 1)} ${widget.supplement.unitType}';
return Container(
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
decoration: BoxDecoration(
color: isCompletelyTaken
? Colors.green.shade600
: Theme.of(context).colorScheme.secondary,
borderRadius: BorderRadius.circular(6),
),
child: Text(
'${intake['time']}$unitsText',
style: TextStyle(
fontSize: 10,
fontWeight: FontWeight.w500,
color: isCompletelyTaken
? Colors.white
: Theme.of(context).colorScheme.onSecondary,
),
),
);
}).toList(),
),
],
),
),
const SizedBox(height: 16),
],
// Ingredients section
Container( Container(
padding: const EdgeInsets.all(12), padding: const EdgeInsets.all(6),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.3), color: widget.showCompletionStatus ? (isCompletelyTaken
borderRadius: BorderRadius.circular(12), ? Colors.green.shade500
: isTakenToday
? Theme.of(context).colorScheme.secondary
: Theme.of(context).colorScheme.primary.withValues(alpha: 0.1)) : Theme.of(context).colorScheme.primary.withValues(alpha: 0.1),
shape: BoxShape.circle,
), ),
child: Icon(
widget.showCompletionStatus ? (isCompletelyTaken
? Icons.check_circle
: isTakenToday
? Icons.schedule
: Icons.medication) : Icons.medication,
color: widget.showCompletionStatus ? (isCompletelyTaken
? Theme.of(context).colorScheme.inverseSurface
: isTakenToday
? Theme.of(context).colorScheme.onSecondary
: Theme.of(context).colorScheme.primary) : Theme.of(context).colorScheme.primary,
size: 18,
),
),
const SizedBox(width: 12),
Expanded(
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text( Row(
'Ingredients per ${widget.supplement.unitType}:', children: [
style: TextStyle( Expanded(
fontSize: 12,
fontWeight: FontWeight.w600,
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 8),
Wrap(
spacing: 8,
runSpacing: 4,
children: widget.supplement.ingredients.map((ingredient) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary.withOpacity(0.1),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Theme.of(context).colorScheme.primary.withOpacity(0.3),
),
),
child: Text( child: Text(
'${ingredient.name} ${ingredient.amount}${ingredient.unit}', widget.supplement.name,
style: TextStyle( style: TextStyle(
fontSize: 11, fontSize: 14,
fontWeight: FontWeight.w500, fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.primary, color: widget.showCompletionStatus ? (isCompletelyTaken
? Theme.of(context).colorScheme.inverseSurface
: isTakenToday
? Theme.of(context).colorScheme.onSecondaryContainer
: Theme.of(context).colorScheme.onSurface) : Theme.of(context).colorScheme.onSurface,
), ),
), ),
); ),
}).toList(), if (widget.showCompletionStatus && isCompletelyTaken) ...[
const SizedBox(width: 8),
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
decoration: BoxDecoration(
color: Colors.green.shade500,
borderRadius: BorderRadius.circular(12),
),
child: Text(
'Complete',
style: TextStyle(
color: Theme.of(context).colorScheme.inverseSurface,
fontSize: 10,
fontWeight: FontWeight.bold,
),
),
),
],
],
),
if (widget.supplement.brand != null && widget.supplement.brand!.isNotEmpty)
Text(
widget.supplement.brand!,
style: TextStyle(
fontSize: 11,
color: widget.showCompletionStatus ? (isCompletelyTaken
? Colors.green.shade200
: isTakenToday
? Theme.of(context).colorScheme.secondary
: Theme.of(context).colorScheme.primary) : Theme.of(context).colorScheme.primary,
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 4),
Row(
children: [
Text(
'${widget.supplement.frequencyPerDay}x daily • ${widget.supplement.numberOfUnits} ${widget.supplement.unitType}',
style: TextStyle(
fontSize: 11,
color: ShadTheme.of(context).colorScheme.foreground.withValues(alpha: 0.7),
),
),
if (widget.showCompletionStatus && isTakenToday && !isCompletelyTaken) ...[
const SizedBox(width: 8),
Container(
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.secondary,
borderRadius: BorderRadius.circular(8),
),
child: Text(
'$todayIntakeCount/${widget.supplement.frequencyPerDay}',
style: TextStyle(
color: Theme.of(context).colorScheme.onSecondary,
fontSize: 9,
fontWeight: FontWeight.bold,
),
),
),
],
],
), ),
], ],
), ),
), ),
const SizedBox(height: 12),
// Schedule and dosage info
Row(
children: [
Expanded(
child: _InfoChip(
icon: Icons.schedule,
label: '${widget.supplement.frequencyPerDay}x daily',
context: context,
),
),
const SizedBox(width: 8),
Expanded(
child: _InfoChip(
icon: Icons.medication,
label: '${widget.supplement.numberOfUnits} ${widget.supplement.unitType}',
context: context,
),
),
],
),
if (widget.supplement.reminderTimes.isNotEmpty) ...[
const SizedBox(height: 8),
_InfoChip(
icon: Icons.notifications,
label: 'Reminders: ${widget.supplement.reminderTimes.join(', ')}',
context: context,
fullWidth: true,
),
],
if (widget.supplement.notes != null && widget.supplement.notes!.isNotEmpty) ...[
const SizedBox(height: 8),
Container(
width: double.infinity,
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.2),
borderRadius: BorderRadius.circular(8),
),
child: Text(
widget.supplement.notes!,
style: TextStyle(
fontSize: 12,
color: Theme.of(context).colorScheme.onSurfaceVariant,
fontStyle: FontStyle.italic,
),
),
),
],
const SizedBox(height: 16),
// Take button
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: widget.onTake,
icon: Icon(
isCompletelyTaken ? Icons.check_circle : Icons.medication,
size: 18,
),
label: Text(
isCompletelyTaken
? 'All doses taken today'
: isTakenToday
? 'Take next dose'
: 'Take supplement',
style: const TextStyle(fontWeight: FontWeight.w600),
),
style: ElevatedButton.styleFrom(
backgroundColor: isCompletelyTaken
? Colors.green.shade500
: Theme.of(context).colorScheme.primary,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
elevation: isCompletelyTaken ? 0 : 2,
),
),
),
], ],
), ),
), ),
@@ -516,7 +334,7 @@ class _InfoChip extends StatelessWidget {
width: fullWidth ? double.infinity : null, width: fullWidth ? double.infinity : null,
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6), padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.4), color: Theme.of(context).colorScheme.surfaceContainerHighest.withValues(alpha: 0.4),
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),
), ),
child: Row( child: Row(
@@ -544,3 +362,4 @@ class _InfoChip extends StatelessWidget {
); );
} }
} }

View File

@@ -1,6 +1,14 @@
# Generated by pub # Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile # See https://dart.dev/tools/pub/glossary#lockfile
packages: packages:
alarm:
dependency: "direct main"
description:
name: alarm
sha256: "1eef91f0b803a2370137e0dada9c7c24cc31edf4f1c30b06442dcf486cc192e0"
url: "https://pub.dev"
source: hosted
version: "5.1.4"
args: args:
dependency: transitive dependency: transitive
description: description:
@@ -25,6 +33,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.2" version: "2.1.2"
boxy:
dependency: transitive
description:
name: boxy
sha256: "71af0cd1bf7889c09787f26219a345aa4f38ccb98384c8ec24189e4d8e746005"
url: "https://pub.dev"
source: hosted
version: "2.2.1"
characters: characters:
dependency: transitive dependency: transitive
description: description:
@@ -113,6 +129,30 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.1" version: "2.1.1"
equatable:
dependency: transitive
description:
name: equatable
sha256: "567c64b3cb4cf82397aac55f4f0cbd3ca20d77c6c03bedbc4ceaddc08904aef7"
url: "https://pub.dev"
source: hosted
version: "2.0.7"
extended_image:
dependency: transitive
description:
name: extended_image
sha256: f6cbb1d798f51262ed1a3d93b4f1f2aa0d76128df39af18ecb77fa740f88b2e0
url: "https://pub.dev"
source: hosted
version: "10.0.1"
extended_image_library:
dependency: transitive
description:
name: extended_image_library
sha256: "1f9a24d3a00c2633891c6a7b5cab2807999eb2d5b597e5133b63f49d113811fe"
url: "https://pub.dev"
source: hosted
version: "5.0.1"
fake_async: fake_async:
dependency: transitive dependency: transitive
description: description:
@@ -150,6 +190,22 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_animate:
dependency: transitive
description:
name: flutter_animate
sha256: "7befe2d3252728afb77aecaaea1dec88a89d35b9b1d2eea6d04479e8af9117b5"
url: "https://pub.dev"
source: hosted
version: "4.5.2"
flutter_fgbg:
dependency: transitive
description:
name: flutter_fgbg
sha256: eb6da9b2047372566a6e17b505975fe5bace94af01f6fc825c4b6f81baa6c447
url: "https://pub.dev"
source: hosted
version: "0.7.1"
flutter_lints: flutter_lints:
dependency: "direct dev" dependency: "direct dev"
description: description:
@@ -190,6 +246,11 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.0.2" version: "1.0.2"
flutter_localizations:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
flutter_secure_storage: flutter_secure_storage:
dependency: "direct main" dependency: "direct main"
description: description:
@@ -239,6 +300,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "4.0.0" version: "4.0.0"
flutter_shaders:
dependency: transitive
description:
name: flutter_shaders
sha256: "34794acadd8275d971e02df03afee3dee0f98dbfb8c4837082ad0034f612a3e2"
url: "https://pub.dev"
source: hosted
version: "0.1.3"
flutter_svg:
dependency: transitive
description:
name: flutter_svg
sha256: cd57f7969b4679317c17af6fd16ee233c1e60a82ed209d8a475c54fd6fd6f845
url: "https://pub.dev"
source: hosted
version: "2.2.0"
flutter_test: flutter_test:
dependency: "direct dev" dependency: "direct dev"
description: flutter description: flutter
@@ -257,6 +334,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.5.0" version: "1.5.0"
http_client_helper:
dependency: transitive
description:
name: http_client_helper
sha256: "8a9127650734da86b5c73760de2b404494c968a3fd55602045ffec789dac3cb1"
url: "https://pub.dev"
source: hosted
version: "3.0.0"
http_parser: http_parser:
dependency: transitive dependency: transitive
description: description:
@@ -273,6 +358,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.20.2" version: "0.20.2"
js:
dependency: transitive
description:
name: js
sha256: "53385261521cc4a0c4658fd0ad07a7d14591cf8fc33abbceae306ddb974888dc"
url: "https://pub.dev"
source: hosted
version: "0.7.2"
json_annotation:
dependency: transitive
description:
name: json_annotation
sha256: "1ce844379ca14835a50d2f019a3099f419082cfdd231cd86a142af94dd5c6bb1"
url: "https://pub.dev"
source: hosted
version: "4.9.0"
leak_tracker: leak_tracker:
dependency: transitive dependency: transitive
description: description:
@@ -305,6 +406,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "6.0.0" version: "6.0.0"
logging:
dependency: transitive
description:
name: logging
sha256: c8245ada5f1717ed44271ed1c26b8ce85ca3228fd2ffdb75468ab01979309d61
url: "https://pub.dev"
source: hosted
version: "1.3.0"
lucide_icons_flutter:
dependency: transitive
description:
name: lucide_icons_flutter
sha256: c88e3611c0aa272ca2f2aa263662174ae4996f5e3ee1c300021514df230b6588
url: "https://pub.dev"
source: hosted
version: "3.0.9"
matcher: matcher:
dependency: transitive dependency: transitive
description: description:
@@ -361,6 +478,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.9.1" version: "1.9.1"
path_parsing:
dependency: transitive
description:
name: path_parsing
sha256: "883402936929eac138ee0a45da5b0f2c80f89913e6dc3bf77eb65b84b409c6ca"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
path_provider: path_provider:
dependency: transitive dependency: transitive
description: description:
@@ -433,6 +558,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.8" version: "2.1.8"
popover:
dependency: "direct main"
description:
name: popover
sha256: "0606f3e10f92fc0459f5c52fd917738c29e7552323b28694d50c2d3312d0e1a2"
url: "https://pub.dev"
source: hosted
version: "0.3.1"
provider: provider:
dependency: "direct main" dependency: "direct main"
description: description:
@@ -441,6 +574,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "6.1.5+1" version: "6.1.5+1"
rxdart:
dependency: transitive
description:
name: rxdart
sha256: "5c3004a4a8dbb94bd4bf5412a4def4acdaa12e12f269737a5751369e12d1a962"
url: "https://pub.dev"
source: hosted
version: "0.28.0"
shadcn_ui:
dependency: "direct main"
description:
name: shadcn_ui
sha256: "628d1a7f36e4c764dae3b86b38abb086adf39ccea0073960f481777d1880f90d"
url: "https://pub.dev"
source: hosted
version: "0.29.2"
shared_preferences: shared_preferences:
dependency: "direct main" dependency: "direct main"
description: description:
@@ -630,6 +779,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.10.1" version: "0.10.1"
two_dimensional_scrollables:
dependency: transitive
description:
name: two_dimensional_scrollables
sha256: "0f77ecb96596f2f82eec2b0a8e60d9305c58315557da9fa3b610c7dbf5ded621"
url: "https://pub.dev"
source: hosted
version: "0.3.7"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:
@@ -638,6 +795,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.4.0" version: "1.4.0"
universal_image:
dependency: transitive
description:
name: universal_image
sha256: ef47a4a002158cf0b36ed3b7605af132d2476cc42703e41b8067d3603705c40d
url: "https://pub.dev"
source: hosted
version: "1.0.11"
url_launcher: url_launcher:
dependency: "direct main" dependency: "direct main"
description: description:
@@ -710,6 +875,30 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "4.5.1" version: "4.5.1"
vector_graphics:
dependency: transitive
description:
name: vector_graphics
sha256: a4f059dc26fc8295b5921376600a194c4ec7d55e72f2fe4c7d2831e103d461e6
url: "https://pub.dev"
source: hosted
version: "1.1.19"
vector_graphics_codec:
dependency: transitive
description:
name: vector_graphics_codec
sha256: "99fd9fbd34d9f9a32efd7b6a6aae14125d8237b10403b422a6a6dfeac2806146"
url: "https://pub.dev"
source: hosted
version: "1.1.13"
vector_graphics_compiler:
dependency: transitive
description:
name: vector_graphics_compiler
sha256: d354a7ec6931e6047785f4db12a1f61ec3d43b207fc0790f863818543f8ff0dc
url: "https://pub.dev"
source: hosted
version: "1.1.19"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:
@@ -768,4 +957,4 @@ packages:
version: "6.6.1" version: "6.6.1"
sdks: sdks:
dart: ">=3.9.0 <4.0.0" dart: ">=3.9.0 <4.0.0"
flutter: ">=3.29.0" flutter: ">=3.32.0"

View File

@@ -1,7 +1,7 @@
name: supplements name: supplements
description: "A supplement tracking app for managing your daily supplements" description: "A supplement tracking app for managing your daily supplements"
publish_to: "none" publish_to: "none"
version: 1.0.6+28082025 version: 1.0.9+31082025
environment: environment:
sdk: ^3.9.0 sdk: ^3.9.0
@@ -29,6 +29,10 @@ dependencies:
# Date time handling # Date time handling
intl: ^0.20.2 intl: ^0.20.2
# UI components
shadcn_ui: ^0.29.2
popover: ^0.3.0
# WebDAV sync functionality # WebDAV sync functionality
webdav_client: ^1.2.2 webdav_client: ^1.2.2
connectivity_plus: ^6.1.5 connectivity_plus: ^6.1.5
@@ -36,6 +40,7 @@ dependencies:
uuid: ^4.5.1 uuid: ^4.5.1
crypto: ^3.0.6 crypto: ^3.0.6
url_launcher: ^6.3.2 url_launcher: ^6.3.2
alarm: ^5.1.4
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
@@ -53,6 +58,3 @@ dependency_overrides:
flutter: flutter:
uses-material-design: true uses-material-design: true
assets:
- assets/canada_health.json