feat: Refactor code structure and optimize performance across multiple modules
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:krow/core/application/clients/api/api_client.dart';
|
||||
import 'package:krow/features/profile/emergency_contacts/data/gql_schemas.dart';
|
||||
import 'package:krow/features/profile/emergency_contacts/data/models/emergency_contact_model.dart';
|
||||
|
||||
@injectable
|
||||
class EmergencyContactApiProvider {
|
||||
final ApiClient _apiClient;
|
||||
|
||||
EmergencyContactApiProvider(this._apiClient);
|
||||
|
||||
Stream<List<EmergencyContactModel>> getStaffEmergencyContacts() async* {
|
||||
await for (var response in _apiClient.queryWithCache(
|
||||
schema: getEmergencyContactsQuerySchema,
|
||||
)) {
|
||||
if (response == null || response.data == null) continue;
|
||||
|
||||
if (response.data == null || response.data!.isEmpty) {
|
||||
if (response.source?.name == 'cache') continue;
|
||||
|
||||
if (response.hasException) {
|
||||
throw Exception(response.exception.toString());
|
||||
}
|
||||
}
|
||||
|
||||
final contactsData =
|
||||
(response.data?['me'] as Map<String, dynamic>?)?['emergency_contacts']
|
||||
as List<dynamic>? ??
|
||||
[];
|
||||
|
||||
yield [
|
||||
for (final contact in contactsData)
|
||||
EmergencyContactModel.fromJson(
|
||||
contact as Map<String, dynamic>? ?? {},
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<EmergencyContactModel>?> updateEmergencyContacts(
|
||||
List<EmergencyContactModel> contacts,
|
||||
) async {
|
||||
var result = await _apiClient.mutate(
|
||||
schema: updateEmergencyContactsMutationSchema,
|
||||
body: {
|
||||
'input': [
|
||||
for (final contact in contacts) contact.toJson(),
|
||||
],
|
||||
},
|
||||
);
|
||||
|
||||
if (result.hasException) {
|
||||
throw Exception(result.exception.toString());
|
||||
}
|
||||
|
||||
if (result.data == null || result.data!.isEmpty) return null;
|
||||
|
||||
//TODO(Heorhii) add contacts return
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import 'package:krow/features/profile/emergency_contacts/data/models/emergency_contact_model.dart';
|
||||
|
||||
abstract class EmergencyContactRepository {
|
||||
Stream<List<EmergencyContactModel>> getStaffEmergencyContacts();
|
||||
|
||||
Future<List<EmergencyContactModel>?> updateEmergencyContacts(
|
||||
List<EmergencyContactModel> contacts,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
const String getEmergencyContactsQuerySchema = r'''
|
||||
query StaffEmergencyContacts {
|
||||
me {
|
||||
id
|
||||
emergency_contacts {
|
||||
id
|
||||
first_name
|
||||
last_name
|
||||
phone
|
||||
}
|
||||
}
|
||||
}
|
||||
''';
|
||||
|
||||
const String updateEmergencyContactsMutationSchema = r'''
|
||||
mutation SaveEmergencyContacts ($input: [CreateStaffEmergencyContactsInput!]) {
|
||||
attach_staff_emergency_contacts(contacts: $input) {
|
||||
emergency_contacts {
|
||||
id
|
||||
first_name
|
||||
last_name
|
||||
phone
|
||||
}
|
||||
}
|
||||
}
|
||||
''';
|
||||
@@ -0,0 +1,58 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
@immutable
|
||||
final class EmergencyContactModel {
|
||||
const EmergencyContactModel({
|
||||
this.contactId,
|
||||
required this.firstName,
|
||||
required this.lastName,
|
||||
required this.phoneNumber,
|
||||
});
|
||||
|
||||
const EmergencyContactModel.empty({
|
||||
this.contactId,
|
||||
this.firstName = '',
|
||||
this.lastName = '',
|
||||
this.phoneNumber = '',
|
||||
});
|
||||
|
||||
factory EmergencyContactModel.fromJson(Map<String, dynamic> json) {
|
||||
return EmergencyContactModel(
|
||||
contactId: json['id'] as String?,
|
||||
firstName: json['first_name'] as String? ?? '',
|
||||
lastName: json['last_name'] as String? ?? '',
|
||||
phoneNumber: json['phone'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
final String? contactId;
|
||||
final String firstName;
|
||||
final String lastName;
|
||||
final String phoneNumber;
|
||||
|
||||
bool get isFilled =>
|
||||
firstName.isNotEmpty && lastName.isNotEmpty && phoneNumber.isNotEmpty;
|
||||
|
||||
EmergencyContactModel copyWith({
|
||||
String? contactId,
|
||||
String? firstName,
|
||||
String? lastName,
|
||||
String? phoneNumber,
|
||||
bool? isSaved,
|
||||
}) {
|
||||
return EmergencyContactModel(
|
||||
contactId: contactId ?? this.contactId,
|
||||
firstName: firstName ?? this.firstName,
|
||||
lastName: lastName ?? this.lastName,
|
||||
phoneNumber: phoneNumber ?? this.phoneNumber,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'first_name': firstName,
|
||||
'last_name': lastName,
|
||||
'phone': phoneNumber,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user