Skip to content

Commit 2342748

Browse files
added auto_request support
1 parent 1695e67 commit 2342748

File tree

5 files changed

+217
-43
lines changed

5 files changed

+217
-43
lines changed

lib/home/chat_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'package:flutter/material.dart';
2-
import 'home_page.dart';
2+
import 'data.dart';
33
import 'package:http/http.dart' as http;
44
import 'dart:convert';
55
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

lib/home/data.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Video {
2+
final String title;
3+
final String videoId;
4+
final String videoDescription;
5+
6+
Video(
7+
{required this.title,
8+
required this.videoId,
9+
required this.videoDescription});
10+
11+
factory Video.fromJson(Map<String, dynamic> json) {
12+
return Video(
13+
title: json['title'].toString() ?? '',
14+
videoId: json['videoId'].toString() ?? '',
15+
videoDescription: json['videoDescription'].toString() ?? '',
16+
);
17+
}
18+
}
19+
20+
class Messages {
21+
final String role;
22+
final String content;
23+
24+
Messages({required this.role, required this.content});
25+
Map<String, dynamic> toJson() {
26+
return {
27+
'role': role,
28+
'content': content,
29+
};
30+
}
31+
}

lib/home/home_page.dart

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import 'package:intl/intl.dart';
1818
import 'package:device_apps/device_apps.dart';
1919
import 'package:shared_preferences/shared_preferences.dart';
2020
import '../settings/apps_selection_page.dart';
21+
import 'data.dart';
22+
import '../settings/auto_request.dart';
2123

