didvan-app/lib/services/app_initalizer.dart

127 lines
4.1 KiB
Dart

import 'package:didvan/models/settings_data.dart';
import 'package:didvan/services/media/media.dart';
import 'package:didvan/services/storage/storage.dart';
import 'package:didvan/views/home/studio/studio_details/studio_details_state.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart';
class AppInitializer {
static String? fcmToken;
static String? clickAction;
static Future<void> setupServices(BuildContext context) async {
if (!kIsWeb) {
StorageService.appDocsDir =
(await getApplicationDocumentsDirectory()).path;
StorageService.appTempsDir = (await getTemporaryDirectory()).path;
}
final studioState = context.read<StudioDetailsState>();
MediaService.audioPlayer.isPlaying.listen((event) {
if (event &&
(MediaService.audioPlayerTag?.contains('podcast') ?? false)) {
studioState.handleTracking(
id: MediaService.currentPodcast!.id,
sendRequest: false,
);
} else if (MediaService.audioPlayerTag?.contains('podcast') ?? false) {
studioState.handleTracking(id: MediaService.currentPodcast!.id);
}
});
}
static Future<SettingsData> initilizeSettings() async {
final brightness = await StorageService.getValue(key: 'brightness');
if (brightness != null) {
final themeMode = brightness == 'dark' ? ThemeMode.dark : ThemeMode.light;
final fontFamily = await StorageService.getValue(key: 'fontFamily');
final fontScale = double.parse(
await StorageService.getValue(key: 'fontSizeScale'),
);
return SettingsData(
fontFamily: fontFamily,
fontScale: fontScale,
themeMode: themeMode,
);
} else {
await StorageService.setValue(
key: 'notificationTimeRangeStart',
value: '0',
);
await StorageService.setValue(
key: 'notificationTimeRangeEnd',
value: '24',
);
await StorageService.setValue(
key: 'fontFamily',
value: 'Dana-FA',
);
await StorageService.setValue(
key: 'fontSizeScale',
value: '1',
);
await StorageService.setValue(
key: 'brightness',
value: 'light',
);
return SettingsData(
fontFamily: 'Dana-FA',
fontScale: 1,
themeMode: ThemeMode.light,
);
}
}
static Future<void> initializeFirebase() async {
try {
await Firebase.initializeApp(
options: kIsWeb
? const FirebaseOptions(
apiKey: "AIzaSyA0HZjKpRuPOi1SC3f_EZTvlS3mcj9UVo0",
authDomain: "didvan-9b7da.firebaseapp.com",
projectId: "didvan-9b7da",
storageBucket: "didvan-9b7da.appspot.com",
messagingSenderId: "935017686266",
appId: "1:935017686266:web:a93f7a19bed23c51d2d543",
measurementId: "G-80B4H9E8Y0")
: const FirebaseOptions(
apiKey: 'AIzaSyBp-UHjWeM0H0UHtX5yguFKG-riMzvvCzw',
appId: '1:935017686266:android:f9cbc9aba8e3d65ed2d543',
messagingSenderId: '935017686266',
projectId: 'didvan-9b7da',
),
);
} catch (e) {
Firebase.app();
}
final initMsg = await FirebaseMessaging.instance.getInitialMessage();
if (initMsg != null) {
clickAction = initMsg.data['click_action'].replaceAll(
'navigate-',
'',
);
}
final FirebaseMessaging fcm = FirebaseMessaging.instance;
fcmToken = await fcm.getToken(
vapidKey: kIsWeb
? 'BMXHGd93t_htpS7c62ceuuLVVmia2cEDmqxp46g9Vt0B3OxNMKIqN9nupsUMtv2Vq8Yy2sQGIqgCm9FxUSKvssU'
: null,
);
// await fcm.subscribeToTopic('general');
await fcm.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
}
}