Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
5.1 kB
2
Indexable
Never
import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:get/get.dart';

import '../models/player_fixtures.dart';
import '/models/fixtures.dart';
import '/models/player_latest.dart';
import '/services/api_services.dart';
import '/utils/helpers.dart';
import '../consts/consts.dart';
import '../models/player_details.dart';
import '../models/player_teams.dart';
import '../models/player_trophies.dart';

class PlayerDetailsController extends GetxController {
  // Initialize loading values
  RxBool isLoading = true.obs;
  RxBool isLoadingMatches = true.obs;
  RxBool isLoadingTeams = true.obs;
  RxBool isLoadingTrophies = true.obs;
  RxInt playerId = 0.obs;

  // Initialize all the needed variable to handle player details
  Rx<PlayerDetails> playerDetails = PlayerDetails().obs;
  RxList<Transfers> playerTransfers = <Transfers>[].obs;
  RxList<Statistics> playerStatistics = <Statistics>[].obs;
  RxList<Trophies> playerTrophies = <Trophies>[].obs;
  RxList<TeamsPlayer> playerTeams = <TeamsPlayer>[].obs;
  RxList<Latest> playerLatest = <Latest>[].obs;
  RxList<FixturesData> playerFixtures = <FixturesData>[].obs;
  RxList<int> latestIds = <int>[].obs;
  Map<int?, List<Trophies>> sortedTrophies = <int?, List<Trophies>>{}.obs;
  Map<int?, Map<String, List<Trophies>>> finalTrophies =
      <int?, Map<String, List<Trophies>>>{}.obs;

  // Load player Details for a specified player Id
  loadPlayerDetails(int id) async {
    try {
      var response = await ApiService.get(
        "${settingController.sportapiBaseUrl.value}${AppConsts.player}$id${settingController.sportapiApiKey.value}&include=position;detailedPosition;country;statistics.details.type;statistics.season.league;transfers.fromTeam;transfers.toTeam;transfers.type",
      );

      // Save initial values
      playerDetails.value = PlayerDetails.fromJson(jsonDecode(response.body));
      playerTransfers.value = playerDetails.value.data?.transfers ?? [];
      playerStatistics.value = playerDetails.value.data?.statistics ?? [];
    } catch (e, s) {
      dd(e);
      dd(s);
    } finally {
      isLoading.value = false;
      // Call the functions at the beginning
      loadPlayerTeams(id);
      loadPlayerLatest(id);
    }
  }

  // Load player teams for the player
  loadPlayerTeams(int id) async {
    isLoadingTeams.value = true;

    try {
      var response = await ApiService.get(
        "${settingController.sportapiBaseUrl.value}${AppConsts.player}$id${settingController.sportapiApiKey.value}&include=teams.team.country;teams.team.activeSeasons",
      );

      // Save the team the player played in
      playerTeams.value =
          PlayerTeams.fromJson(jsonDecode(response.body)).data?.teams ?? [];
    } catch (e, s) {
      dd(e);
      dd(s);
    } finally {
      isLoadingTeams.value = false;
    }
  }

  // Load player Trophies
  loadPlayerTrophies(int id) async {
    try {
      var response = await ApiService.get(
        "${settingController.sportapiBaseUrl.value}${AppConsts.player}$id${settingController.sportapiApiKey.value}&include=trophies.league;trophies.season;trophies.trophy",
      );

      playerTrophies.value =
          PlayerTrophies.fromJson(jsonDecode(response.body)).data?.trophies ??
              [];

      // Group the Trophy by Legue Id
      sortedTrophies = groupBy(
        playerTrophies,
        (Trophies trophies) => trophies.leagueId,
      );

      // Group it again by trophy type
      for (var type in sortedTrophies.keys) {
        Map<String, List<Trophies>> groupedByAttribute = groupBy(
          sortedTrophies[type]!,
          (Trophies trophies) => trophies.trophy?.name ?? 'Unknown',
        );
        finalTrophies[type] = groupedByAttribute;
      }
    } catch (e, s) {
      dd(e);
      dd(s);
    } finally {
      isLoadingTrophies.value = false;
    }
  }

  // Load the latest matches the player has played in
  loadPlayerLatest(int id) async {
    try {
      var response = await ApiService.get(
        "${settingController.sportapiBaseUrl.value}${AppConsts.player}$id${settingController.sportapiApiKey.value}&include=latest",
      );
      playerLatest.value =
          PlayerLatest.fromJson(jsonDecode(response.body)).data?.latest ?? [];

      // Save the latest fixture IDs
      latestIds.clear();
      for (var item in playerLatest) {
        latestIds.add(item.fixtureId!);
      }
      // Load Fixtures for the saved fixture Ids
      await loadFixture(latestIds);
    } catch (e, s) {
      dd(e);
      dd(s);
    } finally {
      isLoadingMatches.value = false;
    }
  }

  // Load fixture details of the matches a player took part in
  loadFixture([ids = '']) async {
    // playerFixtures.value.clear();
    String idlist = ids.join(',');

    var response = await ApiService.get(
      '${settingController.sportapiBaseUrl.value + AppConsts.multiFixtures}$idlist${settingController.sportapiApiKey.value}&include=league;participants;state;scores;events.type;lineups.details.type',
    );

    // Save player Fixture details
    playerFixtures.value =
        PlayerFixtures.fromJson(jsonDecode(response.body)).data ?? [];
  }
}
Leave a Comment