Skip to content

Commit

Permalink
Merge pull request #377 from bobs4462/main
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
bmuddha authored Mar 24, 2021
2 parents c02575d + 469c5f6 commit 9497ac6
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 64 deletions.
19 changes: 17 additions & 2 deletions lib/blocs/channels_bloc/channels_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,23 @@ class ChannelsBloc extends BaseChannelBloc {
item.visibility = event.data.visibility ?? item.visibility;
item.lastMessage = event.data.lastMessage ?? item.lastMessage;
} else {
this.add(
ReloadChannels(workspaceId: selectedParentId, forceFromApi: true));
await repository.reload(
queryParams: {
'workspace_id': selectedParentId,
'company_id': workspacesBloc.selectedCompanyId,
},
filters: [
['workspace_id', '=', selectedParentId]
],
sortFields: {'name': true},
forceFromApi: true,
);
yield ChannelsLoaded(
selected: repository.selected,
channels: repository.items,
force: DateTime.now().toString(),
);

return;
}
await repository.saveOne(item);
Expand Down
18 changes: 18 additions & 0 deletions lib/blocs/workspaces_bloc/workspace_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ class ReloadWorkspaces extends WorkspacesEvent {
List<Object> get props => [companyId];
}

class ForceRefresh extends WorkspacesEvent {
// parent company id
const ForceRefresh();

@override
List<Object> get props => [];
}

class CheckForChange extends WorkspacesEvent {
// parent company id
final String companyId;

const CheckForChange(this.companyId);

@override
List<Object> get props => [companyId];
}

