Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Remove unneeded events / states from BloCs #520 #610

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void main() {
final pushBloc = PushBloc();

WidgetsFlutterBinding.ensureInitialized(); // Required to allow plugin calls prior runApp() (performed by LogManager.setup())
_logManager.setup(logToFile: true, logLevel: Level.INFO).then((value) => runApp(
_logManager.setupAsync(logToFile: true, logLevel: Level.INFO).then((value) => runApp(
MultiBlocProvider(
providers: [
BlocProvider<LifecycleBloc>(
Expand Down
6 changes: 3 additions & 3 deletions lib/src/anti_mobbing/anti_mobbing_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ class _AntiMobbingListState extends State<AntiMobbingList> {
),
);
}
} else if (state is! MessagesLoaded) {
return StateInfo(showLoading: true);
} else {
} else if (state is AntiMobbingListStateFailure) {
return AdaptiveIcon(
icon: IconSource.error,
);
} else {
return StateInfo(showLoading: true);
}
},
),
Expand Down
25 changes: 11 additions & 14 deletions lib/src/anti_mobbing/anti_mobbing_list_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,14 @@ class AntiMobbingListBloc extends Bloc<AntiMobbingListEvent, AntiMobbingListStat
if (event is RequestMessages) {
try {
_messageListRepository = RepositoryManager.get(RepositoryType.chatMessage, Chat.typeInvite);
_registerListeners();
loadMessages();
await _registerListenersAsync();
yield* _loadMessagesAsync();
} catch (error) {
yield AntiMobbingListStateFailure();
}
} else if (event is MessagesLoaded) {
yield AntiMobbingListStateSuccess(
messageIds: event.messageIds, messageLastUpdateValues: event.messageLastUpdateValues, dateMarkerIds: event.dateMarkerIds);
} else if (event is UpdateMessages) {
_messageListRepository = RepositoryManager.get(RepositoryType.chatMessage, Chat.typeInvite);
loadMessages();
yield* _loadMessagesAsync();
}
}

Expand All @@ -84,29 +81,28 @@ class AntiMobbingListBloc extends Bloc<AntiMobbingListEvent, AntiMobbingListStat
return super.close();
}

void _registerListeners() async {
Future<void> _registerListenersAsync() async {
if (!_listenersRegistered) {
_listenersRegistered = true;
_repositoryStreamHandler = RepositoryMultiEventStreamHandler(
Type.publish,
[Event.incomingMsg, Event.msgsChanged, Event.msgDelivered, Event.msgRead],
_onMessagesChanged,
);
_messageListRepository.addListener(_repositoryStreamHandler);
await _messageListRepository.addListenerAsync(_repositoryStreamHandler);
}
}

void _unregisterListeners() {
if (_listenersRegistered) {
_listenersRegistered = false;
_messageListRepository?.removeListener(_repositoryStreamHandler);

}
}

void _onMessagesChanged(event) => add(UpdateMessages());

void loadMessages() async {
Stream<AntiMobbingListState> _loadMessagesAsync() async* {
List<int> dateMakerIds = List();
Context context = Context();
List<int> messageIds = List.from(await context.getChatMessagesAsync(Chat.typeInvite, Context.chatListAddDayMarker));
Expand All @@ -130,9 +126,10 @@ class AntiMobbingListBloc extends Bloc<AntiMobbingListEvent, AntiMobbingListStat
}
});

add(MessagesLoaded(
messageIds: uniqueInviteMap.values.toList(growable: false),
messageLastUpdateValues: lastUpdateValues.toList(growable: false),
dateMarkerIds: dateMakerIds));
yield AntiMobbingListStateSuccess(
messageIds: uniqueInviteMap.values.toList(growable: false),
messageLastUpdateValues: lastUpdateValues.toList(growable: false),
dateMarkerIds: dateMakerIds,
);
}
}
8 changes: 0 additions & 8 deletions lib/src/anti_mobbing/anti_mobbing_list_event_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ abstract class AntiMobbingListEvent {}

class RequestMessages extends AntiMobbingListEvent {}

