add little bit of localization, add SharedPreferences for likes, add change locale button

This commit is contained in:
shirotame 2024-10-06 18:19:38 +04:00
parent 9cb990d7ac
commit 011fbbd05f
30 changed files with 566 additions and 130 deletions

View File

@ -42,4 +42,5 @@
<data android:mimeType="text/plain"/>
</intent>
</queries>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>

View File

@ -1,12 +1,21 @@
{
"@@locale": "en",
"appBarTitle": "Anime List",
"search": "Search",
"liked": "You liked",
"unliked": "Like removed from",
"errorOccured": "Error occured",
"noErrorMsg": "No message provided",
"retry": "Retry",
"unknown": "Unknown",
"apiYear": "Year",
"apiType": "Type",
"apiRating": "Rating",
"apiDesc": "",
"apiNoDesc": "No description provided",
"arbEnding": "t"
}

View File

@ -1,12 +1,21 @@
{
"@@locale": "ru",
"appBarTitle": "Список аниме",
"search": "Поиск",
"liked": "Вы лайкнули",
"unliked": "Лайк снят с",
"errorOccured": "Произошла ошибка",
"noErrorMsg": "Нет сообщения",
"retry": "Повторить",
"unknown": "Неизвестно",
"apiYear": "Год",
"apiType": "Тип",
"apiRating": "Рейтинг",
"apiDesc": "(Описание доступно только на английском языке)",
"apiNoDesc": "Нет описания",
"arbEnding": "t"
}

View File

