Skip to content

Commit

Permalink
fix permission denied error
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-ishita-g committed Jul 24, 2024
1 parent 8a298d0 commit af1bda6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 60 deletions.
16 changes: 7 additions & 9 deletions app/lib/ui/flow/message/thread_list_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ 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';
import 'package:yourspace_flutter/ui/components/resume_detector.dart';
import 'package:yourspace_flutter/ui/flow/message/thread_list_view_model.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

Expand Down Expand Up @@ -55,9 +54,7 @@ class _ThreadListScreenState extends ConsumerState<ThreadListScreen> {

return AppPage(
title: widget.spaceInfo.space.name,
body: ResumeDetector(
onResume: () => notifier.listenThreads(widget.spaceInfo.space.id),
child: _body(context, state)),
body: _body(context, state),
floatingActionButton: widget.spaceInfo.members.length >= 2
? LargeIconButton(
onTap: () {
Expand All @@ -81,11 +78,11 @@ class _ThreadListScreenState extends ConsumerState<ThreadListScreen> {
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
child: state.threadInfo.isEmpty
? _emptyView(context, state)
: _threadList(context, state.threadInfo),
: _threadList(context, state.threadInfo, state.threadMessages),
);
}

Widget _threadList(BuildContext context, List<ThreadInfo> threads) {
Widget _threadList(BuildContext context, List<ThreadInfo> threads, List<List<ApiThreadMessage>> threadMessages) {
List<ThreadInfo> mutableThreads = List.from(threads);

return ListView.builder(
Expand All @@ -99,6 +96,9 @@ class _ThreadListScreenState extends ConsumerState<ThreadListScreen> {
final hasUnreadMessage = thread.threadMessage
.any((message) => !message.seen_by.contains(notifier.currentUser?.id));

final filteredMessages = index < threadMessages.length ? threadMessages[index] : [];
final firstMessage = filteredMessages.isNotEmpty ? filteredMessages.first.message : '';

return Slidable(
endActionPane: ActionPane(
motion: const ScrollMotion(),
Expand Down Expand Up @@ -127,9 +127,7 @@ class _ThreadListScreenState extends ConsumerState<ThreadListScreen> {
context: context,
members: members,
displayedMembers: displayedMembers,
message: thread.threadMessage.isNotEmpty
? thread.threadMessage.first.message
: '',
message: firstMessage,
date: date ?? DateTime.now(),
hasUnreadMessage: hasUnreadMessage,
),
Expand Down
70 changes: 31 additions & 39 deletions app/lib/ui/flow/message/thread_list_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,23 @@ class ThreadListViewNotifier extends StateNotifier<ThreadListViewState> {
final SpaceService spaceService;
final ApiMessageService messageService;
final ApiUser? currentUser;
late StreamSubscription<List<ApiThreadMessage>> _userSubscription;
final List<StreamSubscription<List<ApiThreadMessage>>> _userSubscriptions =
[];

ThreadListViewNotifier(this.spaceId, this.spaceService, this.messageService, this.currentUser) : super(const ThreadListViewState());
ThreadListViewNotifier(
this.spaceId, this.spaceService, this.messageService, this.currentUser)
: super(const ThreadListViewState());

void setSpace(SpaceInfo space) {
state = state.copyWith(space: space);
listenThreads(space.space.id);
if (state.threadInfo.isNotEmpty) {
_listenLastMessage(state.threadInfo);
}
}

void listenThreads(String spaceId) async {
try {
state = state.copyWith(loading: state.threadInfo.isEmpty);
messageService.getThreadsWithLatestMessage(spaceId, currentUser!.id).listen((threads) {
messageService.getThreadsWithMembers(spaceId, currentUser!.id).listen((threads) {
print('called');
final filteredThreads = _filterArchivedThreads(threads);

filteredThreads.sort((a, b) {
Expand All @@ -56,45 +57,33 @@ class ThreadListViewNotifier extends StateNotifier<ThreadListViewState> {
});

state = state.copyWith(threadInfo: filteredThreads, loading: false);
_listenLastMessage(filteredThreads);
_listenLastMessage();
});
} catch (error, stack) {
logger.e(
'ChatViewNotifier: error while listing message threads',
error: error,
stackTrace: stack,
);
logger.e('ChatViewNotifier: error while listing message threads',
error: error, stackTrace: stack);
state = state.copyWith(error: error, loading: false);
}
}

void _listenLastMessage(List<ThreadInfo> threads) async {
void _listenLastMessage() async {
try {
for (ThreadInfo threadInfo in threads) {
_userSubscription = messageService.streamLatestMessages(threadInfo.thread.id).listen((threadMessages) {
_updateThreadMessages(threadInfo.thread.id, threadMessages);
final List<List<ApiThreadMessage>> newThreadMessages = List.generate(state.threadInfo.length, (_) => []);

for (int i = 0; i < state.threadInfo.length; i++) {
final threads = state.threadInfo[i];
final subscription = messageService.streamLatestMessages(threads.thread.id).listen((threadMessages) {
newThreadMessages[i] = threadMessages;
state = state.copyWith(threadMessages: List.from(newThreadMessages));
});
_userSubscriptions.add(subscription);
}
} catch (error, stack) {
logger.e(
'ChatViewNotifier: error while listening to latest messages',
error: error,
stackTrace: stack,
);
logger.e('ChatViewNotifier: error while listening to latest messages',
error: error, stackTrace: stack);
}
}

void _updateThreadMessages(String threadId, List<ApiThreadMessage> threadMessages) {
final updatedThreads = state.threadInfo.map((threadInfo) {
if (threadInfo.thread.id == threadId) {
return threadInfo.copyWith(threadMessage: threadMessages);
}
return threadInfo;
}).toList();

state = state.copyWith(threadInfo: updatedThreads);
}

List<ThreadInfo> _filterArchivedThreads(List<ThreadInfo> threads) {
return threads.where((info) {
final archiveTimestamp = info.thread.archived_for?[currentUser?.id];
Expand Down Expand Up @@ -140,14 +129,17 @@ class ThreadListViewNotifier extends StateNotifier<ThreadListViewState> {
}

void _cancelSubscriptions() {
_userSubscription.cancel();
for (var subscription in _userSubscriptions) {
subscription.cancel();
}
_userSubscriptions.clear();
}

// @override
// void dispose() {
// _cancelSubscriptions();
// super.dispose();
// }
@override
void dispose() {
_cancelSubscriptions();
super.dispose();
}
}

@freezed
Expand All @@ -163,7 +155,7 @@ class ThreadListViewState with _$ThreadListViewState {
@Default('') String message,
@Default([]) List<SpaceInfo> spaceList,
@Default([]) List<ThreadInfo> threadInfo,
@Default([]) List<ApiThreadMessage> threadMessages,
@Default([]) List<List<ApiThreadMessage>> threadMessages,
Object? error,
}) = _ThreadListViewState;
}
20 changes: 10 additions & 10 deletions app/lib/ui/flow/message/thread_list_view_model.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mixin _$ThreadListViewState {
String get message => throw _privateConstructorUsedError;
List<SpaceInfo> get spaceList => throw _privateConstructorUsedError;
List<ThreadInfo> get threadInfo => throw _privateConstructorUsedError;
List<ApiThreadMessage> get threadMessages =>
List<List<ApiThreadMessage>> get threadMessages =>
throw _privateConstructorUsedError;
Object? get error => throw _privateConstructorUsedError;

Expand All @@ -52,7 +52,7 @@ abstract class $ThreadListViewStateCopyWith<$Res> {
String message,
List<SpaceInfo> spaceList,
List<ThreadInfo> threadInfo,
List<ApiThreadMessage> threadMessages,
List<List<ApiThreadMessage>> threadMessages,
Object? error});

$SpaceInfoCopyWith<$Res>? get space;
Expand Down Expand Up @@ -128,7 +128,7 @@ class _$ThreadListViewStateCopyWithImpl<$Res, $Val extends ThreadListViewState>
threadMessages: null == threadMessages
? _value.threadMessages
: threadMessages // ignore: cast_nullable_to_non_nullable
as List<ApiThreadMessage>,
as List<List<ApiThreadMessage>>,
error: freezed == error ? _value.error : error,
) as $Val);
}
Expand Down Expand Up @@ -165,7 +165,7 @@ abstract class _$$ThreadListViewStateImplCopyWith<$Res>
String message,
List<SpaceInfo> spaceList,
List<ThreadInfo> threadInfo,
List<ApiThreadMessage> threadMessages,
List<List<ApiThreadMessage>> threadMessages,
Object? error});

@override
Expand Down Expand Up @@ -240,7 +240,7 @@ class __$$ThreadListViewStateImplCopyWithImpl<$Res>
threadMessages: null == threadMessages
? _value._threadMessages
: threadMessages // ignore: cast_nullable_to_non_nullable
as List<ApiThreadMessage>,
as List<List<ApiThreadMessage>>,
error: freezed == error ? _value.error : error,
));
}
Expand All @@ -260,7 +260,7 @@ class _$ThreadListViewStateImpl implements _ThreadListViewState {
this.message = '',
final List<SpaceInfo> spaceList = const [],
final List<ThreadInfo> threadInfo = const [],
final List<ApiThreadMessage> threadMessages = const [],
final List<List<ApiThreadMessage>> threadMessages = const [],
this.error})
: _spaceList = spaceList,
_threadInfo = threadInfo,
Expand Down Expand Up @@ -307,10 +307,10 @@ class _$ThreadListViewStateImpl implements _ThreadListViewState {
return EqualUnmodifiableListView(_threadInfo);
}

final List<ApiThreadMessage> _threadMessages;
final List<List<ApiThreadMessage>> _threadMessages;
@override
@JsonKey()
List<ApiThreadMessage> get threadMessages {
List<List<ApiThreadMessage>> get threadMessages {
if (_threadMessages is EqualUnmodifiableListView) return _threadMessages;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_threadMessages);
Expand Down Expand Up @@ -387,7 +387,7 @@ abstract class _ThreadListViewState implements ThreadListViewState {
final String message,
final List<SpaceInfo> spaceList,
final List<ThreadInfo> threadInfo,
final List<ApiThreadMessage> threadMessages,
final List<List<ApiThreadMessage>> threadMessages,
final Object? error}) = _$ThreadListViewStateImpl;

@override
Expand All @@ -411,7 +411,7 @@ abstract class _ThreadListViewState implements ThreadListViewState {
@override
List<ThreadInfo> get threadInfo;
@override
List<ApiThreadMessage> get threadMessages;
List<List<ApiThreadMessage>> get threadMessages;
@override
Object? get error;
@override
Expand Down
4 changes: 2 additions & 2 deletions data/lib/api/message/api_message_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class ApiMessageService {
await batch.commit();
}

Stream<List<ThreadInfo>> getThreadsWithLatestMessage(String spaceId, String userId) {
Stream<List<ThreadInfo>> getThreadsWithMembers(String spaceId, String userId) {
return getThreads(spaceId, userId).asyncExpand((threads) {
if (threads.isEmpty) return Stream.value([]);

Expand All @@ -158,7 +158,7 @@ class ApiMessageService {
}

Future<void> deleteThread(ApiThread thread, String userId) async {
final doc = _db.collection('space_threads').doc(thread.id);
final doc = _spaceThreadRef.doc(thread.id);
final archivedThread = Map<String, double>.from(thread.archived_for ?? {});

if (thread.admin_id == userId) {
Expand Down

0 comments on commit af1bda6

Please sign in to comment.