2019-04-23 15:29:58 +00:00
|
|
|
import 'package:comunic/enums/user_page_visibility.dart';
|
2019-04-24 09:24:05 +00:00
|
|
|
import 'package:comunic/helpers/database/users_database_helper.dart';
|
2019-04-23 15:29:58 +00:00
|
|
|
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 {
|
2019-04-24 09:24:05 +00:00
|
|
|
final UsersDatabaseHelper _usersDatabaseHelper = UsersDatabaseHelper();
|
|
|
|
|
2019-04-23 15:29:58 +00:00
|
|
|
/// Download information about some given users ID
|
|
|
|
///
|
|
|
|
/// Return the list of users information in case of success, null in case of
|
|
|
|
/// failure
|
2019-04-24 09:33:58 +00:00
|
|
|
Future<UsersList> _downloadInfo(List<int> users) async {
|
2019-04-23 15:29:58 +00:00
|
|
|
// 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"],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2019-04-24 09:24:05 +00:00
|
|
|
// Save the list
|
|
|
|
_usersDatabaseHelper.insertOrUpdateAll(list);
|
|
|
|
|
2019-04-23 15:29:58 +00:00
|
|
|
return list;
|
|
|
|
}
|
2019-04-24 09:33:58 +00:00
|
|
|
|
|
|
|
/// Get users information
|
|
|
|
///
|
|
|
|
/// If [forceDownload] is set to true, the data will always be retrieved from
|
|
|
|
/// the server, otherwise cached data will be used if available
|
|
|
|
Future<UsersList> getUsersInfo(List<int> users,
|
|
|
|
{bool forceDownload = false}) async {
|
|
|
|
|
|
|
|
List<int> toDownload = List();
|
|
|
|
UsersList list = UsersList();
|
|
|
|
|
|
|
|
// Check cache
|
|
|
|
for(int userID in users){
|
|
|
|
if(!forceDownload && await _usersDatabaseHelper.has(userID))
|
|
|
|
list.add(await _usersDatabaseHelper.get(userID));
|
|
|
|
else
|
|
|
|
toDownload.add(userID);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process download if required
|
|
|
|
if(toDownload.length > 0) {
|
|
|
|
final downloadedList = await _downloadInfo(toDownload);
|
|
|
|
|
|
|
|
if(downloadedList == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
list.addAll(downloadedList);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2019-04-23 15:29:58 +00:00
|
|
|
}
|