add dropdown

This commit is contained in:
Phuoc Nguyen
2025-10-28 16:24:17 +07:00
parent 0010446298
commit 5cfc56f40d
20 changed files with 912 additions and 12 deletions

View File

@@ -0,0 +1,62 @@
import 'package:hive_ce/hive.dart';
import '../models/user_model.dart';
/// Abstract interface for users local data source
abstract class UsersLocalDataSource {
/// Get all users from local storage
Future<List<UserModel>> getUsers();
/// Save users to local storage
Future<void> saveUsers(List<UserModel> users);
/// Clear all users from local storage
Future<void> clearUsers();
}
/// Implementation of UsersLocalDataSource using Hive
class UsersLocalDataSourceImpl implements UsersLocalDataSource {
static const String _boxName = 'users';
Future<Box<UserModel>> get _box async {
if (!Hive.isBoxOpen(_boxName)) {
return await Hive.openBox<UserModel>(_boxName);
}
return Hive.box<UserModel>(_boxName);
}
@override
Future<List<UserModel>> getUsers() async {
try {
final box = await _box;
return box.values.toList();
} catch (e) {
throw Exception('Failed to get users from local storage: $e');
}
}
@override
Future<void> saveUsers(List<UserModel> users) async {
try {
final box = await _box;
await box.clear();
// Save users with their ID as key
for (final user in users) {
await box.put(user.id, user);
}
} catch (e) {
throw Exception('Failed to save users to local storage: $e');
}
}
@override
Future<void> clearUsers() async {
try {
final box = await _box;
await box.clear();
} catch (e) {
throw Exception('Failed to clear users from local storage: $e');
}
}
}

View File

@@ -0,0 +1,57 @@
import '../../../../core/constants/api_endpoints.dart';
import '../../../../core/errors/exceptions.dart';
import '../../../../core/network/api_client.dart';
import '../../../../core/network/api_response.dart';
import '../models/user_model.dart';
/// Abstract interface for users remote data source
abstract class UsersRemoteDataSource {
/// Fetch all users from the API
Future<List<UserModel>> getUsers();
}
/// Implementation of UsersRemoteDataSource using ApiClient
class UsersRemoteDataSourceImpl implements UsersRemoteDataSource {
final ApiClient apiClient;
UsersRemoteDataSourceImpl(this.apiClient);
@override
Future<List<UserModel>> getUsers() async {
try {
// Make API call to get all users
final response = await apiClient.get(ApiEndpoints.users);
// Parse the API response using ApiResponse wrapper
final apiResponse = ApiResponse.fromJson(
response.data as Map<String, dynamic>,
(json) => (json as List)
.map((e) => UserModel.fromJson(e as Map<String, dynamic>))
.toList(),
);
// Check if the API call was successful
if (apiResponse.isSuccess && apiResponse.value != null) {
return apiResponse.value!;
} else {
// Throw exception with error message from API
throw ServerException(
apiResponse.errors.isNotEmpty
? apiResponse.errors.first
: 'Failed to get users',
);
}
} catch (e) {
// Re-throw ServerException as-is
if (e is ServerException) {
rethrow;
}
// Re-throw NetworkException as-is
if (e is NetworkException) {
rethrow;
}
// Wrap other exceptions in ServerException
throw ServerException('Failed to get users: ${e.toString()}');
}
}
}