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

Commit

Permalink
OT-116 Prepare error handling for the login process #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Boehrsi committed Mar 18, 2019
1 parent f22daeb commit 691cbdc
Show file tree
Hide file tree
Showing 18 changed files with 225 additions and 79 deletions.
7 changes: 7 additions & 0 deletions lib/src/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ class _ChatScreenState extends State<ChatScreen> {
_messagesBloc.dispatch(RequestMessages(widget._chatId));
}

@override
void dispose() {
_chatBloc.dispose();
_messagesBloc.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return new Scaffold(
Expand Down
6 changes: 6 additions & 0 deletions lib/src/chat/create_chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class _CreateChatState extends State<CreateChat> {
_contactListBloc.dispatch(RequestContacts());
}

@override
void dispose() {
_contactListBloc.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down
6 changes: 6 additions & 0 deletions lib/src/chatlist/chat_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class _ChatListState extends State<ChatList> {
_chatListBloc.dispatch(RequestChatList());
}

@override
void dispose() {
_chatListBloc.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return BlocBuilder(
Expand Down
20 changes: 11 additions & 9 deletions lib/src/chatlist/chat_list_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ import 'package:ox_talk/src/chatlist/chat_list_event.dart';
import 'package:ox_talk/src/chatlist/chat_list_state.dart';
import 'package:ox_talk/src/data/repository.dart';
import 'package:ox_talk/src/data/repository_manager.dart';
import 'package:ox_talk/src/data/repository_stream_handler.dart';

class ChatListBloc extends Bloc<ChatListEvent, ChatListState> {
final Repository<ChatList> chatListRepository = RepositoryManager.get(RepositoryType.chatList);
final Repository<Chat> chatRepository = RepositoryManager.get(RepositoryType.chat);
StreamSubscription streamSubscription;
RepositoryMultiEventStreamHandler repositoryStreamHandler;

@override
ChatListState get initialState => ChatListStateInitial();
Expand All @@ -76,10 +77,8 @@ class ChatListBloc extends Bloc<ChatListEvent, ChatListState> {

@override
void dispose() {
chatListRepository.removeListener(repositoryStreamHandler);
super.dispose();
chatListRepository.removeListener(hashCode, Event.msgsChanged);
chatListRepository.removeListener(hashCode, Event.contactsChanged);
streamSubscription.cancel();
}

void setupChatList() async {
Expand All @@ -95,10 +94,13 @@ class ChatListBloc extends Bloc<ChatListEvent, ChatListState> {
}

void setupChatListListener() {
if (streamSubscription == null) {
chatListRepository.addListener(hashCode, Event.msgsChanged);
chatListRepository.addListener(hashCode, Event.contactsChanged);
streamSubscription = chatListRepository.observable.listen((event) => dispatch(RequestChatList()));
if (repositoryStreamHandler == null) {
repositoryStreamHandler = RepositoryMultiEventStreamHandler(Type.publish, [Event.msgsChanged, Event.contactsChanged], _requestChatList);
chatListRepository.addListener(repositoryStreamHandler);
}
}
}

_requestChatList() {
dispatch(RequestChatList());
}
}
2 changes: 1 addition & 1 deletion lib/src/chatlist/chat_list_invite_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class _ChatListInviteItemState extends State<ChatListInviteItem> {

@override
void dispose(){
super.dispose();
_messageItemBloc.dispose();
super.dispose();
}
}
2 changes: 0 additions & 2 deletions lib/src/chatlist/chat_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ class ChatListView extends BaseRootChild {
}

class _ChatListViewState extends State<ChatListView> with SingleTickerProviderStateMixin {
ChatListBloc _chatListBloc = ChatListBloc();
TabController controller;

@override
void initState() {
super.initState();
_chatListBloc.dispatch(RequestChatList());
controller = TabController(length: 2, vsync: this);
}

Expand Down
11 changes: 6 additions & 5 deletions lib/src/chatlist/invite_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class _InviteListState extends State<InviteList> {
_messagesBloc.dispatch(RequestMessages(1));
}

@override
void dispose(){
_messagesBloc.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return BlocBuilder(
Expand Down Expand Up @@ -97,9 +103,4 @@ class _InviteListState extends State<InviteList> {
);
}

@override
void dispose(){
super.dispose();
_messagesBloc.dispose();
}
}
10 changes: 8 additions & 2 deletions lib/src/contact/contact_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ import 'package:ox_talk/src/contact/contact_list_bloc.dart';
import 'package:ox_talk/src/contact/contact_list_event.dart';
import 'package:ox_talk/src/contact/contact_list_state.dart';
import 'package:ox_talk/src/l10n/localizations.dart';
import 'package:ox_talk/src/navigation/navigation.dart';
import 'package:ox_talk/src/utils/colors.dart';
import 'package:ox_talk/src/utils/dialog_builder.dart';
import 'package:ox_talk/src/utils/dimensions.dart';
import 'package:ox_talk/src/utils/toast.dart';
import 'package:ox_talk/src/navigation/navigation.dart';
import 'package:rxdart/rxdart.dart';

class ContactListView extends BaseRootChild {
Expand Down Expand Up @@ -133,6 +133,13 @@ class _ContactListState extends State<ContactListView> {
}
}

@override
void dispose() {
_contactImportBloc.dispose();
_contactListBloc.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return BlocBuilder(
Expand Down Expand Up @@ -186,5 +193,4 @@ class _ContactListState extends State<ContactListView> {
return ContactItem(contactId, false, key);
});
}

}
13 changes: 7 additions & 6 deletions lib/src/contact/contact_list_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ import 'package:ox_talk/src/contact/contact_list_event.dart';
import 'package:ox_talk/src/contact/contact_list_state.dart';
import 'package:ox_talk/src/data/repository.dart';
import 'package:ox_talk/src/data/repository_manager.dart';
import 'package:ox_talk/src/data/repository_stream_handler.dart';
import 'package:rxdart/rxdart.dart';

class ContactListBloc extends Bloc<ContactListEvent, ContactListState> {
final Repository<Contact> contactRepository = RepositoryManager.get(RepositoryType.contact);
StreamSubscription streamSubscription;
RepositoryStreamHandler repositoryStreamHandler;
List<int> validContactIds = List();

@override
Expand Down Expand Up @@ -81,17 +83,16 @@ class ContactListBloc extends Bloc<ContactListEvent, ContactListState> {

@override
void dispose() {
contactRepository.removeListener(repositoryStreamHandler);
super.dispose();
contactRepository.removeListener(hashCode, Event.contactsChanged);
streamSubscription.cancel();
}

void setupContactListener() async {
contactRepository.addListener(hashCode, Event.contactsChanged);
streamSubscription = contactRepository.observable.listen((event) => dispatchContactsChanged());
repositoryStreamHandler = RepositoryStreamHandler(Type.publish, Event.contactsChanged, _dispatchContactsChanged);
contactRepository.addListener(repositoryStreamHandler);
}

void dispatchContactsChanged() async {
void _dispatchContactsChanged() async {
await _updateValidContactIds();
dispatch(ContactsChanged());
}
Expand Down
8 changes: 4 additions & 4 deletions lib/src/data/chat_list_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class ChatListRepository extends Repository<ChatList> {
ChatListRepository(RepositoryItemCreator<ChatList> creator) : super(creator);

@override
success(Event event) async {
onData(Event event) async {
if (event.eventId == Event.msgsChanged) {
await setupChatListAfterUpdate();
}
super.success(event);
super.onData(event);
}

Future<void> setupChatListAfterUpdate() async {
Expand All @@ -70,7 +70,7 @@ class ChatListRepository extends Repository<ChatList> {
}

@override
error(error) {
super.error(error);
onError(error) {
super.onError(error);
}
}
8 changes: 4 additions & 4 deletions lib/src/data/chat_message_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ class ChatMessageRepository extends Repository<ChatMsg> {
ChatMessageRepository(RepositoryItemCreator<ChatMsg> creator) : super(creator);

@override
success(Event event) {
super.success(event);
onData(Event event) {
super.onData(event);
}

@override
error(error) {
super.error(error);
onError(error) {
super.onError(error);
}

}
8 changes: 4 additions & 4 deletions lib/src/data/chat_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ class ChatRepository extends Repository<Chat> {
ChatRepository(RepositoryItemCreator<Chat> creator) : super(creator);

@override
success(Event event) {
super.success(event);
onData(Event event) {
super.onData(event);
}

@override
error(error) {
super.error(error);
onError(error) {
super.onError(error);
}

}
8 changes: 4 additions & 4 deletions lib/src/data/contact_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class ContactRepository extends Repository<Contact> {
ContactRepository(RepositoryItemCreator<Contact> creator) : super(creator);

@override
success(Event event) async {
onData(Event event) async {
if (event.eventId == Event.contactsChanged) {
await setupContactsAfterUpdate();
}
super.success(event);
super.onData(event);
}

Future<void> setupContactsAfterUpdate() async {
Expand All @@ -63,7 +63,7 @@ class ContactRepository extends Repository<Contact> {
}

@override
error(error) {
super.error(error);
onError(error) {
super.onError(error);
}
}
64 changes: 39 additions & 25 deletions lib/src/data/repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,22 @@
* for more details.
*/

import 'dart:async';
import 'dart:collection';

import 'package:delta_chat_core/delta_chat_core.dart';
import 'package:rxdart/rxdart.dart';
import 'package:ox_talk/src/data/repository_stream_handler.dart';

typedef T RepositoryItemCreator<T extends Base>(int id);

abstract class Repository<T extends Base> {
final RepositoryItemCreator<T> _creator;
final int id;
final LinkedHashMap<int, T> _items = LinkedHashMap();
final Map<int, Map<int, int>> _eventListeners = Map();
DeltaChatCore _core = DeltaChatCore();
BehaviorSubject<Event> _behaviorSubject = new BehaviorSubject();

Repository(this._creator, [this.id]);

Observable get observable => Observable(_behaviorSubject.stream);

T get(int id) {
var item = _items[id];
if (item == null) {
Expand Down Expand Up @@ -131,33 +128,50 @@ abstract class Repository<T extends Base> {
return _items.containsKey(id);
}

void addListener(int key, int eventId) async {
int listenerId;
Map<int, int> eventConsumers = _eventListeners[eventId];
if (eventConsumers == null || eventConsumers.length == 0) {
listenerId = await _core.listen(eventId, success, error);
} else {
listenerId = eventConsumers.values.elementAt(0);
Future<void> addListener(BaseRepositoryStreamHandler streamHandler) async {
if (streamHandler is RepositoryStreamHandler) {
await _setupCoreListener(streamHandler, streamHandler.eventId);
} else if (streamHandler is RepositoryMultiEventStreamHandler) {
for (int eventId in streamHandler.eventIds) {
await _setupCoreListener(streamHandler, eventId);
}
}
_eventListeners[eventId] = {key: listenerId};
}

void removeListener(int key, int eventId) {
Map<int, int> eventConsumers = _eventListeners[eventId];
if (eventConsumers.containsKey(key)) {
int listenerId = eventConsumers[key];
eventConsumers.remove(key);
if (eventConsumers.isEmpty) {
_core.removeListener(eventId, listenerId);
}
Future _setupCoreListener(BaseRepositoryStreamHandler streamHandler, int eventId) async {
int listenerId = await _core.listen(eventId, streamHandler.streamController);
if (streamHandler is RepositoryStreamHandler) {
streamHandler.listenerId = listenerId;
} else if (streamHandler is RepositoryMultiEventStreamHandler) {
streamHandler.listenerIds.add(listenerId);
}
streamHandler.streamController.stream.listen((event) {
onData(event);
streamHandler.onData();
}).onError((error) {
error(error);
if (streamHandler.onError != null) {
onError(error);
}
});
}

success(Event event) {
_behaviorSubject.add(event);
void removeListener(BaseRepositoryStreamHandler streamHandler) {
streamHandler.tearDown();
if (streamHandler is RepositoryStreamHandler) {
tearDownCoreListener(streamHandler.eventId, streamHandler.listenerId);
} else if (streamHandler is RepositoryMultiEventStreamHandler) {
for (int index = 0; index < streamHandler.listenerIds.length; index++) {
tearDownCoreListener(streamHandler.eventIds[index], streamHandler.listenerIds[index]);
}
}
}

error(dynamic error) {
_behaviorSubject.addError(error);
void tearDownCoreListener(int eventId, int listenerId) {
_core.removeListener(eventId, listenerId);
}

onData(Event event) {}

onError(dynamic error) {}
}
Loading

0 comments on commit 691cbdc

Please sign in to comment.