feat: Refactor code structure and optimize performance across multiple modules
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
const String getStaffMobilityInfoSchema = '''
|
||||
query GetPersonalInfo {
|
||||
me {
|
||||
id
|
||||
accessibility {
|
||||
has_car
|
||||
can_relocate
|
||||
requires_accommodations
|
||||
accommodation_details
|
||||
}
|
||||
}
|
||||
}
|
||||
''';
|
||||
|
||||
const String updateStaffMobilityMutationSchema = '''
|
||||
mutation UpdateStaffAccessibilityInfo(\$input: UpdateStaffAccessibilitiesInput!) {
|
||||
update_staff_accessibilities(input: \$input) {
|
||||
accessibility {
|
||||
has_car
|
||||
can_relocate
|
||||
}
|
||||
}
|
||||
}
|
||||
''';
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
@immutable
|
||||
class MobilityModel {
|
||||
const MobilityModel({
|
||||
required this.hasACar,
|
||||
required this.canRelocate,
|
||||
});
|
||||
|
||||
factory MobilityModel.fromJson(Map<String, dynamic> json) {
|
||||
return MobilityModel(
|
||||
hasACar: json['has_car'] as bool? ?? false,
|
||||
canRelocate: json['can_relocate'] as bool? ?? false,
|
||||
);
|
||||
}
|
||||
|
||||
final bool hasACar;
|
||||
final bool canRelocate;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'has_car': hasACar,
|
||||
'can_relocate': canRelocate,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:krow/core/application/clients/api/api_client.dart';
|
||||
import 'package:krow/features/profile/mobility/data/gql_shemas.dart';
|
||||
import 'package:krow/features/profile/mobility/data/models/mobility_model.dart';
|
||||
|
||||
@injectable
|
||||
class StaffMobilityApiProvider {
|
||||
StaffMobilityApiProvider(this._client);
|
||||
|
||||
final ApiClient _client;
|
||||
|
||||
Stream<MobilityModel> getStaffMobilityWithCache() async* {
|
||||
await for (var response in _client.queryWithCache(
|
||||
schema: getStaffMobilityInfoSchema,
|
||||
)) {
|
||||
if (response == null || response.data == null) continue;
|
||||
|
||||
if (response.hasException) {
|
||||
throw Exception(response.exception.toString());
|
||||
}
|
||||
|
||||
try {
|
||||
yield MobilityModel.fromJson(
|
||||
(response.data?['me'] as Map<String, dynamic>?)?['accessibility'] ??
|
||||
{},
|
||||
);
|
||||
} catch (except) {
|
||||
log(
|
||||
'Exception in StaffMobilityApiProvider '
|
||||
'on getStaffMobilityWithCache()',
|
||||
error: except,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<MobilityModel?> updateStaffMobilityInfo(MobilityModel data) async {
|
||||
var result = await _client.mutate(
|
||||
schema: updateStaffMobilityMutationSchema,
|
||||
body: {
|
||||
'input': data.toJson(),
|
||||
},
|
||||
);
|
||||
|
||||
if (result.hasException) {
|
||||
throw Exception(result.exception.toString());
|
||||
}
|
||||
|
||||
if (result.data == null || result.data!.isEmpty) return null;
|
||||
|
||||
return MobilityModel.fromJson(
|
||||
(result.data?['update_staff_accessibilities']
|
||||
as Map<String, dynamic>?)?['accessibility'] ??
|
||||
{},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import 'package:krow/features/profile/mobility/data/models/mobility_model.dart';
|
||||
|
||||
abstract class StaffMobilityRepository {
|
||||
Stream<MobilityModel> getStaffMobility();
|
||||
|
||||
Future<MobilityModel?> updateStaffMobilityInfo(
|
||||
MobilityModel data,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:krow/core/application/di/injectable.dart';
|
||||
import 'package:krow/core/data/enums/state_status.dart';
|
||||
import 'package:krow/features/profile/mobility/data/models/mobility_model.dart';
|
||||
import 'package:krow/features/profile/mobility/data/staff_mobility_repository.dart';
|
||||
|
||||
part 'mobility_event.dart';
|
||||
part 'mobility_state.dart';
|
||||
|
||||
class MobilityBloc extends Bloc<MobilityEvent, MobilityState> {
|
||||
MobilityBloc() : super(const MobilityState()) {
|
||||
on<InitializeMobilityEvent>((event, emit) async {
|
||||
emit(
|
||||
state.copyWith(
|
||||
isInEditMode: event.isInEditMode,
|
||||
status: event.isInEditMode ? StateStatus.loading : StateStatus.idle,
|
||||
),
|
||||
);
|
||||
if (!state.isInEditMode) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
hasACar: true,
|
||||
canRelocate: true,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await for (final mobilityData
|
||||
in getIt<StaffMobilityRepository>().getStaffMobility()) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
hasACar: mobilityData.hasACar,
|
||||
canRelocate: mobilityData.canRelocate,
|
||||
status: StateStatus.idle,
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (except) {
|
||||
log(except.toString());
|
||||
}
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
hasACar: state.hasACar ?? true,
|
||||
canRelocate: state.canRelocate ?? true,
|
||||
status: state.status == StateStatus.loading
|
||||
? StateStatus.idle
|
||||
: state.status,
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
on<ToggleCarAvailability>((event, emit) {
|
||||
emit(state.copyWith(hasACar: event.index == 0));
|
||||
});
|
||||
|
||||
on<ToggleRelocationPossibility>((event, emit) {
|
||||
emit(state.copyWith(canRelocate: event.index == 0));
|
||||
});
|
||||
|
||||
on<SaveMobilityChanges>((event, emit) async {
|
||||
emit(state.copyWith(status: StateStatus.loading));
|
||||
try {
|
||||
await getIt<StaffMobilityRepository>().updateStaffMobilityInfo(
|
||||
MobilityModel(
|
||||
hasACar: state.hasACar ?? false,
|
||||
canRelocate: state.canRelocate ?? false,
|
||||
),
|
||||
);
|
||||
|
||||
//resave cached data
|
||||
getIt<StaffMobilityRepository>().getStaffMobility();
|
||||
} catch (except) {
|
||||
emit(state.copyWith(status: StateStatus.idle));
|
||||
log(except.toString());
|
||||
}
|
||||
|
||||
emit(state.copyWith(status: StateStatus.success));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
part of 'mobility_bloc.dart';
|
||||
|
||||
@immutable
|
||||
sealed class MobilityEvent {}
|
||||
|
||||
class InitializeMobilityEvent extends MobilityEvent {
|
||||
InitializeMobilityEvent({required this.isInEditMode});
|
||||
|
||||
final bool isInEditMode;
|
||||
}
|
||||
|
||||
class ToggleCarAvailability extends MobilityEvent {
|
||||
ToggleCarAvailability(this.index);
|
||||
|
||||
final int index;
|
||||
}
|
||||
|
||||
class ToggleRelocationPossibility extends MobilityEvent {
|
||||
ToggleRelocationPossibility(this.index);
|
||||
|
||||
final int index;
|
||||
}
|
||||
|
||||
class SaveMobilityChanges extends MobilityEvent {}
|
||||
@@ -0,0 +1,30 @@
|
||||
part of 'mobility_bloc.dart';
|
||||
|
||||
@immutable
|
||||
class MobilityState {
|
||||
const MobilityState({
|
||||
this.hasACar,
|
||||
this.canRelocate,
|
||||
this.isInEditMode = true,
|
||||
this.status = StateStatus.idle,
|
||||
});
|
||||
|
||||
final bool? hasACar;
|
||||
final bool? canRelocate;
|
||||
final bool isInEditMode;
|
||||
final StateStatus status;
|
||||
|
||||
MobilityState copyWith({
|
||||
bool? hasACar,
|
||||
bool? canRelocate,
|
||||
bool? isInEditMode,
|
||||
StateStatus? status,
|
||||
}) {
|
||||
return MobilityState(
|
||||
hasACar: hasACar ?? this.hasACar,
|
||||
canRelocate: canRelocate ?? this.canRelocate,
|
||||
isInEditMode: isInEditMode ?? this.isInEditMode,
|
||||
status: status ?? this.status,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:krow/features/profile/mobility/data/models/mobility_model.dart';
|
||||
import 'package:krow/features/profile/mobility/data/staff_mobility_api_provider.dart';
|
||||
import 'package:krow/features/profile/mobility/data/staff_mobility_repository.dart';
|
||||
|
||||
@LazySingleton(as: StaffMobilityRepository)
|
||||
class StaffMobilityRepositoryImpl extends StaffMobilityRepository {
|
||||
StaffMobilityRepositoryImpl({
|
||||
required StaffMobilityApiProvider apiProvider,
|
||||
}) : _apiProvider = apiProvider;
|
||||
|
||||
final StaffMobilityApiProvider _apiProvider;
|
||||
|
||||
@override
|
||||
Stream<MobilityModel> getStaffMobility() {
|
||||
return _apiProvider.getStaffMobilityWithCache();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<MobilityModel?> updateStaffMobilityInfo(MobilityModel data) {
|
||||
return _apiProvider.updateStaffMobilityInfo(data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:gap/gap.dart';
|
||||
import 'package:krow/core/application/common/bool_extension.dart';
|
||||
import 'package:krow/core/application/routing/routes.gr.dart';
|
||||
import 'package:krow/core/data/enums/state_status.dart';
|
||||
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
|
||||
import 'package:krow/core/presentation/styles/theme.dart';
|
||||
import 'package:krow/core/presentation/widgets/scroll_layout_helper.dart';
|
||||
import 'package:krow/core/presentation/widgets/ui_kit/kw_app_bar.dart';
|
||||
import 'package:krow/core/presentation/widgets/ui_kit/kw_button.dart';
|
||||
import 'package:krow/core/presentation/widgets/ui_kit/kw_loading_overlay.dart';
|
||||
import 'package:krow/core/presentation/widgets/ui_kit/kw_option_selector.dart';
|
||||
import 'package:krow/features/profile/mobility/domain/bloc/mobility_bloc.dart';
|
||||
|
||||
@RoutePage()
|
||||
class MobilityScreen extends StatelessWidget implements AutoRouteWrapper {
|
||||
const MobilityScreen({
|
||||
super.key,
|
||||
this.isInEditMode = true,
|
||||
});
|
||||
|
||||
final bool isInEditMode;
|
||||
|
||||
@override
|
||||
Widget wrappedRoute(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (context) => MobilityBloc()
|
||||
..add(InitializeMobilityEvent(isInEditMode: isInEditMode)),
|
||||
child: this,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: KwAppBar(
|
||||
titleText: 'mobility'.tr(),
|
||||
showNotification: false,
|
||||
),
|
||||
body: ScrollLayoutHelper(
|
||||
padding: const EdgeInsets.all(16),
|
||||
upperWidget: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Gap(4),
|
||||
Text(
|
||||
'mobility_information'.tr(),
|
||||
style: AppTextStyles.headingH1,
|
||||
textAlign: TextAlign.start,
|
||||
),
|
||||
const Gap(8),
|
||||
Text(
|
||||
'help_us_understand_mobility'.tr(),
|
||||
style: AppTextStyles.bodyMediumReg.copyWith(
|
||||
color: AppColors.blackGray,
|
||||
),
|
||||
textAlign: TextAlign.start,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 8, bottom: 16, top: 24),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'1. ',
|
||||
style: AppTextStyles.headingH3,
|
||||
),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'do_you_have_a_car'.tr(),
|
||||
style: AppTextStyles.headingH3,
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
BlocSelector<MobilityBloc, MobilityState, bool?>(
|
||||
selector: (state) => state.hasACar,
|
||||
builder: (context, hasACar) {
|
||||
return KwOptionSelector(
|
||||
selectedIndex: hasACar?.toInt(),
|
||||
selectedColor: AppColors.blackDarkBgBody,
|
||||
itemBorder: const Border.fromBorderSide(
|
||||
BorderSide(color: AppColors.grayStroke),
|
||||
),
|
||||
items: ['yes'.tr(), 'no'.tr()],
|
||||
onChanged: (index) {
|
||||
context
|
||||
.read<MobilityBloc>()
|
||||
.add(ToggleCarAvailability(index));
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 8, bottom: 16, top: 24),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'2. ',
|
||||
style: AppTextStyles.headingH3,
|
||||
),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'can_you_relocate'.tr(),
|
||||
style: AppTextStyles.headingH3,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
BlocSelector<MobilityBloc, MobilityState, bool?>(
|
||||
selector: (state) => state.canRelocate,
|
||||
builder: (context, canRelocate) {
|
||||
return KwOptionSelector(
|
||||
selectedColor: AppColors.blackDarkBgBody,
|
||||
itemBorder: const Border.fromBorderSide(
|
||||
BorderSide(color: AppColors.grayStroke),
|
||||
),
|
||||
selectedIndex: canRelocate?.toInt(),
|
||||
items: ['yes'.tr(), 'no'.tr()],
|
||||
onChanged: (index) {
|
||||
context
|
||||
.read<MobilityBloc>()
|
||||
.add(ToggleRelocationPossibility(index));
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
const Gap(90),
|
||||
],
|
||||
),
|
||||
lowerWidget: BlocConsumer<MobilityBloc, MobilityState>(
|
||||
buildWhen: (previous, current) => previous.status != current.status,
|
||||
listenWhen: (previous, current) => previous.status != current.status,
|
||||
listener: (context, state) {
|
||||
if (state.status == StateStatus.success) {
|
||||
if (isInEditMode) {
|
||||
Navigator.pop(context);
|
||||
} else {
|
||||
context.router.push(
|
||||
InclusiveRoute(isInEditMode: false),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
return KwLoadingOverlay(
|
||||
shouldShowLoading: state.status == StateStatus.loading,
|
||||
child: KwButton.primary(
|
||||
label: isInEditMode ? 'save_changes'.tr() : 'save_and_continue'.tr(),
|
||||
onPressed: () {
|
||||
context.read<MobilityBloc>().add(SaveMobilityChanges());
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user