Skip to content

Commit

Permalink
fix + winnerRomm + winnerTime
Browse files Browse the repository at this point in the history
  • Loading branch information
fedecarroz committed Oct 4, 2024
1 parent e168b4e commit fc39f2c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 67 deletions.
10 changes: 0 additions & 10 deletions lib/data/api/leaderboard_api.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import 'package:cloud_functions/cloud_functions.dart';
import 'package:devfest_bari_2024/data.dart';
import 'package:firebase_database/firebase_database.dart';

class LeaderboardApi {
Future<ServerResponse> getLeaderboard() async {
final result = await FirebaseFunctions.instance
.httpsCallable('getLeaderboard')
.call<String>();

return ServerResponse.fromJson(result.data);
}

Stream<DatabaseEvent> get leaderboardStream {
return FirebaseDatabase.instance
.ref('leaderboard')
Expand Down
19 changes: 18 additions & 1 deletion lib/data/models/leaderboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,33 @@ class Leaderboard extends Equatable {
final List<LeaderboardUser> users;
final List<LeaderboardGroup> groups;
final bool isOpen;
final String winnerRoom;
final String winnerTime;

const Leaderboard({
this.currentUser = const LeaderboardUser(),
this.users = const [],
this.groups = const [],
this.isOpen = true,
this.winnerRoom = '',
this.winnerTime = '',
});

Leaderboard copyWith({
LeaderboardUser? currentUser,
List<LeaderboardUser>? users,
List<LeaderboardGroup>? groups,
bool? isOpen,
String? winnerRoom,
String? winnerTime,
}) {
return Leaderboard(
currentUser: currentUser ?? this.currentUser,
users: users ?? this.users,
groups: groups ?? this.groups,
isOpen: isOpen ?? this.isOpen,
winnerRoom: winnerRoom ?? this.winnerRoom,
winnerTime: winnerTime ?? this.winnerTime,
);
}

Expand All @@ -44,6 +52,8 @@ class Leaderboard extends Equatable {
),
),
isOpen: map['isOpen'] as bool? ?? true,
winnerRoom: map['winnerRoom'] as String? ?? '',
winnerTime: map['winnerTime'] as String? ?? '',
);
}

Expand All @@ -54,5 +64,12 @@ class Leaderboard extends Equatable {
bool get stringify => true;

@override
List<Object> get props => [currentUser, users, groups, isOpen];
List<Object> get props => [
currentUser,
users,
groups,
isOpen,
winnerRoom,
winnerTime,
];
}
25 changes: 11 additions & 14 deletions lib/data/repo/leaderboard_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,16 @@ class LeaderboardRepository {

final LeaderboardApi _leaderboardApi = LeaderboardApi();

Future<Leaderboard> getLeaderboard() async {
try {
final response = await _leaderboardApi.getLeaderboard();

if (response.error.code.isNotEmpty) {
throw UnknownLeaderboardError();
}

return Leaderboard.fromJson(response.data);
} on Exception {
throw UnknownLeaderboardError();
}
}

Stream<Leaderboard> leaderboardStream(String userId) async* {
await for (final event in _leaderboardApi.leaderboardStream) {
int currentUserIndex = -1;
var currentUser = LeaderboardUser();
List<LeaderboardUser> leaderboardUsers = [];
List<LeaderboardGroup> leaderboardGroups = [];
bool isOpen = true;
String winnerRoom = '';
String winnerTime = '';

for (final child in event.snapshot.children) {
if (child.value == null) {
continue;
Expand All @@ -39,6 +28,12 @@ class LeaderboardRepository {
case 'isOpen':
isOpen = child.value as bool;
break;
case 'winnerRoom':
winnerRoom = child.value as String;
break;
case 'winnerTime':
winnerTime = child.value as String;
break;
case 'users':
final map = child.value as Map;

Expand Down Expand Up @@ -112,6 +107,8 @@ class LeaderboardRepository {
users: leaderboardUsers,
groups: leaderboardGroups,
isOpen: isOpen,
winnerRoom: winnerRoom,
winnerTime: winnerTime,
);
}
}
Expand Down
21 changes: 0 additions & 21 deletions lib/logic/cubit/leaderboard_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,4 @@ class LeaderboardCubit extends Cubit<LeaderboardState> {
}

void stopLeaderboardFetch() => _streamSub?.cancel();

Future<void> getLeaderboard() async {
emit(const LeaderboardState()
.copyWith(status: LeaderboardStatus.fetchInProgress));

try {
final leaderboard = await _leaderboardRepo.getLeaderboard();
final groupMaxScore = leaderboard.groups
.map((group) => group.score)
.reduce((a, b) => a > b ? a : b);
emit(
state.copyWith(
status: LeaderboardStatus.fetchSuccess,
leaderboard: leaderboard,
groupMaxScore: groupMaxScore,
),
);
} catch (e) {
emit(state.copyWith(status: LeaderboardStatus.fetchFailure));
}
}
}
82 changes: 61 additions & 21 deletions lib/ui/pages/leaderboard_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,68 @@ class LeaderboardPage extends StatelessWidget {
),
);
case LeaderboardStatus.fetchSuccess:
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
CustomSegmentedButton(
index: state.pageIndex,
onValueChanged: (value) => context
.read<LeaderboardCubit>()
.changeLeaderboard(value),
),
const SizedBox(height: 20),
Expanded(
child: PageView(
controller: pageController,
physics: const NeverScrollableScrollPhysics(),
children: const <Widget>[
UserLeaderboard(),
TeamLeaderboard(),
return state.leaderboard.isOpen
? Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
CustomSegmentedButton(
index: state.pageIndex,
onValueChanged: (value) => context
.read<LeaderboardCubit>()
.changeLeaderboard(value),
),
const SizedBox(height: 20),
Expanded(
child: PageView(
controller: pageController,
physics: const NeverScrollableScrollPhysics(),
children: const <Widget>[
UserLeaderboard(),
TeamLeaderboard(),
],
),
),
],
),
),
],
);
)
: Center(
child: Text.rich(
TextSpan(
text: '🏆 ANNOUNCEMENT 🏆\n\n',
style: PresetTextStyle.black23w700,
children: <InlineSpan>[
TextSpan(
text: 'Join us in ',
style: PresetTextStyle.black21w400,
children: <InlineSpan>[
TextSpan(
text: state.leaderboard.winnerRoom,
style: PresetTextStyle.black21w700,
),
TextSpan(
text: ' at ',
style: PresetTextStyle.black21w400,
),
TextSpan(
text: state.leaderboard.winnerTime,
style: PresetTextStyle.black21w700,
),
TextSpan(
text: ' to find out who the winners are.',
style: PresetTextStyle.black21w400,
),
],
),
TextSpan(text: '\n\n'),
TextSpan(
text:
'Don\'t miss it because this could be your moment! 😉',
style: PresetTextStyle.black21w400,
),
],
),
textAlign: TextAlign.center,
),
);
}
},
),
Expand Down

0 comments on commit fc39f2c

Please sign in to comment.