Skip to content

Commit

Permalink
fix of invitation view models and view to follow MVVM
Browse files Browse the repository at this point in the history
  • Loading branch information
bardram committed Jan 22, 2024
1 parent e234cc4 commit 732973d
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 51 deletions.
9 changes: 1 addition & 8 deletions lib/blocs/app_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,9 @@ class StudyAppBLoC extends ChangeNotifier {
StudyAppState _state = StudyAppState.created;
final CarpBackend _backend = CarpBackend();
final CarpStudyAppViewModel _appViewModel = CarpStudyAppViewModel();
StudyDeploymentStatus? _status;
final StreamController<StudyAppState> _stateStreamController =
StreamController.broadcast();

List<ActiveParticipationInvitation> invitations = [];

List<Message> _messages = [];
final StreamController<int> _messageStreamController =
StreamController.broadcast();
Expand Down Expand Up @@ -88,7 +85,7 @@ class StudyAppBLoC extends ChangeNotifier {

/// Create the BLoC for the app specifying:
/// * debug level
/// * deployment mode (production, test, dev)
/// * deployment mode (production, test, dev, local)
StudyAppBLoC({
this.debugLevel = DebugLevel.info,
this.deploymentMode = DeploymentMode.dev,
Expand Down Expand Up @@ -124,15 +121,11 @@ class StudyAppBLoC extends ChangeNotifier {
/// The deployment running on this phone.
SmartphoneDeployment? get deployment => Sensing().controller?.deployment;

/// Get the latest status of the study deployment.
StudyDeploymentStatus? get status => _status;

/// When was this study deployed on this phone.
DateTime? get studyStartTimestamp => deployment?.deployed;

/// The overall data model for this app
CarpStudyAppViewModel get appViewModel => _appViewModel;
// Future<void>? dataPageInitialization;

/// Initialize this BLOC. Called before being used for anything.
Future<void> initialize() async {
Expand Down
7 changes: 4 additions & 3 deletions lib/carp_study_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class CarpStudyAppState extends State<CarpStudyApp> {
? firstRoute
: (bloc.studyId == null ? InvitationListPage.route : null),
builder: (context, state) => InformedConsentPage(
bloc.appViewModel.informedConsentViewModel,
model: bloc.appViewModel.informedConsentViewModel,
),
),
GoRoute(
Expand All @@ -134,6 +134,7 @@ class CarpStudyAppState extends State<CarpStudyApp> {
path: '${InvitationDetailsPage.route}/:invitationId',
parentNavigatorKey: _rootNavigatorKey,
builder: (context, state) => InvitationDetailsPage(
model: bloc.appViewModel.invitationsListViewModel,
invitationId: state.pathParameters['invitationId'] ?? '',
),
),
Expand All @@ -143,8 +144,8 @@ class CarpStudyAppState extends State<CarpStudyApp> {
redirect: (context, state) => bloc.studyId != null
? InformedConsentPage.route
: (bloc.user == null ? LoginPage.route : null),
builder: (context, state) =>
InvitationListPage(bloc.appViewModel.invitationsListViewModel),
builder: (context, state) => InvitationListPage(
model: bloc.appViewModel.invitationsListViewModel),
),
],
debugLogDiagnostics: true,
Expand Down
27 changes: 27 additions & 0 deletions lib/data/carp_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,33 @@ class CarpBackend {
String? get username => user?.username;
OAuthToken? get oauthToken => user?.token;

/// The list of invitation for this user.
List<ActiveParticipationInvitation> invitations = [];

Future<List<ActiveParticipationInvitation>> getInvitations() async {
CarpParticipationService().configureFrom(CarpService());

invitations =
await CarpParticipationService().getActiveParticipationInvitations();

// Filter the invitations to only include those that
// have a smartphone as a device in [ActiveParticipationInvitation.assignedDevices] list
// (i.e. the invitation is for a smartphone).
// This is done to avoid showing invitations for other devices (e.g. [WebBrowser]).
invitations.removeWhere((invitation) =>
invitation.assignedDevices
?.any((device) => device.device is! Smartphone) ??
false);

// _invitations = _invitations
// .where((invitation) =>
// invitation.assignedDevices
// ?.any((device) => device.device is Smartphone) ??
// false)
// .toList();
return invitations;
}

/// Upload the result of an informed consent flow.
Future<ConsentDocument?> uploadInformedConsent(
RPTaskResult taskResult) async {
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ part 'view_models/study_page_model.dart';
part 'view_models/profile_page_model.dart';
part 'view_models/devices_page_model.dart';
part 'view_models/data_visualization_page_model.dart';
part 'view_models/invitations_list_model.dart';
part 'view_models/invitations_view_model.dart';
part 'view_models/informed_consent_page_model.dart';
part 'view_models/cards/activity_data_model.dart';
part 'view_models/cards/mobility_data_model.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/informed_consent_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ part of carp_study_app;
class InformedConsentPage extends StatefulWidget {
static const String route = '/consent';
final InformedConsentViewModel model;
const InformedConsentPage(this.model, {super.key});
const InformedConsentPage({super.key, required this.model});

@override
InformedConsentState createState() => InformedConsentState();
Expand Down
10 changes: 4 additions & 6 deletions lib/ui/pages/invitation_list_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ part of carp_study_app;

class InvitationListPage extends StatelessWidget {
static const String route = '/invitations';
final InvitationsListViewModel model;
const InvitationListPage(this.model, {super.key});
final InvitationsViewModel model;
const InvitationListPage({super.key, required this.model});

@override
Widget build(BuildContext context) {
RPLocalizations locale = RPLocalizations.of(context)!;
return Scaffold(
body: FutureBuilder<List<ActiveParticipationInvitation>>(
future: model.invitations,
future: bloc.backend.getInvitations(),
builder: (context, snapshot) {
Widget child;

Expand Down Expand Up @@ -50,9 +50,7 @@ class InvitationListPage extends StatelessWidget {
pinned: true,
stretch: true,
stretchTriggerOffset: 20,
onStretchTrigger: () async {
await model.invitations;
},
onStretchTrigger: () async => bloc.backend.getInvitations(),
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
Expand Down
5 changes: 3 additions & 2 deletions lib/ui/pages/invitation_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ part of carp_study_app;
class InvitationDetailsPage extends StatelessWidget {
static const String route = '/invitation';
final String invitationId;
final InvitationsViewModel model;

const InvitationDetailsPage({
super.key,
required this.invitationId,
required this.model,
});

@override
Widget build(BuildContext context) {
RPLocalizations locale = RPLocalizations.of(context)!;
ActiveParticipationInvitation invitation = bloc.invitations.firstWhere(
(element) => element.participation.participantId == invitationId);
var invitation = model.getInvitation(invitationId);

return Scaffold(
appBar: AppBar(
Expand Down
25 changes: 0 additions & 25 deletions lib/view_models/invitations_list_model.dart

This file was deleted.

10 changes: 10 additions & 0 deletions lib/view_models/invitations_view_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
part of carp_study_app;

class InvitationsViewModel extends ViewModel {
List<ActiveParticipationInvitation> get invitations =>
bloc.backend.invitations;

ActiveParticipationInvitation getInvitation(String invitationId) =>
invitations.firstWhere((invitation) =>
invitation.participation.participantId == invitationId);
}
2 changes: 0 additions & 2 deletions lib/view_models/study_page_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,4 @@ class StudyPageViewModel extends ViewModel {
timestamp: DateTime.now(),
image: 'assets/images/kids.png',
);

StudyPageViewModel();
}
5 changes: 2 additions & 3 deletions lib/view_models/view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ class CarpStudyAppViewModel extends ViewModel {
final TaskListPageViewModel _taskListPageViewModel = TaskListPageViewModel();
final ProfilePageViewModel _profilePageViewModel = ProfilePageViewModel();
final DevicesPageViewModel _devicesPageViewModel = DevicesPageViewModel();
final InvitationsListViewModel _invitationsListViewModel =
InvitationsListViewModel();
final InvitationsViewModel _invitationsListViewModel = InvitationsViewModel();
final InformedConsentViewModel _informedConsentViewModel =
InformedConsentViewModel();

Expand All @@ -176,7 +175,7 @@ class CarpStudyAppViewModel extends ViewModel {
TaskListPageViewModel get taskListPageViewModel => _taskListPageViewModel;
ProfilePageViewModel get profilePageViewModel => _profilePageViewModel;
DevicesPageViewModel get devicesPageViewModel => _devicesPageViewModel;
InvitationsListViewModel get invitationsListViewModel =>
InvitationsViewModel get invitationsListViewModel =>
_invitationsListViewModel;
InformedConsentViewModel get informedConsentViewModel =>
_informedConsentViewModel;
Expand Down

0 comments on commit 732973d

Please sign in to comment.