Skip to content

Commit

Permalink
Fetch completion info from user progress bloc - fixes #350
Browse files Browse the repository at this point in the history
  • Loading branch information
JElgar committed Nov 1, 2024
1 parent 8017434 commit 58a7714
Show file tree
Hide file tree
Showing 19 changed files with 457 additions and 449 deletions.
44 changes: 27 additions & 17 deletions lib/assets/components/explore_tiles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import 'package:nowu/models/article.dart';
import 'package:nowu/router.dart';
import 'package:nowu/router.gr.dart';
import 'package:nowu/services/causes_service.dart';
import 'package:nowu/ui/components/user_progress/bloc/user_progress_bloc.dart';
import 'package:nowu/ui/components/user_progress/bloc/user_progress_state.dart';
import 'package:nowu/utils/let.dart';
import 'package:url_launcher/url_launcher.dart';

Expand All @@ -37,7 +39,6 @@ class ExploreCampaignTile extends ExploreTile {
final String headerImage;
final String title;
final Cause cause;
final bool? completed;
final ListCampaign campaign;

ExploreCampaignTile(
Expand All @@ -46,7 +47,6 @@ class ExploreCampaignTile extends ExploreTile {
}) : headerImage = tile.headerImage.url,
title = tile.title,
cause = tile.cause,
completed = tile.isCompleted,
campaign = tile,
super(key: key);

Expand Down Expand Up @@ -74,13 +74,16 @@ class ExploreCampaignTile extends ExploreTile {
width: double.infinity,
height: 150,
),
if (completed != null)
Padding(
padding: const EdgeInsets.all(8.0),
child: _ExploreTileCheckmark(
completed: completed!,
),
),
BlocBuilder<UserProgressBloc, UserProgressState>(
builder: (context, state) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: _ExploreTileCheckmark(
completed: state.campaignIsCompleted(campaign.id),
),
);
},
),
],
),
),
Expand Down Expand Up @@ -120,7 +123,7 @@ class ExploreActionTile extends ExploreResourceTile {
icon: tile.type.icon,
cause: tile.cause,
timeText: tile.timeText,
isCompleted: tile.isCompleted,
getIsComplete: (state) => state.actionIsCompleted(tile.id),
style: style,
onTap: (BuildContext context) =>
context.router.push(ActionInfoRoute(actionId: tile.id)),
Expand Down Expand Up @@ -181,10 +184,10 @@ class ExploreLearningResourceTileInner extends ExploreResourceTile {
icon: tile.icon,
cause: tile.cause,
timeText: tile.timeText,
isCompleted: tile.isCompleted,
key: key,
style: style,
onTap: (_) async => onTap(),
getIsComplete: (state) => state.learningResourceIsCompleted(tile.id),
);
}

