1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-26 04:48:51 +00:00

Get and show the name of conversation members

This commit is contained in:
Pierre HUBERT 2019-04-23 17:29:58 +02:00
parent c94a294252
commit 7fc03ba15c
9 changed files with 199 additions and 13 deletions

View File

@ -0,0 +1,5 @@
/// User page visibility
///
/// @author Pierre HUBERT
enum UserPageVisibility { PRIVATE, PUBLIC, OPEN }

View File

@ -1,3 +1,5 @@
import 'package:comunic/lists/conversations_list.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/conversation.dart';
@ -7,16 +9,15 @@ import 'package:comunic/models/conversation.dart';
class ConversationsHelper {
/// Download the list of conversations from the server
Future<List<Conversation>> downloadList() async {
Future<ConversationsList> downloadList() async {
final response =
await APIRequest(uri: "conversations/getList", needLogin: true).exec();
if (response.code != 200) return null;
try {
List<Conversation> list = List();
response.getArray().forEach((f) =>
list.add(Conversation(
ConversationsList list = ConversationsList();
response.getArray().forEach((f) => list.add(Conversation(
id: f["ID"],
ownerID: f["ID_owner"],
lastActive: f["last_active"],
@ -27,10 +28,23 @@ class ConversationsHelper {
)));
return list;
} on Exception catch(e){
} on Exception catch (e) {
print(e.toString());
return null;
}
}
/// Get the name of a [conversation]. This requires information about the
/// users of this conversation
static String getConversationName(Conversation conversation, UsersList users) {
if (conversation.has_name) return conversation.name;
// TODO : exclude current user name
String name = "";
for (int i = 0; i < 3 && i < conversation.members.length; i++)
name +=
(i > 0 ? ", " : "") + users.getUser(conversation.members[i]).fullName;
return name;
}
}

View File

@ -0,0 +1,47 @@
import 'package:comunic/enums/user_page_visibility.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/user.dart';
/// User helper
///
/// Helper used to get information about the users of Comunic
///
/// @author Pierre HUBERT
class UsersHelper {
/// Download information about some given users ID
///
/// Return the list of users information in case of success, null in case of
/// failure
Future<UsersList> downloadInfo(List<int> users) async {
// Execute the request
final response = await APIRequest(
uri: "user/getInfoMultiple", args: {"usersID": users.join(",")}).exec();
// Check if the request did not execute correctly
if (response.code != 200) return null;
final list = UsersList();
response.getObject().forEach(
(k, v) => list.add(
User(
id: v["userID"],
firstName: v["firstName"],
lastName: v["lastName"],
pageVisibility: v["publicPage"] == "false"
? UserPageVisibility.PRIVATE
: (v["openPage"] == "false"
? UserPageVisibility.PRIVATE
: UserPageVisibility.OPEN),
virtualDirectory: v["virtualDirectory"] == ""
? null
: v["virtualDirectory"],
accountImageURL: v["accountImage"],
),
),
);
return list;
}
}

View File

@ -0,0 +1,33 @@
import 'dart:collection';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/conversation.dart';
/// Conversations list
///
/// @author Pierre HUBERT
class ConversationsList extends ListBase<Conversation> {
final List<Conversation> _list = List();
UsersList users;
set length(l) => _list.length = l;
int get length => _list.length;
@override
Conversation operator [](int index) => _list[index];
@override
void operator []=(int index, Conversation value) => _list[index] = value;
/// Get the entire lists of users ID in this list
List<int> get allUsersID {
final List<int> list = List();
forEach((c) => c.members.forEach((id){
if(!list.contains(id))
list.add(id);
}));
return list;
}
}

34
lib/lists/users_list.dart Normal file
View File

@ -0,0 +1,34 @@
import 'dart:collection';
import 'package:comunic/models/user.dart';
/// Users list
///
/// @author Pierre HUBERT
class UsersList extends ListBase<User> {
List<User> _list = List();
int get length => _list.length;
set length(l) => _list.length = l;
@override
User operator [](int index) {
return _list[index];
}
@override
void operator []=(int index, User value) {
_list[index] = value;
}
/// Find a user with a specific ID
User getUser(int userID){
for(int i = 0; i < this.length; i++)
if(this[i].id == userID)
return this[i];
throw "User not found in the list!";
}
}

View File

@ -27,4 +27,7 @@ class Conversation {
assert(following != null),
assert(sawLastMessage != null),
assert(members != null);
/// Check out whether a conversation has a fixed name or not
bool get has_name => this.name != null;
}

31
lib/models/user.dart Normal file
View File

@ -0,0 +1,31 @@
import 'package:comunic/enums/user_page_visibility.dart';
import 'package:meta/meta.dart';
/// Single user information
///
/// @author Pierre HUBERT
class User {
final int id;
final String firstName;
final String lastName;
final UserPageVisibility pageVisibility;
final String virtualDirectory;
final String accountImageURL;
const User({
@required this.id,
@required this.firstName,
@required this.lastName,
@required this.pageVisibility,
@required this.virtualDirectory,
@required this.accountImageURL,
}) : assert(id != null),
assert(firstName != null),
assert(lastName != null),
assert(pageVisibility != null),
assert(accountImageURL != null);
/// Get user full name
String get fullName => firstName + " " + lastName;
}

View File

@ -1,6 +1,7 @@
import 'package:comunic/enums/load_error_level.dart';
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/models/conversation.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/conversations_list.dart';
import 'package:comunic/ui/tiles/conversation_tile.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
@ -17,7 +18,8 @@ class ConversationsScreen extends StatefulWidget {
class _ConversationScreenState extends State<ConversationsScreen> {
final ConversationsHelper _conversationsHelper = ConversationsHelper();
List<Conversation> _list;
final UsersHelper _usersHelper = UsersHelper();
ConversationsList _list;
LoadErrorLevel _error = LoadErrorLevel.NONE;
bool _loading = true;
@ -31,7 +33,7 @@ class _ConversationScreenState extends State<ConversationsScreen> {
void setLoading(bool loading) => setState(() => _loading = loading);
void gotLoadingError() {
void _gotLoadingError() {
setLoading(false);
setError(_list == null ? LoadErrorLevel.MAJOR : LoadErrorLevel.MINOR);
}
@ -41,12 +43,18 @@ class _ConversationScreenState extends State<ConversationsScreen> {
setError(LoadErrorLevel.NONE);
setLoading(true);
//Process the list of conversations
final list = await _conversationsHelper.downloadList();
if (list == null) return _gotLoadingError();
if (list == null) return gotLoadingError();
//Get information about the members of the conversations
list.users = await _usersHelper.downloadInfo(list.allUsersID);
if(list.users == null) return _gotLoadingError();
//Save list
_list = list;
setState(() {
_list = list;
});
setLoading(false);
}
@ -79,6 +87,7 @@ class _ConversationScreenState extends State<ConversationsScreen> {
itemBuilder: (context, index) {
return ConversationTile(
conversation: _list.elementAt(index),
usersList: _list.users,
);
},
itemCount: _list.length,

View File

@ -1,3 +1,5 @@
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/conversation.dart';
import 'package:comunic/utils/date_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
@ -9,8 +11,13 @@ import 'package:flutter/material.dart';
class ConversationTile extends StatelessWidget {
final Conversation conversation;
final UsersList usersList;
const ConversationTile({Key key, this.conversation}) : super(key: key);
const ConversationTile(
{Key key, @required this.conversation, @required this.usersList})
: assert(conversation != null),
assert(usersList != null),
super(key: key);
_buildSubInformation(IconData icon, String content) {
return Row(
@ -28,7 +35,10 @@ class ConversationTile extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(conversation.name == null ? "Unknown" : conversation.name),
title: Text(ConversationsHelper.getConversationName(
conversation,
usersList,
)),
leading: Icon(
conversation.sawLastMessage ? Icons.check_circle : Icons.lens,
color: conversation.sawLastMessage ? null : Colors.blue,