Skip to content

Commit

Permalink
remove config file
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-ishita-g committed Jul 22, 2024
1 parent 7f04cbb commit 949b7f2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 29 deletions.
16 changes: 14 additions & 2 deletions app/lib/ui/flow/message/chat/chat_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:data/api/auth/api_user_service.dart';
import 'package:data/api/auth/auth_models.dart';
import 'package:data/api/message/api_message_service.dart';
Expand Down Expand Up @@ -36,6 +38,7 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {

bool _hasMoreItems = true;
bool loadingData = false;
StreamSubscription<List<ApiThreadMessage>>? _messageSubscription;

ChatViewNotifier(this.threadId, this.messageService, this.apiMessageService, this.userService, this.currentUser)
: super(ChatViewState(message: TextEditingController(), currentUserId: currentUser?.id ?? ''));
Expand All @@ -61,8 +64,10 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {
void listenThread(String threadId) async {
try {
if (threadId.isEmpty) return;
if (state.creating) return;
_cancelMessageSubscription();
state = state.copyWith(loading: state.messages.isEmpty);
messageService.getLatestMessages(threadId, limit: 20).listen((messages) {
_messageSubscription = messageService.getLatestMessages(threadId, limit: 20).listen((messages) {
state = state.copyWith(messages: messages, loading: false);
_hasMoreItems = messages.length == MAX_PAGE_LIMIT;
});
Expand Down Expand Up @@ -135,6 +140,8 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {

void createNewThread(String spaceId, String message) async {
try {
state = state.copyWith(creating: true);
_cancelMessageSubscription();
List<String> selectedMembers = [];
if (!selectedMembers.contains(currentUser?.id) && state.selectedMember.isNotEmpty) {
selectedMembers.addAll(state.selectedMember);
Expand All @@ -148,7 +155,6 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {
state = state.copyWith(showMemberSelectionView: false, threadId: threadId, isNewThread: true);
if (threadId.isNotEmpty) {
sendMessage(threadId, message);
getCreatedThread(threadId);
}
} catch (error, stack) {
state = state.copyWith(loading: false, error: error);
Expand All @@ -160,6 +166,11 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {
}
}

void _cancelMessageSubscription() {
_messageSubscription?.cancel();
_messageSubscription = null;
}

void getCreatedThread(String threadId) async {
try {
final thread = await messageService.getThread(threadId);
Expand Down Expand Up @@ -323,6 +334,7 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {
class ChatViewState with _$ChatViewState {
const factory ChatViewState({
@Default(false) bool loading,
@Default(false) bool creating,
@Default(false) bool loadingMessages,
@Default(false) bool messageSending,
@Default(false) bool allowSend,
Expand Down
66 changes: 45 additions & 21 deletions app/lib/ui/flow/message/chat/chat_view_model.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final _privateConstructorUsedError = UnsupportedError(
/// @nodoc
mixin _$ChatViewState {
bool get loading => throw _privateConstructorUsedError;
bool get creating => throw _privateConstructorUsedError;
bool get loadingMessages => throw _privateConstructorUsedError;
bool get messageSending => throw _privateConstructorUsedError;
bool get allowSend => throw _privateConstructorUsedError;
Expand Down Expand Up @@ -48,6 +49,7 @@ abstract class $ChatViewStateCopyWith<$Res> {
@useResult
$Res call(
{bool loading,
bool creating,
bool loadingMessages,
bool messageSending,
bool allowSend,
Expand Down Expand Up @@ -84,6 +86,7 @@ class _$ChatViewStateCopyWithImpl<$Res, $Val extends ChatViewState>
@override
$Res call({
Object? loading = null,
Object? creating = null,
Object? loadingMessages = null,
Object? messageSending = null,
Object? allowSend = null,
Expand All @@ -107,6 +110,10 @@ class _$ChatViewStateCopyWithImpl<$Res, $Val extends ChatViewState>
? _value.loading
: loading // ignore: cast_nullable_to_non_nullable
as bool,
creating: null == creating
? _value.creating
: creating // ignore: cast_nullable_to_non_nullable
as bool,
loadingMessages: null == loadingMessages
? _value.loadingMessages
: loadingMessages // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -210,6 +217,7 @@ abstract class _$$ChatViewStateImplCopyWith<$Res>
@useResult
$Res call(
{bool loading,
bool creating,
bool loadingMessages,
bool messageSending,
bool allowSend,
Expand Down Expand Up @@ -246,6 +254,7 @@ class __$$ChatViewStateImplCopyWithImpl<$Res>
@override
$Res call({
Object? loading = null,
Object? creating = null,
Object? loadingMessages = null,
Object? messageSending = null,
Object? allowSend = null,
Expand All @@ -269,6 +278,10 @@ class __$$ChatViewStateImplCopyWithImpl<$Res>
? _value.loading
: loading // ignore: cast_nullable_to_non_nullable
as bool,
creating: null == creating
? _value.creating
: creating // ignore: cast_nullable_to_non_nullable
as bool,
loadingMessages: null == loadingMessages
? _value.loadingMessages
: loadingMessages // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -343,6 +356,7 @@ class __$$ChatViewStateImplCopyWithImpl<$Res>
class _$ChatViewStateImpl implements _ChatViewState {
const _$ChatViewStateImpl(
{this.loading = false,
this.creating = false,
this.loadingMessages = false,
this.messageSending = false,
this.allowSend = false,
Expand Down Expand Up @@ -371,6 +385,9 @@ class _$ChatViewStateImpl implements _ChatViewState {
final bool loading;
@override
@JsonKey()
final bool creating;
@override
@JsonKey()
final bool loadingMessages;
@override
@JsonKey()
Expand Down Expand Up @@ -447,7 +464,7 @@ class _$ChatViewStateImpl implements _ChatViewState {

@override
String toString() {
return 'ChatViewState(loading: $loading, loadingMessages: $loadingMessages, messageSending: $messageSending, allowSend: $allowSend, showMemberSelectionView: $showMemberSelectionView, isNewThread: $isNewThread, users: $users, threadId: $threadId, title: $title, message: $message, messages: $messages, sender: $sender, selectedMember: $selectedMember, error: $error, spaceInfo: $spaceInfo, threadInfo: $threadInfo, currentUserId: $currentUserId, threadList: $threadList)';
return 'ChatViewState(loading: $loading, creating: $creating, loadingMessages: $loadingMessages, messageSending: $messageSending, allowSend: $allowSend, showMemberSelectionView: $showMemberSelectionView, isNewThread: $isNewThread, users: $users, threadId: $threadId, title: $title, message: $message, messages: $messages, sender: $sender, selectedMember: $selectedMember, error: $error, spaceInfo: $spaceInfo, threadInfo: $threadInfo, currentUserId: $currentUserId, threadList: $threadList)';
}

@override
Expand All @@ -456,6 +473,8 @@ class _$ChatViewStateImpl implements _ChatViewState {
(other.runtimeType == runtimeType &&
other is _$ChatViewStateImpl &&
(identical(other.loading, loading) || other.loading == loading) &&
(identical(other.creating, creating) ||
other.creating == creating) &&
(identical(other.loadingMessages, loadingMessages) ||
other.loadingMessages == loadingMessages) &&
(identical(other.messageSending, messageSending) ||
Expand Down Expand Up @@ -488,26 +507,28 @@ class _$ChatViewStateImpl implements _ChatViewState {
}

@override
int get hashCode => Object.hash(
runtimeType,
loading,
loadingMessages,
messageSending,
allowSend,
showMemberSelectionView,
isNewThread,
const DeepCollectionEquality().hash(_users),
threadId,
title,
message,
const DeepCollectionEquality().hash(_messages),
const DeepCollectionEquality().hash(_sender),
const DeepCollectionEquality().hash(_selectedMember),
const DeepCollectionEquality().hash(error),
spaceInfo,
threadInfo,
currentUserId,
const DeepCollectionEquality().hash(_threadList));
int get hashCode => Object.hashAll([
runtimeType,
loading,
creating,
loadingMessages,
messageSending,
allowSend,
showMemberSelectionView,
isNewThread,
const DeepCollectionEquality().hash(_users),
threadId,
title,
message,
const DeepCollectionEquality().hash(_messages),
const DeepCollectionEquality().hash(_sender),
const DeepCollectionEquality().hash(_selectedMember),
const DeepCollectionEquality().hash(error),
spaceInfo,
threadInfo,
currentUserId,
const DeepCollectionEquality().hash(_threadList)
]);

@JsonKey(ignore: true)
@override
Expand All @@ -519,6 +540,7 @@ class _$ChatViewStateImpl implements _ChatViewState {
abstract class _ChatViewState implements ChatViewState {
const factory _ChatViewState(
{final bool loading,
final bool creating,
final bool loadingMessages,
final bool messageSending,
final bool allowSend,
Expand All @@ -540,6 +562,8 @@ abstract class _ChatViewState implements ChatViewState {
@override
bool get loading;
@override
bool get creating;
@override
bool get loadingMessages;
@override
bool get messageSending;
Expand Down
6 changes: 1 addition & 5 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,10 +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 Down
Loading

0 comments on commit 949b7f2

Please sign in to comment.