Tracked players now updates in the background (off by default)

This commit is contained in:
dan63047 2024-06-05 01:14:34 +03:00
parent 68784834fe
commit 8b5bf8c4d2
4 changed files with 50 additions and 7 deletions

View File

@ -92,9 +92,10 @@ void main() async {
}
// I dont want to store old cache
Timer.periodic(Duration(minutes: 5), (Timer timer) {
Timer.periodic(Duration(minutes: 5), (Timer timer) async {
teto.cacheRoutine();
developer.log("Cache routine complete", name: "main");
developer.log("Cache routine complete, next one in ${DateTime.now().add(Duration(minutes: 5))}", name: "main");
if (prefs.getBool("updateInBG") == true) await teto.fetchTracked();
});
runApp(TranslationProvider(

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert';
import 'dart:developer' as developer;
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:tetra_stats/data_objects/tetra_stats.dart';
import 'package:tetra_stats/data_objects/tetrio_multiplayer_replay.dart';
@ -688,6 +689,21 @@ class TetrioService extends DB {
}
}
// i want to know progress, so i trying to figure out this thing:
// Stream<TetrioPlayersLeaderboard> fetchTLLeaderboardAsStream() async {
// TetrioPlayersLeaderboard? cached = _cache.get("league", TetrioPlayersLeaderboard);
// if (cached != null) return cached;
// Uri url;
// if (kIsWeb) {
// url = Uri.https('ts.dan63.by', 'oskware_bridge.php', {"endpoint": "TLLeaderboard"});
// } else {
// url = Uri.https('ch.tetr.io', 'api/users/lists/league/all');
// }
// Stream<TetrioPlayersLeaderboard> stream = http.StreamedRequest("GET", url);
// }
TetrioPlayersLeaderboard? getCachedLeaderboard(){
return _cache.get("league", TetrioPlayersLeaderboard);
}
@ -1157,4 +1173,15 @@ class TetrioService extends DB {
}
return data;
}
Future<void> fetchTracked() async {
for (String userID in (await getAllPlayerToTrack())) {
TetrioPlayer player = await fetchPlayer(userID);
storeState(player);
sleep(Durations.extralong4);
TetraLeagueAlphaStream matches = await fetchTLStream(userID);
saveTLMatchesFromStream(matches);
sleep(Durations.extralong4);
}
}
}

View File

@ -311,9 +311,11 @@ class _MainState extends State<MainView> with TickerProviderStateMixin {
chartsData = [];
}
backgroundUpdate = Timer(me.cachedUntil!.difference(DateTime.now()), () {
changePlayer(me.userId);
});
if (prefs.getBool("updateInBG") == true) {
backgroundUpdate = Timer(me.cachedUntil!.difference(DateTime.now()), () {
changePlayer(me.userId);
});
}
return [me, records, states, tlMatches, compareWith, isTracking, news, topTR];
}

View File

@ -14,6 +14,7 @@ import 'package:tetra_stats/utils/open_in_browser.dart';
import 'package:window_manager/window_manager.dart';
late String oldWindowTitle;
TextStyle subtitleStyle = const TextStyle(fontFamily: "Eurostile Round Condensed", color: Colors.grey);
class SettingsView extends StatefulWidget {
const SettingsView({super.key});
@ -26,6 +27,7 @@ class SettingsState extends State<SettingsView> {
late SharedPreferences prefs;
String defaultNickname = "Checking...";
late bool showPositions;
late bool updateInBG;
final TextEditingController _playertext = TextEditingController();
@override
@ -47,6 +49,7 @@ class SettingsState extends State<SettingsView> {
Future<void> _getPreferences() async {
prefs = await SharedPreferences.getInstance();
showPositions = prefs.getBool("showPositions") ?? false;
updateInBG = prefs.getBool("updateInBG") ?? false;
_setDefaultNickname(prefs.getString("player"));
}
@ -91,7 +94,7 @@ class SettingsState extends State<SettingsView> {
children: [
ListTile(
title: Text(t.exportDB),
subtitle: Text(t.exportDBDescription, style: const TextStyle(fontFamily: "Eurostile Round Condensed", color: Colors.grey)),
subtitle: Text(t.exportDBDescription, style: subtitleStyle),
onTap: () {
if (kIsWeb){
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(t.notForWeb)));
@ -145,7 +148,7 @@ class SettingsState extends State<SettingsView> {
),
ListTile(
title: Text(t.importDB),
subtitle: Text(t.importDBDescription, style: const TextStyle(fontFamily: "Eurostile Round Condensed", color: Colors.grey)),
subtitle: Text(t.importDBDescription, style: subtitleStyle),
onTap: () {
if (kIsWeb){
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(t.notForWeb)));
@ -197,6 +200,7 @@ class SettingsState extends State<SettingsView> {
),
ListTile(
title: Text(t.yourID),
subtitle: Text(t.yourIDText, style: subtitleStyle),
trailing: Text(defaultNickname),
onTap: () => showDialog(
context: context,
@ -242,6 +246,7 @@ class SettingsState extends State<SettingsView> {
),
ListTile(
title: Text(t.language),
subtitle: Text("By default, the system language will be selected (if available among Tetra Stats locales, otherwise English)", style: subtitleStyle),
trailing: DropdownButton(
items: locales,
value: LocaleSettings.currentLocale,
@ -261,6 +266,14 @@ class SettingsState extends State<SettingsView> {
onTap: () {
context.go("/customization");
},),
ListTile(title: Text("Update stats in the background"),
subtitle: Text("While tetra stats is running, it can update stats of the current player when cache expires, as well, as tetra league stats of tracked players", style: const TextStyle(fontFamily: "Eurostile Round Condensed", color: Colors.grey)),
trailing: Switch(value: updateInBG, onChanged: (bool value){
prefs.setBool("updateInBG", value);
setState(() {
updateInBG = value;
});
}),),
ListTile(title: Text(t.lbStats),
subtitle: Text(t.lbStatsDescription, style: const TextStyle(fontFamily: "Eurostile Round Condensed", color: Colors.grey)),
trailing: Switch(value: showPositions, onChanged: (bool value){