didvan-app/lib/services/app_initalizer.dart

211 lines
6.9 KiB
Dart

import 'dart:io';
import 'package:didvan/main.dart';
import 'package:didvan/models/requests/news.dart';
import 'package:didvan/models/requests/radar.dart';
import 'package:didvan/models/requests/studio.dart';
import 'package:didvan/models/settings_data.dart';
import 'package:didvan/routes/routes.dart';
import 'package:didvan/services/media/media.dart';
import 'package:didvan/services/storage/storage.dart';
import 'package:didvan/views/home/home_state.dart';
import 'package:didvan/views/podcasts/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;
}
// ignore: use_build_context_synchronously
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 {
try {
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,
);
}
} catch (e) {
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> handleCLick(
HomeState state, TabController tabController) async {
if (clickAction != null) {
final action = clickAction!.split('-').first;
final isStudio = action != 'radar' && action != 'news';
String routeName = '/${isStudio ? 'studio' : action}-details';
if (action == 'podcast') {
state.currentPageIndex = 3;
tabController.animateTo(3);
await Future.delayed(const Duration(milliseconds: 500));
clickAction = null;
return;
}
Navigator.of(navigatorKey.currentContext!).pushNamed(
routeName,
arguments: {
'id': int.parse(clickAction!.split('-')[1]),
'args': routeName == Routes.newsDetails
? const NewsRequestArgs(page: 0)
: routeName == Routes.radarDetails
? const RadarRequestArgs(page: 0)
: action == 'video'
? const StudioRequestArgs(page: 0)
: null,
'onMarkChanged': (id, value) {},
'onCommentsChanged': (id, count) {},
'hasUnmarkConfirmation': false,
},
);
return;
}
}
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")
: Platform.isAndroid
? const FirebaseOptions(
apiKey: 'AIzaSyBp-UHjWeM0H0UHtX5yguFKG-riMzvvCzw',
appId: '1:935017686266:android:f9cbc9aba8e3d65ed2d543',
messagingSenderId: '935017686266',
projectId: 'didvan-9b7da',
)
: const FirebaseOptions(
apiKey: 'AIzaSyCMa-zg_uVhOfTnea5Klz6aPZlgHwVGj7U',
appId: '1:935017686266:ios:de47638bd662463fd2d543',
messagingSenderId: '935017686266',
projectId: 'didvan-9b7da',
// iosBundleId: "com.didvan.didvanapp",
// storageBucket: "didvan-9b7da.appspot.com",
// iosClientId:
// "935017686266-54hu01v9cc5pqpgofo1gk2n3hegj4r2m.apps.googleusercontent.com"
),
);
} 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,
);
}
}