Skip to content

Commit

Permalink
fix: catch api user, auth, meeting exception
Browse files Browse the repository at this point in the history
  • Loading branch information
chopper985 committed Dec 16, 2024
1 parent 891226f commit 54e3316
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 92 deletions.
2 changes: 1 addition & 1 deletion lib/features/archived/presentation/bloc/archived_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:injectable/injectable.dart';
import 'package:waterbus_sdk/flutter_waterbus_sdk.dart';
import 'package:waterbus_sdk/types/result.dart';

import 'package:waterbus/features/app/bloc/bloc.dart';
import 'package:waterbus/features/conversation/bloc/message_bloc.dart';
import 'package:waterbus_sdk/types/result.dart';

part 'archived_event.dart';
part 'archived_state.dart';
Expand Down
17 changes: 9 additions & 8 deletions lib/features/auth/presentation/bloc/auth_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:injectable/injectable.dart';
import 'package:waterbus_sdk/flutter_waterbus_sdk.dart';
import 'package:waterbus_sdk/types/result.dart';

import 'package:waterbus/core/navigator/app_navigator.dart';
import 'package:waterbus/core/navigator/app_routes.dart';
Expand All @@ -26,14 +27,14 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {

AuthBloc(this._userLocal) : super(AuthInitial()) {
_auth.initialize((payload) async {
final User? user = await WaterbusSdk.instance.createToken(payload);
final Result<User> user = await WaterbusSdk.instance.createToken(payload);

// Pop loading
AppNavigator.pop();

if (user != null) {
_userLocal.saveUser(user);
_user = user;
if (user.value != null) {
_userLocal.saveUser(user.value!);
_user = user.value;
}

add(OnAuthCheckEvent());
Expand Down Expand Up @@ -109,14 +110,14 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
AppNavigator.pop();
return;
}
final User? user = await WaterbusSdk.instance.createToken(payload);
final Result<User> user = await WaterbusSdk.instance.createToken(payload);

// Pop loading
AppNavigator.pop();

if (user != null) {
_userLocal.saveUser(user);
_user = user;
if (user.isSuccess) {
_userLocal.saveUser(user.value!);
_user = user.value;
}
}

Expand Down
25 changes: 16 additions & 9 deletions lib/features/chats/presentation/bloc/chat_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,24 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
if (event is UpdateAvatarConversationEvent) {
displayLoadingLayer();

final String? presignedUrl = await WaterbusSdk().getPresignedUrl();
final Result<String> presignedUrl =
await WaterbusSdk().getPresignedUrl();

if (presignedUrl != null) {
final String? uploadAvatar = await WaterbusSdk().uploadAvatar(
uploadUrl: presignedUrl,
if (presignedUrl.isSuccess) {
final Result<String> uploadAvatar = await WaterbusSdk().uploadAvatar(
uploadUrl: presignedUrl.value ?? "",
image: event.avatar,
);

if (uploadAvatar != null) {
await _handleUpdateConversation(avatar: uploadAvatar);
if (uploadAvatar.isSuccess) {
await _handleUpdateConversation(avatar: uploadAvatar.value);

emit(_getDoneChat);
} else {
showSnackBarWaterbus(content: Strings.uploadImageFail.i18n);
}
} else {
showSnackBarWaterbus(content: Strings.uploadImageFail.i18n);
}

AppNavigator.pop();
Expand Down Expand Up @@ -300,13 +303,17 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
Future<Meeting?> _createConversation(
CreateConversationEvent event,
) async {
final Meeting? meeting = await _waterbusSdk.createRoom(
final Result<Meeting> result = await _waterbusSdk.createRoom(
meeting: Meeting(title: event.title),
password: event.password,
userId: AppBloc.userBloc.user?.id,
);

return meeting;
if (result.isSuccess) {
return result.value;
} else {
// Toast failure
return null;
}
}

void _listenConversationSocket(ConversationSocketEvent socketEvent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:injectable/injectable.dart';
import 'package:waterbus_sdk/flutter_waterbus_sdk.dart';
import 'package:waterbus_sdk/types/result.dart';

import 'package:waterbus/core/app/lang/data/localization.dart';
import 'package:waterbus/core/navigator/app_navigator.dart';
import 'package:waterbus/core/utils/modal/show_snackbar.dart';
import 'package:waterbus/features/app/bloc/bloc.dart';
import 'package:waterbus/features/chats/presentation/bloc/chat_bloc.dart';
import 'package:waterbus_sdk/types/result.dart';

part 'invited_chat_event.dart';
part 'invited_chat_state.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/features/conversation/bloc/message_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import 'package:injectable/injectable.dart';
import 'package:waterbus_sdk/flutter_waterbus_sdk.dart';
import 'package:waterbus_sdk/types/models/message_status_enum.dart';
import 'package:waterbus_sdk/types/models/sending_status_enum.dart';
import 'package:waterbus_sdk/types/result.dart';

import 'package:waterbus/core/constants/constants.dart';
import 'package:waterbus/features/app/bloc/bloc.dart';
import 'package:waterbus/features/chats/presentation/bloc/chat_bloc.dart';
import 'package:waterbus_sdk/types/result.dart';

part 'message_event.dart';
part 'message_state.dart';
Expand Down
78 changes: 48 additions & 30 deletions lib/features/meeting/presentation/bloc/meeting/meeting_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:injectable/injectable.dart';
import 'package:simple_pip_mode/simple_pip.dart';
import 'package:sizer/sizer.dart';
import 'package:waterbus_sdk/flutter_waterbus_sdk.dart';
import 'package:waterbus_sdk/types/result.dart';
import 'package:waterbus_sdk/utils/extensions/duration_extensions.dart';

import 'package:waterbus/core/method_channels/pip_channel.dart';
Expand Down Expand Up @@ -262,10 +263,12 @@ class MeetingBloc extends Bloc<MeetingEvent, MeetingState> {
if (event is StartRecordEvent) {
final recordId = await _waterbusSdk.startRecord();

if (recordId != null) {
_recordId = recordId;
if (recordId.value != null) {
_recordId = recordId.value;

emit(_joinedMeeting);
} else {
// Toast failure
}
}

Expand Down Expand Up @@ -304,78 +307,93 @@ class MeetingBloc extends Bloc<MeetingEvent, MeetingState> {

// MARK: Private
Future<void> _handleCreateMeeting(CreateMeetingEvent event) async {
final Meeting? meeting = await _waterbusSdk.createRoom(
final Result<Meeting> result = await _waterbusSdk.createRoom(
meeting: Meeting(title: event.roomName),
password: event.password,
userId: AppBloc.userBloc.user?.id,
);

AppNavigator.popUntil(Routes.rootRoute);

if (meeting == null) return;

_localDataSource.insertOrUpdate(meeting);
AppBloc.recentJoinedBloc.add(InsertRecentJoinedEvent(meeting: meeting));
if (result.value != null) {
final Meeting meeting = result.value!;
_localDataSource.insertOrUpdate(meeting);
AppBloc.recentJoinedBloc.add(InsertRecentJoinedEvent(meeting: meeting));
} else {
// Toast failure
}
}

Future<bool> _handleJoinRoom(
JoinMeetingWithPasswordEvent event,
) async {
final Meeting? meeting = await _waterbusSdk.joinRoom(
final Result<Meeting> result = await _waterbusSdk.joinRoom(
meeting: _currentMeeting!,
password: event.password,
userId: AppBloc.userBloc.user?.id,
);

if (meeting == null) return false;
if (result.value != null) {
final Meeting meeting = result.value!;
_localDataSource.insertOrUpdate(meeting);

_localDataSource.insertOrUpdate(meeting);
_currentMeeting = meeting;

_currentMeeting = meeting;
AppBloc.recentJoinedBloc.add(
InsertRecentJoinedEvent(meeting: meeting),
);

AppBloc.recentJoinedBloc.add(
InsertRecentJoinedEvent(meeting: meeting),
);
final int indexOfMyParticipant = meeting.participants.lastIndexWhere(
(participant) => participant.isMe,
);

final int indexOfMyParticipant = meeting.participants.lastIndexWhere(
(participant) => participant.isMe,
);
if (indexOfMyParticipant != -1) {
_mParticipant = meeting.participants[indexOfMyParticipant];
}

if (indexOfMyParticipant != -1) {
_mParticipant = meeting.participants[indexOfMyParticipant];
return true;
} else {
// Toast failure
return false;
}

return true;
}

Future<Meeting?> _handleGetInfoMeeting(GetInfoMeetingEvent event) async {
final Meeting? meeting =
final Result<Meeting> result =
await _waterbusSdk.getRoomInfo(code: event.roomCode);

AppNavigator.pop();

return meeting;
if (result.isSuccess) {
return result.value;
} else {
// Toast failure
return null;
}
}

Future<void> _handleUpdateMeeting(UpdateMeetingEvent event) async {
if (_currentMeeting == null) return;

final Meeting? meeting = await _waterbusSdk.updateRoom(
final Result<bool> result = await _waterbusSdk.updateRoom(
meeting: _currentMeeting!.copyWith(title: event.roomName),
password: event.password,
userId: AppBloc.userBloc.user?.id,
);

AppNavigator.pop();

if (meeting == null) return;
if (result.isSuccess) {
final Meeting meeting = _currentMeeting!.copyWith(title: event.roomName);
_localDataSource.insertOrUpdate(meeting);

_localDataSource.insertOrUpdate(meeting);

AppNavigator.pop();
AppBloc.recentJoinedBloc.add(InsertRecentJoinedEvent(meeting: meeting));
AppNavigator.pop();
AppBloc.recentJoinedBloc.add(InsertRecentJoinedEvent(meeting: meeting));

_currentMeeting = meeting;
_currentMeeting = meeting;
} else {
// Toast failure
}
}

Future<void> _handleLeaveMeeting(LeaveMeetingEvent event) async {
Expand Down
Loading

0 comments on commit 54e3316

Please sign in to comment.