class MessagesLoaded extends AntiMobbingListEvent {
final List<int> messageIds;
final List<int> messageLastUpdateValues;
final List<int> dateMarkerIds;

MessagesLoaded({@required this.messageIds, @required this.messageLastUpdateValues, this.dateMarkerIds});
}

class UpdateMessages extends AntiMobbingListEvent {}

abstract class AntiMobbingListState {}
Expand Down
12 changes: 6 additions & 6 deletions lib/src/background_refresh/background_refresh_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ import 'package:ox_coi/src/utils/constants.dart';

const loggerName = "background_refresh_manager";

void backgroundHeadlessTask(String taskId) async {
Future<void> backgroundHeadlessTaskAsync(String taskId) async {
final logManager = LogManager();
await logManager.setup(logToFile: true, logLevel: Level.INFO);
await logManager.setupAsync(logToFile: true, logLevel: Level.INFO);
final logger = Logger(loggerName);
logger.info("Callback (background) triggered");
var core = DeltaChatCore();
var isSetup = await core.setupAsync(dbName: dbName, minimalSetup: true);
if (isSetup) {
logger.info("Callback (background) checking for new messages");
await getMessages();
await getMessagesAsync();
await core.tearDownAsync();
}
logger.info("Callback (background) finishing");
BackgroundFetch.finish(taskId);
}

Future<void> getMessages() async {
Future<void> getMessagesAsync() async {
final localNotificationManager = LocalNotificationManager.newInstance();
localNotificationManager.setup(registerListeners: false);
final context = Context();
Expand All @@ -85,7 +85,7 @@ class BackgroundRefreshManager {
BackgroundRefreshManager._internal();

setupAndStart() {
BackgroundFetch.registerHeadlessTask(backgroundHeadlessTask).then((value) {
BackgroundFetch.registerHeadlessTask(backgroundHeadlessTaskAsync).then((value) {
_logger.info("Register headless task");
});
BackgroundFetch.configure(
Expand All @@ -106,7 +106,7 @@ class BackgroundRefreshManager {
});
}

void start() async {
Future<void> startAsync() async {
if (_running) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/brandable/custom_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ class CustomThemeState extends State<CustomTheme> with WidgetsBindingObserver {
}

Future<void> _checkSavedTheme() async {
var savedThemeKeyString = await getPreference(preferenceApplicationTheme);
var savedThemeKeyString = await getPreferenceAsync(preferenceApplicationTheme);

ThemeKey savedThemeKey;
if (savedThemeKeyString == null) {
savedThemeKey = ThemeKey.system;
savedThemeKeyString = savedThemeKey.stringValue;
await setPreference(preferenceApplicationTheme, savedThemeKeyString);
await setPreferenceAsync(preferenceApplicationTheme, savedThemeKeyString);
}

ThemeKey newThemeKey;
Expand All @@ -215,7 +215,7 @@ class CustomThemeState extends State<CustomTheme> with WidgetsBindingObserver {
_theme = CustomerThemes.getThemeFromKey(themeKey);

if (!preservePreference) {
setPreference(preferenceApplicationTheme, themeKey.stringValue);
setPreferenceAsync(preferenceApplicationTheme, themeKey.stringValue);
}

SystemUiOverlayStyle overlayStyle;
Expand Down
12 changes: 6 additions & 6 deletions lib/src/chat/chat_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
_chatId = event.chatId;
await _registerListeners();
if (_chatId == Chat.typeInvite) {
yield* _setupInviteChat(event.messageId, event.isHeadless);
yield* _setupInviteChatAsync(event.messageId, event.isHeadless);
} else {
yield* _setupChat(event.isHeadless);
yield* _setupChatAsync(event.isHeadless);
}
} catch (error, stackTrace) {
yield ChatStateFailure(error: error, stackTrace: stackTrace);
}
} else if (event is UpdateChat) {
yield* _setupChat(false);
yield* _setupChatAsync(false);
} else if (event is ClearNotifications) {
_removeNotifications();
}
Expand All @@ -105,7 +105,7 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
],
_onCoreEvent,
);
_chatRepository.addListener(_repositoryStreamHandler);
await _chatRepository.addListenerAsync(_repositoryStreamHandler);
}
}

Expand All @@ -123,7 +123,7 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
}
}

Stream<ChatState> _setupInviteChat(int messageId, bool isHeadless) async* {
Stream<ChatState> _setupInviteChatAsync(int messageId, bool isHeadless) async* {
final messageListRepository = RepositoryManager.get(RepositoryType.chatMessage, Chat.typeInvite);
if (isHeadless) {
messageListRepository.putIfAbsent(id: messageId);
Expand Down Expand Up @@ -152,7 +152,7 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
);
}

Stream<ChatState> _setupChat(bool isHeadless) async* {
Stream<ChatState> _setupChatAsync(bool isHeadless) async* {
final context = Context();
if (isHeadless) {
_chatRepository.putIfAbsent(id: _chatId);
Expand Down
54 changes: 26 additions & 28 deletions lib/src/chat/chat_change_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ class ChatChangeBloc extends Bloc<ChatChangeEvent, ChatChangeState> {
@override
Stream<ChatChangeState> mapEventToState(ChatChangeEvent event) async* {
if (event is RequestChatData) {
yield* _requestChatData(event.chatId);
yield* _requestChatDataAsync(event.chatId);
}
else if (event is CreateChat) {
yield CreateChatStateLoading();
try {
_messageListRepository = RepositoryManager.get(RepositoryType.chatMessage, event.chatId);
_createChat(
yield* _createChatAsync(
contactId: event.contactId,
messageId: event.messageId,
verified: event.verified,
Expand All @@ -75,37 +75,35 @@ class ChatChangeBloc extends Bloc<ChatChangeEvent, ChatChangeState> {
} catch (error) {
yield CreateChatStateFailure(error: error.toString());
}
} else if (event is ChatCreated) {
yield CreateChatStateSuccess(chatId: event.chatId);
} else if (event is DeleteChat) {
_deleteChat(event.chatId);
await _deleteChatAsync(event.chatId);
} else if (event is DeleteChats) {
_deleteChats(event.chatIds);
await _deleteChatsAsync(event.chatIds);
} else if (event is LeaveGroupChat) {
_leaveGroupChat(event.chatId);
await _leaveGroupChatAsync(event.chatId);
} else if (event is ChatMarkNoticed) {
_markNoticedChat(event.chatId);
await _markNoticedChatAsync(event.chatId);
} else if (event is ChatMarkMessagesSeen) {
_markMessagesSeen(event.messageIds);
await _markMessagesSeenAsync(event.messageIds);
} else if (event is ChatAddParticipants) {
_addParticipants(event.chatId, event.contactIds);
await _addParticipantsAsync(event.chatId, event.contactIds);
} else if (event is ChatRemoveParticipant) {
_removeParticipant(event.chatId, event.contactId);
await _removeParticipantAsync(event.chatId, event.contactId);
} else if (event is ChangeChatData) {
yield* _changeChatData(event.chatId, event.chatName, event.avatarPath);
yield* _changeChatDataAsync(event.chatId, event.chatName, event.avatarPath);
} else if (event is SetImagePath) {
_setProfileImage(event.chatId, event.newPath);
await _setProfileImageAsync(event.chatId, event.newPath);
}
}

Stream<ChatChangeState> _requestChatData(int chatId) async* {
Stream<ChatChangeState> _requestChatDataAsync(int chatId) async* {
var chat = _chatRepository.get(chatId);
String chatName = await chat.getNameAsync();
String chatAvatarPath = await chat.getProfileImageAsync();
yield ChatDataLoaded(chatName: chatName, avatarPath: chatAvatarPath);
}

void _createChat({int contactId, int messageId, bool verified, String name, List<int> contacts, String imagePath}) async {
Stream<ChatChangeState> _createChatAsync({int contactId, int messageId, bool verified, String name, List<int> contacts, String imagePath}) async* {
Context context = Context();
var chatId;
if (contactId != null) {
Expand All @@ -121,14 +119,14 @@ class ChatChangeBloc extends Bloc<ChatChangeEvent, ChatChangeState> {
context.addContactToChatAsync(chatId, contacts[i]);
}
if (!imagePath.isNullOrEmpty()) {
_setProfileImage(chatId, imagePath);
_setProfileImageAsync(chatId, imagePath);
}
}
_chatRepository.putIfAbsent(id: chatId);
add(ChatCreated(chatId: chatId));
yield CreateChatStateSuccess(chatId: chatId);
}

void _deleteChat(int chatId) async {
Future<void> _deleteChatAsync(int chatId) async {
Context context = Context();
if(_messageListRepository != null) {
_messageListRepository.clear();
Expand All @@ -137,21 +135,21 @@ class ChatChangeBloc extends Bloc<ChatChangeEvent, ChatChangeState> {
await context.deleteChatAsync(chatId);
}

void _deleteChats(List<int> chatIds) async {
Future<void> _deleteChatsAsync(List<int> chatIds) async {
Context context = Context();
for (int chatId in chatIds) {
_chatRepository.remove(id: chatId);
_leaveGroupChat(chatId);
await _leaveGroupChatAsync(chatId);
await context.deleteChatAsync(chatId);
}
}

void _leaveGroupChat(int chatId) async {
Future<void> _leaveGroupChatAsync(int chatId) async {
Context context = Context();
await context.removeContactFromChatAsync(chatId, Contact.idSelf);
}

void _markNoticedChat(int chatId) async {
Future<void> _markNoticedChatAsync(int chatId) async {
Context context = Context();
await context.markNoticedChatAsync(chatId);
if (!_chatRepository.contains(chatId)) {
Expand All @@ -160,32 +158,32 @@ class ChatChangeBloc extends Bloc<ChatChangeEvent, ChatChangeState> {
_chatRepository.get(chatId).setLastUpdate();
}

void _markMessagesSeen(List<int> messageIds) async {
Future<void> _markMessagesSeenAsync(List<int> messageIds) async {
Context context = Context();
await context.markSeenMessagesAsync(messageIds);
}

void _addParticipants(int chatId, List<int> contactIds) async {
Future<void> _addParticipantsAsync(int chatId, List<int> contactIds) async {
Context context = Context();
for (int i = 0; i < contactIds.length; i++) {
await context.addContactToChatAsync(chatId, contactIds[i]);
}
}

void _removeParticipant(int chatId, int contactId) async {
Future<void> _removeParticipantAsync(int chatId, int contactId) async {
Context context = Context();
await context.removeContactFromChatAsync(chatId, contactId);
}

Stream<ChatChangeState> _changeChatData(int chatId, String chatName, String chatAvatarPath) async* {
Stream<ChatChangeState> _changeChatDataAsync(int chatId, String chatName, String chatAvatarPath) async* {
Context context = Context();
await context.setChatNameAsync(chatId, chatName);
RepositoryManager.get(RepositoryType.chat).get(chatId).set(Chat.methodChatGetName, chatName);
await _setProfileImage(chatId, chatAvatarPath);
await _setProfileImageAsync(chatId, chatAvatarPath);
yield(ChatChangeStateSuccess());
}

Future<void> _setProfileImage(int chatId, String chatAvatarPath) async {
Future<void> _setProfileImageAsync(int chatId, String chatAvatarPath) async {
Context context = Context();
await context.setChatProfileImageAsync(chatId, chatAvatarPath);
RepositoryManager.get(RepositoryType.chat).get(chatId).set(Chat.methodChatGetProfileImage, chatAvatarPath);
Expand Down
6 changes: 0 additions & 6 deletions lib/src/chat/chat_change_event_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ class CreateChat extends ChatChangeEvent {
});
}

class ChatCreated extends ChatChangeEvent {
final int chatId;

ChatCreated({this.chatId});
}

class DeleteChat extends ChatChangeEvent {
final int chatId;

Expand Down
Loading