Skip to content

Commit

Permalink
Implement notification
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-pratik-k committed Apr 23, 2024
1 parent 44eedbb commit 941d5cd
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 36 deletions.
1 change: 1 addition & 0 deletions .idea/libraries/Flutter_Plugins.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ PODS:
- FirebaseCoreInternal (10.22.0):
- "GoogleUtilities/NSData+zlib (~> 7.8)"
- Flutter (1.0.0)
- flutter_local_notifications (0.0.1):
- Flutter
- fluttertoast (0.0.2):
- Flutter
- Toast
Expand Down Expand Up @@ -68,6 +70,7 @@ PODS:
DEPENDENCIES:
- firebase_core (from `.symlinks/plugins/firebase_core/ios`)
- Flutter (from `Flutter`)
- flutter_local_notifications (from `.symlinks/plugins/flutter_local_notifications/ios`)
- fluttertoast (from `.symlinks/plugins/fluttertoast/ios`)
- google_sign_in_ios (from `.symlinks/plugins/google_sign_in_ios/darwin`)
- package_info_plus (from `.symlinks/plugins/package_info_plus/ios`)
Expand Down Expand Up @@ -97,6 +100,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/firebase_core/ios"
Flutter:
:path: Flutter
flutter_local_notifications:
:path: ".symlinks/plugins/flutter_local_notifications/ios"
fluttertoast:
:path: ".symlinks/plugins/fluttertoast/ios"
google_sign_in_ios:
Expand Down Expand Up @@ -125,6 +130,7 @@ SPEC CHECKSUMS:
FirebaseCore: 28045c1560a2600d284b9c45a904fe322dc890b6
FirebaseCoreInternal: bca337352024b18424a61e478460547d46c4c753
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_local_notifications: 4cde75091f6327eb8517fa068a0a5950212d2086
fluttertoast: 31b00dabfa7fb7bacd9e7dbee580d7a2ff4bf265
google_sign_in_ios: 989eea5abe94af62050782714daf920be883d4a2
GoogleSignIn: b232380cf495a429b8095d3178a8d5855b42e842
Expand Down
29 changes: 20 additions & 9 deletions app/lib/domain/handlers/notification_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const _androidChannel = AndroidNotificationChannel(
'notification-channel-cloud-gallery', // id
'Processes', // title
description:
'This channel is used to notify you about the processes in the app.',
'This channel is used to notify you about the processes in the app.',
// description
importance: Importance.max,
importance: Importance.defaultImportance,
);

final notificationHandlerProvider = Provider.autoDispose((ref) {
Expand All @@ -34,9 +34,11 @@ class NotificationHandler {
}
}

void requestPermission(){
_flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()?.requestNotificationsPermission();
void requestPermission() {
_flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.requestNotificationsPermission();
}

Future<void> _initIOS(BuildContext context) async {
Expand All @@ -47,8 +49,10 @@ class NotificationHandler {
requestBadgePermission: true,
requestSoundPermission: true,
onDidReceiveLocalNotification: (id, title, body, payload) {
context.go(AppRoutePath.home);
context.push(AppRoutePath.transfer);
if (context.mounted) {
context.go(AppRoutePath.home);
context.push(AppRoutePath.transfer);
}
},
)),
);
Expand All @@ -60,14 +64,20 @@ class NotificationHandler {
android: AndroidInitializationSettings('cloud_gallery_logo'),
),
onDidReceiveNotificationResponse: (response) {
///TODO: On Notification Tap
if (context.mounted) {
context.go(AppRoutePath.home);
context.push(AppRoutePath.transfer);
}
},
);
final initial = await _flutterLocalNotificationsPlugin
.getNotificationAppLaunchDetails();

if (initial?.didNotificationLaunchApp == true) {
///TODO: On Notification Tap
if (context.mounted) {
context.go(AppRoutePath.home);
context.push(AppRoutePath.transfer);
}
}
}

