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

Commit

Permalink
OT-487: Verify event handling is working (#240)
Browse files Browse the repository at this point in the history
* OT-487: Verify event handling is working
  • Loading branch information
phranck authored and Boehrsi committed Oct 30, 2019
1 parent 691f95d commit 60d3e88
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 74 deletions.
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ SPEC CHECKSUMS:
app_settings: 27651d311385d34d91afa163c0e4539cfd0c4c5d
background_fetch: 385552e57d3bde2c7a3abd6b1edc76b87823b6c4
contacts_service: d212985196c101372bb55c42f41dae7be59ab811
delta_chat_core: 4fe0634febea3fe0054e69f480b449346788d7d0
delta_chat_core: b783a6cae2cb336834c873e98b7c8c31e2dcd847
device_info: cbf09d2ec12aa7110e0b09fabe54b5bd6c8efe74
file_picker: 408623be2125b79a4539cf703be3d4b3abe5e245
Firebase: bc9cfc7a96c73268656d5aaab453ff1b4b530e0e
Expand Down
1 change: 0 additions & 1 deletion lib/src/antiMobbing/anti_mobbing_list_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class AntiMobbingListBloc extends Bloc<AntiMobbingListEvent, AntiMobbingListStat
@override
void close() {
_messageListRepository?.removeListener(_repositoryStreamHandler);
_repositoryStreamHandler?.tearDown();
super.close();
}

Expand Down
4 changes: 3 additions & 1 deletion lib/src/chat/chat_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
@override
void close() {
_chatRepository.removeListener(_repositoryStreamHandler);
_repositoryStreamHandler?.tearDown();
super.close();
}

Expand Down Expand Up @@ -156,6 +155,9 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
_chatRepository.putIfAbsent(id: _chatId);
chat = _chatRepository.get(_chatId);
}
if (chat == null) {
return;
}
_isGroup = await chat.isGroup();
String name = await chat.getName();
int colorValue = await chat.getColor();
Expand Down
1 change: 0 additions & 1 deletion lib/src/chatlist/chat_list_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class ChatListBloc extends Bloc<ChatListEvent, ChatListState> {
@override
void close() {
_chatRepository.removeListener(_repositoryStreamHandler);
_repositoryStreamHandler?.tearDown();
_messageListBloc.close();
super.close();
}
Expand Down
1 change: 0 additions & 1 deletion lib/src/contact/contact_list_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ class ContactListBloc extends Bloc<ContactListEvent, ContactListState> with Cont
@override
void close() {
_contactRepository.removeListener(_repositoryStreamHandler);
_repositoryStreamHandler?.tearDown();
super.close();
}

Expand Down
17 changes: 6 additions & 11 deletions lib/src/data/repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,21 @@ abstract class Repository<T extends Base> {
}

Future _setupCoreListener(BaseRepositoryEventStreamHandler streamHandler, int eventId) async {
int listenerId = await _core.listen(eventId, streamHandler.streamController);
if (streamHandler is RepositoryEventStreamHandler) {
streamHandler.listenerId = listenerId;
} else if (streamHandler is RepositoryMultiEventStreamHandler) {
streamHandler.listenerIds.add(listenerId);
}
await _core.listen(eventId, streamHandler.streamController);
}

void removeListener(BaseRepositoryEventStreamHandler streamHandler) {
if (streamHandler is RepositoryEventStreamHandler) {
tearDownCoreListener(streamHandler.eventId, streamHandler.listenerId);
tearDownCoreListener(streamHandler.eventId, streamHandler);
} else if (streamHandler is RepositoryMultiEventStreamHandler) {
for (int index = 0; index < streamHandler.listenerIds.length; index++) {
tearDownCoreListener(streamHandler.eventIds[index], streamHandler.listenerIds[index]);
for (int index = 0; index < streamHandler.eventIds.length; index++) {
tearDownCoreListener(streamHandler.eventIds[index], streamHandler);
}
}
}

void tearDownCoreListener(int eventId, int listenerId) {
_core.removeListener(eventId, listenerId);
void tearDownCoreListener(int eventId, BaseRepositoryEventStreamHandler streamHandler) {
_core.removeListener(eventId, streamHandler.streamController);
}

void transferTo(Repository<T> repository, int id) {
Expand Down
11 changes: 0 additions & 11 deletions lib/src/data/repository_stream_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,12 @@ abstract class BaseRepositoryEventStreamHandler {
class RepositoryEventStreamHandler extends BaseRepositoryEventStreamHandler {
final int eventId;

int listenerId;

RepositoryEventStreamHandler(Type type, this.eventId, onData, [onError]) : super(type, onData, onError);

void tearDown() {
listenerId = null;
}
}

class RepositoryMultiEventStreamHandler extends BaseRepositoryEventStreamHandler {
final List<int> eventIds;

List<int> listenerIds = List();

RepositoryMultiEventStreamHandler(Type type, this.eventIds, onData, [onError]) : super(type, onData, onError);

void tearDown() {
listenerIds.clear();
}
}
17 changes: 8 additions & 9 deletions lib/src/login/login_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ import 'login_provider_list.dart';
class LoginBloc extends Bloc<LoginEvent, LoginState> {
DeltaChatCore _core = DeltaChatCore();
Context _context = Context();
int _loginListenerId;
int _errorListenerId;
bool _listenersRegistered = false;
PublishSubject<Event> _loginSubject = new PublishSubject();

// ignore: close_sinks
Expand Down Expand Up @@ -151,18 +150,18 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
}

void _registerListeners() async {
if (_loginListenerId == null) {
if (!_listenersRegistered) {
_listenersRegistered = true;
_loginSubject.listen(_successCallback, onError: _errorCallback);
_loginListenerId = await _core.listen(Event.configureProgress, _loginSubject);
_errorListenerId = await _core.listen(Event.error, _errorSubject);
await _core.listen(Event.configureProgress, _loginSubject);
await _core.listen(Event.error, _errorSubject);
}
}

void _unregisterListeners() {
_core.removeListener(Event.configureProgress, _loginListenerId);
_core.removeListener(Event.error, _errorListenerId);
_loginListenerId = null;
_errorListenerId = null;
_core.removeListener(Event.configureProgress, _loginSubject);
_core.removeListener(Event.error, _errorSubject);
_listenersRegistered = false;
}

bool _loginSuccess(int progress) {
Expand Down
1 change: 0 additions & 1 deletion lib/src/message/message_list_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ class MessageListBloc extends Bloc<MessageListEvent, MessageListState> with Invi
@override
void close() {
_messageListRepository?.removeListener(_repositoryStreamHandler);
_repositoryStreamHandler?.tearDown();
super.close();
}

Expand Down
11 changes: 6 additions & 5 deletions lib/src/notifications/local_push_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,24 @@ class LocalPushManager {
DeltaChatCore _core = DeltaChatCore();
Context _context = Context();
NotificationManager _notificationManager;
int _listenerId;
bool _listenersRegistered = false;

factory LocalPushManager() => _instance ??= LocalPushManager._internal();

LocalPushManager._internal();

Future<void> setup() async {
if (_listenerId == null) {
if (!_listenersRegistered) {
_listenersRegistered = true;
_notificationManager = NotificationManager();
_messageSubject.listen(_successCallback);
_listenerId = await _core.listen(Event.incomingMsg, _messageSubject);
await _core.listen(Event.incomingMsg, _messageSubject);
}
}

Future<void> tearDown() async {
await _core.removeListener(Event.incomingMsg, _listenerId);
_listenerId = null;
_core.removeListener(Event.incomingMsg, _messageSubject);
_listenersRegistered = false;
}

void _successCallback(Event event) {
Expand Down
23 changes: 10 additions & 13 deletions lib/src/push/push_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ class PushBloc extends Bloc<PushEvent, PushState> {
var pushService = PushService();
var _core = DeltaChatCore();
var _context = Context();
int _metadataListenerId;
int _webPushSubscriptionListenerId;
int _errorListenerId;
bool _listenersRegistered = false;
static const _subscribeListenerId = 1001;
static const _validateListenerId = 1002;

Expand Down Expand Up @@ -274,21 +272,20 @@ class PushBloc extends Bloc<PushEvent, PushState> {
}

void _registerListeners() async {
if (_metadataListenerId == null) {
if (!_listenersRegistered) {
_listenersRegistered = true;
pushSubject.listen(_metadataSuccessCallback, onError: _errorCallback);
_metadataListenerId = await _core.listen(Event.setMetaDataDone, pushSubject);
_webPushSubscriptionListenerId = await _core.listen(Event.webPushSubscription, pushSubject);
_errorListenerId = await _core.listen(Event.error, pushSubject);
await _core.listen(Event.setMetaDataDone, pushSubject);
await _core.listen(Event.webPushSubscription, pushSubject);
await _core.listen(Event.error, pushSubject);
}
}

void _unregisterListeners() {
_core.removeListener(Event.setMetaDataDone, _metadataListenerId);
_core.removeListener(Event.webPushSubscription, _webPushSubscriptionListenerId);
_core.removeListener(Event.error, _errorListenerId);
_metadataListenerId = null;
_webPushSubscriptionListenerId = null;
_errorListenerId = null;
_core.removeListener(Event.setMetaDataDone, pushSubject);
_core.removeListener(Event.webPushSubscription, pushSubject);
_core.removeListener(Event.error, pushSubject);
_listenersRegistered = false;
}

void _metadataSuccessCallback(Event event) {
Expand Down
23 changes: 10 additions & 13 deletions lib/src/qr/qr_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ import 'package:rxdart/rxdart.dart';

class QrBloc extends Bloc<QrEvent, QrState> {
DeltaChatCore _core = DeltaChatCore();
int _qrInviterListenerId;
int _qrJoinerListenerId;
int _errorSubjectListenerId;
bool _listenersRegistered = false;

PublishSubject<Event> _qrSubject = new PublishSubject();

Expand Down Expand Up @@ -126,21 +124,20 @@ class QrBloc extends Bloc<QrEvent, QrState> {
}

void _registerListeners() async {
if (_qrInviterListenerId == null || _qrJoinerListenerId == null) {
if (!_listenersRegistered) {
_qrSubject.listen(_successCallback, onError: _errorCallback);
_qrInviterListenerId = await _core.listen(Event.secureJoinInviterProgress, _qrSubject);
_qrJoinerListenerId = await _core.listen(Event.secureJoinJoinerProgress, _qrSubject);
_errorSubjectListenerId = await _core.listen(Event.error, _errorSubject);
await _core.listen(Event.secureJoinInviterProgress, _qrSubject);
await _core.listen(Event.secureJoinJoinerProgress, _qrSubject);
await _core.listen(Event.error, _errorSubject);
_listenersRegistered = true;
}
}

void _unregisterListeners() {
_core.removeListener(Event.secureJoinInviterProgress, _qrInviterListenerId);
_core.removeListener(Event.secureJoinJoinerProgress, _qrJoinerListenerId);
_core.removeListener(Event.error, _errorSubjectListenerId);
_qrInviterListenerId = null;
_qrJoinerListenerId = null;
_errorSubjectListenerId = null;
_core.removeListener(Event.secureJoinInviterProgress, _qrSubject);
_core.removeListener(Event.secureJoinJoinerProgress, _qrSubject);
_core.removeListener(Event.error, _errorSubject);
_listenersRegistered = false;
}

void getQrText(int chatId) async {
Expand Down
11 changes: 6 additions & 5 deletions lib/src/settings/settings_security_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ enum SettingsSecurityType {
class SettingsSecurityBloc extends Bloc<SettingsSecurityEvent, SettingsSecurityState> {
PublishSubject<Event> _keyActionSubject = new PublishSubject();
DeltaChatCore _core = DeltaChatCore();
int _listenerId;
bool _listenersRegistered = false;

@override
SettingsSecurityState get initialState => SettingsSecurityStateInitial();
Expand Down Expand Up @@ -109,7 +109,8 @@ class SettingsSecurityBloc extends Bloc<SettingsSecurityEvent, SettingsSecurityS
}

void _exportImportKeys(SettingsSecurityType type) async {
if (_listenerId == null) {
if (!_listenersRegistered) {
_listenersRegistered = true;
await _registerListeners();
}
var context = Context();
Expand All @@ -129,12 +130,12 @@ class SettingsSecurityBloc extends Bloc<SettingsSecurityEvent, SettingsSecurityS

Future<void> _registerListeners() async {
_keyActionSubject.listen(_successCallback, onError: _errorCallback);
_listenerId = await _core.listen(Event.imexProgress, _keyActionSubject);
await _core.listen(Event.imexProgress, _keyActionSubject);
}

void _unregisterListeners() {
_core.removeListener(Event.imexProgress, _listenerId);
_listenerId = null;
_core.removeListener(Event.imexProgress, _keyActionSubject);
_listenersRegistered = false;
}

_successCallback(Event event) {
Expand Down
8 changes: 7 additions & 1 deletion lib/src/widgets/fullscreen_progress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ class FullscreenProgress<T extends Bloc> extends StatelessWidget {
final bool showCancelButton;
final Function cancelPressed;

FullscreenProgress({@required this.bloc, @required this.text, this.showProgressValues, this.showCancelButton, this.cancelPressed});
FullscreenProgress({
@required this.bloc,
@required this.text,
this.showProgressValues = false,
this.showCancelButton = false,
this.cancelPressed,
});

@override
Widget build(BuildContext context) {
Expand Down

0 comments on commit 60d3e88

Please sign in to comment.