Skip to content

Commit 7fc72f1

Browse files
committed
➕ device status toggle function
1 parent 1cef9a1 commit 7fc72f1

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

lib/api/check_server_status.dart

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/api/status_sync.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'package:http/http.dart' as http;
2+
import 'package:nix/utils/token_storage.dart';
3+
import 'package:nix/utils/appdata_storage.dart';
4+
5+
Future<bool> getServerStatus() async {
6+
final response = await http.get(
7+
Uri.parse('https://quiet-pup-summary.ngrok-free.app/ping'),
8+
);
9+
10+
return response.statusCode == 200;
11+
}
12+
13+
Future<http.Response> toggleDeviceStatus() async {
14+
final token = await TokenStorage.getAccessToken();
15+
final deviceId = await AppDataStorage.getDeviceUID();
16+
17+
return http.get(
18+
Uri.parse('https://quiet-pup-summary.ngrok-free.app/api/v1/device/manage/toggle-status?uid=$deviceId'),
19+
headers: <String, String>{
20+
'Content-Type': 'application/json; charset=UTF-8',
21+
'Authorization': 'Bearer $token',
22+
},
23+
);
24+
}

lib/screens/dashboard_screen.dart

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:convert';
23
import 'package:flutter/material.dart';
34

45
import '../widgets/custom_button.dart';
@@ -8,7 +9,7 @@ import '../utils/appdata_storage.dart';
89
import '../utils/app_colors.dart';
910
import '../utils/responsive.dart';
1011

11-
import '../api/check_server_status.dart';
12+
import '../api/status_sync.dart';
1213
import '../api/logout_device.dart';
1314

1415
class DashboardScreen extends StatefulWidget {
@@ -26,9 +27,11 @@ class _DashboardScreenState extends State<DashboardScreen> {
2627
@override
2728
void initState() {
2829
super.initState();
29-
_updateStatuses();
30+
_updateDeviceStatus();
31+
_updateServerStatus();
3032
_timer = Timer.periodic(const Duration(seconds: 30), (timer) {
31-
_updateStatuses();
33+
_updateDeviceStatus();
34+
_updateServerStatus();
3235
});
3336
}
3437

@@ -51,7 +54,7 @@ class _DashboardScreenState extends State<DashboardScreen> {
5154
}
5255
}
5356

54-
Future<void> _updateStatuses() async {
57+
Future<void> _updateDeviceStatus() async {
5558
final status = await AppDataStorage.getDeviceStatus();
5659
setState(() {
5760
deviceStatus = (status ?? true) ? 'Online' : 'Offline';
@@ -61,8 +64,6 @@ class _DashboardScreenState extends State<DashboardScreen> {
6164
if (status == null) {
6265
await AppDataStorage.setDeviceStatus(true);
6366
}
64-
65-
await _updateServerStatus();
6667
}
6768

6869
@override
@@ -97,7 +98,9 @@ class _DashboardScreenState extends State<DashboardScreen> {
9798
text: serverStatus,
9899
style: TextStyle(
99100
fontSize: Responsive.sp(context, 24),
100-
color: serverStatus == 'Online'? Colors.green : Colors.redAccent,
101+
color: serverStatus == 'Online'
102+
? Colors.green
103+
: Colors.redAccent,
101104
),
102105
),
103106
],
@@ -114,7 +117,9 @@ class _DashboardScreenState extends State<DashboardScreen> {
114117
text: deviceStatus,
115118
style: TextStyle(
116119
fontSize: Responsive.sp(context, 24),
117-
color: deviceStatus == 'Online'? Colors.green : Colors.redAccent,
120+
color: deviceStatus == 'Online'
121+
? Colors.green
122+
: Colors.redAccent,
118123
),
119124
),
120125
],
@@ -134,10 +139,16 @@ class _DashboardScreenState extends State<DashboardScreen> {
134139
SizedBox(width: Responsive.width(context) * 0.04),
135140
Expanded(
136141
child: CustomButton(
137-
onPressed: () => _changeStatus(deviceStatus),
138-
text: deviceStatus == 'Online'? 'Go Offline' : 'Go Online',
139-
backgroundColor: deviceStatus == 'Online'? Colors.red : Colors.lightGreen,
140-
icon: deviceStatus == 'Online'? Icons.offline_bolt : Icons.online_prediction,
142+
onPressed: () => _changeDeviceStatus(),
143+
text: deviceStatus == 'Online'
144+
? 'Go Offline'
145+
: 'Go Online',
146+
backgroundColor: deviceStatus == 'Online'
147+
? Colors.red
148+
: Colors.lightGreen,
149+
icon: deviceStatus == 'Online'
150+
? Icons.offline_bolt
151+
: Icons.online_prediction,
141152
),
142153
),
143154
],
@@ -158,12 +169,19 @@ class _DashboardScreenState extends State<DashboardScreen> {
158169
);
159170
}
160171

161-
void _refreshConnection() {
162-
_updateStatuses();
172+
void _refreshConnection() async {
173+
await _updateServerStatus();
174+
_updateServerStatus();
163175
}
164176

165-
void _changeStatus(String currentState) {
166-
_updateStatuses();
177+
void _changeDeviceStatus() async {
178+
final res = await toggleDeviceStatus();
179+
final resData = jsonDecode(res.body);
180+
181+
if (res.statusCode == 200) {
182+
AppDataStorage.setDeviceStatus(resData['device_status']);
183+
}
184+
_updateDeviceStatus();
167185
}
168186

169187
void _showLogoutConfirmation(BuildContext context) {
@@ -209,13 +227,18 @@ class _DashboardScreenState extends State<DashboardScreen> {
209227
barrierColor: AppColors.cardBackground,
210228
builder: (context) => AlertDialog(
211229
title: const Text('Logout Error'),
212-
content: const Text('You were unable to logout. Most probably a server issue.'),
230+
content: const Text(
231+
'You were unable to logout. Most probably a server issue.',
232+
),
213233
actions: [
214234
TextButton(
215235
onPressed: () {
216236
Navigator.pop(context);
217237
},
218-
child: const Text('Stay Logged-In', style: TextStyle(color: Colors.green)),
238+
child: const Text(
239+
'Stay Logged-In',
240+
style: TextStyle(color: Colors.green),
241+
),
219242
),
220243
],
221244
),

0 commit comments

Comments
 (0)