Skip to content

Commit

Permalink
live streaming for space info in space list in home
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-ishita-g committed Jul 23, 2024
1 parent eb23acd commit a7da1ba
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 119 deletions.
76 changes: 0 additions & 76 deletions app/ios/Runner/Info.plist

This file was deleted.

3 changes: 3 additions & 0 deletions app/lib/ui/flow/home/components/home_top_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ class _HomeTopBarState extends State<HomeTopBar> with TickerProviderStateMixin {

Widget _spaceListItem(
BuildContext context, SpaceInfo space, int index, bool isSelected) {
if (space.members.isEmpty) {
return const SizedBox();
}
final admin = space.members
.firstWhere(
(member) => member.user.id == space.space.admin_id,
Expand Down
11 changes: 1 addition & 10 deletions app/lib/ui/flow/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:style/extenstions/context_extenstions.dart';
import 'package:yourspace_flutter/domain/extenstions/context_extenstions.dart';
import 'package:yourspace_flutter/domain/extenstions/widget_extensions.dart';
import 'package:yourspace_flutter/ui/app_route.dart';
import 'package:yourspace_flutter/ui/components/app_page.dart';
import 'package:yourspace_flutter/ui/components/error_snakebar.dart';
Expand All @@ -25,17 +24,9 @@ class _HomeScreenState extends ConsumerState<HomeScreen> {
late HomeViewNotifier notifier;
late MapViewNotifier mapNotifier;

@override
void initState() {
super.initState();
runPostFrame(() {
notifier = ref.watch(homeViewStateProvider.notifier);
notifier.getAllSpace();
});
}

@override
Widget build(BuildContext context) {
notifier = ref.watch(homeViewStateProvider.notifier);
final state = ref.watch(homeViewStateProvider);
_observeNavigation(state);
_observeError();
Expand Down
47 changes: 23 additions & 24 deletions app/lib/ui/flow/home/home_screen_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,38 @@ class HomeViewNotifier extends StateNotifier<HomeViewState> {
this._currentSpaceIdController,
this.permissionService,
this._lastBatteryDialogDate,
) : super(const HomeViewState());
) : super(const HomeViewState()) {
getAllSpace();
}

String? get currentSpaceId => _currentSpaceIdController.state;

set currentSpaceId(String? value) {
_currentSpaceIdController.state = value;
}

void getAllSpace() async {
if (state.loading) return;
try {
state = state.copyWith(loading: state.spaceList.isEmpty);
final spaces = await spaceService.getAllSpaceInfo();

final sortedSpaces = spaces.toList();
spaceService.streamAllSpaceInfo().listen((spaces) {
final sortedSpaces = spaces.toList();
if ((currentSpaceId?.isNotEmpty ?? false) && spaces.isNotEmpty) {
final selectedSpaceIndex = sortedSpaces
.indexWhere((space) => space.space.id == currentSpaceId);
if (selectedSpaceIndex > -1) {
final selectedSpace = sortedSpaces.removeAt(selectedSpaceIndex);
sortedSpaces.insert(0, selectedSpace);
updateSelectedSpace(selectedSpace);
}
}
if (spaces.isEmpty) {
state = state.copyWith(loading: false, spaceList: []);
}
state = state.copyWith(loading: false, spaceList: sortedSpaces);

if (currentSpaceId?.isNotEmpty ?? false) {
final selectedSpaceIndex = sortedSpaces
.indexWhere((space) => space.space.id == currentSpaceId);
if (selectedSpaceIndex > -1) {
final selectedSpace = sortedSpaces.removeAt(selectedSpaceIndex);
sortedSpaces.insert(0, selectedSpace);
if ((currentSpaceId?.isEmpty ?? false) && sortedSpaces.isNotEmpty) {
final selectedSpace = sortedSpaces.first;
_currentSpaceIdController.state = selectedSpace.space.id;
updateSelectedSpace(selectedSpace);
}
}

state = state.copyWith(loading: false, spaceList: sortedSpaces);

if ((currentSpaceId?.isEmpty ?? false) && sortedSpaces.isNotEmpty) {
final selectedSpace = sortedSpaces.first;
currentSpaceId = selectedSpace.space.id;
updateSelectedSpace(selectedSpace);
}
});
} catch (error, stack) {
state = state.copyWith(error: error, loading: false);
logger.e(
Expand Down Expand Up @@ -92,7 +91,7 @@ class HomeViewNotifier extends StateNotifier<HomeViewState> {
void updateSelectedSpace(SpaceInfo space) {
if (space != state.selectedSpace) {
state = state.copyWith(selectedSpace: space);
currentSpaceId = space.space.id;
_currentSpaceIdController.state = space.space.id;
}
}

Expand Down
12 changes: 12 additions & 0 deletions data/lib/api/space/api_space_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ class ApiSpaceService {
.toList();
}

Stream<List<ApiSpaceMember>> streamSpaceMemberByUserId(String userId) {
return _spaceRef.firestore
.collectionGroup('space_members')
.where('user_id', isEqualTo: userId)
.snapshots()
.map((querySnapshot) {
return querySnapshot.docs
.map((doc) => ApiSpaceMember.fromJson(doc.data()))
.toList();
});
}

Future<void> enableLocation(
String spaceId, String userId, bool enable) async {
final querySnapshot =
Expand Down
6 changes: 0 additions & 6 deletions data/lib/config.dart

This file was deleted.

51 changes: 48 additions & 3 deletions data/lib/service/space_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,41 @@ class SpaceService {
return spaceInfoList;
}

Stream<List<SpaceInfo>> streamAllSpaceInfo() async* {
await for (final spaces in streamUserSpaces(currentUser?.id ?? '')) {
final List<SpaceInfo> spaceInfoList = [];

if (spaces.isEmpty) yield spaceInfoList;

final spaceInfoStreams = spaces.map((space) {
if (space == null) return Stream.value(null);

return spaceService.getStreamSpaceMemberBySpaceId(space.id).switchMap((members) {
final memberInfoStreams = members.map((member) {
return CombineLatestStream.combine2(
userService.getUserStream(member.user_id),
Stream.value(member.location_enabled),
(user, isLocationEnabled) {
return user != null
? ApiUserInfo(user: user, isLocationEnabled: isLocationEnabled)
: null;
},
);
}).toList();

return CombineLatestStream.list(memberInfoStreams).map((memberInfoList) {
final nonNullMembers = memberInfoList.whereType<ApiUserInfo>().toList();
return SpaceInfo(space: space, members: nonNullMembers);
});
});
}).toList();

yield* CombineLatestStream.list(spaceInfoStreams).map((spaceInfoList) {
return spaceInfoList.where((spaceInfo) => spaceInfo != null).cast<SpaceInfo>().toList();
});
}
}

Future<SpaceInfo?> getCurrentSpaceInfo() async {
final currentSpace = await getCurrentSpace();
if (currentSpace == null) return null;
Expand Down Expand Up @@ -146,6 +181,16 @@ class SpaceService {
return spaces;
}

Stream<List<ApiSpace?>> streamUserSpaces(String userId) async* {
await for (final member in spaceService.streamSpaceMemberByUserId(userId)) {
final spaces = await Future.wait(member.map((spaceMember) async {
final spaceId = spaceMember.space_id;
return await spaceService.getSpace(spaceId);
}).toList());
yield spaces;
}
}

Future<ApiSpace?> getSpace(String spaceId) async {
return spaceService.getSpace(spaceId);
}
Expand Down Expand Up @@ -194,8 +239,8 @@ class SpaceService {
userSpaces
.sort((a, b) => (a?.created_at ?? 0).compareTo(b?.created_at ?? 0));
final currentSpaceId =
userSpaces.isNotEmpty ? userSpaces.firstOrNull?.id ?? '' : '';
this.currentSpaceId = currentSpaceId;
userSpaces.isNotEmpty ? userSpaces.firstOrNull?.id ?? '' : null;
_currentSpaceIdController.state = currentSpaceId;
}

Future<void> leaveSpace(String spaceId) async {
Expand All @@ -206,7 +251,7 @@ class SpaceService {
.sort((a, b) => (a?.created_at ?? 0).compareTo(b?.created_at ?? 0));
final currentSpaceId =
userSpaces.isNotEmpty ? userSpaces.firstOrNull?.id ?? '' : '';
this.currentSpaceId = currentSpaceId;
_currentSpaceIdController.state = currentSpaceId;
}

Future<void> updateSpace(ApiSpace newSpace) async {
Expand Down

0 comments on commit a7da1ba

Please sign in to comment.