Expand All @@ -84,6 +94,7 @@ class NotificationHandler {
description,
NotificationDetails(
android: AndroidNotificationDetails(
channelAction: AndroidNotificationChannelAction.update,
_androidChannel.id,
_androidChannel.name,
icon: "cloud_gallery_logo",
Expand Down
73 changes: 46 additions & 27 deletions app/lib/ui/flow/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:cloud_gallery/components/app_page.dart';
import 'package:cloud_gallery/domain/extensions/widget_extensions.dart';
import 'package:cloud_gallery/domain/formatter/date_formatter.dart';
import 'package:cloud_gallery/domain/extensions/context_extensions.dart';
import 'package:cloud_gallery/domain/handlers/notification_handler.dart';
import 'package:cloud_gallery/ui/flow/home/components/no_local_medias_access_screen.dart';
import 'package:cloud_gallery/ui/flow/home/home_screen_view_model.dart';
import 'package:collection/collection.dart';
Expand Down Expand Up @@ -31,10 +32,14 @@ class HomeScreen extends ConsumerStatefulWidget {

class _HomeScreenState extends ConsumerState<HomeScreen> {
late HomeViewStateNotifier notifier;
late NotificationHandler _notificationHandler;
final _scrollController = ScrollController();

@override
void initState() {
_notificationHandler = ref.read(notificationHandlerProvider);
_notificationHandler.init(context);
_notificationHandler.requestPermission();
notifier = ref.read(homeViewStateNotifier.notifier);
super.initState();
}
Expand All @@ -46,45 +51,59 @@ class _HomeScreenState extends ConsumerState<HomeScreen> {
}

void _errorObserver() {
ref.listen(homeViewStateNotifier.select((value) => value.error),
(previous, next) {
if (next != null) {
showErrorSnackBar(context: context, error: next);
ref.listen(
homeViewStateNotifier.select((value) => value.error),
(previous, next) {
if (next != null) {
showErrorSnackBar(context: context, error: next);
}
},
);
}

void _notificationObserver() {
ref.listen(homeViewStateNotifier, (previous, next) {
if ((previous?.mediaProcesses.isEmpty ?? false) &&
next.mediaProcesses.isNotEmpty) {
_notificationHandler.showNotification(
id: next.mediaProcesses.length,
name: "Sync to Google Drive",
description: "Syncing media files to Google Drive.",
);
}
});
}

@override
Widget build(BuildContext context) {
_errorObserver();
_notificationObserver();
return AppPage(
titleWidget: _titleWidget(context: context),
actions: [
Consumer(
builder: (context, ref, child) {
final showTransferButton = ref.watch(homeViewStateNotifier.select(
(value) => value.showTransfer));
return Visibility(
visible: showTransferButton,
child: Padding(
padding: const EdgeInsets.only(right: 8.0),
child: ActionButton(
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
size: 36,
backgroundColor: context.colorScheme.containerNormal,
onPressed: () {
AppRouter.mediaTransfer.push(context);
},
icon: Icon(
CupertinoIcons.arrow_up_arrow_down,
color: context.colorScheme.textSecondary,
size: 18,
),
Consumer(builder: (context, ref, child) {
final showTransferButton = ref.watch(
homeViewStateNotifier.select((value) => value.showTransfer));
return Visibility(
visible: showTransferButton,
child: Padding(
padding: const EdgeInsets.only(right: 8.0),
child: ActionButton(
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
size: 36,
backgroundColor: context.colorScheme.containerNormal,
onPressed: () {
AppRouter.mediaTransfer.push(context);
},
icon: Icon(
CupertinoIcons.arrow_up_arrow_down,
color: context.colorScheme.textSecondary,
size: 18,
),
),
);
}
),
),
);
}),
ActionButton(
size: 36,
backgroundColor: context.colorScheme.containerNormal,
Expand Down

0 comments on commit 941d5cd

Please sign in to comment.