Skip to content

Commit

Permalink
Implement global challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
omaryasser committed Apr 7, 2024
1 parent 13c4eb3 commit 125490e
Show file tree
Hide file tree
Showing 28 changed files with 759 additions and 298 deletions.
Binary file removed assets/images/add_facebook_friend.png
Binary file not shown.
23 changes: 23 additions & 0 deletions lib/models/global_challenge.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:azkar/models/challenge.dart';


class GlobalChallenge {
GlobalChallenge({
required this.challenge,
required this.finishedCount,
});

Challenge challenge;
int finishedCount;

factory GlobalChallenge.fromJson(Map<String, dynamic> json) =>
GlobalChallenge(
challenge: Challenge.fromJson(json["challenge"]),
finishedCount: json["finishedCount"],
);

Map<String, dynamic> toJson() => {
"challenge": challenge.toJson(),
"finishedCount": finishedCount,
};
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:azkar/net/api_interface/response_base.dart';

class FinishCustomSimpleChallengeResponse extends ResponseBase {
static FinishCustomSimpleChallengeResponse fromJson(
Map<String, dynamic> json) {
FinishCustomSimpleChallengeResponse response =
new FinishCustomSimpleChallengeResponse();
response.setError(json);
if (response.hasError()) {
return response;
}
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:azkar/net/api_interface/response_base.dart';

class FinishGlobalChallengeResponse extends ResponseBase {
static FinishGlobalChallengeResponse fromJson(
Map<String, dynamic> json) {
FinishGlobalChallengeResponse response =
new FinishGlobalChallengeResponse();
response.setError(json);
if (response.hasError()) {
return response;
}
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:azkar/models/global_challenge.dart';
import 'package:azkar/net/api_interface/response_base.dart';

class GetGlobalChallengeResponse extends ResponseBase {
late GlobalChallenge challenge;

GetGlobalChallengeResponse();

static GetGlobalChallengeResponse fromJson(Map<String, dynamic> json) {
GetGlobalChallengeResponse response = new GetGlobalChallengeResponse();
response.setError(json);
if (response.hasError()) {
return response;
}
response.challenge = GlobalChallenge.fromJson(json['data']);
return response;
}

Map<String, dynamic> toJson() =>
{'data': challenge.toJson()};
}
9 changes: 7 additions & 2 deletions lib/net/api_interface/response_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ abstract class ResponseBase {

@protected
setError(Map<String, dynamic> json) {
error =
new Status((json['status'] ?? const {})['code'] ?? Status.API_SUCCESS);
if (json['status'] is Map) {
error = Status((json['status']['code'] ?? Status.API_DEFAULT_ERROR));
} else if (!json.containsKey('status')){
error = Status(Status.API_SUCCESS);
} else {
error = Status(Status.API_DEFAULT_ERROR);
}
}

void setErrorMessage(int errorCode) {
Expand Down
6 changes: 6 additions & 0 deletions lib/net/endpoints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ enum EndpointRoute {
FINISH_MEANING_CHALLENGE,
FINISH_READING_QURAN_CHALLENGE,
FINISH_CUSTOM_SIMPLE_CHALLENGE,
FINISH_GLOBAL_CHALLENGE,
GET_ALL_CHALLENGES,
GET_ALL_CHALLENGES_IN_GROUP,
GET_AZKAR_CHALLENGE,
GET_GLOBAL_CHALLENGE,
DELETE_CHALLENGE,
DELETE_PERSONAL_CHALLENGE,
GET_ORIGINAL_CHALLENGE,
Expand Down Expand Up @@ -175,6 +177,8 @@ class ApiRoutesUtil {
case EndpointRoute.FINISH_CUSTOM_SIMPLE_CHALLENGE:
assert(route.pathVariables.length == 1);
return '/challenges/finish/simple/${route.pathVariables[0]}/';
case EndpointRoute.FINISH_GLOBAL_CHALLENGE:
return '/challenges/finish/global/';
case EndpointRoute.GET_ALL_CHALLENGES:
return '/challenges/v2';
case EndpointRoute.GET_ALL_CHALLENGES_IN_GROUP:
Expand All @@ -183,6 +187,8 @@ class ApiRoutesUtil {
case EndpointRoute.GET_AZKAR_CHALLENGE:
assert(route.pathVariables.length == 1);
return '/challenges/${route.pathVariables[0]}';
case EndpointRoute.GET_GLOBAL_CHALLENGE:
return '/challenges/global';
case EndpointRoute.DELETE_CHALLENGE:
assert(route.pathVariables.length == 1);
return '/challenges/${route.pathVariables[0]}';
Expand Down
32 changes: 31 additions & 1 deletion lib/net/net_services/challenges_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';

import 'package:azkar/models/azkar_challenge.dart';
import 'package:azkar/models/challenge.dart';
import 'package:azkar/models/global_challenge.dart';
import 'package:azkar/net/api_caller.dart';
import 'package:azkar/net/api_exception.dart';
import 'package:azkar/net/api_interface/challenges/requests/add_azkar_challenge_in_group_request.dart';
Expand All @@ -18,12 +19,15 @@ import 'package:azkar/net/api_interface/challenges/responses/add_memorization_ch
import 'package:azkar/net/api_interface/challenges/responses/add_reading_quran_challenge_response.dart';
import 'package:azkar/net/api_interface/challenges/responses/add_simple_custom_challenge_response.dart';
import 'package:azkar/net/api_interface/challenges/responses/delete_challenge_response.dart';
import 'package:azkar/net/api_interface/challenges/responses/finish_custom_simple_challenge_response.dart';
import 'package:azkar/net/api_interface/challenges/responses/finish_global_challenge_response.dart';
import 'package:azkar/net/api_interface/challenges/responses/finish_meaning_challenge_response.dart';
import 'package:azkar/net/api_interface/challenges/responses/finish_memorization_challenge_response.dart';
import 'package:azkar/net/api_interface/challenges/responses/finish_reading_quran_challenge_response.dart';
import 'package:azkar/net/api_interface/challenges/responses/get_azkar_challenge_response.dart';
import 'package:azkar/net/api_interface/challenges/responses/get_challenges_response.dart';
import 'package:azkar/net/api_interface/challenges/responses/get_finished_challenges_count_response.dart';
import 'package:azkar/net/api_interface/challenges/responses/get_global_challenge_response.dart';
import 'package:azkar/net/api_interface/challenges/responses/update_azkar_challenge_response.dart';
import 'package:azkar/net/endpoints.dart';
import 'package:azkar/services/cache_manager.dart';
Expand Down Expand Up @@ -167,7 +171,21 @@ class ChallengesService {
endpointRoute: EndpointRoute.FINISH_CUSTOM_SIMPLE_CHALLENGE,
pathVariables: [id]));

var response = FinishReadingQuranChallengeResponse.fromJson(
var response = FinishCustomSimpleChallengeResponse.fromJson(
jsonDecode(utf8.decode(httpResponse.body.codeUnits)));
if (response.hasError()) {
throw new ApiException(response.error!);
}

await _updateStreakDataAfterFinishingChallenge();
}

Future<void> finishGlobalChallenge() async {
http.Response httpResponse = await ApiCaller.put(
route: Endpoint(
endpointRoute: EndpointRoute.FINISH_GLOBAL_CHALLENGE));

var response = FinishGlobalChallengeResponse.fromJson(
jsonDecode(utf8.decode(httpResponse.body.codeUnits)));
if (response.hasError()) {
throw new ApiException(response.error!);
Expand Down Expand Up @@ -210,6 +228,18 @@ class ChallengesService {
return response.challenge!;
}

Future<GlobalChallenge> getGlobalChallenge() async {
http.Response httpResponse = await ApiCaller.get(
route: Endpoint(
endpointRoute: EndpointRoute.GET_GLOBAL_CHALLENGE));
var response = GetGlobalChallengeResponse.fromJson(
jsonDecode(utf8.decode(httpResponse.body.codeUnits)));
if (response.hasError()) {
throw new ApiException(response.error!);
}
return response.challenge;
}

Future<void> deleteChallenge(String challengeId) async {
ServiceProvider.cacheManager.invalidateFrequentlyChangingData();

Expand Down
2 changes: 1 addition & 1 deletion lib/services/cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CacheManager {
"018";
// Saved in milliseconds since epoch
static const String CACHE_KEY_LAST_FINISHED_CHALLENGE_DATE = "019";
static const String ASKED_FOR_NOTIFICATIONS_PERMISSION = "020";
static const String CACHE_KEY_ASKED_FOR_NOTIFICATIONS_PERMISSION = "020";

Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

Expand Down
16 changes: 16 additions & 0 deletions lib/utils/app_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ class AppLocalizations {
'حدث خطأ أثناء تسجيل الدخول باستخدام جوجل',
'username should not be empty': 'يجب ألا يكون كود المستخدم فارغًا',
'signing in...': 'جاري تسجيل الدخول...',
'global challenge feature': 'تحدي مشترك',
'global challenge feature description':
'هذا تحدي مشترك بين جميع مستخدمي التطبيق',
'global challenge': 'تحدي مشترك',
},
};

Expand Down Expand Up @@ -355,11 +359,23 @@ class AppLocalizations {
return _localizedValues[locale.languageCode]!['delete and copy challenge']!;
}

String get globalChallengeFeature {
return _localizedValues[locale.languageCode]!['global challenge feature']!;
}

String get swipeTheChallengeCardToTheRightToDeleteOrCopyAChallenge {
return _localizedValues[locale.languageCode]![
'swipe the challenge card to the right to delete or copy a challenge']!;
}

String get globalChallengeFeatureDescription {
return _localizedValues[locale.languageCode]!['global challenge feature description']!;
}

String get globalChallenge {
return _localizedValues[locale.languageCode]!['global challenge']!;
}

String get copy {
return _localizedValues[locale.languageCode]!['copy']!;
}
Expand Down
1 change: 1 addition & 0 deletions lib/utils/features.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ class Features {
static const String PROFILE_SCREEN = "profile-screen-feature";
static const String LIVE_SUPPORT_SCREEN = "live-support-screen-feature";
static const String SETTING_SCREEN = "settings-screen-feature";
static const String GLOBAL_CHALLENGE = "global-challenge-feature";
}
1 change: 0 additions & 1 deletion lib/views/auth/auth_main_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ class _AuthMainScreenState extends State<AuthMainScreen>
await account!.authentication;
googleIdToken = authentication.idToken!;
} catch (error) {
print(error);
setState(() {
_isSigningInWithGoogle = false;
});
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 125490e

Please sign in to comment.