Browse Source

add: shop page

pull/2/head
AmirrezaChegini 1 month ago
parent
commit
25f52e207a
  1. 13
      lib/core/params/shop_params.dart
  2. 28
      lib/features/shop/data/datasource/shop_datasource.dart
  3. 13
      lib/features/shop/data/model/shop_model.dart
  4. 29
      lib/features/shop/data/repository_impl/shop_repository_impl.dart
  5. 14
      lib/features/shop/domain/entity/shop_entity.dart
  6. 8
      lib/features/shop/domain/repository/shop_repository.dart
  7. 20
      lib/features/shop/presentation/binding/shop_binding.dart
  8. 54
      lib/features/shop/presentation/controller/shop_controller.dart
  9. 14
      lib/features/shop/presentation/ui/shop_page.dart

13
lib/core/params/shop_params.dart

@ -0,0 +1,13 @@
class ShopParams {
int? id;
ShopParams({this.id});
ShopParams copyWith({
int? id,
}) {
return ShopParams(
id: id ?? this.id,
);
}
}

28
lib/features/shop/data/datasource/shop_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/shop_params.dart';
import 'package:shia_game_flutter/core/response/base_response.dart';
import 'package:shia_game_flutter/features/shop/data/model/shop_model.dart';
import 'package:shia_game_flutter/features/shop/domain/entity/shop_entity.dart';
abstract class IShopDatasource {
Future<ShopEntity> getData({required ShopParams params});
}
class ShopDatasourceImpl implements IShopDatasource {
final IHttpRequest httpRequest;
const ShopDatasourceImpl(this.httpRequest);
@override
Future<ShopEntity> getData({required ShopParams params}) async {
final response = await httpRequest.get(
path: MyApi.baseUrl,
);
return BaseResponse.getData<ShopEntity>(
response?['data'],
(json) => ShopModel.fromJson(json),
);
}
}

13
lib/features/shop/data/model/shop_model.dart

@ -0,0 +1,13 @@
import 'package:shia_game_flutter/features/shop/domain/entity/shop_entity.dart';
class ShopModel extends ShopEntity {
const ShopModel({
super.id,
});
factory ShopModel.fromJson(Map<String, dynamic> json) {
return ShopModel(
id: json['id'],
);
}
}

29
lib/features/shop/data/repository_impl/shop_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/shop_params.dart';
import 'package:shia_game_flutter/core/utils/data_state.dart';
import 'package:shia_game_flutter/features/shop/data/datasource/shop_datasource.dart';
import 'package:shia_game_flutter/features/shop/domain/entity/shop_entity.dart';
import 'package:shia_game_flutter/features/shop/domain/repository/shop_repository.dart';
class ShopRepositoryImpl implements IShopRepository {
final IShopDatasource datasource;
const ShopRepositoryImpl(this.datasource);
@override
Future<DataState<ShopEntity, MyException>> getData({required ShopParams params}) async {
try {
final ShopEntity 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/shop/domain/entity/shop_entity.dart

@ -0,0 +1,14 @@
import 'package:equatable/equatable.dart';
class ShopEntity extends Equatable {
final int? id;
const ShopEntity({
this.id,
});
@override
List<Object?> get props => [
id,
];
}

8
lib/features/shop/domain/repository/shop_repository.dart

@ -0,0 +1,8 @@
import 'package:shia_game_flutter/core/error_handler/my_exception.dart';
import 'package:shia_game_flutter/core/params/shop_params.dart';
import 'package:shia_game_flutter/core/utils/data_state.dart';
import 'package:shia_game_flutter/features/shop/domain/entity/shop_entity.dart';
abstract class IShopRepository {
Future<DataState<ShopEntity, MyException>> getData({required ShopParams params});
}

20
lib/features/shop/presentation/binding/shop_binding.dart

@ -0,0 +1,20 @@
import 'package:shia_game_flutter/features/shop/presentation/controller/shop_controller.dart';
import 'package:get/get.dart';
class ShopBinding extends Bindings {
@override
void dependencies() {
Get.put<ShopController>(ShopController(Get.find()));
}
Future<void> deleteBindings() async {
await Future.wait([
Get.delete<ShopController>(),
]);
}
Future<void> refreshBinding() async {
await deleteBindings();
dependencies();
}
}

54
lib/features/shop/presentation/controller/shop_controller.dart

@ -0,0 +1,54 @@
import 'package:flutter/cupertino.dart';
import 'package:shia_game_flutter/core/params/shop_params.dart';
import 'package:shia_game_flutter/core/status/base_status.dart';
import 'package:shia_game_flutter/features/shop/domain/entity/shop_entity.dart';
import 'package:shia_game_flutter/features/shop/domain/usecases/get_shop_usecase.dart';
import 'package:get/get.dart';
class ShopController extends GetxController with StateMixin {
/// ----- Constructor -----
ShopController(this.getShopUseCase);
@override
void onInit() {
super.onInit();
change('', status: RxStatus.success());
}
@override
void onClose() {
textEditingController.dispose();
super.onClose();
}
/// ----- UseCases -----
final GetShopUseCase getShopUseCase;
/// ----- Variables -----
final Rx<ShopParams> shopParams = Rx(ShopParams());
final Rx<ShopEntity> shopEntity = Rx(const ShopEntity());
/// ------ Controllers ------
final TextEditingController textEditingController = TextEditingController();
/// ------ Statuses ------
final Rx<BaseStatus> getShopStatus = Rx(const BaseInit());
/// ------ Functions ------
/// ------ Api Calls ------
Future<void> getShop() async {
change('', status: RxStatus.loading());
await getShopUseCase(shopParams.value).then(
(value) => value.fold(
(data) {
shopEntity.value = data;
change('', status: RxStatus.success());
},
(error) {
change('', status: RxStatus.error(error.errorMessage));
},
),
);
}
}

14
lib/features/shop/presentation/ui/shop_page.dart

@ -0,0 +1,14 @@
import 'package:flutter/material.dart';
import 'package:shia_game_flutter/features/shop/presentation/controller/shop_controller.dart';
import 'package:get/get.dart';
class ShopPage extends GetView<ShopController> {
const ShopPage({super.key});
@override
Widget build(BuildContext context) {
return const Center(
child: Text('Shop Page'),
);
}
}
Loading…
Cancel
Save