diff --git a/lib/core/params/profile_params.dart b/lib/core/params/profile_params.dart new file mode 100644 index 0000000..9c62c23 --- /dev/null +++ b/lib/core/params/profile_params.dart @@ -0,0 +1,13 @@ +class ProfileParams { + int? id; + + ProfileParams({this.id}); + + ProfileParams copyWith({ + int? id, + }) { + return ProfileParams( + id: id ?? this.id, + ); + } +} diff --git a/lib/features/profile/data/datasource/profile_datasource.dart b/lib/features/profile/data/datasource/profile_datasource.dart new file mode 100644 index 0000000..00cab8f --- /dev/null +++ b/lib/features/profile/data/datasource/profile_datasource.dart @@ -0,0 +1,28 @@ +import 'package:shia_game_flutter/core/constants/my_api.dart'; +import 'package:shia_game_flutter/core/network/http_request.dart'; +import 'package:shia_game_flutter/core/params/profile_params.dart'; +import 'package:shia_game_flutter/core/response/base_response.dart'; +import 'package:shia_game_flutter/features/profile/data/model/profile_model.dart'; +import 'package:shia_game_flutter/features/profile/domain/entity/profile_entity.dart'; + +abstract class IProfileDatasource { + Future getData({required ProfileParams params}); +} + +class ProfileDatasourceImpl implements IProfileDatasource { + final IHttpRequest httpRequest; + + const ProfileDatasourceImpl(this.httpRequest); + + @override + Future getData({required ProfileParams params}) async { + final response = await httpRequest.get( + path: MyApi.baseUrl, + ); + + return BaseResponse.getData( + response?['data'], + (json) => ProfileModel.fromJson(json), + ); + } +} diff --git a/lib/features/profile/data/model/profile_model.dart b/lib/features/profile/data/model/profile_model.dart new file mode 100644 index 0000000..cc17c4b --- /dev/null +++ b/lib/features/profile/data/model/profile_model.dart @@ -0,0 +1,13 @@ +import 'package:shia_game_flutter/features/profile/domain/entity/profile_entity.dart'; + +class ProfileModel extends ProfileEntity { + const ProfileModel({ + super.id, + }); + + factory ProfileModel.fromJson(Map json) { + return ProfileModel( + id: json['id'], + ); + } +} diff --git a/lib/features/profile/data/repository_impl/profile_repository_impl.dart b/lib/features/profile/data/repository_impl/profile_repository_impl.dart new file mode 100644 index 0000000..bdbcaa1 --- /dev/null +++ b/lib/features/profile/data/repository_impl/profile_repository_impl.dart @@ -0,0 +1,29 @@ +import 'package:flutter/foundation.dart'; +import 'package:shia_game_flutter/core/error_handler/my_exception.dart'; +import 'package:shia_game_flutter/core/params/profile_params.dart'; +import 'package:shia_game_flutter/core/utils/data_state.dart'; +import 'package:shia_game_flutter/features/profile/data/datasource/profile_datasource.dart'; +import 'package:shia_game_flutter/features/profile/domain/entity/profile_entity.dart'; +import 'package:shia_game_flutter/features/profile/domain/repository/profile_repository.dart'; + +class ProfileRepositoryImpl implements IProfileRepository { + final IProfileDatasource datasource; + + const ProfileRepositoryImpl(this.datasource); + + @override + Future> getData({required ProfileParams params}) async { + try { + final ProfileEntity response = await datasource.getData(params: params); + return DataState.success(response); + } on MyException catch (e) { + return DataState.error(e); + } catch (e) { + if (kDebugMode) { + rethrow; + } else { + return DataState.error(MyException(errorMessage: '$e')); + } + } + } +} diff --git a/lib/features/profile/domain/entity/profile_entity.dart b/lib/features/profile/domain/entity/profile_entity.dart new file mode 100644 index 0000000..3d7155b --- /dev/null +++ b/lib/features/profile/domain/entity/profile_entity.dart @@ -0,0 +1,14 @@ +import 'package:equatable/equatable.dart'; + +class ProfileEntity extends Equatable { + final int? id; + + const ProfileEntity({ + this.id, + }); + + @override + List get props => [ + id, + ]; +} diff --git a/lib/features/profile/domain/repository/profile_repository.dart b/lib/features/profile/domain/repository/profile_repository.dart new file mode 100644 index 0000000..75a2c82 --- /dev/null +++ b/lib/features/profile/domain/repository/profile_repository.dart @@ -0,0 +1,8 @@ +import 'package:shia_game_flutter/core/error_handler/my_exception.dart'; +import 'package:shia_game_flutter/core/params/profile_params.dart'; +import 'package:shia_game_flutter/core/utils/data_state.dart'; +import 'package:shia_game_flutter/features/profile/domain/entity/profile_entity.dart'; + +abstract class IProfileRepository { + Future> getData({required ProfileParams params}); +} diff --git a/lib/features/profile/presentation/binding/profile_binding.dart b/lib/features/profile/presentation/binding/profile_binding.dart new file mode 100644 index 0000000..67606fe --- /dev/null +++ b/lib/features/profile/presentation/binding/profile_binding.dart @@ -0,0 +1,20 @@ +import 'package:shia_game_flutter/features/profile/presentation/controller/profile_controller.dart'; +import 'package:get/get.dart'; + +class ProfileBinding extends Bindings { + @override + void dependencies() { + Get.put(ProfileController(Get.find())); + } + + Future deleteBindings() async { + await Future.wait([ + Get.delete(), + ]); + } + + Future refreshBinding() async { + await deleteBindings(); + dependencies(); + } +} diff --git a/lib/features/profile/presentation/controller/profile_controller.dart b/lib/features/profile/presentation/controller/profile_controller.dart new file mode 100644 index 0000000..c66df30 --- /dev/null +++ b/lib/features/profile/presentation/controller/profile_controller.dart @@ -0,0 +1,54 @@ +import 'package:flutter/cupertino.dart'; +import 'package:shia_game_flutter/core/params/profile_params.dart'; +import 'package:shia_game_flutter/core/status/base_status.dart'; +import 'package:shia_game_flutter/features/profile/domain/entity/profile_entity.dart'; +import 'package:shia_game_flutter/features/profile/domain/usecases/get_profile_usecase.dart'; +import 'package:get/get.dart'; + +class ProfileController extends GetxController with StateMixin { + /// ----- Constructor ----- + ProfileController(this.getProfileUseCase); + + @override + void onInit() { + super.onInit(); + change('', status: RxStatus.success()); + } + + @override + void onClose() { + textEditingController.dispose(); + super.onClose(); + } + + /// ----- UseCases ----- + final GetProfileUseCase getProfileUseCase; + + /// ----- Variables ----- + final Rx profileParams = Rx(ProfileParams()); + final Rx profileEntity = Rx(const ProfileEntity()); + + /// ------ Controllers ------ + final TextEditingController textEditingController = TextEditingController(); + + /// ------ Statuses ------ + final Rx getProfileStatus = Rx(const BaseInit()); + + /// ------ Functions ------ + + /// ------ Api Calls ------ + Future getProfile() async { + change('', status: RxStatus.loading()); + await getProfileUseCase(profileParams.value).then( + (value) => value.fold( + (data) { + profileEntity.value = data; + change('', status: RxStatus.success()); + }, + (error) { + change('', status: RxStatus.error(error.errorMessage)); + }, + ), + ); + } +} diff --git a/lib/features/profile/presentation/ui/profile_page.dart b/lib/features/profile/presentation/ui/profile_page.dart new file mode 100644 index 0000000..8a4a3a3 --- /dev/null +++ b/lib/features/profile/presentation/ui/profile_page.dart @@ -0,0 +1,14 @@ +import 'package:flutter/material.dart'; +import 'package:shia_game_flutter/features/profile/presentation/controller/profile_controller.dart'; +import 'package:get/get.dart'; + +class ProfilePage extends GetView { + const ProfilePage({super.key}); + + @override + Widget build(BuildContext context) { + return const Center( + child: Text('Profile Page'), + ); + } +}