/* * @Author : Linloir * @Date : 2022-10-11 11:02:19 * @LastEditTime : 2022-10-14 15:12:02 * @Description : */ import 'package:tcp_client/repositories/common_models/message.dart'; import 'package:tcp_client/repositories/common_models/userinfo.dart'; import 'package:tcp_client/repositories/local_service_repository/models/local_file.dart'; enum TCPResponseType { token ('TOKEN'), //Only exists when server is sending message checkState ('STATE'), //Check login state for device token register ('REGISTER'), //Register new user login ('LOGIN'), //Login via username and password logout ('LOGOUT'), //Logout for current device token profile ('PROFILE'), //Fetch current logged in user profile modifyPassword('MODIFYPASSWD'), //Modify user password modifyProfile ('MODIFYPROFILE'), //Modify user profile sendMessage ('SENDMSG'), //Send message forwardMessage('FORWARDMSG'), //Forward message fetchMessage ('FETCHMSG'), //Fetch message findFile ('FINDFILE'), //Find file by md5 before transmitting the file fetchFile ('FETCHFILE'), //Fetch file and file md5 by message md5 searchUser ('SEARCHUSR'), //Search username and userid by username addContact ('ADDCONTACT'), //Add one-way relation to a user fetchContact ('FETCHCONTACT'), //Fetch all contacts, including requesting and pending unknown ('UNKNOWN'); //Wrong command const TCPResponseType(String value): _value = value; final String _value; String get value => _value; //Construct the enum type by value factory TCPResponseType.fromValue(String value) { return TCPResponseType.values.firstWhere((element) => element._value == value, orElse: () => TCPResponseType.unknown); } } enum TCPResponseStatus { ok ('OK'), err ('ERR'), unknown ('UNKNOWN'); const TCPResponseStatus(String value): _value = value; final String _value; String get value => _value; //Construct the enum type by value factory TCPResponseStatus.fromValue(String value) { return TCPResponseStatus.values.firstWhere((element) => element._value == value, orElse: () => TCPResponseStatus.unknown); } } abstract class TCPResponse { late final TCPResponseType _type; late final TCPResponseStatus _status; late final String? _info; TCPResponse({ required Map jsonObject }) { _type = TCPResponseType.fromValue(jsonObject['response'] as String); _status = TCPResponseStatus.fromValue(jsonObject['status'] as String); _info = jsonObject['info'] as String?; } TCPResponseType get type => _type; TCPResponseStatus get status => _status; String? get info => _info; } class SetTokenReponse extends TCPResponse { final int _token; SetTokenReponse({ required Map jsonObject }): _token = (jsonObject['body'] as Map)['tokenid'] as int, super(jsonObject: jsonObject); int get token => _token; } class CheckStateResponse extends TCPResponse { late final UserInfo? _userInfo; CheckStateResponse({ required Map jsonObject }): super(jsonObject: jsonObject) { _userInfo = jsonObject['body'] == null ? null : UserInfo.fromJSONObject(jsonObject: jsonObject['body'] as Map); } UserInfo? get userInfo => _userInfo; } class RegisterResponse extends TCPResponse { late final UserInfo? _userInfo; RegisterResponse({ required Map jsonObject }): super(jsonObject: jsonObject) { _userInfo = jsonObject['body'] == null ? null : UserInfo.fromJSONObject(jsonObject: jsonObject['body'] as Map); } UserInfo? get userInfo => _userInfo; } class LoginResponse extends TCPResponse { late final UserInfo? _userInfo; LoginResponse({ required Map jsonObject }): super(jsonObject: jsonObject) { _userInfo = jsonObject['body'] == null ? null : UserInfo.fromJSONObject(jsonObject: jsonObject['body'] as Map); } UserInfo? get userInfo => _userInfo; } class LogoutResponse extends TCPResponse { LogoutResponse({ required Map jsonObject }): super(jsonObject: jsonObject); } class GetProfileResponse extends TCPResponse { late final UserInfo? _userInfo; GetProfileResponse({ required Map jsonObject }): super(jsonObject: jsonObject) { _userInfo = jsonObject['body'] == null ? null : UserInfo.fromJSONObject(jsonObject: jsonObject['body'] as Map); } UserInfo? get userInfo => _userInfo; } class ModifyPasswordResponse extends TCPResponse { ModifyPasswordResponse({ required Map jsonObject }): super(jsonObject: jsonObject); } class ModifyProfileResponse extends TCPResponse { late final UserInfo? _userInfo; ModifyProfileResponse({ required Map jsonObject }): super(jsonObject: jsonObject) { _userInfo = jsonObject['body'] == null ? null : UserInfo.fromJSONObject(jsonObject: jsonObject['body'] as Map); } UserInfo? get userInfo => _userInfo; } class SendMessageResponse extends TCPResponse { late final String? _md5encoded; SendMessageResponse({ required Map jsonObject }): super(jsonObject: jsonObject) { _md5encoded = jsonObject['body'] == null ? null : (jsonObject['body'] as Map)['md5encoded'] as String?; } String? get md5encoded => _md5encoded; } class ForwardMessageResponse extends TCPResponse { late final Message _message; ForwardMessageResponse({ required Map jsonObject }): super(jsonObject: jsonObject) { _message = Message.fromJSONObject(jsonObject: jsonObject['body'] as Map); } Message get message => _message; } class FetchMessageResponse extends TCPResponse { late final List _messages; FetchMessageResponse({ required Map jsonObject }): super(jsonObject: jsonObject) { _messages = ((jsonObject['body'] as Map)['messages'] as List) .map((e) => Message.fromJSONObject(jsonObject: e)).toList(); } List get messages => _messages; } class FindFileResponse extends TCPResponse { FindFileResponse({ required Map jsonObject }): super(jsonObject: jsonObject); bool get foundIdentical => status == TCPResponseStatus.ok; } class FetchFileResponse extends TCPResponse { late final LocalFile _payload; FetchFileResponse({ required Map jsonObject, required LocalFile payload }): super(jsonObject: jsonObject) { _payload = LocalFile( file: payload.file, filemd5: payload.filemd5 ); } LocalFile get payload => _payload; } class SearchUserResponse extends TCPResponse { late final UserInfo? _userInfo; SearchUserResponse({ required Map jsonObject }): super(jsonObject: jsonObject) { _userInfo = jsonObject['body'] == null ? null : UserInfo.fromJSONObject(jsonObject: jsonObject['body'] as Map); } UserInfo? get userInfo => _userInfo; } class AddContactResponse extends TCPResponse { AddContactResponse({ required Map jsonObject }): super(jsonObject: jsonObject); } class FetchContactResponse extends TCPResponse { late final List _added; late final List _pending; late final List _requesting; FetchContactResponse({ required Map jsonObject }): super(jsonObject: jsonObject) { var body = jsonObject['body'] as Map? ?? { 'contacts': [], 'pending': [], 'requesting': [] }; var rawAddedContacts = body['contacts'] as List; var rawPendingContacts = body['pending'] as List; var rawRequestingContacts = body['requesting'] as List; _added = rawAddedContacts.map((e) => UserInfo.fromJSONObject(jsonObject: e)).toList(); _pending = rawPendingContacts.map((e) => UserInfo.fromJSONObject(jsonObject: e)).toList(); _requesting = rawRequestingContacts.map((e) => UserInfo.fromJSONObject(jsonObject: e)).toList(); } List get addedContacts => _added; List get pendingContacts => _pending; List get requestingContacts => _requesting; }