feature(registration): add location endpoint with debounce

This commit is contained in:
mso7nueh
2024-07-28 18:39:36 +03:00
parent e472121bbc
commit 818721a993
13 changed files with 345 additions and 91 deletions

View File

@@ -15,8 +15,9 @@ class UserRegistrationSheet with _$UserRegistrationSheet {
@Default('accepted') @JsonKey(name: 'terms') String termsAccepted,
required String birthdate,
required String gender,
required String country,
required String city,
required String location,
@JsonKey(name: 'response_body')
required String responseBody,
required String lang,
required int weight,
required int height,

View File

@@ -34,8 +34,9 @@ mixin _$UserRegistrationSheet {
String get termsAccepted => throw _privateConstructorUsedError;
String get birthdate => throw _privateConstructorUsedError;
String get gender => throw _privateConstructorUsedError;
String get country => throw _privateConstructorUsedError;
String get city => throw _privateConstructorUsedError;
String get location => throw _privateConstructorUsedError;
@JsonKey(name: 'response_body')
String get responseBody => throw _privateConstructorUsedError;
String get lang => throw _privateConstructorUsedError;
int get weight => throw _privateConstructorUsedError;
int get height => throw _privateConstructorUsedError;
@@ -90,8 +91,8 @@ abstract class $UserRegistrationSheetCopyWith<$Res> {
@JsonKey(name: 'terms') String termsAccepted,
String birthdate,
String gender,
String country,
String city,
String location,
@JsonKey(name: 'response_body') String responseBody,
String lang,
int weight,
int height,
@@ -134,8 +135,8 @@ class _$UserRegistrationSheetCopyWithImpl<$Res,
Object? termsAccepted = null,
Object? birthdate = null,
Object? gender = null,
Object? country = null,
Object? city = null,
Object? location = null,
Object? responseBody = null,
Object? lang = null,
Object? weight = null,
Object? height = null,
@@ -191,13 +192,13 @@ class _$UserRegistrationSheetCopyWithImpl<$Res,
? _value.gender
: gender // ignore: cast_nullable_to_non_nullable
as String,
country: null == country
? _value.country
: country // ignore: cast_nullable_to_non_nullable
location: null == location
? _value.location
: location // ignore: cast_nullable_to_non_nullable
as String,
city: null == city
? _value.city
: city // ignore: cast_nullable_to_non_nullable
responseBody: null == responseBody
? _value.responseBody
: responseBody // ignore: cast_nullable_to_non_nullable
as String,
lang: null == lang
? _value.lang
@@ -290,8 +291,8 @@ abstract class _$$UserRegistrationSheetImplCopyWith<$Res>
@JsonKey(name: 'terms') String termsAccepted,
String birthdate,
String gender,
String country,
String city,
String location,
@JsonKey(name: 'response_body') String responseBody,
String lang,
int weight,
int height,
@@ -332,8 +333,8 @@ class __$$UserRegistrationSheetImplCopyWithImpl<$Res>
Object? termsAccepted = null,
Object? birthdate = null,
Object? gender = null,
Object? country = null,
Object? city = null,
Object? location = null,
Object? responseBody = null,
Object? lang = null,
Object? weight = null,
Object? height = null,
@@ -389,13 +390,13 @@ class __$$UserRegistrationSheetImplCopyWithImpl<$Res>
? _value.gender
: gender // ignore: cast_nullable_to_non_nullable
as String,
country: null == country
? _value.country
: country // ignore: cast_nullable_to_non_nullable
location: null == location
? _value.location
: location // ignore: cast_nullable_to_non_nullable
as String,
city: null == city
? _value.city
: city // ignore: cast_nullable_to_non_nullable
responseBody: null == responseBody
? _value.responseBody
: responseBody // ignore: cast_nullable_to_non_nullable
as String,
lang: null == lang
? _value.lang
@@ -483,8 +484,8 @@ class _$UserRegistrationSheetImpl implements _UserRegistrationSheet {
@JsonKey(name: 'terms') this.termsAccepted = 'accepted',
required this.birthdate,
required this.gender,
required this.country,
required this.city,
required this.location,
@JsonKey(name: 'response_body') required this.responseBody,
required this.lang,
required this.weight,
required this.height,
@@ -529,9 +530,10 @@ class _$UserRegistrationSheetImpl implements _UserRegistrationSheet {
@override
final String gender;
@override
final String country;
final String location;
@override
final String city;
@JsonKey(name: 'response_body')
final String responseBody;
@override
final String lang;
@override
@@ -583,7 +585,7 @@ class _$UserRegistrationSheetImpl implements _UserRegistrationSheet {
@override
String toString() {
return 'UserRegistrationSheet(name: $name, firstName: $firstName, lastName: $lastName, email: $email, password: $password, passwordConfirmation: $passwordConfirmation, termsAccepted: $termsAccepted, birthdate: $birthdate, gender: $gender, country: $country, city: $city, lang: $lang, weight: $weight, height: $height, chestCircumference: $chestCircumference, waistCircumference: $waistCircumference, hipCircumference: $hipCircumference, bloodPressureUp: $bloodPressureUp, bloodPressureDown: $bloodPressureDown, hasHypo: $hasHypo, hasHyper: $hasHyper, aboutAllergy: $aboutAllergy, aboutTakingPils: $aboutTakingPils, aboutSportHobbies: $aboutSportHobbies, favoriteFoods: $favoriteFoods, unfavoriteFoods: $unfavoriteFoods, additionalInfo: $additionalInfo, activityCoefficient: $activityCoefficient)';
return 'UserRegistrationSheet(name: $name, firstName: $firstName, lastName: $lastName, email: $email, password: $password, passwordConfirmation: $passwordConfirmation, termsAccepted: $termsAccepted, birthdate: $birthdate, gender: $gender, location: $location, responseBody: $responseBody, lang: $lang, weight: $weight, height: $height, chestCircumference: $chestCircumference, waistCircumference: $waistCircumference, hipCircumference: $hipCircumference, bloodPressureUp: $bloodPressureUp, bloodPressureDown: $bloodPressureDown, hasHypo: $hasHypo, hasHyper: $hasHyper, aboutAllergy: $aboutAllergy, aboutTakingPils: $aboutTakingPils, aboutSportHobbies: $aboutSportHobbies, favoriteFoods: $favoriteFoods, unfavoriteFoods: $unfavoriteFoods, additionalInfo: $additionalInfo, activityCoefficient: $activityCoefficient)';
}
@override
@@ -606,8 +608,10 @@ class _$UserRegistrationSheetImpl implements _UserRegistrationSheet {
(identical(other.birthdate, birthdate) ||
other.birthdate == birthdate) &&
(identical(other.gender, gender) || other.gender == gender) &&
(identical(other.country, country) || other.country == country) &&
(identical(other.city, city) || other.city == city) &&
(identical(other.location, location) ||
other.location == location) &&
(identical(other.responseBody, responseBody) ||
other.responseBody == responseBody) &&
(identical(other.lang, lang) || other.lang == lang) &&
(identical(other.weight, weight) || other.weight == weight) &&
(identical(other.height, height) || other.height == height) &&
@@ -653,8 +657,8 @@ class _$UserRegistrationSheetImpl implements _UserRegistrationSheet {
termsAccepted,
birthdate,
gender,
country,
city,
location,
responseBody,
lang,
weight,
height,
@@ -701,8 +705,8 @@ abstract class _UserRegistrationSheet implements UserRegistrationSheet {
@JsonKey(name: 'terms') final String termsAccepted,
required final String birthdate,
required final String gender,
required final String country,
required final String city,
required final String location,
@JsonKey(name: 'response_body') required final String responseBody,
required final String lang,
required final int weight,
required final int height,
@@ -752,9 +756,10 @@ abstract class _UserRegistrationSheet implements UserRegistrationSheet {
@override
String get gender;
@override
String get country;
String get location;
@override
String get city;
@JsonKey(name: 'response_body')
String get responseBody;
@override
String get lang;
@override

View File

@@ -18,8 +18,8 @@ _$UserRegistrationSheetImpl _$$UserRegistrationSheetImplFromJson(
termsAccepted: json['terms'] as String? ?? 'accepted',
birthdate: json['birthdate'] as String,
gender: json['gender'] as String,
country: json['country'] as String,
city: json['city'] as String,
location: json['location'] as String,
responseBody: json['response_body'] as String,
lang: json['lang'] as String,
weight: (json['weight'] as num).toInt(),
height: (json['height'] as num).toInt(),
@@ -52,8 +52,8 @@ Map<String, dynamic> _$$UserRegistrationSheetImplToJson(
'terms': instance.termsAccepted,
'birthdate': instance.birthdate,
'gender': instance.gender,
'country': instance.country,
'city': instance.city,
'location': instance.location,
'response_body': instance.responseBody,
'lang': instance.lang,
'weight': instance.weight,
'height': instance.height,

View File

@@ -0,0 +1,15 @@
import 'package:freezed_annotation/freezed_annotation.dart';
part 'location_model.g.dart';
part 'location_model.freezed.dart';
@freezed
class LocationModel with _$LocationModel {
const factory LocationModel({
required Map<String, dynamic> locations,
@JsonKey(name: 'response_body')
required String responseBody,
}) = _LocationModel;
factory LocationModel.fromJson(Map<String, Object?> json) => _$LocationModelFromJson(json);
}

View File

@@ -0,0 +1,189 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
part of 'location_model.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');
LocationModel _$LocationModelFromJson(Map<String, dynamic> json) {
return _LocationModel.fromJson(json);
}
/// @nodoc
mixin _$LocationModel {
Map<String, dynamic> get locations => throw _privateConstructorUsedError;
@JsonKey(name: 'response_body')
String get responseBody => throw _privateConstructorUsedError;
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
$LocationModelCopyWith<LocationModel> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $LocationModelCopyWith<$Res> {
factory $LocationModelCopyWith(
LocationModel value, $Res Function(LocationModel) then) =
_$LocationModelCopyWithImpl<$Res, LocationModel>;
@useResult
$Res call(
{Map<String, dynamic> locations,
@JsonKey(name: 'response_body') String responseBody});
}
/// @nodoc
class _$LocationModelCopyWithImpl<$Res, $Val extends LocationModel>
implements $LocationModelCopyWith<$Res> {
_$LocationModelCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
@pragma('vm:prefer-inline')
@override
$Res call({
Object? locations = null,
Object? responseBody = null,
}) {
return _then(_value.copyWith(
locations: null == locations
? _value.locations
: locations // ignore: cast_nullable_to_non_nullable
as Map<String, dynamic>,
responseBody: null == responseBody
? _value.responseBody
: responseBody // ignore: cast_nullable_to_non_nullable
as String,
) as $Val);
}
}
/// @nodoc
abstract class _$$LocationModelImplCopyWith<$Res>
implements $LocationModelCopyWith<$Res> {
factory _$$LocationModelImplCopyWith(
_$LocationModelImpl value, $Res Function(_$LocationModelImpl) then) =
__$$LocationModelImplCopyWithImpl<$Res>;
@override
@useResult
$Res call(
{Map<String, dynamic> locations,
@JsonKey(name: 'response_body') String responseBody});
}
/// @nodoc
class __$$LocationModelImplCopyWithImpl<$Res>
extends _$LocationModelCopyWithImpl<$Res, _$LocationModelImpl>
implements _$$LocationModelImplCopyWith<$Res> {
__$$LocationModelImplCopyWithImpl(
_$LocationModelImpl _value, $Res Function(_$LocationModelImpl) _then)
: super(_value, _then);
@pragma('vm:prefer-inline')
@override
$Res call({
Object? locations = null,
Object? responseBody = null,
}) {
return _then(_$LocationModelImpl(
locations: null == locations
? _value._locations
: locations // ignore: cast_nullable_to_non_nullable
as Map<String, dynamic>,
responseBody: null == responseBody
? _value.responseBody
: responseBody // ignore: cast_nullable_to_non_nullable
as String,
));
}
}
/// @nodoc
@JsonSerializable()
class _$LocationModelImpl implements _LocationModel {
const _$LocationModelImpl(
{required final Map<String, dynamic> locations,
@JsonKey(name: 'response_body') required this.responseBody})
: _locations = locations;
factory _$LocationModelImpl.fromJson(Map<String, dynamic> json) =>
_$$LocationModelImplFromJson(json);
final Map<String, dynamic> _locations;
@override
Map<String, dynamic> get locations {
if (_locations is EqualUnmodifiableMapView) return _locations;
// ignore: implicit_dynamic_type
return EqualUnmodifiableMapView(_locations);
}
@override
@JsonKey(name: 'response_body')
final String responseBody;
@override
String toString() {
return 'LocationModel(locations: $locations, responseBody: $responseBody)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$LocationModelImpl &&
const DeepCollectionEquality()
.equals(other._locations, _locations) &&
(identical(other.responseBody, responseBody) ||
other.responseBody == responseBody));
}
@JsonKey(ignore: true)
@override
int get hashCode => Object.hash(runtimeType,
const DeepCollectionEquality().hash(_locations), responseBody);
@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$LocationModelImplCopyWith<_$LocationModelImpl> get copyWith =>
__$$LocationModelImplCopyWithImpl<_$LocationModelImpl>(this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$$LocationModelImplToJson(
this,
);
}
}
abstract class _LocationModel implements LocationModel {
const factory _LocationModel(
{required final Map<String, dynamic> locations,
@JsonKey(name: 'response_body') required final String responseBody}) =
_$LocationModelImpl;
factory _LocationModel.fromJson(Map<String, dynamic> json) =
_$LocationModelImpl.fromJson;
@override
Map<String, dynamic> get locations;
@override
@JsonKey(name: 'response_body')
String get responseBody;
@override
@JsonKey(ignore: true)
_$$LocationModelImplCopyWith<_$LocationModelImpl> get copyWith =>
throw _privateConstructorUsedError;
}

View File

@@ -0,0 +1,19 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'location_model.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_$LocationModelImpl _$$LocationModelImplFromJson(Map<String, dynamic> json) =>
_$LocationModelImpl(
locations: json['locations'] as Map<String, dynamic>,
responseBody: json['response_body'] as String,
);
Map<String, dynamic> _$$LocationModelImplToJson(_$LocationModelImpl instance) =>
<String, dynamic>{
'locations': instance.locations,
'response_body': instance.responseBody,
};

View File

@@ -21,8 +21,8 @@ class RegistrationCubit extends Cubit<RegistrationState> {
passwordConfirmation: '',
birthdate: '',
gender: '',
country: '',
city: '',
location: '',
responseBody: '',
lang: '',
weight: 0,
height: 0,
@@ -54,4 +54,14 @@ class RegistrationCubit extends Cubit<RegistrationState> {
rethrow;
}
}
Future<List<String>> getLocationsByQuery(String query) async {
try {
final location = (await registrationRepository.getLocationsListByQuery(query));
emit(RegistrationState(sheet: state.sheet.copyWith(responseBody: location.responseBody)));
return location.locations.values.toList().map((e) => e.toString()).toList();
} catch (e) {
rethrow;
}
}
}

View File

@@ -1,8 +1,6 @@
import 'package:eatvio_app/core/app_export.dart';
import 'package:eatvio_app/data/models/userRegistrationModel/user_registration_model.dart';
import 'package:eatvio_app/features/registration/consts/cities_list.dart';
import 'package:eatvio_app/features/registration/presentation/bloc/registration_cubit/registration_cubit.dart';
import 'package:eatvio_app/widgets/custom_drop_down.dart';
import 'package:eatvio_app/widgets/custom_edit_text_form_field.dart';
import 'package:flutter/material.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
@@ -27,15 +25,8 @@ class _CityOfLivingScreenState extends State<CityOfLivingScreen> {
@override
void initState() {
super.initState();
if (widget.sheet.country.isNotEmpty) {
selectedItem = dropdownItemList.firstWhere((item) => item.title == widget.sheet.country);
} else {
selectedItem = dropdownItemList.first;
final registrationCubit = context.read<RegistrationCubit>();
registrationCubit.updateSheet(registrationCubit.state.sheet.copyWith(country: selectedItem.title));
}
if (widget.sheet.city.isNotEmpty) {
cityController.text = widget.sheet.city;
if (widget.sheet.location.isNotEmpty) {
cityController.text = widget.sheet.location;
}
}
@@ -46,39 +37,19 @@ class _CityOfLivingScreenState extends State<CityOfLivingScreen> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("country".tr),
SizedBox(height: 4),
CustomDropDown(
icon: Container(
margin: EdgeInsets.fromLTRB(30.h, 11.v, 17.h, 11.v),
decoration: BoxDecoration(borderRadius: BorderRadius.circular(1.h)),
child: CustomImageView(imagePath: ImageConstant.imgFavorite, height: 12.adaptSize, width: 12.adaptSize),
),
hintText: "Не выбрано".tr,
items: dropdownItemList,
value: selectedItem,
onChanged: (newValue) {
setState(() {
selectedItem = newValue;
});
final registrationCubit = context.read<RegistrationCubit>();
registrationCubit.updateSheet(registrationCubit.state.sheet.copyWith(country: newValue.title));
},
),
SizedBox(height: 16),
Text("Населенный пункт"),
Text("Местоположение"),
SizedBox(height: 4),
TypeAheadField(
debounceDuration: Duration.zero,
debounceDuration: Duration(milliseconds: 300),
controller: cityController,
builder: (context, controller, focus) => CustomTextFormField(
controller: cityController,
focusNode: focus,
textStyle: TextStyle(
fontSize: 16,
color: Colors.black,
)
),
controller: cityController,
focusNode: focus,
autofocus: true,
textStyle: TextStyle(
fontSize: 16,
color: Colors.black,
)),
itemBuilder: (BuildContext context, String item) {
return Padding(
padding: const EdgeInsets.all(8.0),
@@ -96,10 +67,14 @@ class _CityOfLivingScreenState extends State<CityOfLivingScreen> {
cityController.text = item;
});
final registrationCubit = context.read<RegistrationCubit>();
registrationCubit.updateSheet(registrationCubit.state.sheet.copyWith(city: item));
registrationCubit.updateSheet(registrationCubit.state.sheet.copyWith(location: item));
},
suggestionsCallback: (query) {
return citiesList.where((country) => country.toLowerCase().contains(query.toLowerCase())).toList();
suggestionsCallback: (query) async {
if (query.length >= 3) {
final registrationCubit = context.read<RegistrationCubit>();
return (await registrationCubit.getLocationsByQuery(query));
}
return null;
},
),
],

View File

@@ -244,7 +244,7 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
return false;
}
case 6:
if (state.sheet.country.isNotEmpty && state.sheet.city.isNotEmpty)
if (state.sheet.location.isNotEmpty)
return true;
else {
showDialog(

View File

@@ -1,4 +1,5 @@
import 'package:eatvio_app/data/models/userRegistrationModel/user_registration_model.dart';
import 'package:eatvio_app/features/registration/models/location_model.dart';
import 'package:eatvio_app/features/registration/services/registration_service.dart';
class RegistrationRepository {
@@ -13,4 +14,12 @@ class RegistrationRepository {
rethrow;
}
}
Future<LocationModel> getLocationsListByQuery(String query) async {
try {
return (await registrationService.getLocations(query: query));
} catch (e) {
rethrow;
}
}
}

View File

@@ -1,4 +1,5 @@
import 'package:dio/dio.dart';
import 'package:eatvio_app/features/registration/models/location_model.dart';
import 'package:retrofit/retrofit.dart';
part 'registration_service.g.dart';
@@ -11,4 +12,9 @@ abstract class RegistrationService {
Future<Map<String, String>> register({
@Body() required Map<String, dynamic> sheet,
});
@GET('/api/locations')
Future<LocationModel> getLocations({
@Query('user_address') required String query,
});
}

View File

@@ -47,6 +47,33 @@ class _RegistrationService implements RegistrationService {
return _value;
}
@override
Future<LocationModel> getLocations({required String query}) async {
final _extra = <String, dynamic>{};
final queryParameters = <String, dynamic>{r'user_address': query};
final _headers = <String, dynamic>{};
const Map<String, dynamic>? _data = null;
final _result = await _dio
.fetch<Map<String, dynamic>>(_setStreamType<LocationModel>(Options(
method: 'GET',
headers: _headers,
extra: _extra,
)
.compose(
_dio.options,
'/api/locations',
queryParameters: queryParameters,
data: _data,
)
.copyWith(
baseUrl: _combineBaseUrls(
_dio.options.baseUrl,
baseUrl,
))));
final _value = LocationModel.fromJson(_result.data!);
return _value;
}
RequestOptions _setStreamType<T>(RequestOptions requestOptions) {
if (T != dynamic &&
!(requestOptions.responseType == ResponseType.bytes ||

View File

@@ -72,9 +72,7 @@ class BlogPostCreateBloc extends Bloc<BlogPostCreateEvent, BlogPostCreateState>
if (response.statusCode == 200 || response.statusCode == 201) {
AppRouter.navigatorKey.currentContext?.goNamed('feedScreen');
} else {
print(
'Failed to create blog post. Status Code: ${response.statusCode} ${response.data}',
);
print('Failed to create blog post. Status Code: ${response.statusCode} ${response.data}');
}
}
}
@@ -85,7 +83,7 @@ class BlogPostCreateBloc extends Bloc<BlogPostCreateEvent, BlogPostCreateState>
FormData formData = FormData.fromMap({
'title': title,
'description': description,
'videoUrl': youtubeURL,
'youtube_url': youtubeURL,
'state': 'active',
'image': await MultipartFile.fromFile(image!.path, filename: image.name),
});