-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
164 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Mon Apr 17 19:02:42 JST 2023 | ||
#Sat May 20 21:55:30 JST 2023 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Flutter imports: | ||
import 'package:flutter/widgets.dart'; | ||
|
||
// Package imports: | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
|
||
/// ref: https://zenn.dev/riscait/books/flutter-riverpod-practical-introduction/viewer/v2-app-lifecycle | ||
final appLifecycleProvider = Provider<AppLifecycleState>((ref) { | ||
final observer = _AppLifecycleObserver((value) => ref.state = value, ref); | ||
|
||
final binding = WidgetsBinding.instance..addObserver(observer); | ||
ref.onDispose(() => binding.removeObserver(observer)); | ||
|
||
return AppLifecycleState.resumed; | ||
}); | ||
|
||
class _AppLifecycleObserver extends WidgetsBindingObserver { | ||
_AppLifecycleObserver(this._didChangeState, this.ref); | ||
final Ref ref; | ||
|
||
final ValueChanged<AppLifecycleState> _didChangeState; | ||
|
||
@override | ||
void didChangeAppLifecycleState(AppLifecycleState state) { | ||
_didChangeState(state); | ||
super.didChangeAppLifecycleState(state); | ||
} | ||
} | ||
|
||
extension AppLifecycleStateExtension on AppLifecycleState { | ||
bool get isResumed => this == AppLifecycleState.resumed; | ||
} |
21 changes: 21 additions & 0 deletions
21
lib/common/provider/notification/model/notification_state_model.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
|
||
part 'notification_state_model.freezed.dart'; | ||
part 'notification_state_model.g.dart'; | ||
|
||
@freezed | ||
class NotificationStateModel with _$NotificationStateModel { | ||
const factory NotificationStateModel({ | ||
/// 通知権限が許可されているかどうか | ||
@Default(false) bool isAccepted, | ||
|
||
/// 通知権限要求ダイアログを今後表示しないかどうか | ||
@Default(false) bool neverShowNotificationPermissionDialog, | ||
|
||
/// FCM Token | ||
String? fcmToken, | ||
}) = _NotificationStateModel; | ||
|
||
factory NotificationStateModel.fromJson(Map<String, dynamic> json) => | ||
_$NotificationStateModelFromJson(json); | ||
} |
67 changes: 67 additions & 0 deletions
67
lib/common/provider/notification/provider/notification_provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:app_settings/app_settings.dart'; | ||
import 'package:eqmonitor/common/provider/app_lifecycle.dart'; | ||
import 'package:eqmonitor/common/provider/notification/model/notification_state_model.dart'; | ||
import 'package:eqmonitor/common/provider/shared_preferences.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:permission_handler/permission_handler.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
part 'notification_provider.g.dart'; | ||
|
||
@Riverpod(keepAlive: true) | ||
class NotificationState extends _$NotificationState { | ||
@override | ||
NotificationStateModel build() { | ||
_prefs = ref.read(sharedPreferencesProvider); | ||
// アプリから一旦離れて戻ってきたときに | ||
// 再度、通知権限の状態を取得する | ||
ref.listen(appLifecycleProvider, (_, next) { | ||
if (next == AppLifecycleState.resumed) { | ||
init(); | ||
} | ||
}); | ||
final res = _loadFromPrefs(); | ||
if (res != null) {} | ||
return const NotificationStateModel(); | ||
} | ||
|
||
late final SharedPreferences _prefs; | ||
|
||
static const _key = 'NotificationState'; | ||
|
||
Future<void> init() async { | ||
/// 通知権限の状態を取得する | ||
final isNotificationPermissionAllowed = await _notificationPermission(); | ||
state = state.copyWith( | ||
isAccepted: isNotificationPermissionAllowed, | ||
); | ||
} | ||
|
||
// 通知権限のリクエスト | ||
Future<void> requestNotificationPermission() async { | ||
final status = await Permission.notification.request(); | ||
if (status != PermissionStatus.granted) { | ||
// 通知設定の画面を開く | ||
await AppSettings.openNotificationSettings(); | ||
} | ||
await init(); | ||
} | ||
|
||
Future<bool> _notificationPermission() async { | ||
final status = await Permission.notification.status; | ||
return status == PermissionStatus.granted; | ||
} | ||
|
||
NotificationStateModel? _loadFromPrefs() { | ||
final data = _prefs.getString(_key); | ||
if (data == null) { | ||
return const NotificationStateModel(); | ||
} | ||
return NotificationStateModel.fromJson( | ||
jsonDecode(data) as Map<String, dynamic>, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters