Skip to content

Commit 2c306cc

Browse files
committed
Add dart format to precommit, end node status task if offline nodes = 0 or notificationsEnabled false, add notifications tab to registered screen
1 parent 42891f3 commit 2c306cc

8 files changed

Lines changed: 86 additions & 50 deletions

File tree

app/lib/jrouter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class JRouter {
8888
view: await Council().widget(),
8989
),
9090
app: Dao()),
91-
AppInfo(
91+
AppInfo(
9292
route: Route(
9393
path: '/notifications',
9494
name: 'Notifications',

app/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Future<void> main() async {
7171
),
7272
(String taskId) async {
7373
logger.i('[BackgroundFetch] Task: $taskId');
74-
await checkNodeStatus();
74+
await checkNodeStatus(taskId);
7575
BackgroundFetch.finish(taskId);
7676
},
7777
(String taskId) async {

app/lib/screens/main_screen.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ class _AppState extends State<MainScreen> {
211211
currentPin: null,
212212
)));
213213
}
214-
// await Navigator.push(context, MaterialPageRoute(builder: (context) => UnregisteredScreen()));
215214
await Navigator.of(context).pushReplacement(PageRouteBuilder(
216215
transitionDuration: const Duration(seconds: 1),
217216
pageBuilder: (_, __, ___) => HomeScreen(

app/lib/screens/notifications_screen.dart

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ class NotificationsScreen extends StatefulWidget {
99
}
1010

1111
class _NotificationsScreenState extends State<NotificationsScreen> {
12-
bool _notificationsEnabled = true;
12+
bool _nodeStatusNotificationEnabled = true;
1313

14-
static const String _notificationsEnabledKey = 'notificationsEnabled';
14+
static const String _nodeStatusNotificationEnabledKey =
15+
'nodeStatusNotificationEnabled';
1516

1617
@override
1718
void initState() {
@@ -22,13 +23,14 @@ class _NotificationsScreenState extends State<NotificationsScreen> {
2223
void _loadNotificationPreference() async {
2324
final prefs = await SharedPreferences.getInstance();
2425
setState(() {
25-
_notificationsEnabled = prefs.getBool(_notificationsEnabledKey) ?? true;
26+
_nodeStatusNotificationEnabled =
27+
prefs.getBool(_nodeStatusNotificationEnabledKey) ?? true;
2628
});
2729
}
2830

29-
void _saveNotificationPreference(bool newValue) async {
31+
void _setNodeStatusNotification(bool newValue) async {
3032
final prefs = await SharedPreferences.getInstance();
31-
await prefs.setBool(_notificationsEnabledKey, newValue);
33+
await prefs.setBool(_nodeStatusNotificationEnabledKey, newValue);
3234
}
3335

3436
@override
@@ -37,23 +39,16 @@ class _NotificationsScreenState extends State<NotificationsScreen> {
3739
padding: const EdgeInsets.symmetric(vertical: 16.0),
3840
child: Column(
3941
mainAxisAlignment: MainAxisAlignment.start,
40-
crossAxisAlignment:
41-
CrossAxisAlignment.stretch,
42+
crossAxisAlignment: CrossAxisAlignment.stretch,
4243
children: <Widget>[
4344
SwitchListTile(
44-
title: const Text('Enable Push Notifications'),
45-
value: _notificationsEnabled,
45+
title: const Text('Enable node status notifications'),
46+
value: _nodeStatusNotificationEnabled,
4647
onChanged: (bool newValue) {
4748
setState(() {
48-
_notificationsEnabled = newValue;
49+
_nodeStatusNotificationEnabled = newValue;
4950
});
50-
_saveNotificationPreference(newValue);
51-
52-
if (newValue) {
53-
print('Notifications enabled. Implement subscription logic.');
54-
} else {
55-
print('Notifications disabled. Implement unsubscription logic.');
56-
}
51+
_setNodeStatusNotification(newValue);
5752
},
5853
secondary: const Icon(Icons.notifications),
5954
),

app/lib/screens/registered_screen.dart

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,20 @@ import 'package:threebotlogin/widgets/home_card.dart';
44
import 'package:threebotlogin/widgets/home_logo.dart';
55

66
class RegisteredScreen extends StatefulWidget {
7-
static final RegisteredScreen _singleton = RegisteredScreen._internal();
7+
static final RegisteredScreen _singleton = const RegisteredScreen._internal();
88

99
factory RegisteredScreen() {
1010
return _singleton;
1111
}
1212

13-
RegisteredScreen._internal() {
14-
//init
15-
}
13+
const RegisteredScreen._internal();
1614

1715
@override
1816
State<RegisteredScreen> createState() => _RegisteredScreenState();
1917
}
2018

2119
class _RegisteredScreenState extends State<RegisteredScreen>
2220
with WidgetsBindingObserver {
23-
// We will treat this error as a singleton
24-
2521
bool showSettings = false;
2622
bool showPreference = false;
2723

@@ -31,6 +27,7 @@ class _RegisteredScreenState extends State<RegisteredScreen>
3127
body: SingleChildScrollView(
3228
child: Column(
3329
mainAxisAlignment: MainAxisAlignment.start,
30+
crossAxisAlignment: CrossAxisAlignment.stretch,
3431
children: <Widget>[
3532
SizedBox(
3633
height: MediaQuery.of(context).size.height * 0.3,
@@ -51,12 +48,12 @@ class _RegisteredScreenState extends State<RegisteredScreen>
5148
],
5249
),
5350
),
54-
Container(
55-
padding: const EdgeInsets.only(left: 10, right: 10, top: 50),
56-
height: MediaQuery.of(context).size.height * 0.6,
57-
width: MediaQuery.of(context).size.width,
51+
Padding(
52+
padding: const EdgeInsets.symmetric(horizontal: 10),
5853
child: Column(
5954
mainAxisAlignment: MainAxisAlignment.start,
55+
crossAxisAlignment: CrossAxisAlignment.center,
56+
mainAxisSize: MainAxisSize.min,
6057
children: [
6158
SizedBox(
6259
width: MediaQuery.of(context).size.width / 1.2,
@@ -76,7 +73,7 @@ class _RegisteredScreenState extends State<RegisteredScreen>
7673
]),
7774
),
7875
),
79-
const Spacer(),
76+
const SizedBox(height: 45),
8077
const Row(
8178
crossAxisAlignment: CrossAxisAlignment.center,
8279
mainAxisAlignment: MainAxisAlignment.center,
@@ -119,10 +116,26 @@ class _RegisteredScreenState extends State<RegisteredScreen>
119116
name: 'Settings', icon: Icons.settings, pageNumber: 6),
120117
],
121118
),
122-
const SizedBox(height: 40),
123119
const Row(
124-
children: [Spacer(), CrispChatbot(), SizedBox(width: 20)],
125-
)
120+
crossAxisAlignment: CrossAxisAlignment.center,
121+
mainAxisAlignment: MainAxisAlignment.center,
122+
children: [
123+
HomeCardWidget(
124+
name: 'Notification Settings',
125+
icon: Icons.notifications,
126+
pageNumber: 8,
127+
fullWidth: true,
128+
),
129+
],
130+
),
131+
const SizedBox(height: 70),
132+
const Align(
133+
alignment: Alignment.bottomRight,
134+
child: Padding(
135+
padding: EdgeInsets.only(right: 20.0),
136+
child: CrispChatbot(),
137+
),
138+
),
126139
],
127140
),
128141
)

app/lib/services/background_service.dart

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import 'package:background_fetch/background_fetch.dart';
22
import 'package:threebotlogin/services/nodes_check_service.dart';
33
import 'notification_service.dart';
4+
import 'package:shared_preferences/shared_preferences.dart';
5+
import 'package:threebotlogin/helpers/logger.dart';
6+
7+
const String _nodeStatusNotificationEnabledKey =
8+
'nodeStatusNotificationEnabled';
49

510
void backgroundFetchHeadlessTask(HeadlessTask task) async {
611
final String taskId = task.taskId;
@@ -10,14 +15,28 @@ void backgroundFetchHeadlessTask(HeadlessTask task) async {
1015
BackgroundFetch.finish(taskId);
1116
return;
1217
}
13-
await checkNodeStatus();
18+
final prefs = await SharedPreferences.getInstance();
19+
final bool notificationsEnabled =
20+
prefs.getBool(_nodeStatusNotificationEnabledKey) ?? true;
1421

15-
BackgroundFetch.finish(taskId);
22+
logger.i(
23+
'Background Fetch Headless Task: $taskId, Notifications Enabled: $notificationsEnabled');
24+
25+
if (!notificationsEnabled) {
26+
logger.i(
27+
'[BackgroundFetch] Node status notifications are disabled. Finishing task: $taskId');
28+
BackgroundFetch.finish(taskId);
29+
return;
30+
}
31+
await checkNodeStatus(taskId);
1632
}
1733

18-
Future<void> checkNodeStatus() async {
34+
Future<void> checkNodeStatus(String taskId) async {
1935
final offlineNodes = await NodeCheckService.pingNodesInBackground();
20-
if (offlineNodes.isEmpty) return;
36+
if (offlineNodes.isEmpty) {
37+
BackgroundFetch.finish(taskId);
38+
return;
39+
}
2140

2241
final now = DateTime.now().millisecondsSinceEpoch;
2342
final sevenDaysAgoTimestamp =
@@ -33,7 +52,10 @@ Future<void> checkNodeStatus() async {
3352
return downtime.inMinutes % checkInterval.inMinutes < 15;
3453
}).toList();
3554

36-
if (nodesToNotify.isEmpty) return;
55+
if (nodesToNotify.isEmpty) {
56+
BackgroundFetch.finish(taskId);
57+
return;
58+
}
3759

3860
const groupKey = 'offline_nodes';
3961
final StringBuffer bodyBuffer = StringBuffer();
@@ -54,6 +76,7 @@ Future<void> checkNodeStatus() async {
5476
body: bodyBuffer.toString().trim(),
5577
groupKey: groupKey,
5678
);
79+
BackgroundFetch.finish(taskId);
5780
}
5881

5982
Duration _getCheckInterval(Duration downtime) {

app/lib/widgets/home_card.dart

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class HomeCardWidget extends StatelessWidget {
3535
Container(
3636
padding: const EdgeInsets.all(10),
3737
height: size / 7,
38-
width: fullWidth ? size * 2 / 2.5 + 2 * margin : size / 2.5,
38+
width: fullWidth ? size * 0.8 : size / 2.5,
3939
child: Row(
4040
crossAxisAlignment: CrossAxisAlignment.center,
4141
mainAxisAlignment: MainAxisAlignment.center,
@@ -45,11 +45,16 @@ class HomeCardWidget extends StatelessWidget {
4545
color: Theme.of(context).colorScheme.onPrimaryContainer,
4646
),
4747
const SizedBox(width: 7),
48-
Text(
49-
name,
50-
style: Theme.of(context).textTheme.titleMedium!.copyWith(
51-
color: Theme.of(context).colorScheme.onPrimaryContainer,
52-
fontWeight: FontWeight.bold),
48+
Flexible(
49+
child: Text(
50+
name,
51+
style: Theme.of(context).textTheme.titleMedium!.copyWith(
52+
color:
53+
Theme.of(context).colorScheme.onPrimaryContainer,
54+
fontWeight: FontWeight.bold),
55+
overflow: TextOverflow.ellipsis,
56+
maxLines: 1,
57+
),
5358
)
5459
],
5560
),

git_hooks/pre-commit

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ fi
1313
STAGED_DART_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.dart$')
1414

1515
if [ -n "$STAGED_DART_FILES" ]; then
16+
echo "Formatting staged Dart files..."
17+
echo "$STAGED_DART_FILES" | xargs -n1 dart format
18+
1619
echo "Analyzing staged Dart files..."
1720
dart analyze --fatal-infos $STAGED_DART_FILES
1821
ANALYZE_EXIT_CODE=$?
1922

20-
if [ $ANALYZE_EXIT_CODE -eq 0 ]; then
21-
exit 0
23+
if [ $ANALYZE_EXIT_CODE -ne 0 ]; then
24+
echo "Applying fixes..."
25+
echo "$STAGED_DART_FILES" | xargs -n1 dart fix --apply
2226
fi
2327

24-
echo "Applying fixes..."
25-
echo "$STAGED_DART_FILES" | xargs -n1 dart fix --apply
26-
2728
echo "Re-adding fixed Dart files to the commit..."
2829
echo "$STAGED_DART_FILES" | xargs git add
2930
fi

0 commit comments

Comments
 (0)