Refactor create order feature with repository pattern

Introduced domain entities, repository interface, and use case for order types in the client create order feature. Moved order type data and logic from the Bloc to a repository implementation. Updated module bindings and imports to support the new architecture, improving separation of concerns and maintainability.
This commit is contained in:
Achintha Isuru
2026-01-22 15:52:28 -05:00
parent 22ead66f71
commit 7090efb583
10 changed files with 137 additions and 98 deletions

View File

@@ -1 +0,0 @@

View File

@@ -1 +0,0 @@

View File

@@ -1,5 +1,8 @@
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_modular/flutter_modular.dart'; import 'package:flutter_modular/flutter_modular.dart';
import 'data/repositories/client_create_order_repository_impl.dart';
import 'domain/repositories/i_client_create_order_repository.dart';
import 'domain/usecases/get_order_types_usecase.dart';
import 'presentation/blocs/client_create_order_bloc.dart'; import 'presentation/blocs/client_create_order_bloc.dart';
import 'presentation/pages/create_order_page.dart'; import 'presentation/pages/create_order_page.dart';
import 'presentation/pages/one_time_order_page.dart'; import 'presentation/pages/one_time_order_page.dart';
@@ -10,6 +13,8 @@ import 'presentation/pages/recurring_order_page.dart';
class ClientCreateOrderModule extends Module { class ClientCreateOrderModule extends Module {
@override @override
void binds(Injector i) { void binds(Injector i) {
i.add<IClientCreateOrderRepository>(ClientCreateOrderRepositoryImpl.new);
i.add<GetOrderTypesUseCase>(GetOrderTypesUseCase.new);
i.addSingleton<ClientCreateOrderBloc>(ClientCreateOrderBloc.new); i.addSingleton<ClientCreateOrderBloc>(ClientCreateOrderBloc.new);
} }

View File

@@ -0,0 +1,62 @@
import 'package:design_system/design_system.dart';
import '../../domain/entities/create_order_type.dart';
import '../../domain/repositories/i_client_create_order_repository.dart';
class ClientCreateOrderRepositoryImpl implements IClientCreateOrderRepository {
@override
Future<List<CreateOrderType>> getOrderTypes() async {
// Simulating async data fetch
await Future.delayed(const Duration(milliseconds: 100));
return [
const CreateOrderType(
id: 'rapid',
icon: UiIcons.zap,
titleKey: 'client_create_order.types.rapid',
descriptionKey: 'client_create_order.types.rapid_desc',
backgroundColor: UiColors.tagError,
borderColor: UiColors.destructive,
iconBackgroundColor: UiColors.tagError,
iconColor: UiColors.destructive,
textColor: UiColors.destructive,
descriptionColor: UiColors.textError,
),
const CreateOrderType(
id: 'one-time',
icon: UiIcons.calendar,
titleKey: 'client_create_order.types.one_time',
descriptionKey: 'client_create_order.types.one_time_desc',
backgroundColor: UiColors.tagInProgress,
borderColor: UiColors.primary,
iconBackgroundColor: UiColors.tagInProgress,
iconColor: UiColors.primary,
textColor: UiColors.primary,
descriptionColor: UiColors.primary,
),
const CreateOrderType(
id: 'recurring',
icon: UiIcons.rotateCcw,
titleKey: 'client_create_order.types.recurring',
descriptionKey: 'client_create_order.types.recurring_desc',
backgroundColor: UiColors.tagRefunded,
borderColor: UiColors.primary,
iconBackgroundColor: UiColors.tagRefunded,
iconColor: UiColors.primary,
textColor: UiColors.primary,
descriptionColor: UiColors.textSecondary,
),
const CreateOrderType(
id: 'permanent',
icon: UiIcons.briefcase,
titleKey: 'client_create_order.types.permanent',
descriptionKey: 'client_create_order.types.permanent_desc',
backgroundColor: UiColors.tagSuccess,
borderColor: UiColors.textSuccess,
iconBackgroundColor: UiColors.tagSuccess,
iconColor: UiColors.textSuccess,
textColor: UiColors.textSuccess,
descriptionColor: UiColors.textSuccess,
),
];
}
}

View File

@@ -0,0 +1,43 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/widgets.dart';
/// Entity representing an Order Type.
class CreateOrderType extends Equatable {
final String id;
final IconData icon;
final String titleKey; // Key for translation
final String descriptionKey; // Key for translation
final Color backgroundColor;
final Color borderColor;
final Color iconBackgroundColor;
final Color iconColor;
final Color textColor;
final Color descriptionColor;
const CreateOrderType({
required this.id,
required this.icon,
required this.titleKey,
required this.descriptionKey,
required this.backgroundColor,
required this.borderColor,
required this.iconBackgroundColor,
required this.iconColor,
required this.textColor,
required this.descriptionColor,
});
@override
List<Object?> get props => [
id,
icon,
titleKey,
descriptionKey,
backgroundColor,
borderColor,
iconBackgroundColor,
iconColor,
textColor,
descriptionColor,
];
}

View File

@@ -0,0 +1,5 @@
import '../entities/create_order_type.dart';
abstract interface class IClientCreateOrderRepository {
Future<List<CreateOrderType>> getOrderTypes();
}

View File

@@ -0,0 +1,12 @@
import '../entities/create_order_type.dart';
import '../repositories/i_client_create_order_repository.dart';
class GetOrderTypesUseCase {
final IClientCreateOrderRepository _repository;
GetOrderTypesUseCase(this._repository);
Future<List<CreateOrderType>> call() {
return _repository.getOrderTypes();
}
}

View File

@@ -1,69 +1,22 @@
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:design_system/design_system.dart'; import '../../domain/usecases/get_order_types_usecase.dart';
import 'client_create_order_event.dart'; import 'client_create_order_event.dart';
import 'client_create_order_state.dart'; import 'client_create_order_state.dart';
class ClientCreateOrderBloc class ClientCreateOrderBloc
extends Bloc<ClientCreateOrderEvent, ClientCreateOrderState> { extends Bloc<ClientCreateOrderEvent, ClientCreateOrderState> {
ClientCreateOrderBloc() : super(const ClientCreateOrderInitial()) { final GetOrderTypesUseCase _getOrderTypesUseCase;
ClientCreateOrderBloc(this._getOrderTypesUseCase)
: super(const ClientCreateOrderInitial()) {
on<ClientCreateOrderTypesRequested>(_onTypesRequested); on<ClientCreateOrderTypesRequested>(_onTypesRequested);
} }
void _onTypesRequested( Future<void> _onTypesRequested(
ClientCreateOrderTypesRequested event, ClientCreateOrderTypesRequested event,
Emitter<ClientCreateOrderState> emit, Emitter<ClientCreateOrderState> emit,
) { ) async {
// In a real app, this might come from a repository or config final types = await _getOrderTypesUseCase();
final List<CreateOrderType> types = [
const CreateOrderType(
id: 'rapid',
icon: UiIcons.zap,
titleKey: 'client_create_order.types.rapid',
descriptionKey: 'client_create_order.types.rapid_desc',
backgroundColor: UiColors.tagError, // Red-ish background
borderColor: UiColors.destructive, // Red border
iconBackgroundColor: UiColors.tagError,
iconColor: UiColors.destructive,
textColor: UiColors.destructive,
descriptionColor: UiColors.textError,
),
const CreateOrderType(
id: 'one-time',
icon: UiIcons.calendar,
titleKey: 'client_create_order.types.one_time',
descriptionKey: 'client_create_order.types.one_time_desc',
backgroundColor: UiColors.tagInProgress, // Blue-ish
borderColor: UiColors.primary,
iconBackgroundColor: UiColors.tagInProgress,
iconColor: UiColors.primary,
textColor: UiColors.primary,
descriptionColor: UiColors.primary,
),
const CreateOrderType(
id: 'recurring',
icon: UiIcons.rotateCcw,
titleKey: 'client_create_order.types.recurring',
descriptionKey: 'client_create_order.types.recurring_desc',
backgroundColor: UiColors.tagRefunded, // Indigo-ish (Purple sub)
borderColor: UiColors.primary, // No purple, use primary or mix
iconBackgroundColor: UiColors.tagRefunded,
iconColor: UiColors.primary,
textColor: UiColors.primary,
descriptionColor: UiColors.textSecondary,
),
const CreateOrderType(
id: 'permanent',
icon: UiIcons.briefcase,
titleKey: 'client_create_order.types.permanent',
descriptionKey: 'client_create_order.types.permanent_desc',
backgroundColor: UiColors.tagSuccess, // Green
borderColor: UiColors.textSuccess,
iconBackgroundColor: UiColors.tagSuccess,
iconColor: UiColors.textSuccess,
textColor: UiColors.textSuccess,
descriptionColor: UiColors.textSuccess,
),
];
emit(ClientCreateOrderLoadSuccess(types)); emit(ClientCreateOrderLoadSuccess(types));
} }
} }

View File

@@ -1,47 +1,7 @@
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import '../../domain/entities/create_order_type.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
/// Represents an available order type.
class CreateOrderType extends Equatable {
final String id;
final IconData icon;
final String titleKey; // Key for translation
final String descriptionKey; // Key for translation
final Color backgroundColor;
final Color borderColor;
final Color iconBackgroundColor;
final Color iconColor;
final Color textColor;
final Color descriptionColor;
const CreateOrderType({
required this.id,
required this.icon,
required this.titleKey,
required this.descriptionKey,
required this.backgroundColor,
required this.borderColor,
required this.iconBackgroundColor,
required this.iconColor,
required this.textColor,
required this.descriptionColor,
});
@override
List<Object?> get props => [
id,
icon,
titleKey,
descriptionKey,
backgroundColor,
borderColor,
iconBackgroundColor,
iconColor,
textColor,
descriptionColor,
];
}
abstract class ClientCreateOrderState extends Equatable { abstract class ClientCreateOrderState extends Equatable {
const ClientCreateOrderState(); const ClientCreateOrderState();

View File

@@ -3,6 +3,7 @@ import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_modular/flutter_modular.dart'; import 'package:flutter_modular/flutter_modular.dart';
import '../../domain/entities/create_order_type.dart';
import '../blocs/client_create_order_bloc.dart'; import '../blocs/client_create_order_bloc.dart';
import '../blocs/client_create_order_event.dart'; import '../blocs/client_create_order_event.dart';
import '../blocs/client_create_order_state.dart'; import '../blocs/client_create_order_state.dart';