refactor: use GET tenders/recommend and improve tender title UX
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
Load the Recommended tab from GET /api/v1/tenders/recommend instead of the AI POST flow with 503 fallback. Fix notification pagination offset, make tender titles selectable across list and detail views, and ignore .cursor. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,19 +1,15 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:tm_app/core/network/app_exceptions.dart';
|
||||
import 'package:tm_app/core/utils/result.dart';
|
||||
import 'package:tm_app/data/repositories/company_ai_repository.dart';
|
||||
import 'package:tm_app/data/repositories/home_repository.dart';
|
||||
import 'package:tm_app/data/services/model/recommend_response/recommend_response.dart';
|
||||
import 'package:tm_app/data/services/model/recommendation_item/recommendation_item.dart';
|
||||
import 'package:tm_app/data/services/model/recommended_tenders_response/recommended_tenders_response.dart';
|
||||
import 'package:tm_app/data/services/model/tender_data/tender_data.dart';
|
||||
|
||||
/// Drives the "Recommended" (AI) tab in the tenders section.
|
||||
/// Drives the "Recommended" tab in the tenders section.
|
||||
///
|
||||
/// Calls `POST /api/v1/recommend`. When the AI service is unavailable (503) it
|
||||
/// falls back to the legacy CPV/keyword list (`GET /api/v1/tenders/recommend`),
|
||||
/// and when the AI returns no results shortly after onboarding it surfaces a
|
||||
/// "still learning" hint instead of an empty error.
|
||||
/// Calls `GET /api/v1/tenders/recommend`. When the result is empty shortly
|
||||
/// after onboarding it surfaces a "still learning" hint instead of an empty
|
||||
/// error.
|
||||
class AiRecommendationsViewModel with ChangeNotifier {
|
||||
AiRecommendationsViewModel({
|
||||
required CompanyAiRepository companyAiRepository,
|
||||
@@ -26,21 +22,17 @@ class AiRecommendationsViewModel with ChangeNotifier {
|
||||
|
||||
/// How long after onboarding an empty result is treated as "still learning".
|
||||
static const Duration _stillLearningWindow = Duration(minutes: 30);
|
||||
static const int _fallbackPageSize = 20;
|
||||
static const int _pageSize = 20;
|
||||
|
||||
bool _isLoading = false;
|
||||
String? _errorMessage;
|
||||
List<RecommendationItem> _items = const [];
|
||||
List<TenderData> _fallbackTenders = const [];
|
||||
bool _usingFallback = false;
|
||||
List<TenderData> _recommendedTenders = const [];
|
||||
bool _stillLearning = false;
|
||||
bool _hasLoaded = false;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
String? get errorMessage => _errorMessage;
|
||||
List<RecommendationItem> get items => _items;
|
||||
List<TenderData> get fallbackTenders => _fallbackTenders;
|
||||
bool get usingFallback => _usingFallback;
|
||||
List<TenderData> get recommendedTenders => _recommendedTenders;
|
||||
bool get stillLearning => _stillLearning;
|
||||
|
||||
/// True once a load has completed (used to distinguish "no results" from
|
||||
@@ -51,8 +43,7 @@ class AiRecommendationsViewModel with ChangeNotifier {
|
||||
bool get isEmpty =>
|
||||
!_isLoading &&
|
||||
_errorMessage == null &&
|
||||
_items.isEmpty &&
|
||||
_fallbackTenders.isEmpty;
|
||||
_recommendedTenders.isEmpty;
|
||||
|
||||
/// Loads recommendations if they haven't been loaded yet this session.
|
||||
Future<void> loadIfNeeded() async {
|
||||
@@ -62,28 +53,32 @@ class AiRecommendationsViewModel with ChangeNotifier {
|
||||
await loadRecommendations();
|
||||
}
|
||||
|
||||
/// Loads AI recommendations, applying the 503 fallback and empty-state logic.
|
||||
/// Loads recommended tenders for the Recommended tab.
|
||||
Future<void> loadRecommendations() async {
|
||||
_isLoading = true;
|
||||
_errorMessage = null;
|
||||
_usingFallback = false;
|
||||
_stillLearning = false;
|
||||
notifyListeners();
|
||||
|
||||
final result = await _companyAiRepository.getRecommendations();
|
||||
final result = await _homeRepository.getHomeRecommendedTenders(
|
||||
limit: _pageSize,
|
||||
offset: 0,
|
||||
);
|
||||
|
||||
var loadedSuccessfully = false;
|
||||
|
||||
switch (result) {
|
||||
case Ok<RecommendResponse>():
|
||||
_items = result.value.data ?? const [];
|
||||
_fallbackTenders = const [];
|
||||
if (_items.isEmpty) {
|
||||
case Ok<RecommendedTendersResponse>():
|
||||
_recommendedTenders =
|
||||
result.value.data?.tenders.whereType<TenderData>().toList() ??
|
||||
const [];
|
||||
if (_recommendedTenders.isEmpty) {
|
||||
_stillLearning = _onboardingWasRecent();
|
||||
}
|
||||
loadedSuccessfully = true;
|
||||
case Error<RecommendResponse>():
|
||||
loadedSuccessfully = await _handleError(result.error);
|
||||
case Error<RecommendedTendersResponse>():
|
||||
_recommendedTenders = const [];
|
||||
_errorMessage = result.error.toString();
|
||||
}
|
||||
|
||||
_isLoading = false;
|
||||
@@ -94,39 +89,6 @@ class AiRecommendationsViewModel with ChangeNotifier {
|
||||
/// Pull-to-refresh: always re-fetches.
|
||||
Future<void> refresh() => loadRecommendations();
|
||||
|
||||
Future<bool> _handleError(Exception error) async {
|
||||
// 503 means the AI service isn't configured/available — fall back to the
|
||||
// legacy recommend list rather than showing an error. NetworkManager only
|
||||
// exposes this status when Dio received an HTTP response; connection and
|
||||
// timeout failures have no reliable status code and should remain retryable.
|
||||
if (error is AppException && error.statusCode == 503) {
|
||||
await _loadFallback();
|
||||
return _errorMessage == null;
|
||||
}
|
||||
_items = const [];
|
||||
_fallbackTenders = const [];
|
||||
_errorMessage = error.toString();
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<void> _loadFallback() async {
|
||||
final result = await _homeRepository.getHomeRecommendedTenders(
|
||||
limit: _fallbackPageSize,
|
||||
offset: 0,
|
||||
);
|
||||
|
||||
switch (result) {
|
||||
case Ok<RecommendedTendersResponse>():
|
||||
_items = const [];
|
||||
_fallbackTenders =
|
||||
result.value.data?.tenders.whereType<TenderData>().toList() ??
|
||||
const [];
|
||||
_usingFallback = true;
|
||||
case Error<RecommendedTendersResponse>():
|
||||
_errorMessage = result.error.toString();
|
||||
}
|
||||
}
|
||||
|
||||
bool _onboardingWasRecent() {
|
||||
final startedAt = _companyAiRepository.lastOnboardingStartedAt();
|
||||
if (startedAt == null) {
|
||||
|
||||
Reference in New Issue
Block a user