feat: Refactor code structure and optimize performance across multiple modules
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
const String getStaffLivePhotoSchema = '''
|
||||
query GetLivePhoto {
|
||||
me {
|
||||
id
|
||||
live_photo
|
||||
live_photo_obj {
|
||||
status
|
||||
url
|
||||
uploaded_at
|
||||
}
|
||||
}
|
||||
}
|
||||
''';
|
||||
|
||||
const String uploadStaffLivePhotoSchema = '''
|
||||
mutation UploadStaffLivePhoto(\$file: Upload!) {
|
||||
upload_staff_live_photo(file: \$file) {
|
||||
id
|
||||
live_photo
|
||||
live_photo_obj {
|
||||
status
|
||||
url
|
||||
uploaded_at
|
||||
}
|
||||
}
|
||||
}
|
||||
''';
|
||||
@@ -0,0 +1,66 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
@immutable
|
||||
class LivePhotoData {
|
||||
const LivePhotoData({
|
||||
required this.id,
|
||||
required this.imageUrl,
|
||||
required this.imagePath,
|
||||
required this.status,
|
||||
required this.timestamp,
|
||||
});
|
||||
|
||||
factory LivePhotoData.fromJson(Map<String, dynamic> json) {
|
||||
// TODO: For now live_photo_obj returns a placeholder from the backend.
|
||||
final livePhotoObj = json['live_photo_obj'] as Map<String, dynamic>;
|
||||
|
||||
return LivePhotoData(
|
||||
id: json['id'] as String? ?? '',
|
||||
imageUrl: json['live_photo'] as String? ?? '',
|
||||
imagePath: null,
|
||||
status: LivePhotoStatus.fromString(
|
||||
livePhotoObj['status'] as String? ?? '',
|
||||
),
|
||||
timestamp: DateTime.tryParse(
|
||||
livePhotoObj['uploaded_at'] as String? ?? '',
|
||||
) ??
|
||||
DateTime.now(),
|
||||
);
|
||||
}
|
||||
|
||||
final String? id;
|
||||
final String? imageUrl;
|
||||
final String? imagePath;
|
||||
final LivePhotoStatus status;
|
||||
final DateTime timestamp;
|
||||
|
||||
LivePhotoData copyWith({
|
||||
String? id,
|
||||
String? imageUrl,
|
||||
String? imagePath,
|
||||
LivePhotoStatus? status,
|
||||
DateTime? timestamp,
|
||||
}) {
|
||||
return LivePhotoData(
|
||||
id: id ?? this.id,
|
||||
imageUrl: imageUrl ?? this.imageUrl,
|
||||
imagePath: imagePath ?? this.imagePath,
|
||||
status: status ?? this.status,
|
||||
timestamp: timestamp ?? this.timestamp,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
enum LivePhotoStatus {
|
||||
pending,
|
||||
verified,
|
||||
declined;
|
||||
|
||||
static fromString(String value) {
|
||||
return switch (value) {
|
||||
'pending' => pending,
|
||||
'verified' => verified,
|
||||
_ => declined,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:http/http.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:krow/core/application/clients/api/api_client.dart';
|
||||
import 'package:krow/features/profile/live_photo/data/gql_schemas.dart';
|
||||
import 'package:krow/features/profile/live_photo/data/models/live_photo_data.dart';
|
||||
|
||||
@injectable
|
||||
class StaffLivePhotoApiProvider {
|
||||
StaffLivePhotoApiProvider(this._client);
|
||||
|
||||
final ApiClient _client;
|
||||
|
||||
Stream<LivePhotoData> getStaffLivePhotoWithCache() async* {
|
||||
await for (var response in _client.queryWithCache(
|
||||
schema: getStaffLivePhotoSchema,
|
||||
)) {
|
||||
if (response == null || response.data == null) continue;
|
||||
|
||||
if (response.hasException) {
|
||||
throw Exception(response.exception.toString());
|
||||
}
|
||||
|
||||
try {
|
||||
yield LivePhotoData.fromJson(
|
||||
response.data?['me'] as Map<String, dynamic>,
|
||||
);
|
||||
} catch (except) {
|
||||
log(
|
||||
'Exception in StaffInclusivityApiProvider '
|
||||
'on getStaffLivePhotoWithCache()',
|
||||
error: except,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<LivePhotoData> uploadStaffLivePhoto(LivePhotoData data) async {
|
||||
var result = await _client.mutate(
|
||||
schema: uploadStaffLivePhotoSchema,
|
||||
body: {
|
||||
'file': await MultipartFile.fromPath(
|
||||
'file',
|
||||
data.imagePath ?? '',
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
if (result.hasException) {
|
||||
throw Exception(result.exception.toString());
|
||||
}
|
||||
|
||||
if (result.data == null || result.data!.isEmpty) {
|
||||
throw Exception('Missing Live photo response from server');
|
||||
}
|
||||
|
||||
return LivePhotoData.fromJson(
|
||||
result.data?['upload_staff_live_photo'] as Map<String, dynamic>,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:krow/features/profile/live_photo/data/models/live_photo_data.dart';
|
||||
import 'package:krow/features/profile/live_photo/data/staff_live_photo_api_provider.dart';
|
||||
import 'package:krow/features/profile/live_photo/domain/staff_live_photo_repository.dart';
|
||||
|
||||
@Injectable(as: StaffLivePhotoRepository)
|
||||
class StaffLivePhotoRepositoryImpl implements StaffLivePhotoRepository {
|
||||
StaffLivePhotoRepositoryImpl({
|
||||
required StaffLivePhotoApiProvider apiProvider,
|
||||
}) : _apiProvider = apiProvider;
|
||||
|
||||
final StaffLivePhotoApiProvider _apiProvider;
|
||||
|
||||
@override
|
||||
Stream<LivePhotoData> getStaffLivePhotoWithCache() {
|
||||
return _apiProvider.getStaffLivePhotoWithCache();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<LivePhotoData> uploadStaffLivePhoto(LivePhotoData data) {
|
||||
return _apiProvider.uploadStaffLivePhoto(data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user