forked from eatvio/eatvio_app
fix: review fixes
This commit is contained in:
@@ -14,6 +14,7 @@ import 'package:eatvio_app/features/diary/models/nutrition_model.dart';
|
||||
import 'package:eatvio_app/features/diary/presentation/bloc/diary_state.dart';
|
||||
import 'package:eatvio_app/features/diary/repositories/diary_repository.dart';
|
||||
import 'package:eatvio_app/features/diets/repositories/diet_repository.dart';
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/glossary/models/glossary_model.dart';
|
||||
import 'package:eatvio_app/features/glossary/repositories/glossary_repository.dart';
|
||||
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
|
||||
@@ -38,6 +39,8 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
final ChatRepository chatRepository;
|
||||
final SettingsRepository settingsRepository;
|
||||
int? diaryId;
|
||||
int _fetchGeneration = 0;
|
||||
|
||||
DiaryCubit({
|
||||
required this.chatRepository,
|
||||
required this.diaryRepository,
|
||||
@@ -61,14 +64,21 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
selectedDay = date;
|
||||
}
|
||||
|
||||
bool _isCurrentFetch(int generation) => generation == _fetchGeneration;
|
||||
|
||||
Future<bool> setSelectedDay(DateTime day) async {
|
||||
final prevDay = selectedDay;
|
||||
final prevMeal = selectedMeal;
|
||||
selectedDay = day;
|
||||
selectedMeal = 1;
|
||||
final canSelect = await fetchDiaryV2(needLoading: true);
|
||||
final generation = ++_fetchGeneration;
|
||||
final canSelect = await fetchDiaryV2(needLoading: true, generation: generation);
|
||||
|
||||
if (!canSelect) {
|
||||
if (!_isCurrentFetch(generation)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!canSelect && state is! DiaryStateError) {
|
||||
selectedDay = prevDay;
|
||||
selectedMeal = prevMeal;
|
||||
}
|
||||
@@ -76,22 +86,33 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
return canSelect;
|
||||
}
|
||||
|
||||
Future<bool> fetchDiaryV2({bool needLoading = false, int? diaryId}) async {
|
||||
Future<bool> fetchDiaryV2({bool needLoading = false, int? diaryId, int? generation}) async {
|
||||
final request = generation ?? ++_fetchGeneration;
|
||||
final requestedDay = selectedDay;
|
||||
final currentState = state;
|
||||
try {
|
||||
if (!_isCurrentFetch(request)) {
|
||||
return false;
|
||||
}
|
||||
if (needLoading) {
|
||||
emit(const DiaryState.loading());
|
||||
}
|
||||
if (diaryId != null) {
|
||||
final diaryDayById = await diaryRepository.getDiaryDay(diaryId);
|
||||
if (!_isCurrentFetch(request)) {
|
||||
return false;
|
||||
}
|
||||
final diaryEntity = diaryDayById.toEntity();
|
||||
if (diaryEntity != null) {
|
||||
setDiary(diaryDay: diaryEntity, diaryId: diaryId);
|
||||
return true;
|
||||
await setDiary(diaryDay: diaryEntity, diaryId: diaryId, generation: request);
|
||||
return _isCurrentFetch(request);
|
||||
}
|
||||
}
|
||||
|
||||
final diaryDay = await getDiary();
|
||||
final diaryDay = await getDiary(day: requestedDay);
|
||||
if (!_isCurrentFetch(request)) {
|
||||
return false;
|
||||
}
|
||||
if (diaryDay == null) {
|
||||
emit(const DiaryState.error(errorMessage: 'Diary not found'));
|
||||
return true;
|
||||
@@ -99,16 +120,26 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
|
||||
final diaryEntity = diaryDay.toEntity();
|
||||
if (diaryEntity != null) {
|
||||
setDiary(diaryDay: diaryEntity, diaryId: diaryId);
|
||||
return true;
|
||||
await setDiary(diaryDay: diaryEntity, diaryId: diaryId, generation: request);
|
||||
return _isCurrentFetch(request);
|
||||
}
|
||||
|
||||
if (_isUnsavedDietPreview(diaryDay)) {
|
||||
return await setDiaryForDietOnly(diaryDay.toDiaryFutureDayEntity());
|
||||
final shown = await setDiaryForDietOnly(diaryDay.toDiaryFutureDayEntity(), generation: request);
|
||||
if (!_isCurrentFetch(request)) {
|
||||
return false;
|
||||
}
|
||||
if (!shown && state is DiaryStateLoading) {
|
||||
emit(currentState);
|
||||
}
|
||||
return shown;
|
||||
}
|
||||
emit(currentState);
|
||||
return false;
|
||||
} catch (e, st) {
|
||||
if (!_isCurrentFetch(request)) {
|
||||
return false;
|
||||
}
|
||||
await reportFrom(e, st, this);
|
||||
log(e.toString(), stackTrace: st);
|
||||
emit(DiaryState.error(errorMessage: e.toString()));
|
||||
@@ -129,17 +160,18 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<DiaryDayModel?> getDiary() async {
|
||||
Future<DiaryDayModel?> getDiary({DateTime? day}) async {
|
||||
final requestedDay = (day ?? selectedDay).toISO8601WithTimeZoneOffset();
|
||||
try {
|
||||
await diaryRepository.createDiaryDay(day: selectedDay.toISO8601WithTimeZoneOffset());
|
||||
final diaryDay = await diaryRepository.getDiaryDayV2(day: selectedDay.toISO8601WithTimeZoneOffset());
|
||||
await diaryRepository.createDiaryDay(day: requestedDay);
|
||||
final diaryDay = await diaryRepository.getDiaryDayV2(day: requestedDay);
|
||||
return diaryDay;
|
||||
} catch (e, st) {
|
||||
await reportFrom(e, st, this);
|
||||
log(e.toString(), stackTrace: st);
|
||||
if (e is DioException) {
|
||||
if (e.response?.statusCode == 422) {
|
||||
final diaryDay = await diaryRepository.getDiaryDayV2(day: selectedDay.toISO8601WithTimeZoneOffset());
|
||||
final diaryDay = await diaryRepository.getDiaryDayV2(day: requestedDay);
|
||||
return diaryDay;
|
||||
}
|
||||
}
|
||||
@@ -163,30 +195,24 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
return diaryDay.typeOfDiary.hasChosenDiet && dayNotStarted;
|
||||
}
|
||||
|
||||
Future<bool> setDiaryForDietOnly(DiaryFutureDietDayEntity diaryDay) async {
|
||||
Future<bool> setDiaryForDietOnly(DiaryFutureDietDayEntity diaryDay, {int? generation}) async {
|
||||
final dietId = diaryDay.diet?.id;
|
||||
final dayOfChosenDiet = diaryDay.dayOfChosenDiet;
|
||||
if (!diaryDay.typeOfDiary.hasChosenDiet || dietId == null || dayOfChosenDiet == null) {
|
||||
return false;
|
||||
}
|
||||
if (generation != null && !_isCurrentFetch(generation)) {
|
||||
return false;
|
||||
}
|
||||
emit(const DiaryState.loading());
|
||||
try {
|
||||
final diet = await dietRepository.getDiet(dietId);
|
||||
if (generation != null && !_isCurrentFetch(generation)) {
|
||||
return false;
|
||||
}
|
||||
final meals = diet.dailySchedule[dayOfChosenDiet];
|
||||
List<PortionModel> portionsOfMeal(int mealNumber) {
|
||||
return meals?[mealNumber]?.values
|
||||
.map(
|
||||
(elem) => PortionModel(
|
||||
id: 0,
|
||||
productName: elem.name,
|
||||
quantity: elem.quantity,
|
||||
portion: elem.portion.getValue(),
|
||||
fromDiet: true,
|
||||
portionState: PortionState.filledDiet,
|
||||
),
|
||||
)
|
||||
.toList() ??
|
||||
[];
|
||||
return meals?[mealNumber]?.values.map(_portionFromDiet).toList() ?? [];
|
||||
}
|
||||
|
||||
MealModel meal(int mealNumber) {
|
||||
@@ -202,13 +228,44 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
);
|
||||
return true;
|
||||
} catch (e, st) {
|
||||
if (generation != null && !_isCurrentFetch(generation)) {
|
||||
return false;
|
||||
}
|
||||
await reportFrom(e, st, this);
|
||||
emit(DiaryState.error(errorMessage: e.toString()));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setDiary({int? diaryId, required DiaryDayEntity diaryDay}) async {
|
||||
PortionModel _portionFromDiet(DailySchedule elem) {
|
||||
final nutrition = elem.dailyNutrition;
|
||||
final per100 = elem.portion == Portion.grams && elem.quantity > 0 ? 100 / elem.quantity : 1.0;
|
||||
return PortionModel(
|
||||
id: 0,
|
||||
productNutritionalValueId: elem.productNutritionalValueId,
|
||||
productCaloriesInfo: nutrition == null
|
||||
? null
|
||||
: ProductCaloriesInfoModel(
|
||||
id: elem.productNutritionalValueId ?? 0,
|
||||
productName: elem.name,
|
||||
calories: nutrition.energy * per100,
|
||||
proteins: nutrition.proteins * per100,
|
||||
fats: nutrition.fats * per100,
|
||||
carbohydrates: nutrition.carbohydrates * per100,
|
||||
energyOnPortion: nutrition.energy,
|
||||
proteinsOnPortion: nutrition.proteins,
|
||||
fatsOnPortion: nutrition.fats,
|
||||
carbohydratesOnPortion: nutrition.carbohydrates,
|
||||
),
|
||||
productName: elem.name,
|
||||
quantity: elem.quantity,
|
||||
portion: elem.portion.getValue(),
|
||||
fromDiet: true,
|
||||
portionState: PortionState.filledDiet,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> setDiary({int? diaryId, required DiaryDayEntity diaryDay, int? generation}) async {
|
||||
this.diaryId ??= diaryId;
|
||||
final isConsultantView = this.diaryId != null;
|
||||
try {
|
||||
@@ -236,6 +293,9 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
currentPlanDay = consultantReview.currentPlanDay;
|
||||
}
|
||||
final nutrition = await getNutrition(diaryDay.id);
|
||||
if (generation != null && !_isCurrentFetch(generation)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final isDiaryFinished = diaryDay.typeOfDiary.diaryState == DiaryStateEnum.finished || diaryDay.typeOfDiary.diaryState == DiaryStateEnum.lastDayFinished;
|
||||
|
||||
@@ -261,6 +321,9 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
),
|
||||
);
|
||||
} catch (e, st) {
|
||||
if (generation != null && !_isCurrentFetch(generation)) {
|
||||
return;
|
||||
}
|
||||
await reportFrom(e, st, this);
|
||||
log(e.toString(), stackTrace: st);
|
||||
emit(DiaryState.error(errorMessage: e.toString()));
|
||||
|
||||
@@ -22,12 +22,26 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
CalendarFormat _calendarFormat = CalendarFormat.week;
|
||||
DateTime _focusedDay = DateTime.now();
|
||||
DateTime _selectedDay = DateTime.now();
|
||||
DateTime? _dayFromState(DiaryState state) {
|
||||
if (state is DiaryStateLoaded) {
|
||||
return state.selectedDay;
|
||||
}
|
||||
if (state is DiaryStateLoadedTomorrowWithDiet) {
|
||||
return state.selectedDay;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final selected = _dayFromState(context.read<DiaryCubit>().state);
|
||||
if (widget.date != null) {
|
||||
_selectedDay = widget.date!;
|
||||
_focusedDay = widget.date!;
|
||||
} else if (selected != null) {
|
||||
_selectedDay = selected;
|
||||
_focusedDay = selected;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,9 +61,11 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
final daysOfWeekHeight = _scaledDaysOfWeekHeight(context);
|
||||
return BlocConsumer<DiaryCubit, DiaryState>(
|
||||
listener: (BuildContext context, DiaryState state) {
|
||||
if (state is DiaryStateLoaded) {
|
||||
final selected = _dayFromState(state);
|
||||
if (selected != null) {
|
||||
setState(() {
|
||||
_selectedDay = state.selectedDay;
|
||||
_selectedDay = selected;
|
||||
_focusedDay = selected;
|
||||
});
|
||||
}
|
||||
},
|
||||
@@ -77,8 +93,12 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
_selectedDay = selectedDay;
|
||||
_focusedDay = focusedDay;
|
||||
});
|
||||
final canOpenNextDay = await context.read<DiaryCubit>().setSelectedDay(selectedDay);
|
||||
if (!canOpenNextDay && context.mounted) {
|
||||
final cubit = context.read<DiaryCubit>();
|
||||
final canOpenNextDay = await cubit.setSelectedDay(selectedDay);
|
||||
if (!context.mounted || cubit.state is DiaryStateError) {
|
||||
return;
|
||||
}
|
||||
if (!canOpenNextDay) {
|
||||
showErrorDialog(context, title: s.want_to_see_tomorrow, description: s.can_not_see_next_day);
|
||||
setState(() {
|
||||
_selectedDay = previousSelectedDay;
|
||||
|
||||
@@ -61,6 +61,9 @@ class _VideoCallRecordingsViewState extends State<_VideoCallRecordingsView> {
|
||||
return;
|
||||
}
|
||||
final position = _scrollController.position;
|
||||
if (position.maxScrollExtent <= 0 || position.pixels < 0) {
|
||||
return;
|
||||
}
|
||||
if (position.pixels >= position.maxScrollExtent - 200) {
|
||||
context.read<VideoCallRecordingsCubit>().loadNextPage();
|
||||
}
|
||||
@@ -177,8 +180,8 @@ class _VideoCallRecordingsViewState extends State<_VideoCallRecordingsView> {
|
||||
return ListView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
children: [
|
||||
SizedBox(
|
||||
height: constraints.maxHeight,
|
||||
ConstrainedBox(
|
||||
constraints: BoxConstraints(minHeight: constraints.maxHeight),
|
||||
child: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
|
||||
@@ -19,7 +19,7 @@ class VideoCallRecordingFileService {
|
||||
final Dio dio;
|
||||
final PublicDownloadsSaver downloadsSaver;
|
||||
|
||||
/// Скачивает запись и сохраняет в `Загрузки/Itvio` (Android)
|
||||
/// Скачивает запись и сохраняет в `Загрузки/Eatvio` (Android)
|
||||
/// или Documents приложения (iOS, видно в Files).
|
||||
Future<String> downloadRecordingFile({
|
||||
required String fileUrl,
|
||||
|
||||
@@ -988,7 +988,7 @@ class MessageLookup extends MessageLookupByLibrary {
|
||||
"Could not download the recording. Please try again later."),
|
||||
"video_call_recordings_download_success":
|
||||
MessageLookupByLibrary.simpleMessage(
|
||||
"Recording saved to Downloads/Itvio"),
|
||||
"Recording saved to Downloads/Eatvio"),
|
||||
"video_call_recordings_download_success_ios":
|
||||
MessageLookupByLibrary.simpleMessage(
|
||||
"Recording saved to the Files app"),
|
||||
|
||||
@@ -592,7 +592,7 @@ class MessageLookup extends MessageLookupByLibrary {
|
||||
"Не удалось скачать запись. Попробуйте позже."),
|
||||
"video_call_recordings_download_success":
|
||||
MessageLookupByLibrary.simpleMessage(
|
||||
"Запись сохранена в Загрузки/Itvio"),
|
||||
"Запись сохранена в Загрузки/Eatvio"),
|
||||
"video_call_recordings_download_success_ios":
|
||||
MessageLookupByLibrary.simpleMessage(
|
||||
"Запись сохранена в приложении «Файлы»"),
|
||||
|
||||
@@ -6322,10 +6322,10 @@ class S {
|
||||
);
|
||||
}
|
||||
|
||||
/// `Recording saved to Downloads/Itvio`
|
||||
/// `Recording saved to Downloads/Eatvio`
|
||||
String get video_call_recordings_download_success {
|
||||
return Intl.message(
|
||||
'Recording saved to Downloads/Itvio',
|
||||
'Recording saved to Downloads/Eatvio',
|
||||
name: 'video_call_recordings_download_success',
|
||||
desc: '',
|
||||
args: [],
|
||||
|
||||
@@ -829,7 +829,7 @@
|
||||
}
|
||||
},
|
||||
"video_call_recordings_download_error": "Could not download the recording. Please try again later.",
|
||||
"video_call_recordings_download_success": "Recording saved to Downloads/Itvio",
|
||||
"video_call_recordings_download_success": "Recording saved to Downloads/Eatvio",
|
||||
"video_call_recordings_download_success_ios": "Recording saved to the Files app",
|
||||
"video_call_recordings_player_error": "Could not play the recording",
|
||||
"video_call_recordings_card_title": "Video consultation with a nutrition specialist",
|
||||
|
||||
@@ -584,7 +584,7 @@
|
||||
}
|
||||
},
|
||||
"video_call_recordings_download_error": "Не удалось скачать запись. Попробуйте позже.",
|
||||
"video_call_recordings_download_success": "Запись сохранена в Загрузки/Itvio",
|
||||
"video_call_recordings_download_success": "Запись сохранена в Загрузки/Eatvio",
|
||||
"video_call_recordings_download_success_ios": "Запись сохранена в приложении «Файлы»",
|
||||
"video_call_recordings_player_error": "Не удалось воспроизвести запись",
|
||||
"video_call_recordings_card_title": "Видеоконсультация со специалистом по питанию",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
name: eatvio_app
|
||||
description: A new Flutter project.
|
||||
version: 1.3.73+162
|
||||
version: 1.3.74+163
|
||||
publish_to: 'none'
|
||||
|
||||
environment:
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:eatvio_app/features/chat/repositories/chat_repository.dart';
|
||||
import 'package:eatvio_app/features/consultations/models/consultation_model.dart';
|
||||
@@ -1424,6 +1426,27 @@ void main() {
|
||||
name: 'Овсянка',
|
||||
portion: Portion.grams,
|
||||
quantity: 150,
|
||||
productNutritionalValueId: 42,
|
||||
dailyNutrition: DailyNutrition(
|
||||
energy: 195,
|
||||
proteins: 7,
|
||||
fats: 4,
|
||||
carbohydrates: 32,
|
||||
),
|
||||
),
|
||||
},
|
||||
2: {
|
||||
1: DailySchedule(
|
||||
name: 'Гречка',
|
||||
portion: Portion.pcs,
|
||||
quantity: 2,
|
||||
productNutritionalValueId: 7,
|
||||
dailyNutrition: DailyNutrition(
|
||||
energy: 220,
|
||||
proteins: 8,
|
||||
fats: 3,
|
||||
carbohydrates: 40,
|
||||
),
|
||||
),
|
||||
},
|
||||
},
|
||||
@@ -1448,8 +1471,267 @@ void main() {
|
||||
expect(result, true);
|
||||
expect(diaryCubit!.state, isA<DiaryStateLoadedTomorrowWithDiet>());
|
||||
final loaded = diaryCubit!.state as DiaryStateLoadedTomorrowWithDiet;
|
||||
expect(loaded.diary.meals.first.portions.single.productName, 'Овсянка');
|
||||
expect(loaded.diary.meals, hasLength(5));
|
||||
final oatmeal = loaded.diary.meals[0];
|
||||
expect(oatmeal.typeNumber, 1);
|
||||
expect(oatmeal.portions.single.productName, 'Овсянка');
|
||||
expect(oatmeal.portions.single.productNutritionalValueId, 42);
|
||||
expect(
|
||||
oatmeal.portions.single.productCaloriesInfo?.calories,
|
||||
closeTo(195 * 100 / 150, 0.001),
|
||||
);
|
||||
final buckwheat = loaded.diary.meals[1];
|
||||
expect(buckwheat.typeNumber, 2);
|
||||
expect(buckwheat.portions.single.productName, 'Гречка');
|
||||
expect(buckwheat.portions.single.productNutritionalValueId, 7);
|
||||
expect(buckwheat.portions.single.productCaloriesInfo?.energyOnPortion, 220);
|
||||
},
|
||||
);
|
||||
|
||||
test('fetchDiaryV2 - пустая диета не оставляет экран в загрузке', () async {
|
||||
diaryCubit = DiaryCubit(
|
||||
diaryRepository: mockDiaryRepository,
|
||||
glossaryRepository: mockGlossaryRepository,
|
||||
profileRepository: mockProfileRepository,
|
||||
dietRepository: mockDietRepository,
|
||||
chatRepository: mockChatRepository,
|
||||
settingsRepository: mockSettingsRepository,
|
||||
);
|
||||
when(
|
||||
mockDiaryRepository.createDiaryDay(day: anyNamed('day')),
|
||||
).thenAnswer((_) async {});
|
||||
when(
|
||||
mockDiaryRepository.getDiaryDayV2(day: anyNamed('day')),
|
||||
).thenAnswer((_) async => testDiaryDay);
|
||||
when(
|
||||
mockDiaryRepository.getNutritionData(any),
|
||||
).thenAnswer((_) async => testNutrition);
|
||||
when(mockSettingsRepository.getProfileSettings()).thenAnswer(
|
||||
(_) async => const ProfileSettingsModel(
|
||||
isMoodFillable: true,
|
||||
isBedtimeFillable: true,
|
||||
isPublicProfile: true,
|
||||
newUpdatesNotifications: null,
|
||||
diaryReminderNotifications: null,
|
||||
),
|
||||
);
|
||||
|
||||
expect(await diaryCubit!.setSelectedDay(DateTime(2024, 1, 15)), true);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 50));
|
||||
expect(diaryCubit!.state, isA<DiaryStateLoaded>());
|
||||
|
||||
when(mockDiaryRepository.getDiaryDayV2(day: anyNamed('day'))).thenAnswer(
|
||||
(_) async => const DiaryDayModel(
|
||||
id: null,
|
||||
date: '2024-01-20',
|
||||
closed: 0,
|
||||
comment: '',
|
||||
typeOfDiary: TypeOfDiaryModel(
|
||||
hasChosenDiet: true,
|
||||
hasChosenPlan: false,
|
||||
diaryState: null,
|
||||
),
|
||||
meals: [],
|
||||
chosenPlanId: null,
|
||||
dayOfChosenDiet: null,
|
||||
diet: null,
|
||||
),
|
||||
);
|
||||
|
||||
final result = await diaryCubit!.setSelectedDay(DateTime(2024, 1, 20));
|
||||
|
||||
expect(result, false);
|
||||
expect(diaryCubit!.state, isA<DiaryStateLoaded>());
|
||||
expect(diaryCubit!.selectedDay, DateTime(2024, 1, 15));
|
||||
});
|
||||
|
||||
test('setSelectedDay - ошибка диеты оставляет выбранный день для повтора', () async {
|
||||
diaryCubit = DiaryCubit(
|
||||
diaryRepository: mockDiaryRepository,
|
||||
glossaryRepository: mockGlossaryRepository,
|
||||
profileRepository: mockProfileRepository,
|
||||
dietRepository: mockDietRepository,
|
||||
chatRepository: mockChatRepository,
|
||||
settingsRepository: mockSettingsRepository,
|
||||
);
|
||||
const preview = DiaryDayModel(
|
||||
id: null,
|
||||
date: '2024-01-20',
|
||||
closed: 0,
|
||||
comment: '',
|
||||
typeOfDiary: TypeOfDiaryModel(
|
||||
hasChosenDiet: true,
|
||||
hasChosenPlan: false,
|
||||
diaryState: DiaryStateEnum.init,
|
||||
),
|
||||
meals: [],
|
||||
chosenPlanId: null,
|
||||
dayOfChosenDiet: 2,
|
||||
diet: DiaryDietModel(id: 1, duration: 7, name: 'Test Diet'),
|
||||
);
|
||||
when(
|
||||
mockDiaryRepository.createDiaryDay(day: anyNamed('day')),
|
||||
).thenAnswer((_) async {});
|
||||
when(
|
||||
mockDiaryRepository.getDiaryDayV2(day: anyNamed('day')),
|
||||
).thenAnswer((_) async => preview);
|
||||
when(mockDietRepository.getDiet(1)).thenThrow(Exception('network'));
|
||||
|
||||
final day = DateTime(2024, 1, 20);
|
||||
final result = await diaryCubit!.setSelectedDay(day);
|
||||
|
||||
expect(result, false);
|
||||
expect(diaryCubit!.state, isA<DiaryStateError>());
|
||||
expect(diaryCubit!.selectedDay, day);
|
||||
|
||||
when(mockDietRepository.getDiet(1)).thenAnswer(
|
||||
(_) async => const Diet(
|
||||
id: 1,
|
||||
slug: 'test-diet',
|
||||
name: 'Test Diet',
|
||||
image: '',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(),
|
||||
details: '',
|
||||
dailySchedule: {
|
||||
2: {
|
||||
1: {
|
||||
1: DailySchedule(
|
||||
name: 'Овсянка',
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
author: Author(
|
||||
authorSlug: 'test',
|
||||
authorName: 'Test',
|
||||
authorProfileIcon: '',
|
||||
authorIsMe: false,
|
||||
),
|
||||
totalLikes: 0,
|
||||
totalViews: 0,
|
||||
totalComments: 0,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
),
|
||||
);
|
||||
|
||||
final retry = await diaryCubit!.fetchDiaryV2(needLoading: true);
|
||||
|
||||
expect(retry, true);
|
||||
final loaded = diaryCubit!.state as DiaryStateLoadedTomorrowWithDiet;
|
||||
expect(loaded.diary.meals.first.portions.single.productName, 'Овсянка');
|
||||
expect(diaryCubit!.selectedDay, day);
|
||||
});
|
||||
|
||||
test('setSelectedDay - поздний ответ не затирает более новый день', () async {
|
||||
diaryCubit = DiaryCubit(
|
||||
diaryRepository: mockDiaryRepository,
|
||||
glossaryRepository: mockGlossaryRepository,
|
||||
profileRepository: mockProfileRepository,
|
||||
dietRepository: mockDietRepository,
|
||||
chatRepository: mockChatRepository,
|
||||
settingsRepository: mockSettingsRepository,
|
||||
);
|
||||
DiaryDayModel preview(String date, int dietId) {
|
||||
return DiaryDayModel(
|
||||
id: null,
|
||||
date: date,
|
||||
closed: 0,
|
||||
comment: '',
|
||||
typeOfDiary: const TypeOfDiaryModel(
|
||||
hasChosenDiet: true,
|
||||
hasChosenPlan: false,
|
||||
diaryState: DiaryStateEnum.init,
|
||||
),
|
||||
meals: [],
|
||||
chosenPlanId: null,
|
||||
dayOfChosenDiet: 1,
|
||||
diet: DiaryDietModel(id: dietId, duration: 7, name: 'Diet $dietId'),
|
||||
);
|
||||
}
|
||||
|
||||
Diet diet(int id, String product) {
|
||||
return Diet(
|
||||
id: id,
|
||||
slug: 'diet-$id',
|
||||
name: 'Diet $id',
|
||||
image: '',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: const DietType(),
|
||||
details: '',
|
||||
dailySchedule: {
|
||||
1: {
|
||||
1: {
|
||||
1: DailySchedule(
|
||||
name: product,
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
author: const Author(
|
||||
authorSlug: 'test',
|
||||
authorName: 'Test',
|
||||
authorProfileIcon: '',
|
||||
authorIsMe: false,
|
||||
),
|
||||
totalLikes: 0,
|
||||
totalViews: 0,
|
||||
totalComments: 0,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
);
|
||||
}
|
||||
|
||||
when(
|
||||
mockDiaryRepository.createDiaryDay(day: anyNamed('day')),
|
||||
).thenAnswer((_) async {});
|
||||
when(mockDiaryRepository.getDiaryDayV2(day: anyNamed('day'))).thenAnswer((
|
||||
invocation,
|
||||
) async {
|
||||
final day = invocation.namedArguments[const Symbol('day')] as String;
|
||||
if (day.startsWith('2024-01-20')) {
|
||||
return preview('2024-01-20', 1);
|
||||
}
|
||||
return preview('2024-01-21', 2);
|
||||
});
|
||||
final firstDiet = Completer<Diet>();
|
||||
final secondDiet = Completer<Diet>();
|
||||
var dietCalls = 0;
|
||||
when(mockDietRepository.getDiet(any)).thenAnswer((invocation) {
|
||||
dietCalls++;
|
||||
final id = invocation.positionalArguments.first as int;
|
||||
return (id == 1 ? firstDiet : secondDiet).future;
|
||||
});
|
||||
|
||||
Future<void> waitForDietCalls(int count) async {
|
||||
for (var i = 0; i < 50 && dietCalls < count; i++) {
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
}
|
||||
expect(dietCalls, greaterThanOrEqualTo(count));
|
||||
}
|
||||
|
||||
final firstSelection = diaryCubit!.setSelectedDay(DateTime(2024, 1, 20));
|
||||
await waitForDietCalls(1);
|
||||
final secondSelection = diaryCubit!.setSelectedDay(DateTime(2024, 1, 21));
|
||||
await waitForDietCalls(2);
|
||||
|
||||
secondDiet.complete(diet(2, 'Новый'));
|
||||
await secondSelection;
|
||||
firstDiet.complete(diet(1, 'Старый'));
|
||||
await firstSelection;
|
||||
|
||||
final loaded = diaryCubit!.state as DiaryStateLoadedTomorrowWithDiet;
|
||||
expect(loaded.diary.meals.first.portions.single.productName, 'Новый');
|
||||
expect(diaryCubit!.selectedDay, DateTime(2024, 1, 21));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -17,7 +17,10 @@ class _MockRecordingsRepository extends Mock implements VideoCallRecordingsRepos
|
||||
void main() {
|
||||
late _MockRecordingsRepository repository;
|
||||
|
||||
VideoCallRecordingsPagination page(List<VideoCallRecordingModel> data) {
|
||||
VideoCallRecordingsPagination page(
|
||||
List<VideoCallRecordingModel> data, {
|
||||
int lastPage = 1,
|
||||
}) {
|
||||
return VideoCallRecordingsPagination(
|
||||
data: data,
|
||||
links: const Links(
|
||||
@@ -29,7 +32,7 @@ void main() {
|
||||
meta: Meta(
|
||||
currentPage: 1,
|
||||
from: data.isEmpty ? null : 1,
|
||||
lastPage: 1,
|
||||
lastPage: lastPage,
|
||||
links: const [],
|
||||
perPage: 20,
|
||||
to: data.isEmpty ? null : data.length,
|
||||
@@ -92,6 +95,51 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('pull on a short list does not request the next page', (tester) async {
|
||||
await mockNetworkImagesFor(() async {
|
||||
when(() => repository.getRecordings(page: 1, period: null)).thenAnswer(
|
||||
(_) async => page([recording(1)], lastPage: 2),
|
||||
);
|
||||
when(() => repository.getRecordings(page: 2, period: null)).thenAnswer(
|
||||
(_) async => page([recording(2)], lastPage: 2),
|
||||
);
|
||||
|
||||
await tester.pumpWidget(wrap(const VideoCallRecordingsScreen()));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.fling(find.byType(ListView), const Offset(0, 400), 1200);
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
verifyNever(() => repository.getRecordings(page: 2, period: null));
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('empty state text stays visible at a large text scale', (tester) async {
|
||||
tester.view.physicalSize = const Size(320, 240);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
|
||||
when(() => repository.getRecordings(page: 1, period: null)).thenAnswer(
|
||||
(_) async => page(const []),
|
||||
);
|
||||
|
||||
await tester.pumpWidget(
|
||||
wrap(
|
||||
const MediaQuery(
|
||||
data: MediaQueryData(textScaler: TextScaler.linear(3)),
|
||||
child: VideoCallRecordingsScreen(),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Пока нет видеоконсультаций'), findsOneWidget);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
|
||||
testWidgets('empty list can be pulled to refresh', (tester) async {
|
||||
when(() => repository.getRecordings(page: 1, period: null)).thenAnswer(
|
||||
(_) async => page(const []),
|
||||
|
||||
@@ -14,6 +14,7 @@ class _MockDio extends Mock implements Dio {}
|
||||
class _FakeDownloadsSaver extends Fake implements PublicDownloadsSaver {
|
||||
String? lastSourcePath;
|
||||
String? lastFileName;
|
||||
String? lastSubfolder;
|
||||
|
||||
@override
|
||||
Future<String> saveToDownloads({
|
||||
@@ -24,7 +25,8 @@ class _FakeDownloadsSaver extends Fake implements PublicDownloadsSaver {
|
||||
}) async {
|
||||
lastSourcePath = sourcePath;
|
||||
lastFileName = fileName;
|
||||
return 'content://downloads/Itvio/$fileName';
|
||||
lastSubfolder = subfolder;
|
||||
return 'content://downloads/$subfolder/$fileName';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,7 +159,8 @@ void main() {
|
||||
);
|
||||
|
||||
expect(downloadsSaver.lastFileName, 'recording_1.mp4');
|
||||
expect(saved, contains('Itvio'));
|
||||
expect(downloadsSaver.lastSubfolder, 'Eatvio');
|
||||
expect(saved, contains('Eatvio'));
|
||||
expect(File(downloadsSaver.lastSourcePath!).existsSync(), isFalse);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user