main_view now fetches data in the background + clearing cache in background

This commit is contained in:
dan63047 2024-06-04 02:42:44 +03:00
parent 3b0eb4009d
commit 68784834fe
12 changed files with 39 additions and 17 deletions

View File

@ -265,6 +265,7 @@ class TetrioPlayer {
List<RecordSingle?> blitz = []; List<RecordSingle?> blitz = [];
TetrioZen? zen; TetrioZen? zen;
Distinguishment? distinguishment; Distinguishment? distinguishment;
DateTime? cachedUntil;
TetrioPlayer({ TetrioPlayer({
required this.userId, required this.userId,
@ -292,11 +293,12 @@ class TetrioPlayer {
required this.blitz, required this.blitz,
this.zen, this.zen,
this.distinguishment, this.distinguishment,
this.cachedUntil
}); });
double get level => pow((xp / 500), 0.6) + (xp / (5000 + (max(0, xp - 4 * pow(10, 6)) / 5000))) + 1; double get level => pow((xp / 500), 0.6) + (xp / (5000 + (max(0, xp - 4 * pow(10, 6)) / 5000))) + 1;
TetrioPlayer.fromJson(Map<String, dynamic> json, DateTime stateTime, String id, String nick) { TetrioPlayer.fromJson(Map<String, dynamic> json, DateTime stateTime, String id, String nick, [DateTime? cUntil]) {
//developer.log("TetrioPlayer.fromJson $stateTime: $json", name: "data_objects/tetrio"); //developer.log("TetrioPlayer.fromJson $stateTime: $json", name: "data_objects/tetrio");
userId = id; userId = id;
username = nick; username = nick;
@ -324,6 +326,7 @@ class TetrioPlayer {
friendCount = json['friend_count'] ?? 0; friendCount = json['friend_count'] ?? 0;
badstanding = json['badstanding']; badstanding = json['badstanding'];
botmaster = json['botmaster']; botmaster = json['botmaster'];
cachedUntil = cUntil;
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {

View File

@ -1,9 +1,12 @@
import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'dart:developer' as developer;
import 'package:package_info_plus/package_info_plus.dart'; import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:tetra_stats/services/tetrio_crud.dart';
import 'package:tetra_stats/views/customization_view.dart'; import 'package:tetra_stats/views/customization_view.dart';
import 'package:window_manager/window_manager.dart'; import 'package:window_manager/window_manager.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart';
@ -18,6 +21,7 @@ import 'package:go_router/go_router.dart';
late final PackageInfo packageInfo; late final PackageInfo packageInfo;
late SharedPreferences prefs; late SharedPreferences prefs;
late TetrioService teto;
ColorScheme sheme = const ColorScheme.dark(primary: Colors.cyanAccent, secondary: Colors.white); ColorScheme sheme = const ColorScheme.dark(primary: Colors.cyanAccent, secondary: Colors.white);
void setAccentColor(Color color){ // does this thing work??? yes??? no??? void setAccentColor(Color color){ // does this thing work??? yes??? no???
@ -77,6 +81,7 @@ void main() async {
packageInfo = await PackageInfo.fromPlatform(); packageInfo = await PackageInfo.fromPlatform();
prefs = await SharedPreferences.getInstance(); prefs = await SharedPreferences.getInstance();
teto = TetrioService();
// Choosing the locale // Choosing the locale
String? locale = prefs.getString("locale"); String? locale = prefs.getString("locale");
@ -86,6 +91,12 @@ void main() async {
LocaleSettings.setLocaleRaw(locale); LocaleSettings.setLocaleRaw(locale);
} }
// I dont want to store old cache
Timer.periodic(Duration(minutes: 5), (Timer timer) {
teto.cacheRoutine();
developer.log("Cache routine complete", name: "main");
});
runApp(TranslationProvider( runApp(TranslationProvider(
child: const MyApp(), child: const MyApp(),
)); ));

View File

@ -125,7 +125,7 @@ class CacheController {
} }
void removeOld() async { void removeOld() async {
_cache.removeWhere((key, value) => DateTime.fromMillisecondsSinceEpoch(int.parse(key.substring(_getObjectId(value).length)), isUtc: true).isAfter(DateTime.now())); _cache.removeWhere((key, value) => int.parse(key.substring(_getObjectId(value).length)) <= DateTime.now().millisecondsSinceEpoch);
} }
void reset(){ void reset(){
@ -210,6 +210,10 @@ class TetrioService extends DB {
return _lbPositions[userID]; return _lbPositions[userID];
} }
void cacheRoutine(){
_cache.removeOld();
}
/// Downloads replay from inoue (szy API). Requiers [replayID]. If request have /// Downloads replay from inoue (szy API). Requiers [replayID]. If request have
/// different from 200 statusCode, it will throw an excepction. Returns list, that contains same replay /// different from 200 statusCode, it will throw an excepction. Returns list, that contains same replay
/// as string and as binary. /// as string and as binary.
@ -1110,7 +1114,7 @@ class TetrioService extends DB {
var json = jsonDecode(response.body); var json = jsonDecode(response.body);
if (json['success']) { if (json['success']) {
// parse and count stats // parse and count stats
TetrioPlayer player = TetrioPlayer.fromJson(json['data']['user'], DateTime.fromMillisecondsSinceEpoch(json['cache']['cached_at'], isUtc: true), json['data']['user']['_id'], json['data']['user']['username']); TetrioPlayer player = TetrioPlayer.fromJson(json['data']['user'], DateTime.fromMillisecondsSinceEpoch(json['cache']['cached_at'], isUtc: true), json['data']['user']['_id'], json['data']['user']['username'], DateTime.fromMillisecondsSinceEpoch(json['cache']['cached_until'], isUtc: true));
_cache.store(player, json['cache']['cached_until']); _cache.store(player, json['cache']['cached_until']);
developer.log("fetchPlayer: $user retrieved and cached", name: "services/tetrio_crud"); developer.log("fetchPlayer: $user retrieved and cached", name: "services/tetrio_crud");
return player; return player;

View File

@ -5,7 +5,7 @@ import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:tetra_stats/data_objects/tetrio.dart'; import 'package:tetra_stats/data_objects/tetrio.dart';
import 'package:tetra_stats/gen/strings.g.dart'; import 'package:tetra_stats/gen/strings.g.dart';
import 'package:tetra_stats/services/tetrio_crud.dart'; import 'package:tetra_stats/main.dart' show teto;
import 'package:tetra_stats/widgets/vs_graphs.dart'; import 'package:tetra_stats/widgets/vs_graphs.dart';
import 'package:window_manager/window_manager.dart'; import 'package:window_manager/window_manager.dart';
@ -18,7 +18,6 @@ Mode greenSideMode = Mode.player;
List<dynamic> theGreenSide = [null, null, null]; // TetrioPlayer?, List<DropdownMenuItem<TetrioPlayer>>?, TetraLeagueAlpha? List<dynamic> theGreenSide = [null, null, null]; // TetrioPlayer?, List<DropdownMenuItem<TetrioPlayer>>?, TetraLeagueAlpha?
Mode redSideMode = Mode.player; Mode redSideMode = Mode.player;
List<dynamic> theRedSide = [null, null, null]; List<dynamic> theRedSide = [null, null, null];
final TetrioService teto = TetrioService();
final DateFormat dateFormat = DateFormat.yMd(LocaleSettings.currentLocale.languageCode).add_Hm(); final DateFormat dateFormat = DateFormat.yMd(LocaleSettings.currentLocale.languageCode).add_Hm();
var numbersReg = RegExp(r'\d+(\.\d*)*'); var numbersReg = RegExp(r'\d+(\.\d*)*');
late String oldWindowTitle; late String oldWindowTitle;

View File

@ -1,5 +1,6 @@
// ignore_for_file: type_literal_in_constant_pattern // ignore_for_file: type_literal_in_constant_pattern
import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
@ -13,8 +14,7 @@ import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:tetra_stats/data_objects/tetra_stats.dart'; import 'package:tetra_stats/data_objects/tetra_stats.dart';
import 'package:tetra_stats/data_objects/tetrio.dart'; import 'package:tetra_stats/data_objects/tetrio.dart';
import 'package:tetra_stats/gen/strings.g.dart'; import 'package:tetra_stats/gen/strings.g.dart';
import 'package:tetra_stats/services/tetrio_crud.dart'; import 'package:tetra_stats/main.dart' show prefs, teto;
import 'package:tetra_stats/main.dart' show prefs;
import 'package:tetra_stats/services/crud_exceptions.dart'; import 'package:tetra_stats/services/crud_exceptions.dart';
import 'package:tetra_stats/utils/numers_formats.dart'; import 'package:tetra_stats/utils/numers_formats.dart';
import 'package:tetra_stats/utils/text_shadow.dart'; import 'package:tetra_stats/utils/text_shadow.dart';
@ -33,7 +33,6 @@ import 'package:window_manager/window_manager.dart';
import 'package:flutter_markdown/flutter_markdown.dart'; import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
final TetrioService teto = TetrioService(); // thing, that manadge our local DB
int _chartsIndex = 0; int _chartsIndex = 0;
bool _gamesPlayedInsteadOfDateAndTime = false; bool _gamesPlayedInsteadOfDateAndTime = false;
late ZoomPanBehavior _zoomPanBehavior; late ZoomPanBehavior _zoomPanBehavior;
@ -95,6 +94,7 @@ class _MainState extends State<MainView> with TickerProviderStateMixin {
//var tableData = <TableRow>[]; //var tableData = <TableRow>[];
final bodyGlobalKey = GlobalKey(); final bodyGlobalKey = GlobalKey();
bool _showSearchBar = false; bool _showSearchBar = false;
Timer backgroundUpdate = Timer(Duration(days: 365), (){});
bool _TLHistoryWasFetched = false; bool _TLHistoryWasFetched = false;
late TabController _tabController; late TabController _tabController;
late TabController _wideScreenTabController; late TabController _wideScreenTabController;
@ -159,6 +159,7 @@ class _MainState extends State<MainView> with TickerProviderStateMixin {
Future<List> fetch(String nickOrID, {bool fetchHistory = false, bool fetchTLmatches = false}) async { Future<List> fetch(String nickOrID, {bool fetchHistory = false, bool fetchTLmatches = false}) async {
TetrioPlayer me; TetrioPlayer me;
_TLHistoryWasFetched = false; _TLHistoryWasFetched = false;
backgroundUpdate.cancel();
// If user trying to search with discord id // If user trying to search with discord id
if (nickOrID.startsWith("ds:")){ if (nickOrID.startsWith("ds:")){
@ -309,6 +310,11 @@ class _MainState extends State<MainView> with TickerProviderStateMixin {
compareWith = null; compareWith = null;
chartsData = []; chartsData = [];
} }
backgroundUpdate = Timer(me.cachedUntil!.difference(DateTime.now()), () {
changePlayer(me.userId);
});
return [me, records, states, tlMatches, compareWith, isTracking, news, topTR]; return [me, records, states, tlMatches, compareWith, isTracking, news, topTR];
} }

View File

@ -2,12 +2,11 @@ import 'dart:io';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:tetra_stats/services/tetrio_crud.dart'; import 'package:tetra_stats/main.dart' show teto;
import 'package:tetra_stats/gen/strings.g.dart'; import 'package:tetra_stats/gen/strings.g.dart';
import 'package:tetra_stats/views/tl_match_view.dart'; import 'package:tetra_stats/views/tl_match_view.dart';
import 'package:window_manager/window_manager.dart'; import 'package:window_manager/window_manager.dart';
final TetrioService teto = TetrioService();
late String oldWindowTitle; late String oldWindowTitle;
class MatchesView extends StatefulWidget { class MatchesView extends StatefulWidget {

View File

@ -5,7 +5,7 @@ import 'package:tetra_stats/gen/strings.g.dart';
import 'package:tetra_stats/utils/numers_formats.dart'; import 'package:tetra_stats/utils/numers_formats.dart';
import 'package:tetra_stats/views/rank_averages_view.dart'; import 'package:tetra_stats/views/rank_averages_view.dart';
import 'package:window_manager/window_manager.dart'; import 'package:window_manager/window_manager.dart';
import 'main_view.dart'; // lol import 'package:tetra_stats/main.dart' show teto;
class RankAveragesView extends StatefulWidget { class RankAveragesView extends StatefulWidget {
const RankAveragesView({super.key}); const RankAveragesView({super.key});

View File

@ -1,7 +1,7 @@
import 'dart:io'; import 'dart:io';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:tetra_stats/data_objects/tetrio.dart'; import 'package:tetra_stats/data_objects/tetrio.dart';
import 'package:tetra_stats/main.dart' show packageInfo; import 'package:tetra_stats/main.dart' show packageInfo, teto;
import 'package:file_selector/file_selector.dart'; import 'package:file_selector/file_selector.dart';
import 'package:file_picker/file_picker.dart'; import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
@ -10,7 +10,6 @@ import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:tetra_stats/gen/strings.g.dart'; import 'package:tetra_stats/gen/strings.g.dart';
import 'package:tetra_stats/services/crud_exceptions.dart'; import 'package:tetra_stats/services/crud_exceptions.dart';
import 'package:tetra_stats/services/tetrio_crud.dart';
import 'package:tetra_stats/utils/open_in_browser.dart'; import 'package:tetra_stats/utils/open_in_browser.dart';
import 'package:window_manager/window_manager.dart'; import 'package:window_manager/window_manager.dart';
@ -25,7 +24,6 @@ class SettingsView extends StatefulWidget {
class SettingsState extends State<SettingsView> { class SettingsState extends State<SettingsView> {
late SharedPreferences prefs; late SharedPreferences prefs;
final TetrioService teto = TetrioService();
String defaultNickname = "Checking..."; String defaultNickname = "Checking...";
late bool showPositions; late bool showPositions;
final TextEditingController _playertext = TextEditingController(); final TextEditingController _playertext = TextEditingController();

View File

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:tetra_stats/data_objects/tetrio.dart'; import 'package:tetra_stats/data_objects/tetrio.dart';
import 'package:tetra_stats/gen/strings.g.dart'; import 'package:tetra_stats/gen/strings.g.dart';
import 'package:tetra_stats/main.dart' show teto;
import 'package:tetra_stats/views/mathes_view.dart'; import 'package:tetra_stats/views/mathes_view.dart';
import 'package:tetra_stats/views/state_view.dart'; import 'package:tetra_stats/views/state_view.dart';
import 'package:window_manager/window_manager.dart'; import 'package:window_manager/window_manager.dart';

View File

@ -6,7 +6,8 @@ import 'package:tetra_stats/services/crud_exceptions.dart';
import 'package:tetra_stats/views/compare_view.dart' show CompareThingy, CompareBoolThingy; import 'package:tetra_stats/views/compare_view.dart' show CompareThingy, CompareBoolThingy;
import 'package:tetra_stats/widgets/list_tile_trailing_stats.dart'; import 'package:tetra_stats/widgets/list_tile_trailing_stats.dart';
import 'package:tetra_stats/widgets/vs_graphs.dart'; import 'package:tetra_stats/widgets/vs_graphs.dart';
import 'main_view.dart' show teto, secs; import 'main_view.dart' show secs;
import 'package:tetra_stats/main.dart' show teto;
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';

View File

@ -4,12 +4,11 @@ import 'package:flutter/material.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:tetra_stats/data_objects/tetrio.dart'; import 'package:tetra_stats/data_objects/tetrio.dart';
import 'package:tetra_stats/gen/strings.g.dart'; import 'package:tetra_stats/gen/strings.g.dart';
import 'package:tetra_stats/services/tetrio_crud.dart'; import 'package:tetra_stats/main.dart' show teto;
import 'package:tetra_stats/utils/filesizes_converter.dart'; import 'package:tetra_stats/utils/filesizes_converter.dart';
import 'package:tetra_stats/views/states_view.dart'; import 'package:tetra_stats/views/states_view.dart';
import 'package:window_manager/window_manager.dart'; import 'package:window_manager/window_manager.dart';
final TetrioService teto = TetrioService();
late String oldWindowTitle; late String oldWindowTitle;
class TrackedPlayersView extends StatefulWidget { class TrackedPlayersView extends StatefulWidget {

View File

@ -4,6 +4,7 @@ import 'package:flutter/services.dart';
import 'package:syncfusion_flutter_gauges/gauges.dart'; import 'package:syncfusion_flutter_gauges/gauges.dart';
import 'package:tetra_stats/data_objects/tetrio.dart'; import 'package:tetra_stats/data_objects/tetrio.dart';
import 'package:tetra_stats/gen/strings.g.dart'; import 'package:tetra_stats/gen/strings.g.dart';
import 'package:tetra_stats/main.dart' show teto;
import 'package:tetra_stats/views/compare_view.dart'; import 'package:tetra_stats/views/compare_view.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:tetra_stats/utils/text_shadow.dart'; import 'package:tetra_stats/utils/text_shadow.dart';