68 lines
2.1 KiB
Dart
68 lines
2.1 KiB
Dart
import 'package:firebase_auth/firebase_auth.dart' show FirebaseAuth, User;
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_mobx/flutter_mobx.dart';
|
|
import 'package:handyman_provider_flutter/utils/extensions/string_extension.dart';
|
|
import 'package:nb_utils/nb_utils.dart';
|
|
|
|
import '../main.dart';
|
|
import '../utils/firebase_messaging_utils.dart';
|
|
import '../utils/images.dart';
|
|
|
|
class SwitchPushNotificationSubscriptionComponent extends StatefulWidget {
|
|
const SwitchPushNotificationSubscriptionComponent({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<SwitchPushNotificationSubscriptionComponent> createState() => _SwitchPushNotificationSubscriptionComponentState();
|
|
}
|
|
|
|
class _SwitchPushNotificationSubscriptionComponentState extends State<SwitchPushNotificationSubscriptionComponent> {
|
|
@override
|
|
void initState() {
|
|
init();
|
|
super.initState();
|
|
}
|
|
|
|
void init() async {
|
|
//
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return SettingItemWidget(
|
|
leading: ic_notification.iconImage(size: 18),
|
|
title: languages.pushNotification,
|
|
trailing: Transform.scale(
|
|
scale: 0.7,
|
|
child: Observer(builder: (context) {
|
|
bool isFirebaseReady = false;
|
|
User? currentUser;
|
|
try {
|
|
if (Firebase.apps.isNotEmpty) {
|
|
currentUser = FirebaseAuth.instance.currentUser;
|
|
isFirebaseReady = true;
|
|
}
|
|
} catch (e) {
|
|
log('Firebase Auth error: $e');
|
|
}
|
|
|
|
return Switch.adaptive(
|
|
value: isFirebaseReady && currentUser != null && appStore.isSubscribedForPushNotification,
|
|
onChanged: (v) async {
|
|
if (appStore.isLoading) return;
|
|
appStore.setLoading(true);
|
|
if (v) {
|
|
await subscribeToFirebaseTopic();
|
|
} else {
|
|
await unsubscribeFirebaseTopic(appStore.userId);
|
|
}
|
|
appStore.setLoading(false);
|
|
setState(() {});
|
|
},
|
|
).withHeight(18);
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
}
|