From 650148d7311f2594732d5d0ef9a337f37f5af276 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Tue, 15 Jun 2021 12:58:50 -0700 Subject: [PATCH] new splash flow in conjunction with startCwtch changes in libcwtch-go; new version of flutter splash with error message display ability; cleaning old code --- LIBCWTCH-GO.version | 2 +- .../kotlin/im/cwtch/flwtch/FlwtchWorker.kt | 36 ++++++++----------- lib/cwtch/cwtch.dart | 7 +--- lib/cwtch/cwtchNotifier.dart | 10 +++++- lib/cwtch/ffi.dart | 32 +++-------------- lib/cwtch/gomobile.dart | 13 +------ lib/main.dart | 30 +++++++++------- lib/model.dart | 15 ++++++++ lib/views/splashView.dart | 29 +++++++++++++-- lib/widgets/profilerow.dart | 1 - 10 files changed, 90 insertions(+), 85 deletions(-) diff --git a/LIBCWTCH-GO.version b/LIBCWTCH-GO.version index 7b09870..bdd71f9 100644 --- a/LIBCWTCH-GO.version +++ b/LIBCWTCH-GO.version @@ -1 +1 @@ -v0.0.2-66-g39187a7-2021-06-15-00-32 \ No newline at end of file +v0.0.2-70-gc5f36a9-2021-06-15-18-07 \ No newline at end of file diff --git a/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt b/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt index ef78d83..3b8495a 100644 --- a/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt +++ b/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt @@ -54,7 +54,7 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : val torPath = (a.get("torPath") as? String) ?: "tor" Log.i("FlwtchWorker.kt", "appDir: '$appDir' torPath: '$torPath'") - if (Cwtch.startCwtch(appDir, torPath) != 0.toByte()) return Result.failure() + if (Cwtch.startCwtch(appDir, torPath) != 0.toLong()) return Result.failure() // infinite coroutine :) while(true) { @@ -62,13 +62,13 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : if (evt.EventType == "NewMessageFromPeer") { val data = JSONObject(evt.Data) val channelId = - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - createMessageNotificationChannel(data.getString("RemotePeer"), data.getString("RemotePeer")) - } else { - // If earlier version channel ID is not used - // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context) - "" - } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + createMessageNotificationChannel(data.getString("RemotePeer"), data.getString("RemotePeer")) + } else { + // If earlier version channel ID is not used + // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context) + "" + } val loader = FlutterInjector.instance().flutterLoader() val key = loader.getLookupKeyForAsset("assets/"+data.getString("Picture"))//"assets/profiles/001-centaur.png") @@ -82,13 +82,13 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : } val newNotification = NotificationCompat.Builder(applicationContext, channelId) - .setContentTitle(data.getString("Nick")) - .setContentText("New message") - .setLargeIcon(BitmapFactory.decodeStream(fh)) - .setSmallIcon(R.mipmap.knott) - .setContentIntent(PendingIntent.getActivity(applicationContext, 1, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT)) - .setAutoCancel(true) - .build() + .setContentTitle(data.getString("Nick")) + .setContentText("New message") + .setLargeIcon(BitmapFactory.decodeStream(fh)) + .setSmallIcon(R.mipmap.knott) + .setContentIntent(PendingIntent.getActivity(applicationContext, 1, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT)) + .setAutoCancel(true) + .build() notificationManager.notify(getNotificationID(data.getString("ProfileOnion"), data.getString("RemotePeer")), newNotification) } @@ -104,10 +104,6 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : "ReconnectCwtchForeground" -> { Cwtch.reconnectCwtchForeground() } - "SelectProfile" -> { - val onion = (a.get("profile") as? String) ?: ""; - Cwtch.selectProfile(onion) - } "CreateProfile" -> { val nick = (a.get("nick") as? String) ?: ""; val pass = (a.get("pass") as? String) ?: ""; @@ -118,8 +114,6 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : Cwtch.loadProfiles(pass) } "GetProfiles" -> Result.success(Data.Builder().putString("result", Cwtch.getProfiles()).build()) - // "ACNEvents" -> result.success(Cwtch.acnEvents()) - "ContactEvents" -> Result.success(Data.Builder().putString("result", Cwtch.contactEvents()).build()) "NumMessages" -> { val profile = (a.get("profile") as? String) ?: ""; val handle = (a.get("contact") as? String) ?: ""; diff --git a/lib/cwtch/cwtch.dart b/lib/cwtch/cwtch.dart index 3e866da..c3477ce 100644 --- a/lib/cwtch/cwtch.dart +++ b/lib/cwtch/cwtch.dart @@ -2,7 +2,7 @@ import 'package:flutter/src/services/text_input.dart'; abstract class Cwtch { // ignore: non_constant_identifier_names - Future Start(); + Future Start(); // ignore: non_constant_identifier_names Future ReconnectCwtchForeground(); @@ -31,11 +31,6 @@ abstract class Cwtch { // ignore: non_constant_identifier_names void DebugResetContact(String profileOnion, String contactHandle); - // ignore: non_constant_identifier_names - Future ACNEvents(); - // ignore: non_constant_identifier_names - Future ContactEvents(); - // ignore: non_constant_identifier_names Future GetProfiles(); diff --git a/lib/cwtch/cwtchNotifier.dart b/lib/cwtch/cwtchNotifier.dart index 05dc60e..a8bb81a 100644 --- a/lib/cwtch/cwtchNotifier.dart +++ b/lib/cwtch/cwtchNotifier.dart @@ -17,17 +17,25 @@ class CwtchNotifier { late ErrorHandler error; late TorStatus torStatus; late NotificationsManager notificationManager; + late AppState appState; - CwtchNotifier(ProfileListState pcn, Settings settingsCN, ErrorHandler errorCN, TorStatus torStatusCN, NotificationsManager notificationManagerP) { + CwtchNotifier(ProfileListState pcn, Settings settingsCN, ErrorHandler errorCN, TorStatus torStatusCN, NotificationsManager notificationManagerP, AppState appStateCN) { profileCN = pcn; settings = settingsCN; error = errorCN; torStatus = torStatusCN; notificationManager = notificationManagerP; + appState = appStateCN; } void handleMessage(String type, dynamic data) { switch (type) { + case "CwtchStarted": + appState.SetCwtchInit(); + break; + case "CwtchStartError": + appState.SetAppError(data["Error"]); + break; case "NewPeer": profileCN.add(ProfileInfoState( onion: data["Identity"], nickname: data["name"], imagePath: data["picture"], contactsJson: data["ContactsJson"], serversJson: data["ServerList"], online: data["Online"] == "true")); diff --git a/lib/cwtch/ffi.dart b/lib/cwtch/ffi.dart index c0f7225..a15fd7d 100644 --- a/lib/cwtch/ffi.dart +++ b/lib/cwtch/ffi.dart @@ -58,8 +58,8 @@ typedef GetJsonBlobFromStrStrIntFn = Pointer Function(Pointer, int, typedef get_json_blob_from_str_str_int_int_function = Pointer Function(Pointer, Int32, Pointer, Int32, Int32, Int32); typedef GetJsonBlobFromStrStrIntIntFn = Pointer Function(Pointer, int, Pointer, int, int, int); -typedef acn_events_function = Pointer Function(); -typedef ACNEventsFn = Pointer Function(); +typedef appbus_events_function = Pointer Function(); +typedef AppbusEventsFn = Pointer Function(); class CwtchFfi implements Cwtch { late DynamicLibrary library; @@ -80,7 +80,7 @@ class CwtchFfi implements Cwtch { } // ignore: non_constant_identifier_names - Future Start() async { + Future Start() async { String home = ""; String bundledTor = ""; Map envVars = Platform.environment; @@ -146,9 +146,9 @@ class CwtchFfi implements Cwtch { library = DynamicLibrary.open("libCwtch.so"); } - var getAppbusEventC = library.lookup>("c_GetAppBusEvent"); + var getAppbusEventC = library.lookup>("c_GetAppBusEvent"); // ignore: non_constant_identifier_names - final GetAppbusEvent = getAppbusEventC.asFunction(); + final GetAppbusEvent = getAppbusEventC.asFunction(); while (true) { Pointer result = GetAppbusEvent(); @@ -185,28 +185,6 @@ class CwtchFfi implements Cwtch { LoadProfiles(ut8pass, ut8pass.length); } - // ignore: non_constant_identifier_names - Future ACNEvents() async { - var acnEventsC = library.lookup>("c_ACNEvents"); - // ignore: non_constant_identifier_names - final ACNEvents = acnEventsC.asFunction(); - - Pointer result = ACNEvents(); - String event = result.toDartString(); - return event; - } - - // ignore: non_constant_identifier_names - Future ContactEvents() async { - var acnEventsC = library.lookup>("c_ContactEvents"); - // ignore: non_constant_identifier_names - final ContactEvents = acnEventsC.asFunction(); - - Pointer result = ContactEvents(); - String event = result.toDartString(); - return event; - } - // ignore: non_constant_identifier_names Future GetProfiles() async { var getProfilesC = library.lookup>("c_GetProfiles"); diff --git a/lib/cwtch/gomobile.dart b/lib/cwtch/gomobile.dart index b37ade2..da389bd 100644 --- a/lib/cwtch/gomobile.dart +++ b/lib/cwtch/gomobile.dart @@ -42,7 +42,7 @@ class CwtchGomobile implements Cwtch { } // ignore: non_constant_identifier_names - Future Start() async { + Future Start() async { print("gomobile.dart: Start()..."); var cwtchDir = path.join((await androidHomeDirectory).path, ".cwtch"); if (EnvironmentConfig.BUILD_VER == dev_version) { @@ -86,17 +86,6 @@ class CwtchGomobile implements Cwtch { cwtchPlatform.invokeMethod("DeleteProfile", {"onion": onion, "pass": pass}); } - // ignore: non_constant_identifier_names - Future ACNEvents() { - return cwtchPlatform.invokeMethod("ACNEvents"); - } - - // ignore: non_constant_identifier_names - Future ContactEvents() { - return cwtchPlatform.invokeMethod("ContactEvents"); - } - - // ignore: non_constant_identifier_names Future GetProfiles() { print("gomobile.dart: GetProfiles()"); return cwtchPlatform.invokeMethod("GetProfiles"); diff --git a/lib/main.dart b/lib/main.dart index 25ad0f5..9e804af 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'dart:isolate'; import 'package:cwtch/notification_manager.dart'; import 'package:cwtch/views/messageview.dart'; @@ -26,9 +27,12 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart'; var globalSettings = Settings(Locale("en", ''), Opaque.dark); var globalErrorHandler = ErrorHandler(); var globalTorStatus = TorStatus(); +var globalAppState = AppState(); void main() { + print("main()"); LicenseRegistry.addLicense(() => licenses()); + print("runApp()"); runApp(Flwtch()); } @@ -42,7 +46,6 @@ class Flwtch extends StatefulWidget { 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 @@ -53,37 +56,37 @@ class FlwtchState extends State { @override initState() { + print("Init state"); super.initState(); - cwtchInit = false; profs = ProfileListState(); notificationClickChannel.setMethodCallHandler(_externalNotificationClicked); - + print("initState set cwtch..."); if (Platform.isAndroid) { - var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, NullNotificationsManager()); + var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, NullNotificationsManager(), globalAppState); cwtch = CwtchGomobile(cwtchNotifier); } else if (Platform.isLinux) { - var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, LinuxNotificationsManager()); + var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, LinuxNotificationsManager(), globalAppState); cwtch = CwtchFfi(cwtchNotifier); } else { - var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, NullNotificationsManager()); + var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, NullNotificationsManager(), globalAppState); cwtch = CwtchFfi(cwtchNotifier); } - + print("initState cwtch.Start()..."); cwtch.Start(); - setState(() { - cwtchInit = true; - }); + print("initState done!"); } ChangeNotifierProvider getTorStatusProvider() => ChangeNotifierProvider.value(value: globalTorStatus); ChangeNotifierProvider getErrorHandlerProvider() => ChangeNotifierProvider.value(value: globalErrorHandler); ChangeNotifierProvider getSettingsProvider() => ChangeNotifierProvider.value(value: globalSettings); + ChangeNotifierProvider getAppStateProvider() => ChangeNotifierProvider.value(value: globalAppState); Provider getFlwtchStateProvider() => Provider(create: (_) => this); ChangeNotifierProvider getProfileListProvider() => ChangeNotifierProvider(create: (context) => profs); @override Widget build(BuildContext context) { + print("build()"); //appStatus = AppModel(cwtch: cwtch); globalSettings.initPackageInfo(); return MultiProvider( @@ -93,10 +96,11 @@ class FlwtchState extends State { getSettingsProvider(), getErrorHandlerProvider(), getTorStatusProvider(), + getAppStateProvider(), ], builder: (context, widget) { - return Consumer( - builder: (context, settings, child) => MaterialApp( + return Consumer2( + builder: (context, settings, appState, child) => MaterialApp( key: Key('app'), navigatorKey: navKey, locale: settings.locale, @@ -104,7 +108,7 @@ class FlwtchState extends State { supportedLocales: AppLocalizations.supportedLocales, title: 'Cwtch', theme: mkThemeData(settings), - home: cwtchInit == true ? (columns.length == 3 ? TripleColumnView() : ShiftRightFixer(child: ProfileMgrView())) : SplashView(), + home: appState.cwtchInit == true ? (columns.length == 3 ? TripleColumnView() : ShiftRightFixer(child: ProfileMgrView())) : SplashView(), ), ); }, diff --git a/lib/model.dart b/lib/model.dart index db689da..93d6f34 100644 --- a/lib/model.dart +++ b/lib/model.dart @@ -61,6 +61,21 @@ class ProfileListState extends ChangeNotifier { } } +class AppState extends ChangeNotifier { + bool cwtchInit = false; + String appError = ""; + + void SetCwtchInit() { + cwtchInit = true; + notifyListeners(); + } + + void SetAppError(String error) { + appError = error; + notifyListeners(); + } +} + class ContactListState extends ChangeNotifier { List _contacts = []; String _filter = ""; diff --git a/lib/views/splashView.dart b/lib/views/splashView.dart index 3b18b5d..5ae7d17 100644 --- a/lib/views/splashView.dart +++ b/lib/views/splashView.dart @@ -1,11 +1,34 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; + +import '../model.dart'; +import '../settings.dart'; class SplashView extends StatelessWidget { @override Widget build(BuildContext context) { - return const Scaffold( - body: const Center(child: const Text("Loading Cwtch...")), - ); + return Consumer( + builder: (context, appState, child) => Scaffold( + body: Center(child: Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + Image(image: AssetImage("assets/knott.png"), + filterQuality: FilterQuality.medium, + isAntiAlias: true, + width: 200, + height: 200,), + Image(image: AssetImage("assets/cwtch_title.png"), + filterQuality: FilterQuality.medium, + isAntiAlias: true,), + Padding( + padding: const EdgeInsets.all(20.0), + child: Text(appState.appError == "" ? "Loading Cwtch..." : appState.appError, + style: TextStyle(fontSize: 16.0, color: appState.appError == "" ? Provider.of(context).theme.mainTextColor() : Provider.of(context).theme.textfieldErrorColor())), + ), + Image(image: AssetImage("assets/Open_Privacy_Logo_lightoutline.png")), + ])), + )); } } diff --git a/lib/widgets/profilerow.dart b/lib/widgets/profilerow.dart index 7edbc36..36345c3 100644 --- a/lib/widgets/profilerow.dart +++ b/lib/widgets/profilerow.dart @@ -66,7 +66,6 @@ class _ProfileRowState extends State { onTap: () { setState(() { var flwtch = Provider.of(context, listen: false); - flwtch.cwtch.SelectProfile(profile.onion); flwtch.setState(() { flwtch.selectedProfile = profile; flwtch.selectedConversation = "";