Expand All @@ -199,9 +202,9 @@ abstract class ExploreResourceTile extends ExploreTile {
final IconData icon;
final Cause cause;
final String timeText;
final bool? isCompleted;
final ExploreTileStyle style;
final Future<void> Function(BuildContext context) onTap;
final bool Function(UserProgressState state) getIsComplete;

ExploreResourceTile({
required this.title,
Expand All @@ -212,8 +215,8 @@ abstract class ExploreResourceTile extends ExploreTile {
required this.icon,
required this.cause,
required this.timeText,
required this.isCompleted,
required this.onTap,
required this.getIsComplete,
ExploreTileStyle? style,
Key? key,
}) : this.style = style ?? ExploreTileStyle.Standard,
Expand Down Expand Up @@ -260,10 +263,17 @@ abstract class ExploreResourceTile extends ExploreTile {
textScaler: const TextScaler.linear(.8),
),
Expanded(child: Container()),
if (isCompleted != null)
_ExploreTileCheckmark(
completed: isCompleted!,
),
BlocBuilder<UserProgressBloc, UserProgressState>(
builder: (context, state) {
final isComplete = getIsComplete(state);
if (isComplete) {
return _ExploreTileCheckmark(
completed: isComplete,
);
}
return Container();
},
),
],
),
),
Expand Down
6 changes: 6 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'package:nowu/services/pushNotifications.dart';
import 'package:nowu/services/storage.dart';
import 'package:nowu/services/user_service.dart';
import 'package:nowu/themes.dart';
import 'package:nowu/ui/components/user_progress/bloc/user_progress_bloc.dart';
import 'package:nowu/ui/views/authentication/bloc/authentication_bloc.dart';
import 'package:nowu/ui/views/authentication/bloc/authentication_state.dart';
import 'package:nowu/ui/views/causes/bloc/causes_bloc.dart';
Expand Down Expand Up @@ -126,6 +127,11 @@ class App extends StatelessWidget {
)..fetchInternalNotifactions();
},
),
BlocProvider(
create: (_) => UserProgressBloc(
causesService: locator<CausesService>(),
)..fetchUserState(),
),
],
child: Builder(
builder: (context) {
Expand Down
16 changes: 1 addition & 15 deletions lib/models/action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:causeApiClient/causeApiClient.dart' as Api;
import 'package:built_collection/built_collection.dart';
import 'package:nowu/assets/icons/customIcons.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:nowu/models/user.dart';
import 'package:nowu/models/cause.dart';
import 'package:nowu/models/exploreable.dart';
import 'package:nowu/models/time.dart';
Expand Down Expand Up @@ -114,17 +113,12 @@ class ListAction implements Explorable {
DateTime releaseAt;
DateTime createdAt;
int time;
bool isCompleted;

DateTime get releaseTime => releaseAt;

bool get isNew => isNewDate(releaseTime);
String get timeText => getTimeText(time);

bool isCompletedByUser(Api.CausesUser user) {
return user.completedActionIds.contains(this.id);
}

ListAction({
required this.id,
required this.title,
Expand All @@ -133,7 +127,6 @@ class ListAction implements Explorable {
required this.time,
required this.cause,
required this.type,
required this.isCompleted,
});

ListAction.fromApiData({
Expand All @@ -144,14 +137,11 @@ class ListAction implements Explorable {
required this.time,
required BuiltList<Api.Cause> causes,
required Api.ActionTypeEnum actionType,
required CausesUser? userInfo,
}) : cause = Cause.fromApiModel(causes[0], userInfo),
isCompleted = userInfo?.completedActionIds.contains(id) ?? false,
}) : cause = Cause.fromApiModel(causes[0]),
type = getActionTypeFromSubtype(actionType);

factory ListAction.fromApiModel(
Api.ListAction apiModel,
CausesUser? userInfo,
) {
return ListAction.fromApiData(
id: apiModel.id,
Expand All @@ -161,7 +151,6 @@ class ListAction implements Explorable {
time: apiModel.time,
causes: apiModel.causes,
actionType: apiModel.actionType,
userInfo: userInfo,
);
}
}
Expand All @@ -182,12 +171,10 @@ class Action extends ListAction {
required super.time,
required super.cause,
required super.type,
required super.isCompleted,
});

Action.fromApiModel(
Api.Action apiModel,
CausesUser? userInfo,
) : whatDescription = apiModel.whatDescription,
whyDescription = apiModel.whyDescription,
link = Uri.parse(apiModel.link),
Expand All @@ -199,6 +186,5 @@ class Action extends ListAction {
time: apiModel.time,
causes: apiModel.causes,
actionType: apiModel.actionType,
userInfo: userInfo,
);
}
30 changes: 5 additions & 25 deletions lib/models/campaign.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:built_collection/built_collection.dart';
import 'package:causeApiClient/causeApiClient.dart' as Api;
import 'package:nowu/models/exploreable.dart';
import 'package:nowu/models/user.dart';
import 'package:nowu/services/causes_service.dart';

class Campaign extends ListCampaign {
Expand All @@ -14,36 +13,22 @@ class Campaign extends ListCampaign {
required super.title,
required super.headerImage,
required super.cause,
required super.isCompleted,
required this.description,
required this.actions,
required this.learningResources,
});

Campaign.fromApiModel(Api.Campaign apiModel, CausesUser? causesUser)
Campaign.fromApiModel(Api.Campaign apiModel)
: description = apiModel.description.replaceAll('\\n', '\n\n'),
actions = apiModel.actions
.map(
(action) => ListAction.fromApiModel(
action,
causesUser,
),
)
.toList(),
actions = apiModel.actions.map(ListAction.fromApiModel).toList(),
learningResources = apiModel.learningResources
.map(
(learningResource) => LearningResource.fromApiModel(
learningResource,
causesUser,
),
)
.map(LearningResource.fromApiModel)
.toList(),
super.fromApiData(
id: apiModel.id,
title: apiModel.title,
headerImage: apiModel.headerImage,
causes: apiModel.causes,
causesUser: causesUser,
);

Future<String> getShareText() async {
Expand All @@ -63,31 +48,26 @@ class ListCampaign implements Explorable {
String title;
Api.Image headerImage;
Cause cause;
bool isCompleted;

ListCampaign({
required this.id,
required this.title,
required this.headerImage,
required this.cause,
required this.isCompleted,
});

ListCampaign.fromApiData({
required this.id,
required this.title,
required this.headerImage,
required BuiltList<Api.Cause> causes,
required CausesUser? causesUser,
}) : cause = Cause.fromApiModel(causes[0], causesUser),
isCompleted = causesUser?.completedCampaignIds.contains(id) ?? false;
}) : cause = Cause.fromApiModel(causes[0]);

ListCampaign.fromApiModel(Api.ListCampaign apiModel, CausesUser? causesUser)
ListCampaign.fromApiModel(Api.ListCampaign apiModel)
: this.fromApiData(
id: apiModel.id,
title: apiModel.title,
headerImage: apiModel.headerImage,
causes: apiModel.causes,
causesUser: causesUser,
);
}
8 changes: 2 additions & 6 deletions lib/models/cause.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:nowu/assets/icons/customIcons.dart';
import 'package:flutter/widgets.dart';
import 'package:causeApiClient/src/model/icon_enum.dart';
import 'package:nowu/models/user.dart';
import 'package:nowu/models/exploreable.dart';