class ClearWorkspaces extends WorkspacesEvent {
const ClearWorkspaces();

Expand Down
4 changes: 3 additions & 1 deletion lib/blocs/workspaces_bloc/workspace_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ abstract class WorkspaceState extends Equatable {
class WorkspacesLoaded extends WorkspaceState {
final List<Workspace> workspaces;
final Workspace selected;
final String force;

const WorkspacesLoaded({
this.workspaces,
this.force,
this.selected,
});
@override
List<Object> get props => [workspaces, selected];
List<Object> get props => [workspaces, selected, force];
}

class WorkspaceSelected extends WorkspacesLoaded {
Expand Down
17 changes: 17 additions & 0 deletions lib/blocs/workspaces_bloc/workspaces_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ class WorkspacesBloc extends Bloc<WorkspacesEvent, WorkspaceState> {
workspaces: repository.items,
selected: repository.selected,
);
} else if (event is CheckForChange) {
// Sorry Pavel, but cannot block the stream here with await
repository.didChange(
filters: [
['company_id', '=', event.companyId]
],
queryParams: {'company_id': event.companyId},
).then((changed) {
if (changed) this.add(ForceRefresh());
});
} else if (event is ForceRefresh) {
repository.items.sort((w1, w2) => w1.name.compareTo(w2.name));
yield WorkspacesLoaded(
workspaces: repository.items,
selected: repository.selected,
force: DateTime.now().toString(),
);
} else if (event is ClearWorkspaces) {
await repository.clean();
yield WorkspacesEmpty();
Expand Down
4 changes: 1 addition & 3 deletions lib/pages/messages_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ class MessagesPage<T extends BaseChannelBloc> extends StatelessWidget {
builder: (ctx, state) {
BaseChannel parentChannel = T is Channel ? Channel() : Direct();

if ((state is MessagesLoaded ||
state is MessagesEmpty) &&
if ((state is MessagesLoaded || state is MessagesEmpty) &&
state.parentChannel.id == ProfileBloc.selectedChannelId) {
parentChannel = state.parentChannel;
}
Expand Down Expand Up @@ -231,7 +230,6 @@ class MessagesPage<T extends BaseChannelBloc> extends StatelessWidget {

return BlocBuilder<MessageEditBloc, MessageEditState>(
builder: (ctx, state) => MessageEditField(
key: UniqueKey(),
autofocus: state is MessageEditing,
initialText:
state is MessageEditing ? state.originalStr : draft,
Expand Down
1 change: 0 additions & 1 deletion lib/pages/thread_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ class _ThreadPageState<T extends BaseChannelBloc> extends State<ThreadPage<T>> {
MessageEditState>(
builder: (ctx, state) {
return MessageEditField(
key: UniqueKey(),
initialText: state is MessageEditing
? state.originalStr
: draft ?? '',
Expand Down
28 changes: 28 additions & 0 deletions lib/repositories/collection_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,34 @@ class CollectionRepository<T extends CollectionItem> {
return true;
}

Future<bool> didChange({
Map<String, dynamic> queryParams,
List<List> filters, // fields to filter by in store
}) async {
final itemsList = await _storage.batchLoad(
type: _typeToStorageType[T],
filters: filters,
limit: 100000,
offset: 0,
);
List remote;
if (itemsList.isEmpty) return false;
try {
remote = await _api.get(apiEndpoint, params: queryParams);
logger.w("GOT WORKSPACES: ${remote.map((w) => w['name']).toSet()}");
} on ApiError catch (error) {
logger.d('ERROR while reloading $T items from api\n${error.message}');
return false;
}
if (remote.length != itemsList.length) {
logger.w("LOCAL != REMOTE");
await _storage.batchDelete(type: _typeToStorageType[T], filters: filters);
_updateItems(remote, saveToStore: true);
return true;
}
return false;
}

Future<bool> pullOne(
Map<String, dynamic> queryParams, {
bool addToItems = true,
Expand Down
9 changes: 3 additions & 6 deletions lib/sql/v1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ CREATE TABLE workspace (
color TEXT,
user_last_access INT,
total_members INT DEFAULT 0,
is_selected INT DEFAULT 0,
FOREIGN KEY(company_id) REFERENCES company(id)
is_selected INT DEFAULT 0
);
CREATE INDEX workspace_company_idx ON workspace(company_id);
''';
Expand All @@ -44,8 +43,7 @@ CREATE TABLE channel (
members_count INT DEFAULT 0,
last_message TEXT,
messages_unread INT DEFAULT 0,
is_selected INT DEFAULT 0,
FOREIGN KEY(workspace_id) REFERENCES workspace(id)
is_selected INT DEFAULT 0
);
CREATE INDEX channel_workspace_idx ON channel(workspace_id);
''';
Expand All @@ -64,8 +62,7 @@ CREATE TABLE direct (
messages_unread INT DEFAULT 0,
last_message TEXT,
is_selected INT DEFAULT 0,
has_unread INT DEFAULT 0,
FOREIGN KEY(company_id) REFERENCES company(id)
has_unread INT DEFAULT 0
);
CREATE INDEX direct_company_idx ON direct(company_id);
''';
Expand Down
8 changes: 7 additions & 1 deletion lib/widgets/bars/main_app_bar.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:twake/blocs/workspaces_bloc/workspaces_bloc.dart';
import 'package:twake/blocs/profile_bloc/profile_bloc.dart' show ProfileBloc;
import 'package:twake/config/dimensions_config.dart';
import 'package:twake/widgets/common/image_avatar.dart';

Expand All @@ -14,7 +15,12 @@ class MainAppBar extends StatelessWidget implements PreferredSizeWidget {
return AppBar(
titleSpacing: 0.0,
leading: InkWell(
onTap: () => scaffoldKey.currentState.openDrawer(),
onTap: () {
context
.read<WorkspacesBloc>()
.add(CheckForChange(ProfileBloc.selectedCompany.id));
scaffoldKey.currentState.openDrawer();
},
child: Image.asset('assets/images/menu.png'),
),
backgroundColor: Colors.white,
Expand Down
21 changes: 9 additions & 12 deletions lib/widgets/message/message_edit_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class MessageEditField extends StatefulWidget {
final String initialText;

MessageEditField({
Key key,
@required this.onMessageSend,
@required this.onTextUpdated,
this.autofocus = false,
Expand Down Expand Up @@ -113,16 +112,6 @@ class _MessageEditField extends State<MessageEditField> {
return false;
}

Widget buildEmojiBoard() {
return EmojiKeyboard(
onEmojiSelected: (emoji) {
_controller.text += emoji.text;
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
},
height: MediaQuery.of(context).size.height * 0.35,
);
}

@override
Widget build(BuildContext context) {
return WillPopScope(
Expand All @@ -139,7 +128,15 @@ class _MessageEditField extends State<MessageEditField> {
onMessageSend: widget.onMessageSend,
canSend: _canSend,
),
_emojiVisible ? buildEmojiBoard() : Container(),
if (_emojiVisible)
EmojiKeyboard(
onEmojiSelected: (emoji) {
_controller.text += emoji.text;
_scrollController
.jumpTo(_scrollController.position.maxScrollExtent);
},
height: MediaQuery.of(context).size.height * 0.35,
),
],
),
);
Expand Down
90 changes: 52 additions & 38 deletions lib/widgets/sheets/add/participants_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class _ParticipantsListState extends State<ParticipantsList> {
bool _shouldFocus = true;
String _title;

var _selectedIds = <String>[];
var _selectedUsers = <User>[];

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -144,15 +147,17 @@ class _ParticipantsListState extends State<ParticipantsList> {
}
} else if (state is Error) {
// Show an error
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
state.message,
style: TextStyle(
color: Colors.red,
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
state.message,
style: TextStyle(
color: Colors.red,
),
),
duration: Duration(seconds: 2),
),
duration: Duration(seconds: 2),
));
);
}
},
buildWhen: (_, current) {
Expand Down Expand Up @@ -210,66 +215,75 @@ class _ParticipantsListState extends State<ParticipantsList> {
return BlocBuilder<AddChannelBloc, AddChannelState>(
buildWhen: (previous, current) => current is Updated,
builder: (context, state) {
var selectedIds = <String>[];
var selectedUsers = <User>[];
var deselectedUsers = users;
var name = '';
var description = '';
if (state is Updated) {
name = state.repository?.name;
description = state.repository?.description;
selectedIds = state.repository?.members;
_selectedIds = state.repository?.members;
if (!_isDirect) {
selectedUsers = users
.where((user) => selectedIds.contains(user.id))
print('UsERS: ${users.map((e) => e.username)}');
print('UsERS IDS: ${users.map((e) => e.id)}');
print('selected ids: $_selectedIds');

// _selectedUsers = users
// .where((user) => _selectedIds.contains(user.id))
// .toList();
deselectedUsers = users
.where((user) => !_selectedIds.contains(user.id))
.toList();
users.excludeUsers(selectedUsers);
// users.excludeUsers(selectedUsers);
}
}
// print('Selected UsERS: ${selectedUsers.map((e) => e.username)}');
print(
'Selected UsERS: ${_selectedUsers.map((e) => e.username)}');
// print('Selected Ids: $selectedIds}');

return ListView.builder(
shrinkWrap: true,
padding: EdgeInsets.only(top: 0),
itemCount: users.length + selectedUsers.length,
itemCount: _isDirect
? users.length
: deselectedUsers.length + _selectedUsers.length,
itemBuilder: (context, index) {
User user;
if (!_isDirect) {
if (index < selectedUsers.length) {
user = selectedUsers[index];
if (_isDirect) {
user = users[index];
} else {
if (index < _selectedUsers.length) {
user = _selectedUsers[index];
} else {
user = users[index - selectedUsers.length];
user = deselectedUsers[index - _selectedUsers.length];
}
} else {
user = users[index];
}
return SearchItem(
title: user.firstName.isNotEmpty ||
user.lastName.isNotEmpty
? '${user.firstName} ${user.lastName}'
: '${user.username}',
selected: selectedIds.contains(user.id),
selected: _selectedIds.contains(user.id),
allowMultipleChoice: !_isDirect,
onTap: () {
FocusScope.of(context).requestFocus(FocusNode());
if (_isDirect) {
selectedIds = [user.id];
_createDirect(selectedIds);
_selectedIds = [user.id];
_createDirect(_selectedIds);
} else {
if (selectedIds.contains(user.id)) {
setState(() {
selectedIds.remove(user.id);
// selectedUsers.removeWhere((selected) => selected.id == user.id);
});
if (_selectedIds.contains(user.id)) {
_selectedIds.remove(user.id);
_selectedUsers.removeWhere((selected) => selected.id == user.id);
} else {
setState(() {
selectedIds.add(user.id);
// selectedUsers.add(user);
});
_selectedIds.add(user.id);
_selectedUsers.add(user);
}
context.read<AddChannelBloc>().add(Update(
name: name,
description: description,
participants: selectedIds,
));
context.read<AddChannelBloc>().add(
Update(
name: name,
description: description,
participants: _selectedIds,
),
);
}
},
);
Expand Down

0 comments on commit 9497ac6

Please sign in to comment.