Skip to content

Commit

Permalink
chore: leaderboard pages reloading logic done
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMuuo committed Sep 6, 2024
1 parent e670e3d commit a4746b5
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 194 deletions.
71 changes: 11 additions & 60 deletions lib/controllers/reward_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,75 +18,27 @@ class RewardController extends GetxController {
@override
void onInit() {
super.onInit();
if (_userController.isLoggedIn.value) {
_awardPointsForDailyLaunch();
}
loadRewards();
debugPrint("[+] Rewards Loaded!");
}

// Function to award daily app launch
Future<void> _awardPointsForDailyLaunch() async {
final now = DateTime.now();
bool award = true;
final userRewards = await RewardModelHelper().queryAll();
userRewards.map((e) => Reward.fromJson(e)).toList().forEach((element) {
if (element.awardedAt.day == now.day &&
element.awardedAt.month == now.month &&
element.awardedAt.year == now.year) {
award = false;
}
});

if (award) {
awardPoints(2, "Daily app launch").then((value) {});
}
}

Future<void> awardPoints(int points, String reason) async {
final newReward = Reward(
id: null,
studentId: _userController.user.value!.id!,
points: points,
reason: reason,
awardedAt: DateTime.now(),
);

final res = await service.award(newReward, _userController.authHeaders);
res.fold((l) {
debugPrint(l);
}, (r) {
RewardModelHelper().create(r.toJson()).then((value) {});
allPoints.add(r);
vibePoints.value += newReward.points;
_userController.user.value!.vibePoints += newReward.points;

HapticFeedback.mediumImpact().then((value) {
Get.rawSnackbar(
messageText: Text(
reason,
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.white,
),
),
duration: const Duration(seconds: 2),
isDismissible: true,
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.green[400]!,
icon: const Icon(Ionicons.balloon_outline, color: Colors.white),
);
});
});
}

// Returns a list of the current leaderboard
Future<Either<String, List<User>>> fetchLeaderBoard() async {
return await service.fetchLeaderBoard(_userController.authHeaders);
}

Future<Either<String, List<Reward>>> fetchCurrentUserRewards() async {
return await service.fetchUserRewards(
final result = await service.fetchUserRewards(
_userController.user.value!.id!, _userController.authHeaders);

return result.fold((l) {
return left(l);
}, (r) async {
for (Reward reward in r) {
await RewardModelHelper().create(reward.toJson());
}
return right(r);
});
}

Future<Either<String, List<Reward>>> loadRewards() async {
Expand All @@ -96,7 +48,6 @@ class RewardController extends GetxController {
final result = await fetchCurrentUserRewards();
return result.fold((l) => left(l), (r) async {
r.map((e) async {
await UserModelHelper().truncate();
await UserModelHelper().create(e.toJson());
});
return right(r);
Expand Down
4 changes: 1 addition & 3 deletions lib/pages/auth/register_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class _RegisterPageState extends State<RegisterPage> {
);
}, (r) {
if (r && userController.isLoggedIn.value) {
HapticFeedback.heavyImpact().then((value) {
rewardsController.awardPoints(10, "Newbie login");
});
HapticFeedback.heavyImpact().then((value) {});
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => const LayoutPage(),
Expand Down
8 changes: 3 additions & 5 deletions lib/tools/leaderboard/achievements_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:academia/exports/barrel.dart';
import 'package:academia/models/core/user/user.dart';
import 'package:academia/models/core/reward/reward.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:lottie/lottie.dart';
Expand Down Expand Up @@ -28,11 +28,9 @@ class _AchievementPageState extends State<AchievementPage>
onRefresh: () async {
final result = await rewardsController.fetchCurrentUserRewards();
result.fold((l) => null, (r) async {
r.map((e) async {
await UserModelHelper().truncate();
await UserModelHelper().create(e.toJson());
});
await RewardModelHelper().truncate();
});
setState(() {});
},
child: FutureBuilder(
future: rewardsController.loadRewards(),
Expand Down
Loading

0 comments on commit a4746b5

Please sign in to comment.