@ -95,6 +95,12 @@ abstract class AppLocale {
Locale('ru')
];
/// No description provided for @appBarTitle.
///
/// In ru, this message translates to:
/// **'Список аниме'**
String get appBarTitle;
/// No description provided for @search.
///
/// In ru, this message translates to:
@ -131,6 +137,42 @@ abstract class AppLocale {
/// **'Повторить'**
String get retry;
/// No description provided for @unknown.
///
/// In ru, this message translates to:
/// **'Неизвестно'**
String get unknown;
/// No description provided for @apiYear.
///
/// In ru, this message translates to:
/// **'Год'**
String get apiYear;
/// No description provided for @apiType.
///
/// In ru, this message translates to:
/// **'Тип'**
String get apiType;
/// No description provided for @apiRating.
///
/// In ru, this message translates to:
/// **'Рейтинг'**
String get apiRating;
/// No description provided for @apiDesc.
///
/// In ru, this message translates to:
/// **'(Описание доступно только на английском языке)'**
String get apiDesc;
/// No description provided for @apiNoDesc.
///
/// In ru, this message translates to:
/// **'Нет описания'**
String get apiNoDesc;
/// No description provided for @arbEnding.
///
/// In ru, this message translates to:

View File

@ -6,6 +6,9 @@ import 'app_localizations.dart';
class AppLocaleEn extends AppLocale {
AppLocaleEn([String locale = 'en']) : super(locale);
@override
String get appBarTitle => 'Anime List';
@override
String get search => 'Search';
@ -24,6 +27,24 @@ class AppLocaleEn extends AppLocale {
@override
String get retry => 'Retry';
@override
String get unknown => 'Unknown';
@override
String get apiYear => 'Year';
@override
String get apiType => 'Type';
@override
String get apiRating => 'Rating';
@override
String get apiDesc => '';
@override
String get apiNoDesc => 'No description provided';
@override
String get arbEnding => 't';
}

View File

@ -6,6 +6,9 @@ import 'app_localizations.dart';
class AppLocaleRu extends AppLocale {
AppLocaleRu([String locale = 'ru']) : super(locale);
@override
String get appBarTitle => 'Список аниме';
@override
String get search => 'Поиск';
@ -24,6 +27,24 @@ class AppLocaleRu extends AppLocale {
@override
String get retry => 'Повторить';
@override
String get unknown => 'Неизвестно';
@override
String get apiYear => 'Год';
@override
String get apiType => 'Тип';
@override
String get apiRating => 'Рейтинг';
@override
String get apiDesc => '(Описание доступно только на английском языке)';
@override
String get apiNoDesc => 'Нет описания';
@override
String get arbEnding => 't';
}

View File

@ -8,7 +8,8 @@ class AnimesDto {
final PaginationDto? pagination;
const AnimesDto({this.data, this.pagination});
factory AnimesDto.fromJson(Map<String, dynamic> json) => _$AnimesDtoFromJson(json);
factory AnimesDto.fromJson(Map<String, dynamic> json) =>
_$AnimesDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
@ -22,11 +23,14 @@ class PaginationDto {
const PaginationDto({this.currentPage, this.hasNextPage, this.lastPage});
factory PaginationDto.fromJson(Map<String, dynamic> json) => _$PaginationDtoFromJson(json);
factory PaginationDto.fromJson(Map<String, dynamic> json) =>
_$PaginationDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class AnimeDto {
@JsonKey(name: "mal_id")
final int? id;
final String? title;
final int? year;
final String? type;
@ -34,9 +38,17 @@ class AnimeDto {
final String? rating;
final ImagesDto? images;
const AnimeDto({this.title, this.rating, this.synopsis, this.type, this.year, this.images});
const AnimeDto(
{this.id,
this.title,
this.rating,
this.synopsis,
this.type,
this.year,
this.images});
factory AnimeDto.fromJson(Map<String, dynamic> json) => _$AnimeDtoFromJson(json);
factory AnimeDto.fromJson(Map<String, dynamic> json) =>
_$AnimeDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
@ -45,7 +57,8 @@ class ImagesDto {
const ImagesDto({this.jpg});
factory ImagesDto.fromJson(Map<String, dynamic> json) => _$ImagesDtoFromJson(json);
factory ImagesDto.fromJson(Map<String, dynamic> json) =>
_$ImagesDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
@ -55,5 +68,6 @@ class ImageDto {
const ImageDto({this.imageUrl});
factory ImageDto.fromJson(Map<String, dynamic> json) => _$ImageDtoFromJson(json);
factory ImageDto.fromJson(Map<String, dynamic> json) =>
_$ImageDtoFromJson(json);
}

View File

@ -23,6 +23,7 @@ PaginationDto _$PaginationDtoFromJson(Map<String, dynamic> json) =>
);
AnimeDto _$AnimeDtoFromJson(Map<String, dynamic> json) => AnimeDto(
id: (json['mal_id'] as num?)?.toInt(),
title: json['title'] as String?,
rating: json['rating'] as String?,
synopsis: json['synopsis'] as String?,

View File

@ -6,15 +6,18 @@ import '../dtos/animes_dto.dart';
extension AnimesMapper on AnimesDto {
HomeData toDomain() => HomeData(
data: data?.map((dto) => dto.toDomain()).toList(),
nextPage: (pagination?.hasNextPage ?? false) ? ((pagination?.currentPage ?? 0) + 1) : null);
nextPage: (pagination?.hasNextPage ?? false)
? ((pagination?.currentPage ?? 0) + 1)
: null);
}
extension AnimeMapper on AnimeDto {
CardData toDomain() => CardData(
id: id.toString(),
name: title ?? "",
imageUrl: images?.jpg?.imageUrl ?? "placeholder.co/250",
descr:
"Rating: ${rating ?? "unknown"}\nYear: ${year ?? "unknown"}\nType: ${type ?? "unknown"}.\n\n${synopsis ?? "No description provided"} ",
cuttedDescr:
"Rating: ${rating ?? "unknown"}\nYear: ${year ?? "unknown"}\nType: ${type ?? "unknown"}");
type: type,
year: year,
descr: synopsis,
rating: rating);
}

View File

@ -1,10 +1,10 @@
import 'dart:developer';
import 'package:dio/dio.dart';
import 'package:flutter_project/components/utils/error_callback.dart';
import 'package:flutter_project/data/mappers/animes_mapper.dart';
import 'package:flutter_project/data/repositories/api_interface.dart';
import '../../components/utils/error_callback.dart';
import '../../domain/models/home.dart';
import '../dtos/animes_dto.dart';
@ -15,14 +15,22 @@ class AnimeRepository extends ApiInterface {
@override
Future<HomeData?> loadData(
{OnErrorCallback onError, String? q, int page = 1, int pageSize = 25}) async {
{OnErrorCallback onError,
String? q,
int page = 1,
int pageSize = 25}) async {
try {
const String url = "$_baseUrl/v4/anime?sfw";
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url,
queryParameters: {'q': q, 'page': page, 'limit': !(pageSize > 25) ? pageSize : 25});
final Response<dynamic> response = await _dio
.get<Map<dynamic, dynamic>>(url, queryParameters: {
'q': q,
'page': page,
'limit': !(pageSize > 25) ? pageSize : 25
});
final AnimesDto dto = AnimesDto.fromJson(response.data as Map<String, dynamic>);
final AnimesDto dto =
AnimesDto.fromJson(response.data as Map<String, dynamic>);
final HomeData data = dto.toDomain();

View File

@ -12,21 +12,24 @@ class MockRepository extends ApiInterface {
CardData(
name: "Test",
imageUrl: "https://loremflickr.com/250/150/cat",
descr: "Description",
cuttedDescr: "cutted",
),
descr: "descr",
type: "Type",
year: 2024,
rating: "R"),
CardData(
name: "Test 2",
imageUrl: "https://loremflickr.com/200/250/cat",
descr: "Description",
cuttedDescr: "cutted",
),
descr: "descr",
type: "Type",
year: 2024,
rating: "R"),
CardData(
name: "Test 3",
imageUrl: "https://loremflickr.com/200/200/cat",
descr: "Description",
cuttedDescr: "cutted",
),
descr: "descr",
type: "Type",
year: 2024,
rating: "R"),
],
);
}

View File

@ -1,9 +1,18 @@
class CardData {
final String name;
final String imageUrl;
final String descr;
final String cuttedDescr;
final String? type;
final int? year;
final String? rating;
final String? descr;
final String? id;
const CardData(
{required this.name, required this.imageUrl, required this.descr, required this.cuttedDescr});
{required this.name,
required this.imageUrl,
required this.type,
required this.year,
required this.rating,
required this.descr,
this.id});
}

View File

@ -1,9 +1,14 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_project/components/locale/l10n/app_localizations.dart';
import 'package:flutter_project/data/repositories/anime_repository.dart';
import 'package:flutter_project/views/home_page/bloc/bloc.dart';
import 'package:flutter_project/views/home_page/home_bloc/bloc.dart';
import 'package:flutter_project/views/home_page/home_page.dart';
import 'package:flutter_project/views/like_bloc/like_bloc.dart';
import 'package:flutter_project/views/locale_bloc/locale_bloc.dart';
import 'package:flutter_project/views/locale_bloc/locale_state.dart';
void main() {
runApp(const MyApp());
@ -14,21 +19,31 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return RepositoryProvider<AnimeRepository>(
lazy: true,
create: (_) => AnimeRepository(),
child: BlocProvider<HomeBloc>(
return BlocProvider<LikeBloc>(
lazy: false,
create: (context) => HomeBloc(context.read<AnimeRepository>()),
child: MaterialApp(
create: (context) => LikeBloc(),
child: BlocProvider<LocaleBloc>(
lazy: false,
create: (context) => LocaleBloc(Locale(Platform.localeName)),
child: BlocBuilder<LocaleBloc, LocaleState>(
builder: (context, state) => MaterialApp(
title: 'Anime list',
locale: state.currentLocale,
localizationsDelegates: AppLocale.localizationsDelegates,
supportedLocales: AppLocale.supportedLocales,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.lightGreen),
useMaterial3: true,
),
home: const HomePage(),
home: RepositoryProvider<AnimeRepository>(
lazy: true,
create: (_) => AnimeRepository(),
child: BlocProvider<HomeBloc>(
lazy: false,
create: (context) =>
HomeBloc(context.read<AnimeRepository>()),
child: const HomePage())),
),
),
),
);

View File

@ -1,11 +1,13 @@
import 'package:flutter/material.dart';
import 'package:flutter_project/components/locale/l10n/app_localizations.dart';
import '../../domain/models/card.dart';
class DetailsPage extends StatelessWidget {
final AppLocale locale;
final CardData data;
const DetailsPage(this.data, {super.key});
const DetailsPage(this.locale, this.data, {super.key});
@override
Widget build(BuildContext context) {
@ -38,7 +40,7 @@ class DetailsPage extends StatelessWidget {
Padding(
padding: const EdgeInsets.all(10),
child: Text(
data.descr,
'${locale.apiYear}: ${data.year ?? locale.unknown}, ${locale.apiType}: ${data.type ?? locale.unknown}, ${locale.apiRating}: ${data.rating ?? locale.unknown}\n${locale.apiDesc != "" ? "\n${locale.apiDesc}\n" : ""}\n${data.descr ?? locale.apiNoDesc}',
style: Theme.of(context).textTheme.bodyLarge,
),
)

View File

@ -1,36 +1,49 @@
part of 'home_page.dart';
typedef onLikeCallback = void Function(String title, bool isLiked)?;
typedef onLikeCallback = void Function(String? id, String title, bool isLiked)?;
class _Card extends StatefulWidget {
class _Card extends StatelessWidget {
final AppLocale locale;
final String name;
final String imageUrl;
final String descr;
final String? type;
final int? year;
final String? rating;
final String? id;
final onLikeCallback onLike;
final VoidCallback? onTap;
final bool isLiked;
const _Card(
{required this.name, required this.imageUrl, this.onLike, this.onTap, required this.descr});
{required this.locale,
required this.name,
required this.imageUrl,
required this.type,
required this.year,
required this.rating,
this.id,
this.onLike,
this.onTap,
this.isLiked = false});
factory _Card.withData(CardData d, {onLikeCallback onLike, VoidCallback? onTap}) => _Card(
factory _Card.withData(AppLocale locale, CardData d,
{onLikeCallback onLike, VoidCallback? onTap, bool isLiked = false}) =>
_Card(
locale: locale,
name: d.name,
imageUrl: d.imageUrl,
onLike: onLike,
onTap: onTap,
descr: d.cuttedDescr,
);
@override
State<_Card> createState() => _CardState();
}
class _CardState extends State<_Card> {
bool isLiked = false;
type: d.type,
year: d.year,
rating: d.rating,
isLiked: isLiked,
id: d.id);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: widget.onTap,
onTap: onTap,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
@ -51,14 +64,15 @@ class _CardState extends State<_Card> {
children: [
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20), bottomLeft: Radius.circular(20)),
topLeft: Radius.circular(20),
bottomLeft: Radius.circular(20)),
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: 200),
child: SizedBox(
width: 150,
height: double.infinity,
child: Image.network(
widget.imageUrl,
imageUrl,
fit: BoxFit.cover,
),
),
@ -71,11 +85,11 @@ class _CardState extends State<_Card> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.name,
name,
style: Theme.of(context).textTheme.headlineSmall,
),
Text(
widget.descr,
'${locale.apiYear}: ${year ?? locale.unknown}, ${locale.apiType}: ${type ?? locale.unknown}, ${locale.apiRating}: ${rating ?? locale.unknown}',
style: Theme.of(context).textTheme.labelSmall,
),
],
@ -87,12 +101,7 @@ class _CardState extends State<_Card> {
Padding(
padding: const EdgeInsets.only(top: 16.0, right: 8.0),
child: GestureDetector(
onTap: () {
setState(() {
isLiked = !isLiked;
});
widget.onLike?.call(widget.name, isLiked);
},
onTap: () => onLike?.call(id, name, isLiked),
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 150),
child: isLiked

View File

@ -1,7 +1,7 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_project/data/repositories/anime_repository.dart';
import 'package:flutter_project/views/home_page/bloc/events.dart';
import 'package:flutter_project/views/home_page/bloc/state.dart';
import 'package:flutter_project/views/home_page/home_bloc/events.dart';
import 'package:flutter_project/views/home_page/home_bloc/state.dart';
class HomeBloc extends Bloc<HomeEvent, HomeState> {
final AnimeRepository repo;
@ -11,7 +11,8 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
on<HomeShowButtonToTopEvent>(_onShowButton);
}
void _onShowButton(HomeShowButtonToTopEvent event, Emitter<HomeState> emit) {
Future<void> _onShowButton(
HomeShowButtonToTopEvent event, Emitter<HomeState> emit) async {
if (event.isShown != null) {
if (event.isShown == true) {
emit(state.copyWith(isButtonToTopShown: true));
@ -21,7 +22,8 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
}
}
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
Future<void> _onLoadData(
HomeLoadDataEvent event, Emitter<HomeState> emit) async {
if (event.hasError != null && event.hasError == true) {
emit(state.copyWith(error: null));
}
@ -34,13 +36,17 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
String? error;
final data =
await repo.loadData(q: event.search, page: event.nextPage ?? 1, onError: (e) => error = e);
final data = await repo.loadData(
q: event.search, page: event.nextPage ?? 1, onError: (e) => error = e);
if (event.nextPage != null) {
data?.data?.insertAll(0, state.data?.data ?? []);
}
emit(state.copyWith(data: data, isLoading: false, isPaginationLoading: false, error: error));
emit(state.copyWith(
data: data,
isLoading: false,
isPaginationLoading: false,
error: error));
}
}

View File

@ -2,14 +2,21 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_project/components/extensions/context_x.dart';
import 'package:flutter_project/components/locale/l10n/app_localizations.dart';
import 'package:flutter_project/components/utils/debounce.dart';
import 'package:flutter_project/domain/models/card.dart';
import 'package:flutter_project/views/common/svg_objects.dart';
import 'package:flutter_project/views/details_page/details_page.dart';
import 'package:flutter_project/views/home_page/bloc/events.dart';
import 'package:flutter_project/views/home_page/bloc/state.dart';
import 'package:flutter_project/views/home_page/home_bloc/events.dart';
import 'package:flutter_project/views/home_page/home_bloc/state.dart';
import 'package:flutter_project/views/like_bloc/like_events.dart';
import 'package:flutter_project/views/like_bloc/like_state.dart';
import 'package:flutter_project/views/locale_bloc/locale_bloc.dart';
import 'package:flutter_project/views/locale_bloc/locale_events.dart';
import 'package:flutter_project/views/locale_bloc/locale_state.dart';
import 'bloc/bloc.dart';
import '../like_bloc/like_bloc.dart';
import 'home_bloc/bloc.dart';
part 'card.dart';
@ -29,7 +36,7 @@ class _HomePageState extends State<HomePage> {
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Center(
child: Text(
'Anime list',
context.locale.appBarTitle,
),
),
),
@ -53,8 +60,10 @@ class _BodyState extends State<Body> {
void initState() {
SvgObjects.init();
WidgetsBinding.instance.addPostFrameCallback(
(_) => context.read<HomeBloc>().add(const HomeLoadDataEvent()));
WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<HomeBloc>().add(const HomeLoadDataEvent());
context.read<LikeBloc>().add(const LoadLikesEvent());
});
scrollController.addListener(_viewListScrollListener);
super.initState();
@ -72,8 +81,11 @@ class _BodyState extends State<Body> {
return Stack(children: [
Column(
children: [
Padding(
padding: EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
Row(
children: [
Expanded(
child: Padding(
padding: EdgeInsets.only(left: 16, top: 10, bottom: 10),
child: Center(
child: CupertinoSearchTextField(
placeholder: context.locale.search,
@ -86,6 +98,24 @@ class _BodyState extends State<Body> {
),
),
),
),
GestureDetector(
onTap: () =>
context.read<LocaleBloc>().add(const ChangeLocaleEvent()),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: BlocBuilder<LocaleBloc, LocaleState>(
builder: (context, state) {
return AnimatedSwitcher(
duration: const Duration(milliseconds: 250),
child: state.currentLocale.languageCode == 'ru'
? const SvgRu(key: ValueKey<int>(1))
: const SvgUs(key: ValueKey<int>(0)));
}),
),
)
],
),
BlocConsumer<HomeBloc, HomeState>(
listener: (context, state) {
if (state.error != null) {
@ -94,7 +124,8 @@ class _BodyState extends State<Body> {
},
builder: (context, state) => state.isLoading
? const CircularProgressIndicator()
: Expanded(
: BlocBuilder<LikeBloc, LikeState>(
builder: (context, likeState) => Expanded(
child: RefreshIndicator(
onRefresh: _onRefresh,
child: ListView.builder(
@ -105,15 +136,18 @@ class _BodyState extends State<Body> {
final data = state.data?.data?[index];
return data != null
? _Card.withData(data,
onLike: (title, isLiked) =>
_showSnackBar(context, isLiked, title),
? _Card.withData(context.locale, data,
isLiked:
likeState.likedIds?.contains(data.id) ==
true,
onLike: _onLike,
onTap: () => _navToDetails(context, data))
: const SizedBox.shrink();
},
),
)),
),
),
BlocBuilder<HomeBloc, HomeState>(
builder: (context, state) => state.isPaginationLoading
? const CircularProgressIndicator()
@ -134,6 +168,13 @@ class _BodyState extends State<Body> {
]);
}
void _onLike(String? id, String title, bool isLiked) {
if (id != null) {
context.read<LikeBloc>().add(ChangeLikeEvent(id));
_showSnackBar(context, !isLiked, title);
}
}
void _onErrorShowDialog(BuildContext context, HomeState state) {
showCupertinoDialog(
context: context,
@ -207,6 +248,8 @@ class _BodyState extends State<Body> {
void _navToDetails(BuildContext context, CardData d) {
Navigator.push(
context, CupertinoPageRoute(builder: (context) => DetailsPage(d)));
context,
CupertinoPageRoute(
builder: (context) => DetailsPage(context.locale, d)));
}
}

View File

@ -0,0 +1,37 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_project/views/like_bloc/like_events.dart';
import 'package:flutter_project/views/like_bloc/like_state.dart';
import 'package:shared_preferences/shared_preferences.dart';
const String _likedPrefsKey = 'liked';
class LikeBloc extends Bloc<LikeEvent, LikeState> {
LikeBloc() : super(const LikeState(likedIds: [])) {
on<ChangeLikeEvent>(_onChangeLike);
on<LoadLikesEvent>(_onLoadLikes);
}
Future<void> _onLoadLikes(
LoadLikesEvent event, Emitter<LikeState> emit) async {
final prefs = await SharedPreferences.getInstance();
final data = prefs.getStringList(_likedPrefsKey);
emit(state.copyWith(likedIds: data));
}
Future<void> _onChangeLike(
ChangeLikeEvent event, Emitter<LikeState> emit) async {
final updatedList = List<String>.from(state.likedIds ?? []);
if (updatedList.contains(event.id)) {
updatedList.remove(event.id);
} else {
updatedList.add(event.id);
}
final prefs = await SharedPreferences.getInstance();
prefs.setStringList(_likedPrefsKey, updatedList);
emit(state.copyWith(likedIds: updatedList));
}
}

View File

@ -0,0 +1,12 @@
abstract class LikeEvent {
const LikeEvent();
}
class LoadLikesEvent extends LikeEvent {
const LoadLikesEvent();
}
class ChangeLikeEvent extends LikeEvent {
final String id;
const ChangeLikeEvent(this.id);
}

View File

@ -0,0 +1,14 @@
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:equatable/equatable.dart';
part 'like_state.g.dart';
@CopyWith()
class LikeState extends Equatable {
final List<String>? likedIds;
const LikeState({this.likedIds});
@override
List<Object?> get props => [likedIds];
}

View File

@ -0,0 +1,56 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'like_state.dart';
// **************************************************************************
// CopyWithGenerator
// **************************************************************************
abstract class _$LikeStateCWProxy {
LikeState likedIds(List<String>? likedIds);
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `LikeState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
///
/// Usage
/// ```dart
/// LikeState(...).copyWith(id: 12, name: "My name")
/// ````
LikeState call({
List<String>? likedIds,
});
}
/// Proxy class for `copyWith` functionality. This is a callable class and can be used as follows: `instanceOfLikeState.copyWith(...)`. Additionally contains functions for specific fields e.g. `instanceOfLikeState.copyWith.fieldName(...)`
class _$LikeStateCWProxyImpl implements _$LikeStateCWProxy {
const _$LikeStateCWProxyImpl(this._value);
final LikeState _value;
@override
LikeState likedIds(List<String>? likedIds) => this(likedIds: likedIds);
@override
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `LikeState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
///
/// Usage
/// ```dart
/// LikeState(...).copyWith(id: 12, name: "My name")
/// ````
LikeState call({
Object? likedIds = const $CopyWithPlaceholder(),
}) {
return LikeState(
likedIds: likedIds == const $CopyWithPlaceholder()
? _value.likedIds
// ignore: cast_nullable_to_non_nullable
: likedIds as List<String>?,
);
}
}
extension $LikeStateCopyWith on LikeState {
/// Returns a callable class that can be used as follows: `instanceOfLikeState.copyWith(...)` or like so:`instanceOfLikeState.copyWith.fieldName(...)`.
// ignore: library_private_types_in_public_api
_$LikeStateCWProxy get copyWith => _$LikeStateCWProxyImpl(this);
}

View File

@ -0,0 +1,21 @@
import 'dart:ui';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_project/components/locale/l10n/app_localizations.dart';
import 'locale_events.dart';
import 'locale_state.dart';
class LocaleBloc extends Bloc<LocaleEvent, LocaleState> {
LocaleBloc(Locale defaultLocale)
: super(LocaleState(currentLocale: defaultLocale)) {
on<ChangeLocaleEvent>(_onChangeLocale);
}
Future<void> _onChangeLocale(
ChangeLocaleEvent event, Emitter<LocaleState> emit) async {
final toChange = AppLocale.supportedLocales.firstWhere(
(loc) => loc.languageCode != state.currentLocale.languageCode);
emit(state.copyWith(currentLocale: toChange));
}
}

View File

@ -0,0 +1,7 @@
abstract class LocaleEvent {
const LocaleEvent();
}
class ChangeLocaleEvent extends LocaleEvent {
const ChangeLocaleEvent();
}

View File

@ -0,0 +1,16 @@
import 'dart:ui';
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:equatable/equatable.dart';
part 'locale_state.g.dart';
@CopyWith()
class LocaleState extends Equatable {
final Locale currentLocale;
const LocaleState({required this.currentLocale});
@override
List<Object?> get props => [currentLocale];
}

View File

@ -0,0 +1,58 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'locale_state.dart';
// **************************************************************************
// CopyWithGenerator
// **************************************************************************
abstract class _$LocaleStateCWProxy {
LocaleState currentLocale(Locale currentLocale);
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `LocaleState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
///
/// Usage
/// ```dart
/// LocaleState(...).copyWith(id: 12, name: "My name")
/// ````
LocaleState call({
Locale? currentLocale,
});
}
/// Proxy class for `copyWith` functionality. This is a callable class and can be used as follows: `instanceOfLocaleState.copyWith(...)`. Additionally contains functions for specific fields e.g. `instanceOfLocaleState.copyWith.fieldName(...)`
class _$LocaleStateCWProxyImpl implements _$LocaleStateCWProxy {
const _$LocaleStateCWProxyImpl(this._value);
final LocaleState _value;
@override
LocaleState currentLocale(Locale currentLocale) =>
this(currentLocale: currentLocale);
@override
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `LocaleState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
///
/// Usage
/// ```dart
/// LocaleState(...).copyWith(id: 12, name: "My name")
/// ````
LocaleState call({
Object? currentLocale = const $CopyWithPlaceholder(),
}) {
return LocaleState(
currentLocale:
currentLocale == const $CopyWithPlaceholder() || currentLocale == null
? _value.currentLocale
// ignore: cast_nullable_to_non_nullable
: currentLocale as Locale,
);
}
}
extension $LocaleStateCopyWith on LocaleState {
/// Returns a callable class that can be used as follows: `instanceOfLocaleState.copyWith(...)` or like so:`instanceOfLocaleState.copyWith.fieldName(...)`.
// ignore: library_private_types_in_public_api
_$LocaleStateCWProxy get copyWith => _$LocaleStateCWProxyImpl(this);
}

View File

@ -183,7 +183,7 @@ packages:
source: hosted
version: "3.1.1"
copy_with_extension:
dependency: "direct dev"
dependency: "direct main"
description:
name: copy_with_extension
sha256: fbcf890b0c34aedf0894f91a11a579994b61b4e04080204656b582708b5b1125
@ -191,7 +191,7 @@ packages:
source: hosted
version: "5.0.4"
copy_with_extension_gen:
dependency: "direct dev"
dependency: "direct main"
description:
name: copy_with_extension_gen
sha256: "51cd11094096d40824c8da629ca7f16f3b7cea5fc44132b679617483d43346b0"
@ -627,7 +627,7 @@ packages:
source: hosted
version: "1.3.0"
shared_preferences:
dependency: "direct dev"
dependency: "direct main"
description:
name: shared_preferences
sha256: "746e5369a43170c25816cc472ee016d3a66bc13fcf430c0bc41ad7b4b2922051"

View File

@ -41,6 +41,9 @@ dependencies:
pretty_dio_logger: ^1.4.0
flutter_bloc: ^8.1.6
equatable: ^2.0.5
copy_with_extension: ^5.0.4
copy_with_extension_gen: ^5.0.4
shared_preferences: ^2.3.2
dev_dependencies:
flutter_test:
@ -60,10 +63,6 @@ dev_dependencies:
sdk: flutter
intl: ^0.19.0
shared_preferences: ^2.3.2
copy_with_extension_gen: ^5.0.4
copy_with_extension: ^5.0.4
json_serializable: ^6.7.1
build_runner: ^2.4.9