Skip to content

Commit

Permalink
temp
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 2e19b32
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 38 deletions.
12 changes: 1 addition & 11 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 All @@ -48,7 +39,6 @@ class _HomeScreenState extends ConsumerState<HomeScreen> {
body: ResumeDetector(
onResume: () {
if(state.selectedSpace != null) {
notifier.getAllSpace();
notifier.showBatteryOptimizationDialog();
mapNotifier.checkUserPermission();
}
Expand Down
38 changes: 20 additions & 18 deletions app/lib/ui/flow/home/home_screen_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class HomeViewNotifier extends StateNotifier<HomeViewState> {
this._currentSpaceIdController,
this.permissionService,
this._lastBatteryDialogDate,
) : super(const HomeViewState());
) : super(const HomeViewState()) {
getAllSpace();
}

String? get currentSpaceId => _currentSpaceIdController.state;

Expand All @@ -41,27 +43,27 @@ class HomeViewNotifier extends StateNotifier<HomeViewState> {
if (state.loading) return;
try {
state = state.copyWith(loading: state.spaceList.isEmpty);
final spaces = await spaceService.getAllSpaceInfo();
spaceService.streamAllSpaceInfo().listen((spaces) {
final sortedSpaces = spaces.toList();
state = state.copyWith(spaceList: [], selectedSpace: null);
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);
updateSelectedSpace(selectedSpace);
}
}

final sortedSpaces = spaces.toList();
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;
currentSpaceId = 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
24 changes: 24 additions & 0 deletions data/lib/api/space/api_space_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ class ApiSpaceService {
return null;
}

Stream<ApiSpace?> streamSpace(String spaceId) {
return _spaceRef.doc(spaceId).snapshots().map((docSnapshot) {
if (docSnapshot.exists) {
final data = docSnapshot.data();
if (data != null && data is ApiSpace) {
return data;
}
}
return null;
});
}

Future<void> joinSpace(String spaceId,
{int role = SPACE_MEMBER_ROLE_MEMBER}) async {
final userId = _currentUser?.id ?? '';
Expand Down Expand Up @@ -102,6 +114,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.streamSpace(spaceId).first;
}).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 2e19b32

Please sign in to comment.