export 'package:causeApiClient/causeApiClient.dart' show Cause, IconEnum;
Expand All @@ -18,25 +17,22 @@ class Cause implements Explorable {
final String title;
final String description;
final IconData icon;
final bool isSelected;
final Api.Image headerImage;

Cause({
required this.id,
required this.title,
required this.description,
required this.icon,
required this.isSelected,
required this.headerImage,
});

Cause.fromApiModel(Api.Cause apiModel, CausesUser? userInfo)
Cause.fromApiModel(Api.Cause apiModel)
: id = apiModel.id,
title = apiModel.title,
description = apiModel.description,
headerImage = apiModel.headerImage,
icon = _iconFromApiModelIcon(apiModel.icon),
isSelected = userInfo?.selectedCausesIds.contains(apiModel.id) ?? false;
icon = _iconFromApiModelIcon(apiModel.icon);
}

IconData _iconFromApiModelIcon(IconEnum icon) {
Expand Down
11 changes: 2 additions & 9 deletions lib/models/learning.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:causeApiClient/causeApiClient.dart' as Api;
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:nowu/assets/icons/customIcons.dart';
import 'package:nowu/models/user.dart';
import 'package:nowu/models/cause.dart';
import 'package:nowu/models/exploreable.dart';
import 'package:nowu/models/time.dart';
Expand Down Expand Up @@ -66,7 +65,6 @@ class LearningResource extends Explorable {
int time;
DateTime createdAt;
Uri link;
bool isCompleted;

LearningResource({
required this.id,
Expand All @@ -76,22 +74,17 @@ class LearningResource extends Explorable {
required this.type,
required this.time,
required this.createdAt,
required this.isCompleted,
});

LearningResource.fromApiModel(
Api.LearningResource apiModel,
CausesUser? userInfo,
) : id = apiModel.id,
title = apiModel.title,
link = Uri.parse(apiModel.link),
cause = Cause.fromApiModel(apiModel.causes[0], userInfo),
cause = Cause.fromApiModel(apiModel.causes[0]),
type = getResourceTypeFromEnum(apiModel.learningResourceType),
time = apiModel.time,
createdAt = apiModel.createdAt,
isCompleted =
userInfo?.completedLearningResourceIds.contains(apiModel.id) ??
false;
createdAt = apiModel.createdAt;

String get timeText => timeBrackets.firstWhere((b) => b.maxTime > time).text;

Expand Down
Loading

0 comments on commit 58a7714

Please sign in to comment.