Browse Source

add: profile page

pull/2/head
AmirrezaChegini 1 month ago
parent
commit
ecf16bc2c0
  1. 13
      lib/core/params/profile_params.dart
  2. 28
      lib/features/profile/data/datasource/profile_datasource.dart
  3. 13
      lib/features/profile/data/model/profile_model.dart
  4. 29
      lib/features/profile/data/repository_impl/profile_repository_impl.dart
  5. 14
      lib/features/profile/domain/entity/profile_entity.dart
  6. 8
      lib/features/profile/domain/repository/profile_repository.dart
  7. 20
      lib/features/profile/presentation/binding/profile_binding.dart
  8. 54
      lib/features/profile/presentation/controller/profile_controller.dart
  9. 14
      lib/features/profile/presentation/ui/profile_page.dart

13
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,
);
}
}

28
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<ProfileEntity> getData({required ProfileParams params});
}
class ProfileDatasourceImpl implements IProfileDatasource {
final IHttpRequest httpRequest;
const ProfileDatasourceImpl(this.httpRequest);
@override
Future<ProfileEntity> getData({required ProfileParams params}) async {
final response = await httpRequest.get(
path: MyApi.baseUrl,
);
return BaseResponse.getData<ProfileEntity>(
response?['data'],
(json) => ProfileModel.fromJson(json),
);
}
}

13
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<String, dynamic> json) {
return ProfileModel(
id: json['id'],
);
}
}

29
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<DataState<ProfileEntity, MyException>> 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'));
}
}
}
}

14
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<Object?> get props => [
id,
];
}

8
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<DataState<ProfileEntity, MyException>> getData({required ProfileParams params});
}

20
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>(ProfileController(Get.find()));
}
Future<void> deleteBindings() async {
await Future.wait([
Get.delete<ProfileController>(),
]);
}
Future<void> refreshBinding() async {
await deleteBindings();
dependencies();
}
}

54
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> profileParams = Rx(ProfileParams());
final Rx<ProfileEntity> profileEntity = Rx(const ProfileEntity());
/// ------ Controllers ------
final TextEditingController textEditingController = TextEditingController();
/// ------ Statuses ------
final Rx<BaseStatus> getProfileStatus = Rx(const BaseInit());
/// ------ Functions ------
/// ------ Api Calls ------
Future<void> 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));
},
),
);
}
}

14
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<ProfileController> {
const ProfilePage({super.key});
@override
Widget build(BuildContext context) {
return const Center(
child: Text('Profile Page'),
);
}
}
Loading…
Cancel
Save