2224
class MentorPage extends StatefulWidget {
2325
const MentorPage({super.key});
@@ -40,9 +42,9 @@ class _MentorPageState extends State<MentorPage> {
4042
List<Application> selected_apps_data = [];
4143
String serverurl = '';
4244
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
45+
late ScheduleManager scheduleManager;
4346

4447
List<Application> loadedApps = [];
45-
4648
Future<void> _loadstuffFromSharedPreferences() async {
4749
if (loadedApps.isEmpty) {
4850
loadedApps = await loadApps();
@@ -51,6 +53,15 @@ class _MentorPageState extends State<MentorPage> {
5153
apps_data = loadedApps;
5254

5355
final SharedPreferences prefs = await _prefs;
56+
late TimeOfDay defaultTime;
57+
final scheduledTime = prefs.getString('scheduledTime');
58+
defaultTime = scheduledTime != null
59+
? TimeOfDay.fromDateTime(DateTime.parse(scheduledTime))
60+
: TimeOfDay.now();
61+
62+
scheduleManager = ScheduleManager(callback: _emulateRequest);
63+
scheduleManager.scheduleEmulateRequest(defaultTime);
64+
5465
List<String>? selectedAppNames = prefs.getStringList('selectedApps');
5566
if (selectedAppNames != null) {
5667
setState(() {
@@ -277,8 +288,15 @@ class _MentorPageState extends State<MentorPage> {
277288
void initState() {
278289
super.initState();
279290
_loadstuffFromSharedPreferences();
291+
292+
// scheduleManager = ScheduleManager(callback: _emulateRequest);
280293
}
281294

295+
// void _setScheduledTime(TimeOfDay selectedTime) {
296+
// setState(() {
297+
// scheduleManager.scheduleEmulateRequest(selectedTime);
298+
// });
299+
// }
282300
@override
283301
Widget build(BuildContext context) {
284302
return AnnotatedRegion<SystemUiOverlayStyle>(
@@ -427,8 +445,7 @@ class _MentorPageState extends State<MentorPage> {
427445
child: CircularProgressIndicator(),
428446
),
429447
SizedBox(height: 10.0),
430-
Text(
431-
"Note: It might take some time as the AI is in a relationship",
448+
Text("YOLO",
432449
style: TextStyle(
433450
color: Color.fromARGB(255, 50, 204, 102)))
434451
])
@@ -443,18 +460,33 @@ class _MentorPageState extends State<MentorPage> {
443460
);
444461
},
445462
child: Card(
446-
color: const Color.fromARGB(255, 19, 19, 19),
447-
child: ListTile(
448-
title: const Text(
463+
color: const Color.fromARGB(255, 19, 19, 19),
464+
child: ListTile(
465+
title: Row(
466+
children: [
467+
IconButton(
468+
onPressed: () {
469+
_emulateRequest();
470+
},
471+
icon: const Icon(Icons.refresh,
472+
color: Color.fromARGB(255, 50, 204, 102)),
473+
),
474+
const Text(
449475
"Mentor: ",
450476
style: TextStyle(
451477
color: Color.fromARGB(255, 50, 204, 102),
452478
),
453479
),
454-
subtitle: Text(result,
455-
style: const TextStyle(
456-
color: Color.fromARGB(255, 50, 204, 102),
457-
))))),
480+
],
481+
),
482+
subtitle: Text(
483+
result,
484+
style: const TextStyle(
485+
color: Color.fromARGB(255, 50, 204, 102),
486+
),
487+
),
488+
),
489+
)),
458490
const SizedBox(height: 16.0),
459491
Row(
460492
children: [
@@ -573,35 +605,3 @@ class _MentorPageState extends State<MentorPage> {
573605
));
574606
}
575607
}
576-
577-
class Video {
578-
final String title;
579-
final String videoId;
580-
final String videoDescription;
581-
582-
Video(
583-
{required this.title,
584-
required this.videoId,
585-
required this.videoDescription});
586-
587-
factory Video.fromJson(Map<String, dynamic> json) {
588-
return Video(
589-
title: json['title'].toString() ?? '',
590-
videoId: json['videoId'].toString() ?? '',
591-
videoDescription: json['videoDescription'].toString() ?? '',
592-
);
593-
}
594-
}
595-
596-
class Messages {
597-
final String role;
598-
final String content;
599-
600-
Messages({required this.role, required this.content});
601-
Map<String, dynamic> toJson() {
602-
return {
603-
'role': role,
604-
'content': content,
605-
};
606-
}
607-
}

lib/settings/auto_request.dart

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import 'dart:async';
2+
import 'package:flutter/material.dart';
3+
import 'package:shared_preferences/shared_preferences.dart';
4+
5+
class AutoRequest extends StatefulWidget {
6+
const AutoRequest({super.key});
7+
8+
@override
9+
State<AutoRequest> createState() => _AutoRequestState();
10+
}
11+
12+
class _AutoRequestState extends State<AutoRequest> {
13+
late SharedPreferences sharedPreferences;
14+
TimeOfDay default_time = TimeOfDay.now();
15+
@override
16+
void initState() {
17+
super.initState();
18+
_loadScheduledTime();
19+
}
20+
21+
void _loadScheduledTime() async {
22+
sharedPreferences = await SharedPreferences.getInstance();
23+
final scheduledTime = sharedPreferences.getString('scheduledTime');
24+
if (scheduledTime != null) {
25+
if (this.mounted) {
26+
setState(() {
27+
final parsedTime =
28+
TimeOfDay.fromDateTime(DateTime.parse(scheduledTime));
29+
default_time = parsedTime;
30+
});
31+
}
32+
}
33+
}
34+
35+
void _saveScheduledTime(TimeOfDay selectedTime) async {
36+
final dateTime = DateTime(DateTime.now().year, DateTime.now().month,
37+
DateTime.now().day, selectedTime.hour, selectedTime.minute);
38+
final formattedTime = dateTime.toIso8601String();
39+
await sharedPreferences.setString('scheduledTime', formattedTime);
40+
}
41+
42+
@override
43+
Widget build(BuildContext context) {
44+
return Scaffold(
45+
appBar: AppBar(
46+
title: const Text(
47+
'Mentor/Settings/Auto_Request',
48+
style: TextStyle(color: Color.fromARGB(255, 50, 204, 102)),
49+
),
50+
backgroundColor: Colors.black,
51+
),
52+
backgroundColor: Colors.black,
53+
body: SingleChildScrollView(
54+
child: Padding(
55+
padding: const EdgeInsets.all(16.0),
56+
child: Column(
57+
children: [
58+
ElevatedButton(
59+
onPressed: () {
60+
showTimePicker(
61+
context: context,
62+
initialTime: default_time,
63+
).then((selectedTime) {
64+
if (selectedTime != null) {
65+
_saveScheduledTime(selectedTime);
66+
}
67+
});
68+
},
69+
child: Text('Set Scheduled Time',
70+
style: TextStyle(color: Color.fromARGB(255, 50, 204, 102))),
71+
),
72+
],
73+
),
74+
)));
75+
}
76+
}
77+
78+
class ScheduleManager {
79+
final VoidCallback callback;
80+
late TimeOfDay scheduledTime;
81+
late Timer _timer;
82+
83+
ScheduleManager({required this.callback});
84+
85+
void scheduleEmulateRequest(TimeOfDay desiredTime) {
86+
// Cancel any existing timer
87+
_cancelTimer();
88+
89+
// Set the desired time for scheduling
90+
scheduledTime = desiredTime;
91+
92+
// Start the timer to schedule the callback at the desired time
93+
_startTimer();
94+
}
95+
96+
void _startTimer() {
97+
final currentTime = TimeOfDay.now();
98+
final now = DateTime.now();
99+
final scheduledDateTime = DateTime(
100+
now.year,
101+
now.month,
102+
now.day,
103+
scheduledTime.hour,
104+
scheduledTime.minute,
105+
);
106+
107+
// Calculate the duration until the desired time
108+
Duration duration;
109+
if (scheduledDateTime.isBefore(now)) {
110+
final tomorrow = DateTime(now.year, now.month, now.day + 1);
111+
duration = scheduledDateTime.difference(now) + tomorrow.difference(now);
112+
} else {
113+
duration = scheduledDateTime.difference(now);
114+
}
115+
116+
// Schedule the callback to be called after the duration
117+
_timer = Timer(duration, callback);
118+
}
119+
120+
void _cancelTimer() {
121+
if (_timer.isActive) {
122+
_timer.cancel();
123+
}
124+
}
125+
}

lib/settings/settings_page.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'habitica_integration_page.dart'; // New page for Habitica integration
88
import 'package:firebase_auth/firebase_auth.dart';
99
import 'tellusmore_page.dart';
1010
import 'apps_selection_page.dart';
11+
import 'auto_request.dart';
1112

1213
class SettingsPage extends StatefulWidget {
1314
const SettingsPage({Key? key}) : super(key: key);
@@ -130,6 +131,23 @@ class _SettingsPageState extends State<SettingsPage> {
130131
TextStyle(color: Color.fromARGB(255, 19, 19, 19))),
131132
),
132133
const SizedBox(height: 16.0),
134+
ElevatedButton(
135+
onPressed: () {
136+
Navigator.push(
137+
context,
138+
MaterialPageRoute(
139+
builder: (context) => const AutoRequest(),
140+
),
141+
);
142+
},
143+
style: ElevatedButton.styleFrom(
144+
backgroundColor: const Color.fromARGB(255, 50, 204, 102),
145+
),
146+
child: const Text('Set Auto Mentor',
147+
style:
148+
TextStyle(color: Color.fromARGB(255, 19, 19, 19))),
149+
),
150+
const SizedBox(height: 16.0),
133151
ElevatedButton(
134152
onPressed: () {
135153
Navigator.push(

0 commit comments

Comments
 (0)