library;
import 'dart:io';
import 'dart:html' as html;
import 'package:bson/bson.dart';
import 'package:dio/dio.dart';
import 'package:fikoyt_ui/core/fikoyt_device_info.dart';
import 'package:fikoyt_ui/core/http_overrides.dart';
import 'package:fikoyt_ui/fikoyt_ui.dart';
import 'package:fikoyt_ui/migration/dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart' hide Intent;
import 'package:flutter/services.dart';
import 'package:mobile_device_identifier/mobile_device_identifier.dart';
import 'package:receive_intent/receive_intent.dart';
import 'package:socket_io_client/socket_io_client.dart' as IO;
import 'package:timezone/data/latest.dart' as tz;
// ignore: constant_identifier_names
// enum TemaModu { ACIK, KOYU, SISTEM }
enum TemaModu {
ACIK(0, "Açık Tema", "🔆", ThemeMode.light),
KOYU(1, "Koyu Tema", "🌙", ThemeMode.dark),
SISTEM(-1, "Sistem Teması", "⚡", ThemeMode.system);
const TemaModu(this.value, this.ismi, this.simge, this.temaModu);
final int value;
final String ismi;
final String simge;
final ThemeMode temaModu;
// List tumu() => [EkOdemeTuru.IKRAMIYE, EkOdemeTuru.PRIM, EkOdemeTuru.YOL, EkOdemeTuru.YEMEK, EkOdemeTuru.SEYYANEN, EkOdemeTuru.DIGER];
}
class FikoYTCoreService {
static FikoYTEventSystem events = FikoYTEventSystem();
static GlobalKey navigatorKey = GlobalKey();
static FikoYTServiceSettings serviceSettings = FikoYTServiceSettings();
static TemaModu temaModu = TemaModu.SISTEM;
static Rx parlaklik = Brightness.light.obs;
///
static final AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
static final InitializationSettings initializationSettings = InitializationSettings(android: initializationSettingsAndroid);
static final FlutterLocalNotificationsPlugin notificationPlugin = FlutterLocalNotificationsPlugin();
///
static late IO.Socket socket;
static Rx wsBagli = Rx(false);
///
static init({FikoYTServiceSettings? settings}) async {
FikoYTCoreService.serviceSettings = settings ?? FikoYTServiceSettings();
WidgetsFlutterBinding.ensureInitialized();
await SystemChrome.setPreferredOrientations(serviceSettings.deviceOrientation);
HttpOverrides.global = HttpSSlIptalUzerineYaz();
tz.initializeTimeZones();
dio?.interceptors.add(InterceptorsWrapper(onRequest: (options, handler) {
options.extra["withCredentials"] = true;
return handler.next(options);
}));
///
await FikoYTTranslationManager.loadFromFiles([
"packages/fikoyt_ui/assets/translate/app_tr.json",
"packages/fikoyt_ui/assets/translate/app_us.json",
...(FikoYTCoreService.serviceSettings.appSettings.translationFiles ?? []),
]);
///
await SystemChrome.setEnabledSystemUIMode(serviceSettings.systemUiMode, overlays: serviceSettings.systemUiOverlay);
///
if (!kIsWeb) {
await initializeNotifications();
///
await onInitialIntent();
}
///
if (serviceSettings.connectWebSocket) {
socket = IO.io(
serviceSettings.fikoytWebSocketUrl,
IO.OptionBuilder()
.setTransports(['websocket'])
.enableReconnection()
.setExtraHeaders({
"uygulama": serviceSettings.appSettings.uygulamaIsmi,
"cihaz_uuid": await FikoYTCoreService.getDeviceID(),
})
.enableAutoConnect() // for Flutter or Dart VM
// .disableAutoConnect() // disable auto-connection
// .setExtraHeaders({'foo': 'bar'}) // optional
.build());
///
if (serviceSettings.socketMiddleWare != null) {
serviceSettings.socketMiddleWare!(socket);
socket.onConnect(
(data) {
// print("bağlandıııııııııııı");
wsBagli.value = true;
},
);
socket.onDisconnect(
(data) {
// print("koptuuuuuuuuuuuuuuuuuuuuu");
wsBagli.value = false;
},
);
}
socket.connect();
}
return FikoYTCoreService;
}
static Future ioEmit(String event, dynamic data, {Function? ack, bool binary = false}) async {
Map FikoYTData = {
"FikoYT": {
"kid": null,
"cihaz_kimlik": await getDeviceID(),
"cihaz_tipi": "Android",
"uygulama": FikoYTCoreService.serviceSettings.appSettings.uygulamaIsmi,
...(FikoYTCoreService.serviceSettings.socketDataMiddleWare ?? {}),
}
};
data = [
data,
FikoYTData,
];
return socket.emitWithAckAsync(event, data, ack: ack, binary: binary);
}
///
static initializeNotifications() async {
await FlutterLocalNotificationsPlugin().initialize(
initializationSettings,
onDidReceiveNotificationResponse: serviceSettings.notificatinSettings.onDidReceiveNotificationResponse,
onDidReceiveBackgroundNotificationResponse: serviceSettings.notificatinSettings.onDidReceiveBackNotificationResponse,
);
for (var i = 0; i < serviceSettings.notificatinSettings.channels.length; i++) {
final AndroidNotificationChannel channel = serviceSettings.notificatinSettings.channels[i];
await notificationPlugin.resolvePlatformSpecificImplementation()?.createNotificationChannel(channel);
}
}
///
static onInitialIntent() async {
if (Platform.isWindows == false) {
try {
final receivedIntent = await ReceiveIntent.getInitialIntent();
serviceSettings.onInitialIntent(receivedIntent);
} on PlatformException {
// Handle exception
}
}
}
///
static uygulamaCalistir() async {
if (!kIsWeb) {
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
// await Window.initialize();
// await Window.setEffect(
// effect: WindowEffect.transparent,
// color: const Color.fromARGB(155, 0, 0, 0),
// );
doWhenWindowReady(() async {
// const initialSize = Size(600, 450);
// appWindow.minSize = initialSize;
// appWindow.size = initialSize;appWindow
// appWindow.alignment = Alignment.center;
// appWindow.maximize();
// await loadWindowSizeAndPosition();
appWindow.show();
});
runApp(FikoYTApp());
} else {
runApp(FikoYTApp());
}
} else {
runApp(FikoYTApp());
}
}
static Future getDeviceID() async {
if (!kIsWeb) {
if (Platform.isAndroid) {
return await MobileDeviceIdentifier().getDeviceId();
} else {
return null;
}
} else {
final storage = html.window.localStorage;
String? deviceId = storage['device_id'];
if (deviceId == null) {
// Eğer yoksa benzersiz bir kimlik oluştur
deviceId = ObjectId().oid;
storage['device_id'] = deviceId; // UUID'yi kaydet
}
return deviceId;
}
}
static Future getDeviceInfo() async {
return await getAllDeviceInfo();
}
}
class FikoYTServiceSettings {
final List deviceOrientation;
final String fikoytServerUrl;
final String fikoytServerIstekOnEk;
final String fikoytWebSocketUrl;
final bool connectWebSocket;
final SystemUiMode systemUiMode;
final List systemUiOverlay;
final FikoYTNotificationSettings notificatinSettings;
final FikoYTAppSettings appSettings;
final Function(Intent? intent) onInitialIntent;
Function(IO.Socket socket)? socketMiddleWare;
Map? socketDataMiddleWare;
FikoYTServiceSettings({
this.deviceOrientation = DeviceOrientation.values,
this.fikoytServerUrl = "https://fikoyt.com:3000/",
this.fikoytServerIstekOnEk = "/uyar/api/v1",
this.fikoytWebSocketUrl = "https://fikoyt.com:3000/",
this.connectWebSocket = false,
this.systemUiMode = SystemUiMode.edgeToEdge,
this.systemUiOverlay = SystemUiOverlay.values,
FikoYTNotificationSettings? notificationSettings,
Function(Intent? intent)? onInitialIntent,
this.socketMiddleWare,
this.socketDataMiddleWare = const {},
appSettings,
}) : appSettings = appSettings ?? FikoYTAppSettings(),
notificatinSettings = notificationSettings ?? FikoYTNotificationSettings(),
onInitialIntent = onInitialIntent ?? defaultOnInitialIntent;
static defaultOnInitialIntent(Intent? intent) {}
}
class FikoYTNotificationSettings {
final List channels;
final Function(NotificationResponse notificationResponse) onDidReceiveNotificationResponse;
final Function(NotificationResponse notificationResponse) onDidReceiveBackNotificationResponse;
FikoYTNotificationSettings({this.channels = const [], onDidReceiveNotificationResponse, onDidReceiveBackNotificationResponse})
: onDidReceiveNotificationResponse = onDidReceiveNotificationResponse ?? defaultOnDidReceiveNotificationResponse,
onDidReceiveBackNotificationResponse = onDidReceiveBackNotificationResponse ?? defaultOnDidReceiveNotificationResponse;
@pragma('vm:entry-point')
static defaultOnDidReceiveNotificationResponse(NotificationResponse notificationResponse) async {}
}