1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 23:09:21 +00:00
comunicmobile/lib/helpers/settings_helper.dart

117 lines
4.3 KiB
Dart
Raw Normal View History

2020-04-16 17:16:44 +00:00
import 'dart:io';
2020-04-27 11:27:37 +00:00
import 'package:comunic/enums/user_page_visibility.dart';
2020-04-16 17:16:44 +00:00
import 'package:comunic/models/account_image_settings.dart';
import 'package:comunic/models/api_request.dart';
2020-04-27 11:27:37 +00:00
import 'package:comunic/models/general_settings.dart';
2020-04-16 17:16:44 +00:00
/// Settings helper
///
/// @author Pierre Hubert
const _APIAccountImageVisibilityAPILevels = {
"open": AccountImageVisibilityLevels.EVERYONE,
"public": AccountImageVisibilityLevels.COMUNIC_USERS,
"friends": AccountImageVisibilityLevels.FRIENDS_ONLY,
};
class SettingsHelper {
2020-04-27 11:27:37 +00:00
/// Get & return general user settings
static Future<GeneralSettings> getGeneralSettings() async {
final response =
(await APIRequest(uri: "settings/get_general", needLogin: true).exec())
.assertOk()
.getObject();
return GeneralSettings(
email: response["email"],
firstName: response["firstName"],
lastName: response["lastName"],
pageVisibility: response["is_open"]
? UserPageVisibility.OPEN
: response["is_public"]
? UserPageVisibility.PUBLIC
: UserPageVisibility.PRIVATE,
allowComments: response["allow_comments"],
allowPostsFromFriends: response["allow_posts_from_friends"],
allowComunicEmails: response["allow_comunic_mails"],
publicFriendsList: response["public_friends_list"],
virtualDirectory: response["virtual_directory"],
2020-04-27 11:27:59 +00:00
personalWebsite: response["personnal_website"],
2020-04-27 11:27:37 +00:00
publicNote: response["publicNote"],
);
}
/// Apply new general settings
static Future<void> updateGeneralSettings(GeneralSettings settings) async {
(await APIRequest(uri: "settings/set_general", needLogin: true, args: {
"firstName": settings.firstName,
"lastName": settings.lastName,
"allow_comunic_mails": settings.allowComunicEmails ? "true" : "false",
2020-04-27 16:44:41 +00:00
"isPublic": settings.pageVisibility != UserPageVisibility.PRIVATE
2020-04-27 11:27:37 +00:00
? "true"
: "false",
"isOpen":
settings.pageVisibility == UserPageVisibility.OPEN ? "true" : "false",
"allowComments": settings.allowComments ? "true" : "false",
"allowPostsFromFriends":
settings.allowPostsFromFriends ? "true" : "false",
"publicFriendsList": settings.publicFriendsList ? "true" : "false",
2020-04-27 11:27:59 +00:00
"personnalWebsite": settings.personalWebsite,
2020-04-27 11:27:37 +00:00
"virtualDirectory": settings.virtualDirectory,
"publicNote": settings.publicNote,
}).exec())
.assertOk();
}
2020-04-16 17:16:44 +00:00
/// Get & return account image settings
static Future<AccountImageSettings> getAccountImageSettings() async {
final response =
(await APIRequest(uri: "settings/get_account_image", needLogin: true)
.exec())
.assertOk()
.getObject();
return AccountImageSettings(
hasImage: response["has_image"],
imageURL: response["image_url"],
visibility:
_APIAccountImageVisibilityAPILevels[response["visibility"]]);
}
/// Upload a new account image
static Future<bool> uploadAccountImage(File newImage) async =>
(await APIRequest(uri: "settings/upload_account_image", needLogin: true)
.addFile("picture", newImage)
.execWithFiles())
.isOK;
2020-04-17 06:44:57 +00:00
/// Upload a new account image from memory
static Future<bool> uploadAccountImageFromMemory(List<int> bytes) async =>
(await APIRequest(uri: "settings/upload_account_image", needLogin: true)
.addBytesFile("picture", BytesFile("accountImage.png", bytes))
.execWithFiles())
.isOK;
/// Change account image visibility level
static Future<bool> setAccountImageVisibilityLevel(
AccountImageVisibilityLevels level) async =>
(await APIRequest(
uri: "settings/set_account_image_visibility", needLogin: true)
.addString(
"visibility",
// Find appropriate visibility level
_APIAccountImageVisibilityAPILevels.entries
.firstWhere((f) => f.value == level)
.key)
.exec())
.isOK;
2020-04-16 17:34:55 +00:00
/// Delete user account image
static Future<bool> deleteAccountImage() async =>
(await APIRequest(uri: "settings/delete_account_image", needLogin: true)
.exec())
.isOK;
2020-04-16 17:16:44 +00:00
}