game_model.dart 2.4 KB

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