import 'dart:convert'; import 'package:cwtch/notification_manager.dart'; import 'package:cwtch/views/messageview.dart'; import 'package:cwtch/widgets/rightshiftfixer.dart'; import 'package:flutter/foundation.dart'; import 'package:cwtch/cwtch/ffi.dart'; import 'package:cwtch/cwtch/gomobile.dart'; import 'package:flutter/material.dart'; import 'package:cwtch/errorHandler.dart'; import 'package:cwtch/settings.dart'; import 'package:cwtch/torstatus.dart'; import 'package:cwtch/views/triplecolview.dart'; import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; import 'cwtch/cwtch.dart'; import 'cwtch/cwtchNotifier.dart'; import 'licenses.dart'; import 'model.dart'; import 'views/profilemgrview.dart'; import 'views/splashView.dart'; import 'dart:io' show Platform; import 'opaque.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; var globalSettings = Settings(Locale("en", ''), Opaque.dark); var globalErrorHandler = ErrorHandler(); var globalTorStatus = TorStatus(); void main() { LicenseRegistry.addLicense(() => licenses()); runApp(Flwtch()); } class Flwtch extends StatefulWidget { final Key flwtch = GlobalKey(); @override FlwtchState createState() => FlwtchState(); } class FlwtchState extends State { final TextStyle biggerFont = const TextStyle(fontSize: 18); late Cwtch cwtch; bool cwtchInit = false; late ProfileInfoState selectedProfile; String selectedConversation = ""; var columns = [1]; // default or 'single column' mode //var columns = [1, 1, 2]; late ProfileListState profs; final MethodChannel notificationClickChannel = MethodChannel('im.cwtch.flwtch/notificationClickHandler'); final GlobalKey navKey = GlobalKey(); @override initState() { super.initState(); cwtchInit = false; profs = ProfileListState(); notificationClickChannel.setMethodCallHandler(_externalNotificationClicked); if (Platform.isAndroid) { var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, NullNotificationsManager()); cwtch = CwtchGomobile(cwtchNotifier); } else if (Platform.isLinux) { var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, LinuxNotificationsManager()); cwtch = CwtchFfi(cwtchNotifier); } else { var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, NullNotificationsManager()); cwtch = CwtchFfi(cwtchNotifier); } cwtch.Start(); setState(() { cwtchInit = true; }); } ChangeNotifierProvider getTorStatusProvider() => ChangeNotifierProvider.value(value: globalTorStatus); ChangeNotifierProvider getErrorHandlerProvider() => ChangeNotifierProvider.value(value: globalErrorHandler); ChangeNotifierProvider getSettingsProvider() => ChangeNotifierProvider.value(value: globalSettings); Provider getFlwtchStateProvider() => Provider(create: (_) => this); ChangeNotifierProvider getProfileListProvider() => ChangeNotifierProvider(create: (context) => profs); @override Widget build(BuildContext context) { //appStatus = AppModel(cwtch: cwtch); globalSettings.initPackageInfo(); return MultiProvider( providers: [ getFlwtchStateProvider(), getProfileListProvider(), getSettingsProvider(), getErrorHandlerProvider(), getTorStatusProvider(), ], builder: (context, widget) { return Consumer( builder: (context, settings, child) => MaterialApp( key: Key('app'), navigatorKey: navKey, locale: settings.locale, localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, title: 'Cwtch', theme: mkThemeData(settings), home: cwtchInit == true ? (columns.length == 3 ? TripleColumnView() : ShiftRightFixer(child: ProfileMgrView())) : SplashView(), ), ); }, ); } Future _externalNotificationClicked(MethodCall call) async { var args = jsonDecode(call.arguments); var profile = profs.getProfile(args["ProfileOnion"])!; var contact = profile.contactList.getContact(args["RemotePeer"])!; contact.unreadMessages = 0; navKey.currentState?.push( MaterialPageRoute( builder: (BuildContext builderContext) { return MultiProvider( providers: [ ChangeNotifierProvider.value(value: profile), ChangeNotifierProvider.value(value: contact), ], builder: (context, child) => MessageView(), ); }, ), ); } @override void dispose() { cwtch.dispose(); super.dispose(); } }