Skip to content

Commit

Permalink
Merge pull request #173 from DELTSV/profile-refactoring
Browse files Browse the repository at this point in the history
Profile is not stored by a single bloc state anymore
  • Loading branch information
enzoSoa authored Jul 11, 2024
2 parents c1265a7 + 3ca9827 commit ac033b3
Show file tree
Hide file tree
Showing 18 changed files with 419 additions and 222 deletions.
16 changes: 8 additions & 8 deletions packages/app/lib/app/app_router.gr.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/app/lib/event/fragments/user_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ class UserEvents extends StatelessWidget {
Widget build(BuildContext context) {
return BlocProvidedBuilder<ProfileBloc, ProfileState>(
builder: (context, bloc, state) {
if (bloc.currentProfile == null) return const Text("loading");
final currentProfile = bloc.currentProfile;
if (currentProfile is! ProfileLoadSuccessEvent) return const Text("loading");

return BlocProvider<UserEventsBloc>(
create: (context) => UserEventsBloc(
eventRepository: RepositoryProvider.of<EventRepository>(context),
userId: bloc.currentProfile!.id,
userId: currentProfile.profile.id,
)..add(SubscribeToEvents()),
child: Builder(
builder: (context) {
Expand Down
4 changes: 0 additions & 4 deletions packages/app/lib/event/widgets/map/journey_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import 'package:hollybike/shared/websocket/recieve/websocket_receive_position.da
import 'package:hollybike/theme/bloc/theme_bloc.dart';
import 'package:http/http.dart' as http;
import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';
import '../../../profile/bloc/profile_bloc/profile_bloc.dart';
import '../../../shared/utils/waiter.dart';

class JourneyMap extends StatefulWidget {
Expand Down Expand Up @@ -259,11 +258,9 @@ class _JourneyMapState extends State<JourneyMap> {
PointAnnotationManager pointManager,
UserPositionsState userPositionsState,
) async {
final profileBloc = BlocProvider.of<ProfileBloc>(context);
final colorScheme = Theme.of(context).colorScheme;
final options = await Future.wait(
userPositionsState.userPositions.map((position) async {
final user = profileBloc.getProfileById(position.userId);
final icon = await rootBundle
.load("assets/images/placeholder_profile_picture.jpg");
return PointAnnotationOptions(
Expand All @@ -278,7 +275,6 @@ class _JourneyMapState extends State<JourneyMap> {
iconAnchor: IconAnchor.BOTTOM,
textAnchor: TextAnchor.TOP,
textSize: 12,
textField: user?.username,
textHaloWidth: 2,
textHaloColor: colorScheme.primary.value,
textColor: colorScheme.onPrimary.value,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hollybike/journey/type/user_journey.dart';
import 'package:hollybike/profile/bloc/profile_bloc/profile_bloc.dart';
import 'package:hollybike/shared/utils/add_separators.dart';
import 'package:lottie/lottie.dart';
import '../../../shared/utils/dates.dart';
import '../../../shared/widgets/bloc_provided_builder.dart';
import '../../../shared/widgets/gradient_progress_bar.dart';

enum JourneyModalAction {
Expand Down Expand Up @@ -52,7 +52,8 @@ class _EventParticipationJourneyModalState
}) /
isBetterThan.length;

_betterThanCount = isBetterThan.values.where((element) => element == 100).length;
_betterThanCount =
isBetterThan.values.where((element) => element == 100).length;
}

@override
Expand All @@ -66,10 +67,13 @@ class _EventParticipationJourneyModalState
right: 16,
),
child: SafeArea(
child: BlocBuilder<ProfileBloc, ProfileState>(
builder: (context, state) {
final currentProfile = state.currentProfile;
final isCurrentUser = currentProfile?.id == widget.userId;
child: BlocProvidedBuilder<ProfileBloc, ProfileState>(
builder: (context, bloc, state) {
final currentProfile = bloc.currentProfile;
final isCurrentUser = (currentProfile is ProfileLoadSuccessEvent
? currentProfile.profile.id
: null) ==
widget.userId;

return Column(
mainAxisSize: MainAxisSize.min,
Expand Down Expand Up @@ -224,7 +228,6 @@ class _EventParticipationJourneyModalState
],
),
),

const SizedBox(height: 16),
];
}
Expand Down Expand Up @@ -462,7 +465,6 @@ class _StatItem extends StatelessWidget {
children: _getStats(context),
),
),

if (value == 100)
Positioned(
left: 9,
Expand Down
2 changes: 0 additions & 2 deletions packages/app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ class _MyAppState extends State<MyApp> {
BlocProvider<ProfileBloc>(
lazy: false,
create: (context) => ProfileBloc(
authRepository:
RepositoryProvider.of<AuthRepository>(context),
authSessionRepository:
RepositoryProvider.of<AuthSessionRepository>(context),
profileRepository:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
part of '../profile_bloc.dart';

@immutable
abstract class ProfileEvent {
const ProfileEvent();
}

class SubscribeToCurrentSessionChange extends ProfileEvent {}

extension ProfileEventOperations<T extends ProfileEvent> on List<T> {
int? elementIndex(T profileEvent) {
for (int i = 0; i < length; i++) {
if (profileEvent == this[i]) return i;
}
return null;
}

copyUpdatedFromNullable(T? profileEvent) {
final copy = [...this];
if (profileEvent == null) return copy;

final alreadyExistingLoadEvent = elementIndex(profileEvent);
if (alreadyExistingLoadEvent is int) {
copy[alreadyExistingLoadEvent] = profileEvent;
} else {
copy.add(profileEvent);
}

return copy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
part of '../profile_bloc.dart';

abstract class ProfileLoadEvent extends ProfileEvent {
final AuthSession session;

const ProfileLoadEvent({required this.session}) : super();

@override
bool operator ==(covariant ProfileLoadEvent other) {
return session == other.session;
}

@override
int get hashCode => Object.hash(session, super.hashCode);
}

class ProfileLoadingEvent extends ProfileLoadEvent {
const ProfileLoadingEvent({required super.session});

ProfileLoadSuccessEvent succeeded(Profile profile) =>
ProfileLoadSuccessEvent(session: session, profile: profile);

ProfileLoadErrorEvent failed(Error error) =>
ProfileLoadErrorEvent(session: session, error: error);
}

class ProfileLoadSuccessEvent extends ProfileLoadEvent {
final Profile profile;

const ProfileLoadSuccessEvent({
required super.session,
required this.profile,
});
}

class ProfileLoadErrorEvent extends ProfileLoadEvent {
final Error error;

const ProfileLoadErrorEvent({
required super.session,
required this.error,
});
}

extension ProfileLoadEventFactories on ProfileLoadEvent {
static ProfileLoadingEvent loading({
required AuthSession session,
}) =>
ProfileLoadingEvent(session: session);

static ProfileLoadSuccessEvent success({
required AuthSession session,
required Profile profile,
}) =>
ProfileLoadSuccessEvent(session: session, profile: profile);

static ProfileLoadErrorEvent error({
required AuthSession session,
required Error error,
}) =>
ProfileLoadErrorEvent(session: session, error: error);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
part of '../profile_bloc.dart';

abstract class UserLoadEvent extends ProfileEvent {
final AuthSession observerSession;
final int id;

const UserLoadEvent({required this.observerSession, required this.id})
: super();

@override
bool operator ==(covariant UserLoadEvent other) {
return observerSession == other.observerSession && id == other.id;
}

@override
int get hashCode => Object.hash(observerSession, id.hashCode);
}

class UserLoadingEvent extends UserLoadEvent {
const UserLoadingEvent({required super.observerSession, required super.id});

UserLoadSuccessEvent succeeded(MinimalUser user) => UserLoadSuccessEvent(
observerSession: observerSession,
id: id,
user: user,
);

UserLoadErrorEvent failed(Error error) => UserLoadErrorEvent(
observerSession: observerSession,
id: id,
error: error,
);
}

class UserLoadSuccessEvent extends UserLoadEvent {
final MinimalUser user;

const UserLoadSuccessEvent({
required super.observerSession,
required super.id,
required this.user,
});
}

class UserLoadErrorEvent extends UserLoadEvent {
final Error error;

const UserLoadErrorEvent({
required super.observerSession,
required super.id,
required this.error,
});
}

extension UserLoadEventFactories on UserLoadEvent {
static UserLoadingEvent loading({
required AuthSession observerSession,
required int id,
}) =>
UserLoadingEvent(observerSession: observerSession, id: id);

static UserLoadSuccessEvent success({
required AuthSession observerSession,
required int id,
required MinimalUser user,
}) =>
UserLoadSuccessEvent(
observerSession: observerSession,
id: id,
user: user,
);

static UserLoadErrorEvent error({
required AuthSession observerSession,
required int id,
required Error error,
}) =>
UserLoadErrorEvent(
observerSession: observerSession,
id: id,
error: error,
);
}
Loading

0 comments on commit ac033b3

Please sign in to comment.