feat: Implement dynamic top professionals section with agent/lender tabs using Riverpod for state management.
This commit is contained in:
91
lib/features/agents/data/agents_repository.dart
Normal file
91
lib/features/agents/data/agents_repository.dart
Normal file
@@ -0,0 +1,91 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:real_estate_mobile/core/constants/api_constants.dart';
|
||||
import 'package:real_estate_mobile/core/network/api_client.dart';
|
||||
import 'package:real_estate_mobile/core/network/api_exceptions.dart';
|
||||
import 'package:real_estate_mobile/features/agents/data/models/agent_profile.dart';
|
||||
import 'package:real_estate_mobile/features/agents/data/models/agent_type.dart';
|
||||
|
||||
class SearchAgentsResponse {
|
||||
final List<AgentProfile> data;
|
||||
final int total;
|
||||
final int page;
|
||||
final int limit;
|
||||
final int totalPages;
|
||||
|
||||
const SearchAgentsResponse({
|
||||
required this.data,
|
||||
required this.total,
|
||||
required this.page,
|
||||
required this.limit,
|
||||
required this.totalPages,
|
||||
});
|
||||
|
||||
factory SearchAgentsResponse.fromJson(Map<String, dynamic> json) {
|
||||
return SearchAgentsResponse(
|
||||
data: (json['data'] as List<dynamic>)
|
||||
.map((e) => AgentProfile.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
total: json['total'] as int? ?? 0,
|
||||
page: json['page'] as int? ?? 1,
|
||||
limit: json['limit'] as int? ?? 10,
|
||||
totalPages: json['totalPages'] as int? ?? 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AgentsRepository {
|
||||
final Dio _dio = ApiClient.instance.dio;
|
||||
|
||||
Future<SearchAgentsResponse> searchAgents({
|
||||
String? agentTypeId,
|
||||
int page = 1,
|
||||
int limit = 10,
|
||||
String? sortBy,
|
||||
String? sortOrder,
|
||||
}) async {
|
||||
try {
|
||||
final queryParams = <String, dynamic>{
|
||||
'page': page,
|
||||
'limit': limit,
|
||||
};
|
||||
|
||||
if (agentTypeId != null) queryParams['agentTypeId'] = agentTypeId;
|
||||
if (sortBy != null) queryParams['sortBy'] = sortBy;
|
||||
if (sortOrder != null) queryParams['sortOrder'] = sortOrder;
|
||||
|
||||
final response = await _dio.get(
|
||||
ApiConstants.agents,
|
||||
queryParameters: queryParams,
|
||||
);
|
||||
|
||||
final data = response.data['data'] as Map<String, dynamic>;
|
||||
return SearchAgentsResponse.fromJson(data);
|
||||
} on DioException catch (e) {
|
||||
if (e.error is ApiException) {
|
||||
throw e.error as ApiException;
|
||||
}
|
||||
throw ApiException(
|
||||
message: e.message ?? 'Failed to fetch agents',
|
||||
statusCode: e.response?.statusCode,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<AgentType>> getAgentTypes() async {
|
||||
try {
|
||||
final response = await _dio.get(ApiConstants.agentTypes);
|
||||
final data = response.data['data'] as List<dynamic>;
|
||||
return data
|
||||
.map((e) => AgentType.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
} on DioException catch (e) {
|
||||
if (e.error is ApiException) {
|
||||
throw e.error as ApiException;
|
||||
}
|
||||
throw ApiException(
|
||||
message: e.message ?? 'Failed to fetch agent types',
|
||||
statusCode: e.response?.statusCode,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
165
lib/features/agents/data/models/agent_profile.dart
Normal file
165
lib/features/agents/data/models/agent_profile.dart
Normal file
@@ -0,0 +1,165 @@
|
||||
import 'package:real_estate_mobile/features/agents/data/models/agent_type.dart';
|
||||
|
||||
class AgentProfile {
|
||||
final String id;
|
||||
final String firstName;
|
||||
final String lastName;
|
||||
final String slug;
|
||||
final String? email;
|
||||
final String? phone;
|
||||
final String? bio;
|
||||
final String? avatar;
|
||||
final String? licenseNumber;
|
||||
final int? experience;
|
||||
final List<String> specializations;
|
||||
final List<String> languages;
|
||||
final List<String> serviceAreas;
|
||||
final String? city;
|
||||
final String? state;
|
||||
final String? country;
|
||||
final double? rating;
|
||||
final int reviewCount;
|
||||
final bool isVerified;
|
||||
final bool isFeatured;
|
||||
final bool isAvailable;
|
||||
final String? agentTypeId;
|
||||
final AgentType? agentType;
|
||||
final List<AgentFieldValue> fieldValues;
|
||||
|
||||
const AgentProfile({
|
||||
required this.id,
|
||||
required this.firstName,
|
||||
required this.lastName,
|
||||
required this.slug,
|
||||
this.email,
|
||||
this.phone,
|
||||
this.bio,
|
||||
this.avatar,
|
||||
this.licenseNumber,
|
||||
this.experience,
|
||||
this.specializations = const [],
|
||||
this.languages = const [],
|
||||
this.serviceAreas = const [],
|
||||
this.city,
|
||||
this.state,
|
||||
this.country,
|
||||
this.rating,
|
||||
this.reviewCount = 0,
|
||||
this.isVerified = false,
|
||||
this.isFeatured = false,
|
||||
this.isAvailable = false,
|
||||
this.agentTypeId,
|
||||
this.agentType,
|
||||
this.fieldValues = const [],
|
||||
});
|
||||
|
||||
String get fullName => '$firstName $lastName'.trim();
|
||||
|
||||
String get location {
|
||||
if (city != null && state != null) return '$city, $state';
|
||||
if (city != null) return city!;
|
||||
if (state != null) return state!;
|
||||
if (serviceAreas.isNotEmpty) return serviceAreas.first;
|
||||
return '';
|
||||
}
|
||||
|
||||
String get experienceText {
|
||||
if (experience == null) return '';
|
||||
return '$experience+ years in the real estate industry.';
|
||||
}
|
||||
|
||||
factory AgentProfile.fromJson(Map<String, dynamic> json) {
|
||||
return AgentProfile(
|
||||
id: json['id'] as String,
|
||||
firstName: json['firstName'] as String? ?? '',
|
||||
lastName: json['lastName'] as String? ?? '',
|
||||
slug: json['slug'] as String? ?? '',
|
||||
email: json['email'] as String?,
|
||||
phone: json['phone'] as String?,
|
||||
bio: json['bio'] as String?,
|
||||
avatar: json['avatar'] as String?,
|
||||
licenseNumber: json['licenseNumber'] as String?,
|
||||
experience: json['experience'] as int?,
|
||||
specializations: _parseStringList(json['specializations']),
|
||||
languages: _parseStringList(json['languages']),
|
||||
serviceAreas: _parseStringList(json['serviceAreas']),
|
||||
city: json['city'] as String?,
|
||||
state: json['state'] as String?,
|
||||
country: json['country'] as String?,
|
||||
rating: (json['rating'] as num?)?.toDouble(),
|
||||
reviewCount: json['reviewCount'] as int? ?? 0,
|
||||
isVerified: json['isVerified'] as bool? ?? false,
|
||||
isFeatured: json['isFeatured'] as bool? ?? false,
|
||||
isAvailable: json['isAvailable'] as bool? ?? false,
|
||||
agentTypeId: json['agentTypeId'] as String?,
|
||||
agentType: json['agentType'] != null
|
||||
? AgentType.fromJson(json['agentType'] as Map<String, dynamic>)
|
||||
: null,
|
||||
fieldValues: (json['fieldValues'] as List<dynamic>?)
|
||||
?.map((e) => AgentFieldValue.fromJson(e as Map<String, dynamic>))
|
||||
.toList() ??
|
||||
const [],
|
||||
);
|
||||
}
|
||||
|
||||
static List<String> _parseStringList(dynamic value) {
|
||||
if (value == null) return const [];
|
||||
if (value is List) return value.map((e) => e.toString()).toList();
|
||||
return const [];
|
||||
}
|
||||
}
|
||||
|
||||
class AgentFieldValue {
|
||||
final String id;
|
||||
final String fieldId;
|
||||
final String? textValue;
|
||||
final num? numberValue;
|
||||
final bool? booleanValue;
|
||||
final dynamic jsonValue;
|
||||
final String? dateValue;
|
||||
final AgentField field;
|
||||
|
||||
const AgentFieldValue({
|
||||
required this.id,
|
||||
required this.fieldId,
|
||||
this.textValue,
|
||||
this.numberValue,
|
||||
this.booleanValue,
|
||||
this.jsonValue,
|
||||
this.dateValue,
|
||||
required this.field,
|
||||
});
|
||||
|
||||
factory AgentFieldValue.fromJson(Map<String, dynamic> json) {
|
||||
return AgentFieldValue(
|
||||
id: json['id'] as String,
|
||||
fieldId: json['fieldId'] as String,
|
||||
textValue: json['textValue'] as String?,
|
||||
numberValue: json['numberValue'] as num?,
|
||||
booleanValue: json['booleanValue'] as bool?,
|
||||
jsonValue: json['jsonValue'],
|
||||
dateValue: json['dateValue'] as String?,
|
||||
field: AgentField.fromJson(json['field'] as Map<String, dynamic>),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AgentField {
|
||||
final String slug;
|
||||
final String name;
|
||||
final String fieldType;
|
||||
|
||||
const AgentField({
|
||||
required this.slug,
|
||||
required this.name,
|
||||
required this.fieldType,
|
||||
});
|
||||
|
||||
factory AgentField.fromJson(Map<String, dynamic> json) {
|
||||
return AgentField(
|
||||
slug: json['slug'] as String,
|
||||
name: json['name'] as String,
|
||||
fieldType: json['fieldType'] as String,
|
||||
);
|
||||
}
|
||||
}
|
||||
22
lib/features/agents/data/models/agent_type.dart
Normal file
22
lib/features/agents/data/models/agent_type.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
class AgentType {
|
||||
final String id;
|
||||
final String name;
|
||||
final String slug;
|
||||
final String? description;
|
||||
|
||||
const AgentType({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.slug,
|
||||
this.description,
|
||||
});
|
||||
|
||||
factory AgentType.fromJson(Map<String, dynamic> json) {
|
||||
return AgentType(
|
||||
id: json['id'] as String,
|
||||
name: json['name'] as String,
|
||||
slug: json['slug'] as String,
|
||||
description: json['description'] as String?,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user