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 2, 2024
1 parent e99a693 commit f0e44e1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
10 changes: 6 additions & 4 deletions app/lib/ui/flow/message/chat/chat_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
return const AppProgressIndicator(size: AppProgressIndicatorSize.small);
}
if (index == messages.length - 1) {
runPostFrame(() => notifier.onLoadMore(threadId));
runPostFrame(() => notifier.onLoadMore());
}

final message = messages[index];
Expand Down Expand Up @@ -189,7 +189,9 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
? 0
: showTimeHeader
? 0
: 32
: memberCount > 2
? 32
: 0
: 46,
right: isSender ? 48 : 0),
decoration: BoxDecoration(
Expand Down Expand Up @@ -464,8 +466,8 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
const SizedBox(width: 8),
IconPrimaryButton(
onTap: () {
if (state.threadId.isNotEmpty) {
notifier.sendMessage(state.threadId, state.message.text);
if (state.threadInfo != null || widget.threadInfo != null) {
notifier.sendMessage(state.threadId.isEmpty ? widget.threadInfo?.thread.id ?? '' : state.threadId, state.message.text);
} else {
notifier.createNewThread(widget.spaceInfo.space.id, state.message.text);
}
Expand Down
13 changes: 5 additions & 8 deletions app/lib/ui/flow/message/chat/chat_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,11 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {
: state.spaceInfo?.members.map((members) => members.user.id).toList();

final threadId = await messageService.createThread(spaceId, currentUser?.id ?? '', threadMembersIds ?? []);

if (state.threadId == threadId) return;

state = state.copyWith(showMemberSelectionView: false, threadId: threadId, isNewThread: true);
if (threadId.isNotEmpty) {
sendMessage(threadId, message);
getCreatedThread(threadId);
}
state = state.copyWith(showMemberSelectionView: false, threadId: threadId, isNewThread: true);
} catch (error, stack) {
state = state.copyWith(loading: false, error: error);
logger.e(
Expand All @@ -167,7 +164,7 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {
try {
final thread = await messageService.getThread(threadId);
getThreadMembers(thread!.thread);
state = state.copyWith(threadInfo: thread);
state = state.copyWith(threadInfo: thread, sender: thread.members);
formatMemberNames(thread.members);
} catch (error, stack) {
logger.e(
Expand Down Expand Up @@ -201,7 +198,7 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {
state = state.copyWith(threadId: matchedThread.thread.id, threadInfo: matchedThread, sender: matchedThread.members);
}
} else {
state = state.copyWith(sender: [], messages: [], isNewThread: true);
state = state.copyWith(sender: [], messages: [], isNewThread: true, threadId: '', threadInfo: null);
}
}

Expand Down Expand Up @@ -233,7 +230,7 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {
}
}

void onLoadMore(String threadId) {
void onLoadMore() {
if (!_hasMoreItems || loadingData) return;
loadMoreMessages(state.threadId, true);
}
Expand All @@ -252,7 +249,7 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {
} else if (filteredMembers.length == 1) {
state = state.copyWith(title: filteredMembers[0].user.first_name ?? '');
} else {
state = state.copyWith(title: '');
state = state.copyWith(title: 'Start a new chat');
}
}

Expand Down
5 changes: 1 addition & 4 deletions app/lib/ui/flow/message/thread_list_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ class _ThreadListScreenState extends ConsumerState<ThreadListScreen> {
SlidableAction(
onPressed: (context) {
_showDeleteConfirmation(() {
setState(() {
notifier.deleteThread(thread.thread);
mutableThreads.removeAt(index);
});
notifier.deleteThread(thread.thread);
});
},
backgroundColor: context.colorScheme.alert,
Expand Down
7 changes: 6 additions & 1 deletion app/lib/ui/flow/message/thread_list_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class ThreadListViewNotifier extends StateNotifier<ThreadListViewState> {
try {
state = state.copyWith(loading: state.threadInfo.isEmpty);
messageService.getThreadsWithLatestMessage(spaceId, currentUser!.id).listen((thread) {
if (!state.deleting) {
// thread.sort((a, b) => b.threadMessage.first.created_at!.compareTo(a.threadMessage.first.created_at ?? DateTime.now()));
}
state = state.copyWith(threadInfo: thread, loading: false);
});
} catch (error, stack) {
Expand Down Expand Up @@ -66,8 +69,9 @@ class ThreadListViewNotifier extends StateNotifier<ThreadListViewState> {

void deleteThread(ApiThread thread) async {
try {
state = state.copyWith(deleting: true);
await messageService.deleteThread(thread, currentUser?.id ?? '');
listenThreads(state.space?.space.id ?? '');
state = state.copyWith(deleting: false);
} catch (error, stack) {
state = state.copyWith(error: error);
logger.e(
Expand All @@ -86,6 +90,7 @@ class ThreadListViewState with _$ThreadListViewState {
@Default(false) bool isCreating,
@Default(false) bool loading,
@Default(false) bool fetchingInviteCode,
@Default(false) bool deleting,
SpaceInfo? space,
@Default('') String spaceInvitationCode,
@Default('') String message,
Expand Down
25 changes: 24 additions & 1 deletion app/lib/ui/flow/message/thread_list_view_model.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mixin _$ThreadListViewState {
bool get isCreating => throw _privateConstructorUsedError;
bool get loading => throw _privateConstructorUsedError;
bool get fetchingInviteCode => throw _privateConstructorUsedError;
bool get deleting => throw _privateConstructorUsedError;
SpaceInfo? get space => throw _privateConstructorUsedError;
String get spaceInvitationCode => throw _privateConstructorUsedError;
String get message => throw _privateConstructorUsedError;
Expand All @@ -45,6 +46,7 @@ abstract class $ThreadListViewStateCopyWith<$Res> {
bool isCreating,
bool loading,
bool fetchingInviteCode,
bool deleting,
SpaceInfo? space,
String spaceInvitationCode,
String message,
Expand Down Expand Up @@ -73,6 +75,7 @@ class _$ThreadListViewStateCopyWithImpl<$Res, $Val extends ThreadListViewState>
Object? isCreating = null,
Object? loading = null,
Object? fetchingInviteCode = null,
Object? deleting = null,
Object? space = freezed,
Object? spaceInvitationCode = null,
Object? message = null,
Expand All @@ -98,6 +101,10 @@ class _$ThreadListViewStateCopyWithImpl<$Res, $Val extends ThreadListViewState>
? _value.fetchingInviteCode
: fetchingInviteCode // ignore: cast_nullable_to_non_nullable
as bool,
deleting: null == deleting
? _value.deleting
: deleting // ignore: cast_nullable_to_non_nullable
as bool,
space: freezed == space
? _value.space
: space // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -152,6 +159,7 @@ abstract class _$$ThreadListViewStateImplCopyWith<$Res>
bool isCreating,
bool loading,
bool fetchingInviteCode,
bool deleting,
SpaceInfo? space,
String spaceInvitationCode,
String message,
Expand Down Expand Up @@ -179,6 +187,7 @@ class __$$ThreadListViewStateImplCopyWithImpl<$Res>
Object? isCreating = null,
Object? loading = null,
Object? fetchingInviteCode = null,
Object? deleting = null,
Object? space = freezed,
Object? spaceInvitationCode = null,
Object? message = null,
Expand All @@ -204,6 +213,10 @@ class __$$ThreadListViewStateImplCopyWithImpl<$Res>
? _value.fetchingInviteCode
: fetchingInviteCode // ignore: cast_nullable_to_non_nullable
as bool,
deleting: null == deleting
? _value.deleting
: deleting // ignore: cast_nullable_to_non_nullable
as bool,
space: freezed == space
? _value.space
: space // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -241,6 +254,7 @@ class _$ThreadListViewStateImpl implements _ThreadListViewState {
this.isCreating = false,
this.loading = false,
this.fetchingInviteCode = false,
this.deleting = false,
this.space,
this.spaceInvitationCode = '',
this.message = '',
Expand All @@ -265,6 +279,9 @@ class _$ThreadListViewStateImpl implements _ThreadListViewState {
@JsonKey()
final bool fetchingInviteCode;
@override
@JsonKey()
final bool deleting;
@override
final SpaceInfo? space;
@override
@JsonKey()
Expand Down Expand Up @@ -304,7 +321,7 @@ class _$ThreadListViewStateImpl implements _ThreadListViewState {

@override
String toString() {
return 'ThreadListViewState(allowSave: $allowSave, isCreating: $isCreating, loading: $loading, fetchingInviteCode: $fetchingInviteCode, space: $space, spaceInvitationCode: $spaceInvitationCode, message: $message, spaceList: $spaceList, threadInfo: $threadInfo, threadMessages: $threadMessages, error: $error)';
return 'ThreadListViewState(allowSave: $allowSave, isCreating: $isCreating, loading: $loading, fetchingInviteCode: $fetchingInviteCode, deleting: $deleting, space: $space, spaceInvitationCode: $spaceInvitationCode, message: $message, spaceList: $spaceList, threadInfo: $threadInfo, threadMessages: $threadMessages, error: $error)';
}

@override
Expand All @@ -319,6 +336,8 @@ class _$ThreadListViewStateImpl implements _ThreadListViewState {
(identical(other.loading, loading) || other.loading == loading) &&
(identical(other.fetchingInviteCode, fetchingInviteCode) ||
other.fetchingInviteCode == fetchingInviteCode) &&
(identical(other.deleting, deleting) ||
other.deleting == deleting) &&
(identical(other.space, space) || other.space == space) &&
(identical(other.spaceInvitationCode, spaceInvitationCode) ||
other.spaceInvitationCode == spaceInvitationCode) &&
Expand All @@ -339,6 +358,7 @@ class _$ThreadListViewStateImpl implements _ThreadListViewState {
isCreating,
loading,
fetchingInviteCode,
deleting,
space,
spaceInvitationCode,
message,
Expand All @@ -361,6 +381,7 @@ abstract class _ThreadListViewState implements ThreadListViewState {
final bool isCreating,
final bool loading,
final bool fetchingInviteCode,
final bool deleting,
final SpaceInfo? space,
final String spaceInvitationCode,
final String message,
Expand All @@ -378,6 +399,8 @@ abstract class _ThreadListViewState implements ThreadListViewState {
@override
bool get fetchingInviteCode;
@override
bool get deleting;
@override
SpaceInfo? get space;
@override
String get spaceInvitationCode;
Expand Down
2 changes: 1 addition & 1 deletion data/lib/api/message/api_message_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class ApiMessageService {
}

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

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

0 comments on commit f0e44e1

Please sign in to comment.