forked from eatvio/eatvio_app
feat(insights): review fixes
This commit is contained in:
@@ -29,18 +29,26 @@ class InsightsCubit extends Cubit<InsightsState> {
|
||||
|
||||
bool _isHeightLoaded = false;
|
||||
int? _heightCm;
|
||||
int _requestId = 0;
|
||||
|
||||
/// Полный payload и вкладка «В течение дня» координируются отдельно: смена
|
||||
/// дня не должна отменять загрузку периода, и наоборот.
|
||||
int _fullRequestId = 0;
|
||||
int _intraRequestId = 0;
|
||||
int _inflightFull = 0;
|
||||
int _inflightIntra = 0;
|
||||
|
||||
List<InsightsPanel> get panels =>
|
||||
InsightsPanel.available(kbjuEnabled: kbjuEnabled);
|
||||
|
||||
Future<void> load() async {
|
||||
bool get _isBusy => _inflightFull > 0 || _inflightIntra > 0;
|
||||
|
||||
Future<void> load({String? intraDate}) async {
|
||||
emit(const InsightsState.loading());
|
||||
await _fetch(
|
||||
bodyPeriod: BodyPeriod.threeMonths,
|
||||
dailyPeriod: DailyPeriod.twoWeeks,
|
||||
intraDate: null,
|
||||
previous: null,
|
||||
intraDate: intraDate,
|
||||
isInitial: true,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -53,12 +61,11 @@ class InsightsCubit extends Cubit<InsightsState> {
|
||||
if (current is! InsightsStateLoaded) {
|
||||
return load();
|
||||
}
|
||||
emit(current.copyWith(isRefreshing: true));
|
||||
await _fetch(
|
||||
bodyPeriod: current.bodyPeriod,
|
||||
dailyPeriod: current.dailyPeriod,
|
||||
intraDate: current.intraDate,
|
||||
previous: current,
|
||||
isInitial: false,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -72,7 +79,7 @@ class InsightsCubit extends Cubit<InsightsState> {
|
||||
bodyPeriod: period,
|
||||
dailyPeriod: current.dailyPeriod,
|
||||
intraDate: current.intraDate,
|
||||
previous: current,
|
||||
isInitial: false,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -89,7 +96,7 @@ class InsightsCubit extends Cubit<InsightsState> {
|
||||
bodyPeriod: current.bodyPeriod,
|
||||
dailyPeriod: period,
|
||||
intraDate: current.intraDate,
|
||||
previous: current,
|
||||
isInitial: false,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -110,51 +117,87 @@ class InsightsCubit extends Cubit<InsightsState> {
|
||||
final base = _currentIntraDay();
|
||||
if (base == null ||
|
||||
current is! InsightsStateLoaded ||
|
||||
(current.payload.intra?.isToday ?? true)) {
|
||||
!_canGoToNextDay(current, base)) {
|
||||
return;
|
||||
}
|
||||
await setIntraDate(DateTime(base.year, base.month, base.day + 1));
|
||||
}
|
||||
|
||||
/// База для листания: сначала оптимистично запрошенная дата, иначе ответ сервера.
|
||||
DateTime? _currentIntraDay() {
|
||||
final current = state;
|
||||
if (!kbjuEnabled || current is! InsightsStateLoaded) {
|
||||
return null;
|
||||
}
|
||||
final optimistic = current.intraDate;
|
||||
if (optimistic != null && optimistic.isNotEmpty) {
|
||||
return DateTime.tryParse(optimistic);
|
||||
}
|
||||
return DateTime.tryParse(current.payload.intra?.date ?? '');
|
||||
}
|
||||
|
||||
bool _canGoToNextDay(InsightsStateLoaded current, DateTime base) {
|
||||
final maxRaw =
|
||||
current.payload.intra?.maxDate ?? current.payload.intra?.date ?? '';
|
||||
final maxDate = DateTime.tryParse(maxRaw);
|
||||
if (maxDate == null) {
|
||||
return !(current.payload.intra?.isToday ?? true);
|
||||
}
|
||||
final baseDay = DateTime(base.year, base.month, base.day);
|
||||
final maxDay = DateTime(maxDate.year, maxDate.month, maxDate.day);
|
||||
return baseDay.isBefore(maxDay);
|
||||
}
|
||||
|
||||
Future<void> _loadIntra(String date) async {
|
||||
final current = state;
|
||||
if (!kbjuEnabled || current is! InsightsStateLoaded) {
|
||||
return;
|
||||
}
|
||||
final requestId = ++_requestId;
|
||||
emit(current.copyWith(isRefreshing: true));
|
||||
final requestId = ++_intraRequestId;
|
||||
_inflightIntra++;
|
||||
final previousIntraDate = current.intraDate;
|
||||
// Оптимистично фиксируем запрошенный день до ответа, чтобы быстрые
|
||||
// нажатия «‹ ›» считали базу от него, а не от устаревшего payload.
|
||||
emit(current.copyWith(intraDate: date, isRefreshing: true));
|
||||
try {
|
||||
final intra = await repository.fetchIntra(slug: slug, intraDate: date);
|
||||
if (requestId != _requestId || isClosed) {
|
||||
if (requestId != _intraRequestId || isClosed) {
|
||||
return;
|
||||
}
|
||||
final latest = state;
|
||||
if (latest is! InsightsStateLoaded) {
|
||||
return;
|
||||
}
|
||||
emit(
|
||||
current.copyWith(
|
||||
payload: current.payload.copyWith(intra: intra),
|
||||
latest.copyWith(
|
||||
payload: latest.payload.copyWith(intra: intra),
|
||||
intraDate: intra.isToday
|
||||
? null
|
||||
: (intra.date.isEmpty ? date : intra.date),
|
||||
isRefreshing: false,
|
||||
isRefreshing: _inflightFull > 0 || _inflightIntra > 1,
|
||||
),
|
||||
);
|
||||
} on InsightsForbidden {
|
||||
if (requestId == _requestId && !isClosed) {
|
||||
if (requestId == _intraRequestId && !isClosed) {
|
||||
emit(const InsightsState.hidden());
|
||||
}
|
||||
} catch (e, st) {
|
||||
await reportFrom(e, st, this);
|
||||
log(e.toString(), stackTrace: st);
|
||||
if (requestId == _requestId && !isClosed) {
|
||||
emit(current.copyWith(isRefreshing: false));
|
||||
if (requestId == _intraRequestId && !isClosed) {
|
||||
final latest = state;
|
||||
if (latest is InsightsStateLoaded) {
|
||||
emit(
|
||||
latest.copyWith(
|
||||
intraDate: previousIntraDate,
|
||||
isRefreshing: _inflightFull > 0 || _inflightIntra > 1,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
_inflightIntra--;
|
||||
_syncRefreshingFlag();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,50 +314,87 @@ class InsightsCubit extends Cubit<InsightsState> {
|
||||
required BodyPeriod bodyPeriod,
|
||||
required DailyPeriod dailyPeriod,
|
||||
required String? intraDate,
|
||||
required InsightsStateLoaded? previous,
|
||||
required bool isInitial,
|
||||
}) async {
|
||||
final requestId = ++_requestId;
|
||||
final requestId = ++_fullRequestId;
|
||||
final intraGenerationAtStart = _intraRequestId;
|
||||
_inflightFull++;
|
||||
final before = state;
|
||||
if (before is InsightsStateLoaded && !before.isRefreshing) {
|
||||
emit(before.copyWith(isRefreshing: true));
|
||||
}
|
||||
try {
|
||||
final payload = await _requestPayload(
|
||||
bodyPeriod: bodyPeriod,
|
||||
dailyPeriod: dailyPeriod,
|
||||
intraDate: intraDate,
|
||||
);
|
||||
if (requestId != _requestId || isClosed) {
|
||||
if (requestId != _fullRequestId || isClosed) {
|
||||
return;
|
||||
}
|
||||
emit(
|
||||
previous == null
|
||||
? InsightsState.loaded(
|
||||
payload: payload,
|
||||
bodyPeriod: bodyPeriod,
|
||||
dailyPeriod: dailyPeriod,
|
||||
intraDate: intraDate,
|
||||
)
|
||||
: previous.copyWith(
|
||||
payload: payload,
|
||||
bodyPeriod: bodyPeriod,
|
||||
dailyPeriod: dailyPeriod,
|
||||
intraDate: intraDate,
|
||||
isRefreshing: false,
|
||||
),
|
||||
);
|
||||
final latest = state;
|
||||
if (latest is InsightsStateLoaded) {
|
||||
// Более свежий intra-запрос не перезаписываем полным ответом.
|
||||
final keepIntra = _intraRequestId > intraGenerationAtStart;
|
||||
emit(
|
||||
latest.copyWith(
|
||||
payload: keepIntra
|
||||
? payload.copyWith(intra: latest.payload.intra)
|
||||
: payload,
|
||||
bodyPeriod: bodyPeriod,
|
||||
dailyPeriod: dailyPeriod,
|
||||
intraDate: keepIntra ? latest.intraDate : intraDate,
|
||||
isRefreshing: _inflightIntra > 0 || _inflightFull > 1,
|
||||
),
|
||||
);
|
||||
} else if (isInitial || latest is! InsightsStateError) {
|
||||
emit(
|
||||
InsightsState.loaded(
|
||||
payload: payload,
|
||||
bodyPeriod: bodyPeriod,
|
||||
dailyPeriod: dailyPeriod,
|
||||
intraDate: intraDate,
|
||||
),
|
||||
);
|
||||
}
|
||||
} on InsightsForbidden {
|
||||
if (requestId == _requestId && !isClosed) {
|
||||
if (requestId == _fullRequestId && !isClosed) {
|
||||
emit(const InsightsState.hidden());
|
||||
}
|
||||
} catch (e, st) {
|
||||
await reportFrom(e, st, this);
|
||||
log(e.toString(), stackTrace: st);
|
||||
if (requestId != _requestId || isClosed) {
|
||||
if (requestId != _fullRequestId || isClosed) {
|
||||
return;
|
||||
}
|
||||
// Ошибка перезапроса не должна стирать уже показанные данные.
|
||||
emit(
|
||||
previous == null
|
||||
? InsightsState.error(message: e.toString())
|
||||
: previous.copyWith(isRefreshing: false),
|
||||
);
|
||||
final latest = state;
|
||||
// Ошибка перезапроса не должна стирать уже показанные данные и UI-выбор.
|
||||
if (latest is InsightsStateLoaded) {
|
||||
emit(
|
||||
latest.copyWith(
|
||||
isRefreshing: _inflightIntra > 0 || _inflightFull > 1,
|
||||
),
|
||||
);
|
||||
} else if (isInitial) {
|
||||
emit(InsightsState.error(message: e.toString()));
|
||||
}
|
||||
} finally {
|
||||
_inflightFull--;
|
||||
_syncRefreshingFlag();
|
||||
}
|
||||
}
|
||||
|
||||
void _syncRefreshingFlag() {
|
||||
if (isClosed) {
|
||||
return;
|
||||
}
|
||||
final current = state;
|
||||
if (current is! InsightsStateLoaded) {
|
||||
return;
|
||||
}
|
||||
final busy = _isBusy;
|
||||
if (current.isRefreshing != busy) {
|
||||
emit(current.copyWith(isRefreshing: busy));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user