game_model.dart 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import 'dart:io';
  2. import 'package:broadcast/broadcast.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:just_audio/just_audio.dart';
  5. import 'package:sport/bean/game.dart';
  6. import 'package:sport/bean/game_record.dart';
  7. import 'package:sport/bean/sport_detail.dart';
  8. import 'package:sport/pages/home/sport_history_share_page.dart';
  9. import 'package:sport/router/navigator_util.dart';
  10. import 'package:sport/services/api/inject_api.dart';
  11. import 'package:sport/services/api/resp.dart';
  12. import 'package:sport/utils/sport_utils.dart';
  13. class GameModel extends ChangeNotifier with InjectApi {
  14. List<GameInfoData> gameList = [];
  15. Map<GameInfoData, RecordsTodaySum?> gameRecord = {};
  16. String tag = "";
  17. int count = 0;
  18. bool noCheck = false;
  19. AudioPlayer? _player;
  20. int gameCount = 0;
  21. setGameAll(List<GameInfoData> list) {
  22. gameList.clear();
  23. if (list.isNotEmpty == true) gameList.addAll(list);
  24. }
  25. Future<List<GameInfoData>> getGameAll() async {
  26. if(gameList.isEmpty) {
  27. final data = await api.getGameAll();
  28. if (data.results.isNotEmpty) {
  29. gameList.addAll(data.results);
  30. }
  31. }
  32. return gameList;
  33. }
  34. Future<GameInfoData?> getGame(int id) async {
  35. List<GameInfoData> gameList = await getGameAll();
  36. try {
  37. return gameList.firstWhere((element) => element.id == id);
  38. } catch (e) {
  39. }
  40. return null;
  41. }
  42. check(BuildContext context) async {
  43. if (gameRecord.isEmpty) return;
  44. if (noCheck) return;
  45. Map<GameInfoData, RecordsTodaySum> records = {};
  46. for (var item in gameRecord.keys) {
  47. GameRecord? record = (await api.getGameRecord(item.id)).data;
  48. if (record != null && record.today != null) {
  49. if ((gameRecord[item] == null && (record.today?.times ?? 0) > 0) || (record.today?.times ?? 0) > (gameRecord[item]?.times ?? 0)) {
  50. records[item] = record.today!;
  51. }
  52. gameRecord[item] = record.today;
  53. }
  54. }
  55. // gameRecord.clear();
  56. if (records.isNotEmpty) {
  57. NavigatorUtil.goPage(context, (context) => SportHistorySharePage(records));
  58. }
  59. }
  60. void resumed(){
  61. Broadcast.broadcast("xie.hiyd.com.GameService.stop");
  62. _player?.dispose();
  63. }
  64. void addGame(GameInfoData game) {
  65. gameRecord[game] = gameRecord[game] ?? null;
  66. if(Platform.isIOS) {
  67. if ((game.h5 ?? 0) == 0) {
  68. _player = AudioPlayer(handleInterruptions: false)
  69. ..setAudioSource(AudioSource.uri(Uri.parse("asset:///assets/audio/blank.mp3")))
  70. ..setLoopMode(LoopMode.all).then((value) => _player?.play());
  71. }
  72. }
  73. }
  74. }