Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.7 KiB

import 'dart:io';
import 'package:Envelope/components/custom_title_bar.dart';
import 'package:Envelope/components/flash_message.dart';
import 'package:Envelope/database/repositories/conversations_repository.dart';
import 'package:Envelope/database/repositories/friends_repository.dart';
import 'package:Envelope/services/conversations_service.dart';
import 'package:flutter/material.dart';
import 'create_add_users.dart';
import 'detail.dart';
import '/database/models/friends.dart';
import '/database/models/conversations.dart';
import '/views/main/conversation/edit_details.dart';
import '/views/main/conversation/list_item.dart';
class ConversationList extends StatefulWidget {
final List<Conversation> conversations;
final List<Friend> friends;
const ConversationList({
Key? key,
required this.conversations,
required this.friends,
}) : super(key: key);
@override
State<ConversationList> createState() => _ConversationListState();
}
class _ConversationListState extends State<ConversationList> {
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = GlobalKey<RefreshIndicatorState>();
List<Conversation> conversations = [];
List<Friend> friends = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const CustomTitleBar(
title: Text(
'Conversations',
style: TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold
)
),
showBack: false,
backgroundColor: Colors.transparent,
),
body: Padding(
padding: const EdgeInsets.only(top: 16,left: 16,right: 16, bottom: 65),
child: Stack(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 50),
child: RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: _refresh,
child: list(),
),
),
TextField(
decoration: const InputDecoration(
hintText: 'Search...',
prefixIcon: Icon(
Icons.search,
size: 20
),
),
onChanged: (value) => filterSearchResults(value.toLowerCase())
),
],
),
),
floatingActionButton: Padding(
padding: const EdgeInsets.only(right: 10, bottom: 60),
child: FloatingActionButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => ConversationEditDetails(
saveCallback: (String conversationName, File? file) {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => ConversationAddFriendsList(
friends: friends,
saveCallback: (List<Friend> friendsSelected) async {
Conversation conversation = await ConversationsRepository.createConversation(
conversationName,
friendsSelected,
false,
);
ConversationsService.uploadConversation(conversation)
.catchError((dynamic) {
showMessage('Failed to create conversation', context);
});
if (!mounted) {
return;
}
Navigator.of(context).popUntil((route) => route.isFirst);
Navigator.push(context, MaterialPageRoute(builder: (context) {
return ConversationDetail(
conversation: conversation,
);
}));
},
))
);
},
)),
).then(onGoBack);
},
backgroundColor: Theme.of(context).colorScheme.primary,
child: Icon(
Icons.add,
size: 30,
color: Theme.of(context).colorScheme.onPrimary,
),
),
),
);
}
void filterSearchResults(String query) {
List<Conversation> dummySearchList = [];
dummySearchList.addAll(widget.conversations);
if(query.isNotEmpty) {
List<Conversation> dummyListData = [];
for (Conversation item in dummySearchList) {
if (item.name.toLowerCase().contains(query)) {
dummyListData.add(item);
}
}
setState(() {
conversations.clear();
conversations.addAll(dummyListData);
});
return;
}
setState(() {
conversations.clear();
conversations.addAll(widget.conversations);
});
}
@override
void initState() {
super.initState();
conversations.addAll(widget.conversations);
friends.addAll(widget.friends);
setState(() {});
}
Widget list() {
if (conversations.isEmpty) {
return const Center(
child: Text('No Conversations'),
);
}
return ListView.builder(
itemCount: conversations.length,
shrinkWrap: false,
physics: const AlwaysScrollableScrollPhysics(),
itemBuilder: (context, i) {
return ConversationListItem(
conversation: conversations[i],
);
},
);
}
Future<void> _refresh() async {
Future.delayed(
const Duration(seconds: 1),
() {
print('Doing thing');
}
);
}
onGoBack(dynamic value) async {
conversations = await ConversationsRepository.getConversations();
friends = await FriendsRepository.getFriends();
setState(() {});
}
}