Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Future requestPermissions() async{
}
}

void initNotification() async {
void initNotification(AlarmListManager manager) async {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
Expand All @@ -49,16 +49,15 @@ void initNotification() async {
macOS: initializationSettingsDarwin,
linux: initializationSettingsLinux);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onDidReceiveNotificationResponse: onDidReceiveNotificationResponse);
onDidReceiveNotificationResponse: (NotificationResponse response) {
onDidReceiveNotificationResponse(response, manager);
});

flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()?.requestNotificationsPermission();
}

void onDidReceiveNotificationResponse(NotificationResponse notificationResponse) async {

AlarmListManager manager = AlarmListManager();
await manager.loadAllFromStorage();
void onDidReceiveNotificationResponse(NotificationResponse notificationResponse, AlarmListManager manager) async {
print("Notification received");
if(notificationResponse.id != null) {
print("Notification id owo: ${notificationResponse.id}");
Expand All @@ -82,7 +81,9 @@ void onDidReceiveNotificationResponse(NotificationResponse notificationResponse)

void main() async {
WidgetsFlutterBinding.ensureInitialized();
initNotification();
AlarmListManager manager = AlarmListManager();
await manager.loadAllFromStorage();
initNotification(manager);
if(isAndroid()) {
await AndroidAlarmManager.initialize();
await requestPermissions();
Expand All @@ -93,10 +94,10 @@ void main() async {

@pragma('vm:entry-point')
void alarmCallback(int id) async {

AlarmListManager manager = AlarmListManager();
initNotification();
await manager.loadAllFromStorage();

initNotification(manager);
manager.getAlarms().forEach((element) {
print("Checking alarm");
if(element.active && id ==element.id) {
Expand All @@ -105,10 +106,32 @@ void alarmCallback(int id) async {
});
}

class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
final int? alarmId;
const MyApp(this.alarmId, {super.key});

@override
State<StatefulWidget> createState() => MyAppState();

}

class MyAppState extends State<MyApp> {
int? alarmId;
MyApp(this.alarmId);
ThemeMode themeMode = AlarmListManager.getInstance().settings.theme;

@override
void initState() {
super.initState();
alarmId = widget.alarmId;
}

void setThemeMode(ThemeMode themeMode) {
AlarmListManager.getInstance().settings.theme = themeMode;
AlarmListManager.getInstance().saveSettings();
setState(() {
this.themeMode = themeMode;
});
}

// This widget is the root of your application.
@override
Expand All @@ -119,15 +142,15 @@ class MyApp extends StatelessWidget {
return DynamicColorBuilder(builder: (lightColorScheme, darkColorScheme) {
return MaterialApp(
title: 'ShockAlarm',
theme: ThemeData(
theme: lightColorScheme != null ? ThemeData(
useMaterial3: true,
colorScheme: lightColorScheme,
),
darkTheme: ThemeData(
): ThemeData.light(),
darkTheme: darkColorScheme != null ? ThemeData(
useMaterial3: true,
colorScheme: darkColorScheme,
),
themeMode: ThemeMode.system,
): ThemeData.dark(),
themeMode: themeMode,
home: ScreenSelector(manager: manager)
);
});
Expand Down
43 changes: 39 additions & 4 deletions lib/screens/tokens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:shock_alarm_app/components/shock_disclamer.dart';
import 'package:shock_alarm_app/main.dart';
import 'package:shock_alarm_app/services/alarm_list_manager.dart';
import 'package:url_launcher/url_launcher.dart';
import '../components/token_item.dart';
import '../main.dart';
import 'shares.dart';

class TokenScreen extends StatefulWidget {
Expand Down Expand Up @@ -56,9 +56,9 @@ class TokenScreenState extends State<TokenScreen> {
content: SingleChildScrollView(child:
Column(
children: <Widget>[
DefaultTextStyle(style: TextStyle(), child: SelectableText.rich(TextSpan(children: [
DefaultTextStyle(style: Theme.of(context).textTheme.bodyMedium!, child: SelectableText.rich(TextSpan(children: [
TextSpan(text: "As you are using a browser, you must use a token to sign in. To get one visit "),
TextSpan(text: "https://next.openshock.app/settings/api-tokens", recognizer: recognizer, style: TextStyle(decoration: TextDecoration.underline), mouseCursor: SystemMouseCursors.click),
TextSpan(text: "https://next.openshock.app/settings/api-tokens", recognizer: recognizer, style: TextStyle(color: Theme.of(context).hintColor, decoration: TextDecoration.underline), mouseCursor: SystemMouseCursors.click),
TextSpan(text: " and generate a token with all permissions. Then paste it here.")
]

Expand Down Expand Up @@ -224,7 +224,42 @@ class TokenScreenState extends State<TokenScreen> {
showLoginPopup();
}
}, child: Text("Log in to OpenShock", style: TextStyle(fontSize: t.textTheme.titleMedium!.fontSize)),),

Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Theme"),
SegmentedButton<int>(
segments: [
ButtonSegment(value: 0, label: Icon(Icons.devices)),
ButtonSegment(value: 1, label: Icon(Icons.sunny)),
ButtonSegment(value: 2, label: Icon(Icons.nightlight)),
],
selected: {
switch(context.findAncestorStateOfType<MyAppState>()?.themeMode){
null => throw UnimplementedError(), // should never be null ig
ThemeMode.system => 0,
ThemeMode.light => 1,
ThemeMode.dark => 2,
}
},
onSelectionChanged: (Set<int> newSelection) {
if(newSelection.isNotEmpty) {
switch(newSelection.first) {
case 0:
context.findAncestorStateOfType<MyAppState>()?.setThemeMode(ThemeMode.system);
break;
case 1:
context.findAncestorStateOfType<MyAppState>()?.setThemeMode(ThemeMode.light);
break;
case 2:
context.findAncestorStateOfType<MyAppState>()?.setThemeMode(ThemeMode.dark);
break;
}
}
},
)
],
),
// Actual options
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
Expand Down
9 changes: 7 additions & 2 deletions lib/services/alarm_list_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Settings {

int maxAlarmLengthSeconds = 60;

ThemeMode theme = ThemeMode.system;


Settings();

Expand All @@ -46,6 +48,8 @@ class Settings {
useHttpShocking = json["useHttpShocking"];
if(json["useGroupedShockerSelection"] != null)
useGroupedShockerSelection = json["useGroupedShockerSelection"];
if(json["theme"] != null)
theme = ThemeMode.values[json["theme"]];
}

Map<String, dynamic> toJson() {
Expand All @@ -57,7 +61,8 @@ class Settings {
"disableHubFiltering": disableHubFiltering,
"allowTokenEditing": allowTokenEditing,
"useHttpShocking": useHttpShocking,
"useGroupedShockerSelection": useGroupedShockerSelection
"useGroupedShockerSelection": useGroupedShockerSelection,
"theme": theme.index
};
}
}
Expand Down Expand Up @@ -149,7 +154,7 @@ class AlarmListManager {
void saveSettings() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("settings", jsonEncode(settings));
reloadAllMethod!();
reloadAllMethod?.call();
}

void rescheduleAlarms() async {
Expand Down
Loading