import 'dart:io'; import 'package:broadcast/broadcast.dart'; import 'package:flutter/material.dart'; import 'package:just_audio/just_audio.dart'; import 'package:sport/bean/game.dart'; import 'package:sport/bean/game_record.dart'; import 'package:sport/bean/sport_detail.dart'; import 'package:sport/pages/home/sport_history_share_page.dart'; import 'package:sport/router/navigator_util.dart'; import 'package:sport/services/api/inject_api.dart'; import 'package:sport/services/api/resp.dart'; import 'package:sport/utils/sport_utils.dart'; class GameModel extends ChangeNotifier with InjectApi { List gameList = []; Map gameRecord = {}; String tag = ""; int count = 0; bool noCheck = false; AudioPlayer? _player; int gameCount = 0; setGameAll(List list) { gameList.clear(); if (list.isNotEmpty == true) gameList.addAll(list); } Future> getGameAll() async { if(gameList.isEmpty) { final data = await api.getGameAll(); if (data.results.isNotEmpty) { gameList.addAll(data.results); } } return gameList; } Future getGame(int id) async { List gameList = await getGameAll(); try { return gameList.firstWhere((element) => element.id == id); } catch (e) { } return null; } check(BuildContext context) async { if (gameRecord.isEmpty) return; if (noCheck) return; Map records = {}; for (var item in gameRecord.keys) { GameRecord? record = (await api.getGameRecord(item.id)).data; if (record != null && record.today != null) { if ((gameRecord[item] == null && (record.today?.times ?? 0) > 0) || (record.today?.times ?? 0) > (gameRecord[item]?.times ?? 0)) { records[item] = record.today!; } gameRecord[item] = record.today; } } // gameRecord.clear(); if (records.isNotEmpty) { NavigatorUtil.goPage(context, (context) => SportHistorySharePage(records)); } } void resumed(){ Broadcast.broadcast("xie.hiyd.com.GameService.stop"); _player?.dispose(); } void addGame(GameInfoData game) { gameRecord[game] = gameRecord[game] ?? null; if(Platform.isIOS) { if ((game.h5 ?? 0) == 0) { _player = AudioPlayer(handleInterruptions: false) ..setAudioSource(AudioSource.uri(Uri.parse("asset:///assets/audio/blank.mp3"))) ..setLoopMode(LoopMode.all).then((value) => _player?.play()); } } } }