Skip to content
Open
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
5 changes: 2 additions & 3 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml
include: package:very_good_analysis/analysis_options.yaml

linter:
# The lint rules applied to this project can be customized in the
Expand All @@ -21,6 +19,7 @@ linter:
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
public_member_api_docs: false
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule

Expand Down
4 changes: 2 additions & 2 deletions lib/alarm_actions_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class _AlarmActionsScreenState extends State<AlarmActionsScreen> {
void initState() {
super.initState();
_databaseService = DatabaseService.instance;

final actions = _databaseService.getAllAlarmActions();
log(actions.length.toString());
}
Expand Down Expand Up @@ -50,4 +50,4 @@ class _AlarmActionsScreenState extends State<AlarmActionsScreen> {
),
);
}
}
}
11 changes: 7 additions & 4 deletions lib/alarm_manager_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ class AlarmManagerScreen extends StatelessWidget {
const AlarmManagerScreen({super.key});

Future<void> _requestNotificationPermission(BuildContext context) async {
// get a reference to the ScaffoldMessenger before calling async method
final scaffoldMessenger = ScaffoldMessenger.of(context);
final status = await Permission.notification.request();

if (status.isGranted) {
await AlarmMethodChannel.scheduleAlarm();
} else {
ScaffoldMessenger.of(context).showSnackBar(
scaffoldMessenger.showSnackBar(
const SnackBar(
content:
Text('Notification permission is required to schedule alarms.'),
Expand All @@ -24,15 +27,15 @@ class AlarmManagerScreen extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Alarm Manager Screen"),
title: const Text('Alarm Manager Screen'),
centerTitle: true,
actions: [
IconButton(
icon: const Icon(Icons.access_alarm),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
MaterialPageRoute<AlarmActionsScreen>(
builder: (_) => const AlarmActionsScreen()));
},
)
Expand All @@ -43,7 +46,7 @@ class AlarmManagerScreen extends StatelessWidget {
onPressed: () async {
await _requestNotificationPermission(context);
},
child: const Text("Schedule Alarm")),
child: const Text('Schedule Alarm')),
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/hive/models/alarm_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ part 'alarm_action.g.dart';

@HiveType(typeId: 0) //typeId should be unique for each model
class AlarmAction {
AlarmAction(this.actionType, this.timestamp);

@HiveField(0)
final String actionType;
@HiveField(1)
final DateTime timestamp;

AlarmAction(this.actionType, this.timestamp);
}
30 changes: 14 additions & 16 deletions lib/hive/service/database_service.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import 'dart:developer';

import 'package:flutter/foundation.dart';
import 'package:flutter_alarm_manager_poc/hive/models/alarm_action.dart';
import 'package:hive_flutter/hive_flutter.dart';
import '../models/alarm_action.dart';

class DatabaseService {
static const String alarmBoxName = 'alarm_actions';
static DatabaseService? _instance;
late Box<AlarmAction> _alarmBox;

// Private constructor
DatabaseService._();

// Singleton instance getter
static DatabaseService get instance {
_instance ??= DatabaseService._();
return _instance!;
}
static final DatabaseService _instance = DatabaseService._();

static DatabaseService get instance => _instance;

static const String alarmBoxName = 'alarm_actions';
late Box<AlarmAction> _alarmBox;

ValueListenable<Box<AlarmAction>> get alarmBoxListenable =>
_alarmBox.listenable();
Expand All @@ -27,7 +25,7 @@ class DatabaseService {
Hive.registerAdapter(AlarmActionAdapter());
_alarmBox = await Hive.openBox<AlarmAction>(alarmBoxName);
log('Hive initialized and box opened successfully.');
} catch (e) {
} on Exception catch (e) {
log('Failed to initialize Hive or open box: $e');
}
}
Expand All @@ -40,20 +38,20 @@ class DatabaseService {
);
log('Stored alarm action: $actionType');

var actions = getAllAlarmActions();
final actions = getAllAlarmActions();
log('Retrieved ${actions.length} alarm actions.');
} catch (e) {
} on Exception catch (e) {
log('Failed to store alarm action: $e');
}
}

// Retrieve all alarm actions from the Hive box
List<AlarmAction> getAllAlarmActions() {
try {
var actions = _alarmBox.values;
final actions = _alarmBox.values;
log('Retrieved ${actions.length} alarm actions.');
return actions.toList();
} catch (e) {
} on Exception catch (e) {
log('Failed to retrieve alarm actions: $e');
return [];
}
Expand All @@ -64,7 +62,7 @@ class DatabaseService {
try {
await _alarmBox.clear();
log('All alarm actions cleared.');
} catch (e) {
} on Exception catch (e) {
log('Failed to clear alarm actions: $e');
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_alarm_manager_poc/alarm_manager_screen.dart';
import 'package:flutter_alarm_manager_poc/hive/service/database_service.dart';
import 'utils/alarm_method_channel.dart';
import 'package:flutter_alarm_manager_poc/utils/alarm_method_channel.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand Down
16 changes: 7 additions & 9 deletions lib/utils/alarm_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_alarm_manager_poc/hive/service/database_service.dart';

class AlarmMethodChannel {
static const name = "Flutter";
static const name = 'Flutter';
static const platform = MethodChannel('com.example/alarm_manager');

static Future<void> scheduleAlarm() async {
Expand All @@ -27,20 +27,18 @@ class AlarmMethodChannel {
log(name: name, 'Alarm was accepted');
// await alarmBox.add(AlarmAction('accept', DateTime.now()));

await DatabaseService.instance.storeAlarmAction("accept");
await DatabaseService.instance.storeAlarmAction('accept');

// Handle alarm accepted
// You can call a function or update state here
break;
// Handle alarm accepted
// You can call a function or update state here
case 'alarmSnoozed':
log(name: name, 'Alarm was snoozed');
// await alarmBox.add(AlarmAction('snooze', DateTime.now()));

await DatabaseService.instance.storeAlarmAction("snooze");
await DatabaseService.instance.storeAlarmAction('snooze');

// Handle alarm snoozed
// You can call a function or update state here
break;
// Handle alarm snoozed
// You can call a function or update state here
default:
log('Unrecognized method ${call.method}');
}
Expand Down
Loading