- 자동 로그인 구현: 앱 시작 시 토큰 확인 후 적절한 화면으로 라우팅 - AuthInterceptor 개선: AuthService와 통합하여 토큰 관리 일원화 - 로그아웃 기능 개선: AuthService를 사용한 API 로그아웃 처리 - 대시보드 API 연동: MockDataService에서 실제 API로 완전 전환 - Dashboard DTO 모델 생성 (OverviewStats, RecentActivity 등) - DashboardRemoteDataSource 및 DashboardService 구현 - OverviewController를 ChangeNotifier 패턴으로 개선 - OverviewScreenRedesign에 Provider 패턴 적용 - API 통합 진행 상황 문서 업데이트
132 lines
5.1 KiB
Dart
132 lines
5.1 KiB
Dart
import 'package:dartz/dartz.dart';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:injectable/injectable.dart';
|
|
import 'package:superport/core/constants/api_endpoints.dart';
|
|
import 'package:superport/core/errors/exceptions.dart';
|
|
import 'package:superport/core/errors/failures.dart';
|
|
import 'package:superport/data/datasources/remote/api_client.dart';
|
|
import 'package:superport/data/models/dashboard/equipment_status_distribution.dart';
|
|
import 'package:superport/data/models/dashboard/expiring_license.dart';
|
|
import 'package:superport/data/models/dashboard/overview_stats.dart';
|
|
import 'package:superport/data/models/dashboard/recent_activity.dart';
|
|
|
|
abstract class DashboardRemoteDataSource {
|
|
Future<Either<Failure, OverviewStats>> getOverviewStats();
|
|
Future<Either<Failure, List<RecentActivity>>> getRecentActivities();
|
|
Future<Either<Failure, EquipmentStatusDistribution>> getEquipmentStatusDistribution();
|
|
Future<Either<Failure, List<ExpiringLicense>>> getExpiringLicenses({int days = 30});
|
|
}
|
|
|
|
@LazySingleton(as: DashboardRemoteDataSource)
|
|
class DashboardRemoteDataSourceImpl implements DashboardRemoteDataSource {
|
|
final ApiClient _apiClient;
|
|
|
|
DashboardRemoteDataSourceImpl(this._apiClient);
|
|
|
|
@override
|
|
Future<Either<Failure, OverviewStats>> getOverviewStats() async {
|
|
try {
|
|
final response = await _apiClient.get('/overview/stats');
|
|
|
|
if (response.data != null) {
|
|
final stats = OverviewStats.fromJson(response.data);
|
|
return Right(stats);
|
|
} else {
|
|
return Left(ServerFailure('응답 데이터가 없습니다'));
|
|
}
|
|
} on DioException catch (e) {
|
|
return Left(_handleDioError(e));
|
|
} catch (e) {
|
|
return Left(ServerFailure('통계 데이터를 가져오는 중 오류가 발생했습니다'));
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<Either<Failure, List<RecentActivity>>> getRecentActivities() async {
|
|
try {
|
|
final response = await _apiClient.get('/overview/recent-activities');
|
|
|
|
if (response.data != null && response.data is List) {
|
|
final activities = (response.data as List)
|
|
.map((json) => RecentActivity.fromJson(json))
|
|
.toList();
|
|
return Right(activities);
|
|
} else {
|
|
return Left(ServerFailure('응답 데이터가 올바르지 않습니다'));
|
|
}
|
|
} on DioException catch (e) {
|
|
return Left(_handleDioError(e));
|
|
} catch (e) {
|
|
return Left(ServerFailure('최근 활동을 가져오는 중 오류가 발생했습니다'));
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<Either<Failure, EquipmentStatusDistribution>> getEquipmentStatusDistribution() async {
|
|
try {
|
|
final response = await _apiClient.get('/equipment/status-distribution');
|
|
|
|
if (response.data != null) {
|
|
final distribution = EquipmentStatusDistribution.fromJson(response.data);
|
|
return Right(distribution);
|
|
} else {
|
|
return Left(ServerFailure('응답 데이터가 없습니다'));
|
|
}
|
|
} on DioException catch (e) {
|
|
return Left(_handleDioError(e));
|
|
} catch (e) {
|
|
return Left(ServerFailure('장비 상태 분포를 가져오는 중 오류가 발생했습니다'));
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<Either<Failure, List<ExpiringLicense>>> getExpiringLicenses({int days = 30}) async {
|
|
try {
|
|
final response = await _apiClient.get(
|
|
'/licenses/expiring-soon',
|
|
queryParameters: {'days': days},
|
|
);
|
|
|
|
if (response.data != null && response.data is List) {
|
|
final licenses = (response.data as List)
|
|
.map((json) => ExpiringLicense.fromJson(json))
|
|
.toList();
|
|
return Right(licenses);
|
|
} else {
|
|
return Left(ServerFailure('응답 데이터가 올바르지 않습니다'));
|
|
}
|
|
} on DioException catch (e) {
|
|
return Left(_handleDioError(e));
|
|
} catch (e) {
|
|
return Left(ServerFailure('만료 예정 라이선스를 가져오는 중 오류가 발생했습니다'));
|
|
}
|
|
}
|
|
|
|
Failure _handleDioError(DioException error) {
|
|
switch (error.type) {
|
|
case DioExceptionType.connectionTimeout:
|
|
case DioExceptionType.sendTimeout:
|
|
case DioExceptionType.receiveTimeout:
|
|
return NetworkFailure('네트워크 연결 시간이 초과되었습니다');
|
|
case DioExceptionType.connectionError:
|
|
return NetworkFailure('서버에 연결할 수 없습니다');
|
|
case DioExceptionType.badResponse:
|
|
final statusCode = error.response?.statusCode ?? 0;
|
|
final message = error.response?.data?['message'] ?? '서버 오류가 발생했습니다';
|
|
|
|
if (statusCode == 401) {
|
|
return AuthFailure('인증이 만료되었습니다');
|
|
} else if (statusCode == 403) {
|
|
return AuthFailure('접근 권한이 없습니다');
|
|
} else if (statusCode >= 400 && statusCode < 500) {
|
|
return ServerFailure(message);
|
|
} else {
|
|
return ServerFailure('서버 오류가 발생했습니다 ($statusCode)');
|
|
}
|
|
case DioExceptionType.cancel:
|
|
return ServerFailure('요청이 취소되었습니다');
|
|
default:
|
|
return ServerFailure('알 수 없는 오류가 발생했습니다');
|
|
}
|
|
}
|
|
} |