Skip to content

Commit

Permalink
Add legacy update mode to use http request interval on demand, add fa…
Browse files Browse the repository at this point in the history
…llback methods when any of the servers are not available. Switch to my own server.
  • Loading branch information
Vonarian committed Apr 23, 2023
1 parent 8f450b0 commit 943d892
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ app.*.map.json
local.properties
/.vscode/launch.json
/.vscode/settings.json
# Temporarily used
lib/services/data/vps_data.dart
41 changes: 34 additions & 7 deletions lib/services/data/news.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'dart:convert';
import 'dart:developer';

import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

import '../../main.dart';
// Currently private
import 'vps_data.dart' as vps;

class News extends Equatable {
final String title;
Expand Down Expand Up @@ -55,21 +58,45 @@ class News extends Equatable {

static WebSocketChannel connectNews() {
//Get from news section
final channel = WebSocketChannel.connect(
Uri.parse('wss://wtnews-pro.vonarian.workers.dev/ws?onlyUpdates=true'),
);
final channel = WebSocketChannel.connect(Uri.parse('ws://${vps.address}'));
return channel;
}

static WebSocketChannel connectChangelog() {
//Get from changelog section
final channel = WebSocketChannel.connect(
Uri.parse(
'wss://wtnews-changelog-pro.vonarian.workers.dev/ws?onlyUpdates=true'),
);
final channel = WebSocketChannel.connect(Uri.parse('ws://${vps.address}'));
return channel;
}

static Future<List<News>> getAllNews() async {
List<News> news = [];
try {
final response = await dio
.get('http://${vps.address}')
.timeout(const Duration(seconds: 7));
final jsonList = jsonDecode(response.data) as List;
news = (jsonList.map((e) => News.fromJson(e))).toList();
} catch (e, st) {
log(e.toString(), stackTrace: st);
news = await getAllNewsFallback();
}
return news;
}

static Future<List<News>> getAllNewsFallback() async {
List<News> list = [];
try {
final news = await getNews();
final changelog = await getChangelog();
final finalList = [...news, ...changelog]
..sort((a, b) => a.date.compareTo(b.date));
list.addAll(finalList);
} catch (e) {
//
}
return list;
}

static Future<List<News>> getNews() async {
final responseNews =
await dio.get('https://wtnews-server.vonarian.workers.dev/');
Expand Down
33 changes: 21 additions & 12 deletions lib/services/preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class PreferencesNotifier extends StateNotifier<Preferences> {
PaneDisplayMode? paneDisplayMode,
bool? openInsideApp,
String? username,
bool? disableBackgroundTransparency}) {
bool? disableBackgroundTransparency,
bool? legacyUpdate}) {
state = state.copyWith(
runAtStartup: runAtStartup ?? state.runAtStartup,
focusedMode: focusedMode ?? state.focusedMode,
Expand All @@ -42,6 +43,7 @@ class PreferencesNotifier extends StateNotifier<Preferences> {
username: username ?? state.username,
disableBackgroundTransparency:
disableBackgroundTransparency ?? state.disableBackgroundTransparency,
legacyUpdate: legacyUpdate ?? state.legacyUpdate,
);
save();
}
Expand All @@ -59,18 +61,21 @@ class Preferences {
final String? username;
final bool openInsideApp;
final bool disableBackgroundTransparency;
final bool legacyUpdate;

const Preferences(
{this.runAtStartup = false,
this.minimizeAtStartup = true,
this.playSound = true,
this.focusedMode = false,
this.readNewTitle = false,
this.readNewCaption = false,
this.paneDisplayMode = PaneDisplayMode.auto,
this.openInsideApp = false,
this.username,
this.disableBackgroundTransparency = false});
const Preferences({
this.runAtStartup = false,
this.minimizeAtStartup = true,
this.playSound = true,
this.focusedMode = false,
this.readNewTitle = false,
this.readNewCaption = false,
this.paneDisplayMode = PaneDisplayMode.auto,
this.openInsideApp = false,
this.username,
this.disableBackgroundTransparency = false,
this.legacyUpdate = false,
});

Map<String, dynamic> toMap() {
return {
Expand All @@ -84,6 +89,7 @@ class Preferences {
'username': username,
'openInsideApp': openInsideApp,
'disableBackgroundTransparency': disableBackgroundTransparency,
'legacyUpdate': legacyUpdate,
};
}

Expand All @@ -106,6 +112,7 @@ class Preferences {
username: map['username'],
disableBackgroundTransparency:
map['disableBackgroundTransparency'] ?? false,
legacyUpdate: map['legacyUpdate'] ?? false,
);
}

Expand All @@ -120,6 +127,7 @@ class Preferences {
String? username,
bool? openInsideApp,
bool? disableBackgroundTransparency,
bool? legacyUpdate,
}) {
return Preferences(
runAtStartup: runAtStartup ?? this.runAtStartup,
Expand All @@ -133,6 +141,7 @@ class Preferences {
username: username ?? this.username,
disableBackgroundTransparency:
disableBackgroundTransparency ?? this.disableBackgroundTransparency,
legacyUpdate: legacyUpdate ?? this.legacyUpdate,
);
}
}
21 changes: 21 additions & 0 deletions lib/widgets/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,27 @@ class SettingsState extends ConsumerState<Settings> {
leadingContent: true,
content: Text(prefs.readNewCaption ? 'On' : 'Off')),
),
Text(
'Advanced',
style: theme.typography.bodyStrong,
),
CardHighlight(
leading: const Icon(
FluentIcons.product_warning,
size: 20,
),
title:
Text('Use Legacy Updates', style: theme.typography.body),
description: Text(
'This is a cost-ineffective method, use only when news items are not arriving',
style: theme.typography.caption,
),
trailing: ToggleSwitch(
checked: prefs.legacyUpdate,
onChanged: (v) => prefsNotifier.update(legacyUpdate: v),
leadingContent: true,
content: Text(prefs.legacyUpdate ? 'On' : 'Off')),
),
Text(
'About',
style: theme.typography.bodyStrong,
Expand Down
33 changes: 21 additions & 12 deletions lib/widgets/top_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class AppState extends ConsumerState<App> with TrayListener, WindowListener {
super.initState();
loadFromPrefs();
avoidEmptyNews();
legacyChecker();
AppUtil.setupTTS().then((value) => tts = value);
trayManager.addListener(this);
windowManager.addListener(this);
Expand All @@ -64,6 +65,7 @@ class AppState extends ConsumerState<App> with TrayListener, WindowListener {
final newsNotifier = ref.read(provider.newsProvider.notifier);
final newsChannel = channels.first;
newsChannel.ready.then((_) {
log('NewsChannel Ready');
newsChannel.stream.listen((event) {
final json = jsonDecode(event);
if (json['error'] != null) return;
Expand All @@ -82,6 +84,7 @@ class AppState extends ConsumerState<App> with TrayListener, WindowListener {
});
final changelogChannel = channels.last;
changelogChannel.ready.then((_) {
log('ChangelogChannel Ready');
changelogChannel.stream.listen((event) {
final json = jsonDecode(event);
if (json['error'] != null) return;
Expand Down Expand Up @@ -149,33 +152,39 @@ class AppState extends ConsumerState<App> with TrayListener, WindowListener {
});
}

Future<List<News>> getAllNews() async {
try {
final result = await Future.wait([News.getNews(), News.getChangelog()]);
final List<News> finalList = [...result.first, ...result.last];
finalList.sort((a, b) => b.date.compareTo(a.date));
return finalList;
} catch (e) {
rethrow;
}
}

Future<void> avoidEmptyNews() async {
final newsNotifier = ref.read(provider.newsProvider.notifier);
while (ref.read(provider.newsProvider).isEmpty) {
try {
final value = await getAllNews()
final value = await News.getAllNews()
.timeout(const Duration(seconds: 7), onTimeout: () => []);
if (value.isNotEmpty) {
newsNotifier.addAll(value);
newsNotifier.deduplicate();
newsNotifier.sortByTime();
}
} on DioError catch (e, st) {
log(e.toString(), stackTrace: st);
}
}
}

Future<void> legacyChecker() async {
final newsNotifier = ref.read(provider.newsProvider.notifier);
Timer.periodic(const Duration(seconds: 20), (timer) async {
if (ref
.read(provider.prefsProvider.select((value) => value.legacyUpdate))) {
final value = await News.getAllNews()
.timeout(const Duration(seconds: 7), onTimeout: () => []);
if (value.isNotEmpty) {
newsNotifier.addAll(value);
newsNotifier.deduplicate();
newsNotifier.sortByTime();
}
}
});
}

@override
void dispose() {
trayManager.removeListener(this);
Expand Down

0 comments on commit 943d892

Please sign in to comment.