diff --git a/lib/data/api/leaderboard_api.dart b/lib/data/api/leaderboard_api.dart index 8efda68..15e4656 100644 --- a/lib/data/api/leaderboard_api.dart +++ b/lib/data/api/leaderboard_api.dart @@ -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 getLeaderboard() async { - final result = await FirebaseFunctions.instance - .httpsCallable('getLeaderboard') - .call(); - - return ServerResponse.fromJson(result.data); - } - Stream get leaderboardStream { return FirebaseDatabase.instance .ref('leaderboard') diff --git a/lib/data/models/leaderboard.dart b/lib/data/models/leaderboard.dart index c12467a..2b7ab7e 100644 --- a/lib/data/models/leaderboard.dart +++ b/lib/data/models/leaderboard.dart @@ -8,12 +8,16 @@ class Leaderboard extends Equatable { final List users; final List 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({ @@ -21,12 +25,16 @@ class Leaderboard extends Equatable { List? users, List? 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, ); } @@ -44,6 +52,8 @@ class Leaderboard extends Equatable { ), ), isOpen: map['isOpen'] as bool? ?? true, + winnerRoom: map['winnerRoom'] as String? ?? '', + winnerTime: map['winnerTime'] as String? ?? '', ); } @@ -54,5 +64,12 @@ class Leaderboard extends Equatable { bool get stringify => true; @override - List get props => [currentUser, users, groups, isOpen]; + List get props => [ + currentUser, + users, + groups, + isOpen, + winnerRoom, + winnerTime, + ]; } diff --git a/lib/data/repo/leaderboard_repository.dart b/lib/data/repo/leaderboard_repository.dart index ed16bf0..180fe4d 100644 --- a/lib/data/repo/leaderboard_repository.dart +++ b/lib/data/repo/leaderboard_repository.dart @@ -10,20 +10,6 @@ class LeaderboardRepository { final LeaderboardApi _leaderboardApi = LeaderboardApi(); - Future 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 leaderboardStream(String userId) async* { await for (final event in _leaderboardApi.leaderboardStream) { int currentUserIndex = -1; @@ -31,6 +17,9 @@ class LeaderboardRepository { List leaderboardUsers = []; List leaderboardGroups = []; bool isOpen = true; + String winnerRoom = ''; + String winnerTime = ''; + for (final child in event.snapshot.children) { if (child.value == null) { continue; @@ -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; @@ -112,6 +107,8 @@ class LeaderboardRepository { users: leaderboardUsers, groups: leaderboardGroups, isOpen: isOpen, + winnerRoom: winnerRoom, + winnerTime: winnerTime, ); } } diff --git a/lib/logic/cubit/leaderboard_cubit.dart b/lib/logic/cubit/leaderboard_cubit.dart index 136c0b4..6796c1c 100644 --- a/lib/logic/cubit/leaderboard_cubit.dart +++ b/lib/logic/cubit/leaderboard_cubit.dart @@ -45,25 +45,4 @@ class LeaderboardCubit extends Cubit { } void stopLeaderboardFetch() => _streamSub?.cancel(); - - Future 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)); - } - } } diff --git a/lib/ui/pages/leaderboard_page.dart b/lib/ui/pages/leaderboard_page.dart index 28d841b..fa62bb4 100644 --- a/lib/ui/pages/leaderboard_page.dart +++ b/lib/ui/pages/leaderboard_page.dart @@ -34,28 +34,68 @@ class LeaderboardPage extends StatelessWidget { ), ); case LeaderboardStatus.fetchSuccess: - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - CustomSegmentedButton( - index: state.pageIndex, - onValueChanged: (value) => context - .read() - .changeLeaderboard(value), - ), - const SizedBox(height: 20), - Expanded( - child: PageView( - controller: pageController, - physics: const NeverScrollableScrollPhysics(), - children: const [ - UserLeaderboard(), - TeamLeaderboard(), + return state.leaderboard.isOpen + ? Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + CustomSegmentedButton( + index: state.pageIndex, + onValueChanged: (value) => context + .read() + .changeLeaderboard(value), + ), + const SizedBox(height: 20), + Expanded( + child: PageView( + controller: pageController, + physics: const NeverScrollableScrollPhysics(), + children: const [ + UserLeaderboard(), + TeamLeaderboard(), + ], + ), + ), ], - ), - ), - ], - ); + ) + : Center( + child: Text.rich( + TextSpan( + text: '🏆 ANNOUNCEMENT 🏆\n\n', + style: PresetTextStyle.black23w700, + children: [ + TextSpan( + text: 'Join us in ', + style: PresetTextStyle.black21w400, + children: [ + 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, + ), + ); } }, ),