Skip to content

Commit

Permalink
Merge pull request #189 from tusharlock10/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
tusharlock10 authored Feb 28, 2022
2 parents 77d4430 + 0988537 commit a08da02
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 460 deletions.
1 change: 1 addition & 0 deletions lib/data_classes/champions/index.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export './champions.dart';
export './player_champions.dart';
13 changes: 13 additions & 0 deletions lib/data_classes/champions/player_champions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class PlayerChampionsSortData {
final String iconUrl;
final int sortedIndex;

PlayerChampionsSortData({
required this.iconUrl,
required this.sortedIndex,
});

int compareTo(PlayerChampionsSortData other) {
return sortedIndex - other.sortedIndex;
}
}
170 changes: 1 addition & 169 deletions lib/providers/champions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class _ChampionsNotifier extends ChangeNotifier {
champions = response.champions;

// sort champions based on their name
champions
.sort((a, b) => a.name.toLowerCase().compareTo(b.name.toLowerCase()));
champions.sortedBy((champion) => champion.name);

// save champion locally for future use
champions.forEach(utilities.Database.saveChampion);
Expand Down Expand Up @@ -86,173 +85,6 @@ class _ChampionsNotifier extends ChangeNotifier {
void resetPlayerChampions() {
playerChampions = null;
}

/// Sort playerChampions on basis of name
void sortPlayerChampionsName(bool ascending) {
if (playerChampions == null) return;

final _playerChampions = [...playerChampions!];

_playerChampions.sort((a, b) {
// get the name of the champion from champions list
final aName =
champions.firstOrNullWhere((_) => _.championId == a.championId)?.name;
final bName =
champions.firstOrNullWhere((_) => _.championId == b.championId)?.name;

if (aName == null || bName == null) return 0;

if (ascending) return bName.compareTo(aName);

return aName.compareTo(bName);
});

playerChampions = _playerChampions;

utilities.postFrameCallback(notifyListeners);
}

/// Sort playerChampions on basis of matches
void sortPlayerChampionsMatches(bool ascending) {
if (playerChampions == null) return;

final _playerChampions = [...playerChampions!];

_playerChampions.sort((a, b) {
final aMatches = a.wins + a.losses;
final bMatches = b.wins + b.losses;
if (ascending) return bMatches.compareTo(aMatches);

return aMatches.compareTo(bMatches);
});

playerChampions = _playerChampions;

utilities.postFrameCallback(notifyListeners);
}

/// Sort playerChampions on basis of kills
void sortPlayerChampionsKills(bool ascending) {
if (playerChampions == null) return;

final _playerChampions = [...playerChampions!];

_playerChampions.sort((a, b) {
if (ascending) return b.totalKills.compareTo(a.totalKills);

return a.totalKills.compareTo(b.totalKills);
});

playerChampions = _playerChampions;

utilities.postFrameCallback(notifyListeners);
}

/// Sort playerChampions on basis of deaths
void sortPlayerChampionsDeaths(bool ascending) {
if (playerChampions == null) return;

final _playerChampions = [...playerChampions!];

_playerChampions.sort((a, b) {
if (ascending) return b.totalDeaths.compareTo(a.totalDeaths);

return a.totalDeaths.compareTo(b.totalDeaths);
});

playerChampions = _playerChampions;

utilities.postFrameCallback(notifyListeners);
}

/// Sort playerChampions on basis of KDA
void sortPlayerChampionsKDA(bool ascending) {
if (playerChampions == null) return;

final _playerChampions = [...playerChampions!];

_playerChampions.sort((a, b) {
final aKda = (a.totalKills + a.totalAssists) / a.totalDeaths;
final bKda = (b.totalKills + b.totalAssists) / b.totalDeaths;
if (ascending) return bKda.compareTo(aKda);

return aKda.compareTo(bKda);
});

playerChampions = _playerChampions;

utilities.postFrameCallback(notifyListeners);
}

/// Sort playerChampions on basis of win rate
void sortPlayerChampionsWinRate(bool ascending) {
if (playerChampions == null) return;

final _playerChampions = [...playerChampions!];

_playerChampions.sort((a, b) {
final aWinRate = a.wins * 100 / (a.losses + a.wins);
final bWinRate = b.wins * 100 / (b.losses + b.wins);
if (ascending) return bWinRate.compareTo(aWinRate);

return aWinRate.compareTo(bWinRate);
});

playerChampions = _playerChampions;

utilities.postFrameCallback(notifyListeners);
}

/// Sort playerChampions on basis of play time
void sortPlayerChampionsPlayTime(bool ascending) {
if (playerChampions == null) return;

final _playerChampions = [...playerChampions!];

_playerChampions.sort((a, b) {
if (ascending) return b.playTime.compareTo(a.playTime);

return a.playTime.compareTo(a.playTime);
});

playerChampions = _playerChampions;

utilities.postFrameCallback(notifyListeners);
}

/// Sort playerChampions on basis of level
void sortPlayerChampionsLevel(bool ascending) {
if (playerChampions == null) return;

final _playerChampions = [...playerChampions!];

_playerChampions.sort((a, b) {
if (ascending) return b.level.compareTo(a.level);

return a.level.compareTo(b.level);
});

playerChampions = _playerChampions;

utilities.postFrameCallback(notifyListeners);
}

/// Sort playerChampions on basis of last played
void sortPlayerChampionsLastPlayed(bool ascending) {
if (playerChampions == null) return;

final _playerChampions = [...playerChampions!];

_playerChampions.sort((a, b) {
if (ascending) return b.lastPlayed.compareTo(a.lastPlayed);

return a.lastPlayed.compareTo(b.lastPlayed);
});

playerChampions = _playerChampions;

utilities.postFrameCallback(notifyListeners);
}
}

/// Provider to handle champions
Expand Down
1 change: 1 addition & 0 deletions lib/screens/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export './home/index.dart';
export './loadouts/index.dart';
export './login/login.dart';
export './match_detail/index.dart';
export './player_champions/index.dart';
export './player_detail/index.dart';
export './routes.dart';
export './search/index.dart';
1 change: 1 addition & 0 deletions lib/screens/player_champions/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export './player_champions.dart';
121 changes: 121 additions & 0 deletions lib/screens/player_champions/player_champions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:paladinsedge/providers/index.dart' as providers;
import 'package:paladinsedge/screens/player_champions/player_champions_data_source.dart';
import 'package:paladinsedge/widgets/index.dart' as widgets;
import 'package:syncfusion_flutter_datagrid/datagrid.dart';

class PlayerChampions extends HookConsumerWidget {
static const routeName = '/playerChampions';

const PlayerChampions({Key? key}) : super(key: key);

@override
Widget build(BuildContext context, WidgetRef ref) {
// Providers
final playerChampions = ref.watch(
providers.champions.select((_) => _.playerChampions),
);
final championsProvider = ref.read(providers.champions);

// Variables
final champions = championsProvider.champions;

// State
final _playerChampionsDataSource =
useState<PlayerChampionsDataSource?>(null);

// Effects
useEffect(
() {
if (playerChampions == null) return null;

_playerChampionsDataSource.value = PlayerChampionsDataSource(
champions: champions,
playerChampions: playerChampions,
);

return null;
},
[playerChampions],
);

return Scaffold(
appBar: AppBar(
title: const Text('Player Champions'),
),
body: _playerChampionsDataSource.value == null
? const Center(
child: widgets.LoadingIndicator(
size: 32,
),
)
: SfDataGrid(
allowSorting: true,
rowHeight: 60,
source: _playerChampionsDataSource.value!,
columnWidthMode: ColumnWidthMode.fitByColumnName,
headerGridLinesVisibility: GridLinesVisibility.both,
gridLinesVisibility: GridLinesVisibility.both,
columns: [
GridColumn(
columnName: 'Champ',
label: const Center(
child: Text('Champs'),
),
),
GridColumn(
columnName: 'Matches',
label: const Center(
child: Text('Matches'),
),
),
GridColumn(
columnName: 'Kills',
label: const Center(
child: Text('Kills'),
),
),
GridColumn(
columnName: 'Deaths',
label: const Center(
child: Text('Deaths'),
),
),
GridColumn(
columnName: 'KDA',
label: const Center(
child: Text('KDA'),
),
),
GridColumn(
columnName: 'Win Rate',
label: const Center(
child: Text('Win Rate'),
),
),
GridColumn(
columnName: 'Play Time',
autoFitPadding: const EdgeInsets.symmetric(horizontal: 10),
label: const Center(
child: Text('Play Time'),
),
),
GridColumn(
columnName: 'Level',
label: const Center(
child: Text('Level'),
),
),
GridColumn(
columnName: 'Last Played',
label: const Center(
child: Text('Last Played'),
),
),
],
),
);
}
}
Loading

0 comments on commit a08da02

Please sign in to comment.