flutter_app/lib/main.dart

140 lines
4.9 KiB
Dart
Raw Normal View History

import 'dart:convert';
2021-05-25 20:43:13 +00:00
import 'package:cwtch/notification_manager.dart';
import 'package:cwtch/views/messageview.dart';
import 'package:cwtch/widgets/rightshiftfixer.dart';
2021-03-16 19:54:48 +00:00
import 'package:flutter/foundation.dart';
2021-05-19 21:39:52 +00:00
import 'package:cwtch/cwtch/ffi.dart';
import 'package:cwtch/cwtch/gomobile.dart';
import 'package:flutter/material.dart';
2021-05-19 21:39:52 +00:00
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';
2021-03-16 19:54:48 +00:00
import 'licenses.dart';
import 'model.dart';
import 'views/profilemgrview.dart';
import 'views/splashView.dart';
import 'dart:io' show Platform;
2021-01-21 20:37:35 +00:00
import 'opaque.dart';
2021-02-23 02:55:10 +00:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2021-02-25 23:33:15 +00:00
2021-03-16 21:04:14 +00:00
var globalSettings = Settings(Locale("en", ''), Opaque.dark);
2021-03-24 23:35:24 +00:00
var globalErrorHandler = ErrorHandler();
2021-04-13 22:29:23 +00:00
var globalTorStatus = TorStatus();
2021-03-10 17:40:14 +00:00
2021-03-16 19:54:48 +00:00
void main() {
LicenseRegistry.addLicense(() => licenses());
runApp(Flwtch());
}
class Flwtch extends StatefulWidget {
final Key flwtch = GlobalKey();
@override
FlwtchState createState() => FlwtchState();
}
class FlwtchState extends State<Flwtch> {
final TextStyle biggerFont = const TextStyle(fontSize: 18);
2021-05-25 00:11:39 +00:00
late Cwtch cwtch;
bool cwtchInit = false;
2021-05-25 00:11:39 +00:00
late ProfileInfoState selectedProfile;
2021-01-21 20:37:35 +00:00
String selectedConversation = "";
2021-01-27 20:18:28 +00:00
var columns = [1]; // default or 'single column' mode
2021-01-21 20:37:35 +00:00
//var columns = [1, 1, 2];
2021-05-25 00:11:39 +00:00
late ProfileListState profs;
final MethodChannel notificationClickChannel = MethodChannel('im.cwtch.flwtch/notificationClickHandler');
final GlobalKey<NavigatorState> navKey = GlobalKey<NavigatorState>();
@override
initState() {
super.initState();
cwtchInit = false;
profs = ProfileListState();
notificationClickChannel.setMethodCallHandler(_externalNotificationClicked);
if (Platform.isAndroid) {
2021-05-25 20:43:13 +00:00
var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, NullNotificationsManager());
cwtch = CwtchGomobile(cwtchNotifier);
2021-05-25 20:43:13 +00:00
} else if (Platform.isLinux) {
var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, LinuxNotificationsManager());
cwtch = CwtchFfi(cwtchNotifier);
} else {
2021-05-25 20:43:13 +00:00
var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, NullNotificationsManager());
cwtch = CwtchFfi(cwtchNotifier);
}
cwtch.Start();
setState(() {
cwtchInit = true;
});
2021-01-27 20:18:28 +00:00
}
2021-04-13 22:29:23 +00:00
ChangeNotifierProvider<TorStatus> getTorStatusProvider() => ChangeNotifierProvider.value(value: globalTorStatus);
2021-03-24 23:35:24 +00:00
ChangeNotifierProvider<ErrorHandler> getErrorHandlerProvider() => ChangeNotifierProvider.value(value: globalErrorHandler);
2021-03-17 22:54:41 +00:00
ChangeNotifierProvider<Settings> getSettingsProvider() => ChangeNotifierProvider.value(value: globalSettings);
2021-03-16 23:33:03 +00:00
Provider<FlwtchState> getFlwtchStateProvider() => Provider<FlwtchState>(create: (_) => this);
ChangeNotifierProvider<ProfileListState> getProfileListProvider() => ChangeNotifierProvider(create: (context) => profs);
2021-01-27 20:18:28 +00:00
@override
Widget build(BuildContext context) {
2021-03-23 22:42:10 +00:00
//appStatus = AppModel(cwtch: cwtch);
globalSettings.initPackageInfo();
2021-01-27 20:18:28 +00:00
return MultiProvider(
2021-05-25 20:43:13 +00:00
providers: [
getFlwtchStateProvider(),
getProfileListProvider(),
getSettingsProvider(),
getErrorHandlerProvider(),
getTorStatusProvider(),
],
builder: (context, widget) {
2021-03-10 17:40:14 +00:00
return Consumer<Settings>(
builder: (context, settings, child) => MaterialApp(
2021-04-21 00:24:28 +00:00
key: Key('app'),
navigatorKey: navKey,
locale: settings.locale,
2021-02-23 02:55:10 +00:00
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
title: 'Cwtch',
theme: mkThemeData(settings),
home: cwtchInit == true ? (columns.length == 3 ? TripleColumnView() : ShiftRightFixer(child: ProfileMgrView())) : SplashView(),
),
);
},
2021-01-27 20:18:28 +00:00
);
}
2021-03-23 22:42:10 +00:00
Future<void> _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<void>(
builder: (BuildContext builderContext) {
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: profile),
ChangeNotifierProvider.value(value: contact),
],
builder: (context, child) => MessageView(),
);
},
),
);
}
2021-03-23 22:42:10 +00:00
@override
void dispose() {
cwtch.dispose();
super.dispose();
}
2021-01-27 20:18:28 +00:00
}