From 01f0a4741df1d024135b57e70fedaf0b80eb57b7 Mon Sep 17 00:00:00 2001 From: Vonarian <68429735+Vonarian@users.noreply.github.com> Date: Wed, 18 May 2022 16:21:07 +0430 Subject: [PATCH] Design improvement, downloader.dart fix, data mine notifier under test --- assets/install/version.txt | 2 +- lib/main.dart | 4 +- lib/pages/downloader.dart | 76 ++++++++++++++++++++++---------------- lib/pages/rss_feed.dart | 1 - lib/pages/settings.dart | 13 +++---- lib/providers.dart | 2 +- lib/widgets/titlebar.dart | 54 +++++++++++++++------------ 7 files changed, 85 insertions(+), 67 deletions(-) diff --git a/assets/install/version.txt b/assets/install/version.txt index a1d55a5..e113081 100644 --- a/assets/install/version.txt +++ b/assets/install/version.txt @@ -1 +1 @@ -1.7.8.0 \ No newline at end of file +1.7.9.0 \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 264f897..6b357f8 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -49,7 +49,7 @@ Future main() async { await Window.hideWindowControls(); await Window.setEffect( effect: WindowEffect.aero, - color: Colors.black.withOpacity(0.45), + color: Colors.black.withOpacity(0.55), ); }); await protocolHandler.register('wtnews'); @@ -73,7 +73,7 @@ Future main() async { options.tracesSampleRate = 1.0; options.enableAutoSessionTracking = true; options.enableOutOfMemoryTracking = true; - options.reportPackages = false; + options.reportPackages = true; options.release = 'WTNews@${file.readAsStringSync()}'; options.tracesSampler = (samplingContext) { return 1.0; diff --git a/lib/pages/downloader.dart b/lib/pages/downloader.dart index 66a4eaf..fce412a 100644 --- a/lib/pages/downloader.dart +++ b/lib/pages/downloader.dart @@ -52,14 +52,21 @@ class _DownloaderState extends State await windowManager.center(); try { Data data = await Data.getData(); - + final deleteFile = File(p.joinAll( + [p.dirname(Platform.resolvedExecutable), 'data\\update.zip'])); + final deleteFolder = Directory( + p.joinAll([p.dirname(Platform.resolvedExecutable), 'data\\out'])); + if (await deleteFile.exists() || await deleteFolder.exists()) { + await deleteFile.delete(recursive: true); + await deleteFolder.delete(recursive: true); + } Dio dio = Dio(); await dio.download(data.assets.last.browserDownloadUrl, '${p.dirname(Platform.resolvedExecutable)}/data/update.zip', onReceiveProgress: (downloaded, full) async { progress = downloaded / full * 100; setState(() {}); - }).whenComplete(() async { + }, deleteOnError: true).whenComplete(() async { final File filePath = File('${p.dirname(Platform.resolvedExecutable)}/data/update.zip'); final Uint8List bytes = await File( @@ -96,7 +103,6 @@ class _DownloaderState extends State duration: const Duration(seconds: 10), content: Text(e.toString()))); await Sentry.captureException(e, stackTrace: st); - error = true; setState(() {}); } @@ -106,38 +112,44 @@ class _DownloaderState extends State double progress = 0; @override Widget build(BuildContext context) { - return Scaffold( - backgroundColor: Colors.blueGrey, - body: Center( - child: SizedBox( - height: 200, - width: 200, - child: CircularPercentIndicator( - center: !error - ? Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text( - 'Downloading', + return GestureDetector( + behavior: HitTestBehavior.translucent, + onPanStart: (details) { + windowManager.startDragging(); + }, + child: Scaffold( + backgroundColor: Colors.transparent, + body: Center( + child: SizedBox( + height: 200, + width: 200, + child: CircularPercentIndicator( + center: !error + ? Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text( + 'Downloading', + style: TextStyle(fontSize: 15), + ), + Text( + '${progress.toStringAsFixed(1)} %', + style: const TextStyle(fontSize: 15), + ), + ], + ) + : const Center( + child: Text( + 'ERROR', style: TextStyle(fontSize: 15), ), - Text( - '${progress.toStringAsFixed(1)} %', - style: const TextStyle(fontSize: 15), - ), - ], - ) - : const Center( - child: Text( - 'ERROR', - style: TextStyle(fontSize: 15), ), - ), - backgroundColor: Colors.blue, - percent: double.parse(progress.toStringAsFixed(0)) / 100, - radius: 100, - )), - )); + backgroundColor: Colors.blue, + percent: double.parse(progress.toStringAsFixed(0)) / 100, + radius: 100, + )), + )), + ); } final bool _showWindowBelowTrayIcon = false; diff --git a/lib/pages/rss_feed.dart b/lib/pages/rss_feed.dart index cefd962..c214bb1 100644 --- a/lib/pages/rss_feed.dart +++ b/lib/pages/rss_feed.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'dart:io'; -import 'dart:ui'; import 'package:device_info_plus/device_info_plus.dart'; import 'package:dio/dio.dart'; diff --git a/lib/pages/settings.dart b/lib/pages/settings.dart index c979e4d..f8bf077 100644 --- a/lib/pages/settings.dart +++ b/lib/pages/settings.dart @@ -1,5 +1,4 @@ import 'dart:io'; -import 'dart:ui'; import 'package:device_info_plus/device_info_plus.dart'; import 'package:flutter/material.dart'; @@ -28,9 +27,9 @@ class _SettingsState extends ConsumerState { void initState() { super.initState(); WidgetsBinding.instance!.addPostFrameCallback((_) async { - ref.read(isStartupEnabled.notifier).state = + ref.read(startupEnabled.notifier).state = prefs.getBool('startup') ?? false; - if (ref.read(isStartupEnabled.notifier).state) { + if (ref.read(startupEnabled.notifier).state) { await Process.run(pathToUpdateShortcut, []); } ref.read(playSound.notifier).state = prefs.getBool('playSound') ?? false; @@ -86,7 +85,7 @@ class _SettingsState extends ConsumerState { title: const Text('Main'), tiles: [ SettingsTile.switchTile( - initialValue: ref.watch(isStartupEnabled), + initialValue: ref.watch(startupEnabled), onToggle: (value) { if (value) { Process.run(pathToUpdateShortcut, []); @@ -95,7 +94,7 @@ class _SettingsState extends ConsumerState { ref.read(minimizeOnStart.notifier).state = false; prefs.setBool('minimize', false); } - ref.read(isStartupEnabled.notifier).state = value; + ref.read(startupEnabled.notifier).state = value; prefs.setBool('startup', value); }, title: const Text('Run at Startup'), @@ -125,10 +124,10 @@ class _SettingsState extends ConsumerState { ), SettingsSection(tiles: [ SettingsTile.switchTile( - initialValue: prefs.getBool('checkDataMine'), + initialValue: ref.watch(checkDataMine), onToggle: (value) async { prefs.setBool('checkDataMine', value); - ref.read(playSound.notifier).state = value; + ref.read(checkDataMine.notifier).state = value; }, title: const Text('DataMine Notifier'), leading: Image.asset( diff --git a/lib/providers.dart b/lib/providers.dart index a3ac907..04257a7 100644 --- a/lib/providers.dart +++ b/lib/providers.dart @@ -1,6 +1,6 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; -final StateProvider isStartupEnabled = StateProvider((ref) => false); +final StateProvider startupEnabled = StateProvider((ref) => false); final StateProvider playSound = StateProvider((ref) => true); final StateProvider minimizeOnStart = StateProvider((ref) => false); final StateProvider checkDataMine = StateProvider((ref) => false); diff --git a/lib/widgets/titlebar.dart b/lib/widgets/titlebar.dart index 39b98b7..6ef3d25 100644 --- a/lib/widgets/titlebar.dart +++ b/lib/widgets/titlebar.dart @@ -59,21 +59,32 @@ class _WindowTitleBarState extends ConsumerState ref.read(checkDataMine.notifier).state = prefs.getBool('checkDataMine') ?? false; }); - + lastPubDate.addListener(() async { + await notify(lastPubDate.value!, lastItemLink!); + await prefs.setString('previous', lastPubDate.value!); + }); Timer.periodic(const Duration(seconds: 10), (timer) async { if (ref.watch(checkDataMine)) { - rssFeed = await getDataMine(); - if (rssFeed != null && rssFeed?.items != null) { - RssItem item = rssFeed!.items!.first; - if (item.description != null) { - String itemDescription = item.description!; - bool? isDataMine = (itemDescription.contains('Raw changes:') && - itemDescription.contains('→') && - itemDescription.contains('Current dev version')) || - itemDescription.contains(' '); - if (isDataMine) notify(item.pubDate!, item.link!); + rssFeed = await getDataMine() + .timeout(const Duration(seconds: 10)) + .whenComplete(() async { + if (rssFeed != null && rssFeed?.items != null) { + RssItem item = rssFeed!.items!.first; + if (item.description != null) { + String itemDescription = item.description!; + bool? isDataMine = (itemDescription.contains('Raw changes:') && + itemDescription.contains('→') && + itemDescription.contains('Current dev version')) || + itemDescription.contains(' '); + if (isDataMine) { + if (lastPubDate.value != item.pubDate.toString()) { + lastPubDate.value = item.pubDate.toString(); + lastItemLink = item.link; + } + } + } } - } + }); } }); } @@ -86,17 +97,14 @@ class _WindowTitleBarState extends ConsumerState return rssFeed; } - Future notify(DateTime pubDate, String url) async { + Future notify(String pubDate, String url) async { if (prefs.getString('previous') != null) { - String? previous = prefs.getString('previous'); - - bool isNew = - previous != pubDate.toString() && pubDate.toString() != lastPubDate; - if (isNew) { + if (prefs.getString('previous') != pubDate) { + String? previous = prefs.getString('previous'); if (kDebugMode) { print('isNew'); print(previous); - print(pubDate.toString()); + print(pubDate); } AppUtil().playSound(newSound); @@ -110,9 +118,8 @@ class _WindowTitleBarState extends ConsumerState launchUrl(Uri.parse(url)); } }); - await prefs.setString('previous', pubDate.toString()); + await prefs.setString('previous', pubDate); } - lastPubDate = pubDate.toString(); } else { if (kDebugMode) { print('Null'); @@ -128,7 +135,6 @@ class _WindowTitleBarState extends ConsumerState } }); await prefs.setString('previous', pubDate.toString()); - lastPubDate = pubDate.toString(); } } @@ -137,10 +143,12 @@ class _WindowTitleBarState extends ConsumerState trayManager.removeListener(this); windowManager.removeListener(this); protocolHandler.removeListener(this); + lastPubDate.removeListener(() {}); super.dispose(); } - String? lastPubDate; + ValueNotifier lastPubDate = ValueNotifier(null); + String? lastItemLink; RssFeed? rssFeed; @override Widget build(BuildContext context) {