207 lines
6.2 KiB
Dart
207 lines
6.2 KiB
Dart
import 'package:didvan/main.dart';
|
|
import 'package:didvan/models/notification_message.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/network/request.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:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:url_launcher/url_launcher_string.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.playingStream.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 int createNotificationId(NotificationMessage data) {
|
|
int t = 0;
|
|
switch (data.type!) {
|
|
case "infography":
|
|
t = 1;
|
|
break;
|
|
case "news":
|
|
t = 2;
|
|
|
|
break;
|
|
case "radar":
|
|
t = 3;
|
|
|
|
break;
|
|
case "video":
|
|
t = 4;
|
|
|
|
break;
|
|
case "podcast":
|
|
t = 5;
|
|
|
|
break;
|
|
}
|
|
int id = int.parse('${data.userId}${data.id}$t');
|
|
return id;
|
|
}
|
|
|
|
static Map<String, int> messagesData(String dataMessgae) {
|
|
dataMessgae = dataMessgae.replaceAll('{{{', '');
|
|
dataMessgae = dataMessgae.replaceAll('}}}', '');
|
|
final pairs = dataMessgae.substring(1, dataMessgae.length - 1).split(',');
|
|
|
|
// Create a map to store the key-value pairs
|
|
final data = <String, int>{};
|
|
// Iterate over the key-value pairs and add them to the map
|
|
for (final pair in pairs) {
|
|
final keyValue = pair.split(':');
|
|
if (keyValue.length == 2) {
|
|
final key = keyValue[0].trim().replaceAll('"', '');
|
|
final value = keyValue[1].trim().replaceAll('"', '');
|
|
data[key] = int.parse(value.replaceAll(' ', ''));
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
static openWebLink(BuildContext context, String src,
|
|
{final LaunchMode mode = LaunchMode.platformDefault}) {
|
|
if (kIsWeb) {
|
|
launchUrlString(
|
|
src,
|
|
mode: mode,
|
|
);
|
|
} else {
|
|
Navigator.of(context).pushNamed(Routes.web, arguments: src);
|
|
}
|
|
}
|
|
}
|