diff --git a/lib/main.dart b/lib/main.dart index 61378ae..a5458b6 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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( diff --git a/lib/services/tetrio_crud.dart b/lib/services/tetrio_crud.dart index 9041167..70273e1 100644 --- a/lib/services/tetrio_crud.dart +++ b/lib/services/tetrio_crud.dart @@ -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 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 stream = http.StreamedRequest("GET", url); + // } + TetrioPlayersLeaderboard? getCachedLeaderboard(){ return _cache.get("league", TetrioPlayersLeaderboard); } @@ -1157,4 +1173,15 @@ class TetrioService extends DB { } return data; } + + Future 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); + } + } } diff --git a/lib/views/main_view.dart b/lib/views/main_view.dart index 092de49..0e52c2d 100644 --- a/lib/views/main_view.dart +++ b/lib/views/main_view.dart @@ -311,9 +311,11 @@ class _MainState extends State 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]; } diff --git a/lib/views/settings_view.dart b/lib/views/settings_view.dart index ecb8ef3..bad170c 100644 --- a/lib/views/settings_view.dart +++ b/lib/views/settings_view.dart @@ -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 { 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 { Future _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 { 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 { ), 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 { ), 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 { ), 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 { 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){