From 67209961b426bdded6fc05e4429e28b12a742e04 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Sat, 15 Feb 2025 11:54:02 +0400 Subject: [PATCH] added project model --- lib/features/auth/model/project_model.dart | 27 +++++++++++ lib/features/auth/model/user_model.dart | 53 ++++++++++++--------- lib/features/common/bloc/project_cubit.dart | 19 ++++++++ 3 files changed, 76 insertions(+), 23 deletions(-) create mode 100644 lib/features/auth/model/project_model.dart create mode 100644 lib/features/common/bloc/project_cubit.dart diff --git a/lib/features/auth/model/project_model.dart b/lib/features/auth/model/project_model.dart new file mode 100644 index 0000000..4f2f424 --- /dev/null +++ b/lib/features/auth/model/project_model.dart @@ -0,0 +1,27 @@ +class Project { + final String uuid; + final String name; + final String description; + + const Project({ + required this.uuid, + required this.name, + required this.description, + }); + + factory Project.fromJson(Map json) { + return Project( + uuid: json['uuid'] as String, + name: json['name'] as String, + description: json['description'] as String, + ); + } + + Map toJson() { + return { + 'uuid': uuid, + 'name': name, + 'description': description, + }; + } +} diff --git a/lib/features/auth/model/user_model.dart b/lib/features/auth/model/user_model.dart index 143b114..0f50a8b 100644 --- a/lib/features/auth/model/user_model.dart +++ b/lib/features/auth/model/user_model.dart @@ -1,5 +1,6 @@ import 'dart:convert'; import 'package:flutter/foundation.dart'; +import 'package:syncrow_app/features/auth/model/project_model.dart'; import 'package:syncrow_app/features/auth/model/token.dart'; class UserModel { @@ -20,6 +21,7 @@ class UserModel { final bool? hasAcceptedAppAgreement; final DateTime? appAgreementAcceptedAt; final Role? role; + final Project? project; UserModel({ required this.uuid, @@ -38,6 +40,7 @@ class UserModel { required this.hasAcceptedAppAgreement, required this.appAgreementAcceptedAt, required this.role, + required this.project, }); factory UserModel.fromJson(Map json) { @@ -62,33 +65,35 @@ class UserModel { ? DateTime.parse(json['appAgreementAcceptedAt']) : null, role: json['role'] != null ? Role.fromJson(json['role']) : null, + project: + json['project'] != null ? Project.fromJson(json['project']) : null, ); } factory UserModel.fromToken(Token token) { Map tempJson = Token.decodeToken(token.accessToken); return UserModel( - uuid: tempJson['uuid'].toString(), - email: tempJson['email'], - lastName: tempJson['lastName'], - firstName: tempJson['firstName'], - profilePicture: UserModel.decodeBase64Image(tempJson['profilePicture']), - phoneNumber: null, - isEmailVerified: null, - isAgreementAccepted: null, - regionUuid: null, - regionName: tempJson['region']?['regionName'], - timeZone: tempJson['timezone']?['timeZoneOffset'], - hasAcceptedWebAgreement: tempJson['hasAcceptedWebAgreement'], - webAgreementAcceptedAt: tempJson['webAgreementAcceptedAt'] != null - ? DateTime.parse(tempJson['webAgreementAcceptedAt']) - : null, - hasAcceptedAppAgreement: tempJson['hasAcceptedAppAgreement'], - appAgreementAcceptedAt: tempJson['appAgreementAcceptedAt'] != null - ? DateTime.parse(tempJson['appAgreementAcceptedAt']) - : null, - role: tempJson['role'] != null ? Role.fromJson(tempJson['role']) : null, - ); + uuid: tempJson['uuid'].toString(), + email: tempJson['email'], + lastName: tempJson['lastName'], + firstName: tempJson['firstName'], + profilePicture: UserModel.decodeBase64Image(tempJson['profilePicture']), + phoneNumber: null, + isEmailVerified: null, + isAgreementAccepted: null, + regionUuid: null, + regionName: tempJson['region']?['regionName'], + timeZone: tempJson['timezone']?['timeZoneOffset'], + hasAcceptedWebAgreement: tempJson['hasAcceptedWebAgreement'], + webAgreementAcceptedAt: tempJson['webAgreementAcceptedAt'] != null + ? DateTime.parse(tempJson['webAgreementAcceptedAt']) + : null, + hasAcceptedAppAgreement: tempJson['hasAcceptedAppAgreement'], + appAgreementAcceptedAt: tempJson['appAgreementAcceptedAt'] != null + ? DateTime.parse(tempJson['appAgreementAcceptedAt']) + : null, + role: tempJson['role'] != null ? Role.fromJson(tempJson['role']) : null, + project: null); } static Uint8List? decodeBase64Image(String? base64String) { @@ -137,8 +142,10 @@ class Role { factory Role.fromJson(Map json) { return Role( uuid: json['uuid'], - createdAt: json['createdAt'] != null ? DateTime.parse(json['createdAt']) : null, - updatedAt: json['updatedAt'] != null ? DateTime.parse(json['updatedAt']) : null, + createdAt: + json['createdAt'] != null ? DateTime.parse(json['createdAt']) : null, + updatedAt: + json['updatedAt'] != null ? DateTime.parse(json['updatedAt']) : null, type: json['type'], ); } diff --git a/lib/features/common/bloc/project_cubit.dart b/lib/features/common/bloc/project_cubit.dart new file mode 100644 index 0000000..ab0c891 --- /dev/null +++ b/lib/features/common/bloc/project_cubit.dart @@ -0,0 +1,19 @@ +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; + +class ProjectCubit extends Cubit { + final FlutterSecureStorage storage; + static const String projectKey = "selected_project_uuid"; + + ProjectCubit(this.storage) : super(null); + + Future setProjectUUID(String newUUID) async { + await storage.write(key: projectKey, value: newUUID); + emit(newUUID); + } + + Future clearProjectUUID() async { + await storage.delete(key: projectKey); + emit(null); + } +}