1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 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<GameInfoData> gameList = [];
- Map<GameInfoData, RecordsTodaySum?> gameRecord = {};
- String tag = "";
- int count = 0;
- bool noCheck = false;
- AudioPlayer? _player;
- int gameCount = 0;
- setGameAll(List<GameInfoData> list) {
- gameList.clear();
- if (list.isNotEmpty == true) gameList.addAll(list);
- }
- Future<List<GameInfoData>> getGameAll() async {
- if(gameList.isEmpty) {
- final data = await api.getGameAll();
- if (data.results.isNotEmpty) {
- gameList.addAll(data.results);
- }
- }
- return gameList;
- }
- Future<GameInfoData?> getGame(int id) async {
- List<GameInfoData> 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<GameInfoData, RecordsTodaySum> 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());
- }
- }
- }
- }
|