LabWork06 is Done
This commit is contained in:
parent
2523f8b0d1
commit
c4feaa5638
20
lib/components/utils/debounce.dart
Normal file
20
lib/components/utils/debounce.dart
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
|
class Debounce {
|
||||||
|
factory Debounce() => _instance;
|
||||||
|
|
||||||
|
Debounce._();
|
||||||
|
|
||||||
|
static final Debounce _instance = Debounce._();
|
||||||
|
|
||||||
|
static Timer? _timer;
|
||||||
|
|
||||||
|
static void run(
|
||||||
|
VoidCallback action, {
|
||||||
|
Duration delay = const Duration(milliseconds: 500),
|
||||||
|
}) {
|
||||||
|
_timer?.cancel();
|
||||||
|
_timer = Timer(delay, action);
|
||||||
|
}
|
||||||
|
}
|
@ -5,8 +5,12 @@ part 'characters_dto.g.dart';
|
|||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
class CharactersDto {
|
class CharactersDto {
|
||||||
final List<CharacterDataDto>? data;
|
final List<CharacterDataDto>? data;
|
||||||
|
final MetaDto? meta;
|
||||||
|
|
||||||
const CharactersDto({this.data});
|
const CharactersDto({
|
||||||
|
this.data,
|
||||||
|
this.meta,
|
||||||
|
});
|
||||||
|
|
||||||
factory CharactersDto.fromJson(Map<String, dynamic> json) =>
|
factory CharactersDto.fromJson(Map<String, dynamic> json) =>
|
||||||
_$CharactersDtoFromJson(json);
|
_$CharactersDtoFromJson(json);
|
||||||
@ -37,3 +41,25 @@ class CharacterAttributesDataDto {
|
|||||||
factory CharacterAttributesDataDto.fromJson(Map<String, dynamic> json) =>
|
factory CharacterAttributesDataDto.fromJson(Map<String, dynamic> json) =>
|
||||||
_$CharacterAttributesDataDtoFromJson(json);
|
_$CharacterAttributesDataDtoFromJson(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class MetaDto {
|
||||||
|
final PaginationDto? pagination;
|
||||||
|
|
||||||
|
const MetaDto({this.pagination});
|
||||||
|
|
||||||
|
factory MetaDto.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$MetaDtoFromJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class PaginationDto {
|
||||||
|
final int? current;
|
||||||
|
final int? next;
|
||||||
|
final int? last;
|
||||||
|
|
||||||
|
const PaginationDto({this.current, this.next, this.last});
|
||||||
|
|
||||||
|
factory PaginationDto.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$PaginationDtoFromJson(json);
|
||||||
|
}
|
||||||
|
@ -11,6 +11,9 @@ CharactersDto _$CharactersDtoFromJson(Map<String, dynamic> json) =>
|
|||||||
data: (json['data'] as List<dynamic>?)
|
data: (json['data'] as List<dynamic>?)
|
||||||
?.map((e) => CharacterDataDto.fromJson(e as Map<String, dynamic>))
|
?.map((e) => CharacterDataDto.fromJson(e as Map<String, dynamic>))
|
||||||
.toList(),
|
.toList(),
|
||||||
|
meta: json['meta'] == null
|
||||||
|
? null
|
||||||
|
: MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
|
||||||
);
|
);
|
||||||
|
|
||||||
CharacterDataDto _$CharacterDataDtoFromJson(Map<String, dynamic> json) =>
|
CharacterDataDto _$CharacterDataDtoFromJson(Map<String, dynamic> json) =>
|
||||||
@ -31,3 +34,16 @@ CharacterAttributesDataDto _$CharacterAttributesDataDtoFromJson(
|
|||||||
died: json['died'] as String?,
|
died: json['died'] as String?,
|
||||||
image: json['image'] as String?,
|
image: json['image'] as String?,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
MetaDto _$MetaDtoFromJson(Map<String, dynamic> json) => MetaDto(
|
||||||
|
pagination: json['pagination'] == null
|
||||||
|
? null
|
||||||
|
: PaginationDto.fromJson(json['pagination'] as Map<String, dynamic>),
|
||||||
|
);
|
||||||
|
|
||||||
|
PaginationDto _$PaginationDtoFromJson(Map<String, dynamic> json) =>
|
||||||
|
PaginationDto(
|
||||||
|
current: (json['current'] as num?)?.toInt(),
|
||||||
|
next: (json['next'] as num?)?.toInt(),
|
||||||
|
last: (json['last'] as num?)?.toInt(),
|
||||||
|
);
|
||||||
|
@ -1,12 +1,20 @@
|
|||||||
import 'package:pmu_labworks/data/dtos/characters_dto.dart';
|
import 'package:pmu_labworks/data/dtos/characters_dto.dart';
|
||||||
import 'package:pmu_labworks/domain/models/comment.dart';
|
import 'package:pmu_labworks/domain/models/comment.dart';
|
||||||
|
import 'package:pmu_labworks/domain/models/home.dart';
|
||||||
import 'package:pmu_labworks/domain/models/user.dart';
|
import 'package:pmu_labworks/domain/models/user.dart';
|
||||||
|
|
||||||
|
extension CharactersDtoToModel on CharactersDto {
|
||||||
|
HomeData toDomain() => HomeData(
|
||||||
|
data: data?.map((e) => e.toDomain()).toList(),
|
||||||
|
nextPage: meta?.pagination?.next,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
extension CharacterDataDtoToModel on CharacterDataDto {
|
extension CharacterDataDtoToModel on CharacterDataDto {
|
||||||
CommentModel toDomain() => CommentModel(
|
CommentData toDomain() => CommentData(
|
||||||
title: type ?? 'UNKNOWN',
|
title: type ?? 'UNKNOWN',
|
||||||
text: _makeDescriptionText(attributes?.born, attributes?.died),
|
text: _makeDescriptionText(attributes?.born, attributes?.died),
|
||||||
user: UserModel(
|
user: UserData(
|
||||||
nickname: attributes?.name ?? 'Noname',
|
nickname: attributes?.name ?? 'Noname',
|
||||||
avatarUrl: attributes?.image),
|
avatarUrl: attributes?.image),
|
||||||
);
|
);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import 'package:pmu_labworks/domain/models/comment.dart';
|
import 'package:pmu_labworks/domain/models/home.dart';
|
||||||
|
|
||||||
typedef OnErrorCallback = void Function(String? error);
|
typedef OnErrorCallback = void Function(String? error);
|
||||||
|
|
||||||
abstract class ApiInterface {
|
abstract class ApiInterface {
|
||||||
Future<List<CommentModel>?> loadData({OnErrorCallback? onError});
|
Future<HomeData?> loadData({OnErrorCallback? onError});
|
||||||
}
|
}
|
||||||
|
@ -1,54 +1,55 @@
|
|||||||
import 'package:pmu_labworks/data/repositories/api_interface.dart';
|
import 'package:pmu_labworks/data/repositories/api_interface.dart';
|
||||||
import 'package:pmu_labworks/domain/models/comment.dart';
|
import 'package:pmu_labworks/domain/models/comment.dart';
|
||||||
|
import 'package:pmu_labworks/domain/models/home.dart';
|
||||||
import 'package:pmu_labworks/domain/models/user.dart';
|
import 'package:pmu_labworks/domain/models/user.dart';
|
||||||
|
|
||||||
class MockRepository extends ApiInterface {
|
class MockRepository extends ApiInterface {
|
||||||
@override
|
@override
|
||||||
Future<List<CommentModel>?> loadData({OnErrorCallback? onError}) async {
|
Future<HomeData?> loadData({OnErrorCallback? onError}) async {
|
||||||
final data = [
|
final data = [
|
||||||
CommentModel(
|
CommentData(
|
||||||
title: 'Oh my god',
|
title: 'Oh my god',
|
||||||
text: 'this app is so cool ' * 10,
|
text: 'this app is so cool ' * 10,
|
||||||
user: UserModel(
|
user: UserData(
|
||||||
nickname: 'Steve',
|
nickname: 'Steve',
|
||||||
avatarUrl:
|
avatarUrl:
|
||||||
'https://preview.free3d.com/img/2016/03/1875481443430303321/hld8c0oa.jpg',
|
'https://preview.free3d.com/img/2016/03/1875481443430303321/hld8c0oa.jpg',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
CommentModel(
|
CommentData(
|
||||||
title: 'Listen to me',
|
title: 'Listen to me',
|
||||||
text: 'This app is like Half-Life 3 - it will never be released',
|
text: 'This app is like Half-Life 3 - it will never be released',
|
||||||
user: UserModel(
|
user: UserData(
|
||||||
nickname: 'G-Man',
|
nickname: 'G-Man',
|
||||||
avatarUrl:
|
avatarUrl:
|
||||||
'https://us-tuna-sounds-images.voicemod.net/356aeabd-18d5-40d8-af31-b479f4eff183-1704672174928.png',
|
'https://us-tuna-sounds-images.voicemod.net/356aeabd-18d5-40d8-af31-b479f4eff183-1704672174928.png',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
CommentModel(
|
CommentData(
|
||||||
title: 'BREAKING!!!',
|
title: 'BREAKING!!!',
|
||||||
text:
|
text:
|
||||||
'ONLY NOW you can purchase the ability to put likes and dislikes for only 299\$',
|
'ONLY NOW you can purchase the ability to put likes and dislikes for only 299\$',
|
||||||
user: UserModel(
|
user: UserData(
|
||||||
nickname: 'Bethesda',
|
nickname: 'Bethesda',
|
||||||
avatarUrl: 'https://i.playground.ru/p/BWixorSTeZQfoPvdVL9lgA.jpeg',
|
avatarUrl: 'https://i.playground.ru/p/BWixorSTeZQfoPvdVL9lgA.jpeg',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
CommentModel(
|
CommentData(
|
||||||
title: 'Test',
|
title: 'Test',
|
||||||
text: 'Test',
|
text: 'Test',
|
||||||
user: UserModel(
|
user: UserData(
|
||||||
nickname: 'Noname',
|
nickname: 'Noname',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
CommentModel(
|
CommentData(
|
||||||
title: 'Test',
|
title: 'Test',
|
||||||
text: 'Test',
|
text: 'Test',
|
||||||
user: UserModel(
|
user: UserData(
|
||||||
nickname: 'Noname',
|
nickname: 'Noname',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
return data;
|
return HomeData(data: data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ import 'package:dio/dio.dart';
|
|||||||
import 'package:pmu_labworks/data/dtos/characters_dto.dart';
|
import 'package:pmu_labworks/data/dtos/characters_dto.dart';
|
||||||
import 'package:pmu_labworks/data/mappers/characters_mapper.dart';
|
import 'package:pmu_labworks/data/mappers/characters_mapper.dart';
|
||||||
import 'package:pmu_labworks/data/repositories/api_interface.dart';
|
import 'package:pmu_labworks/data/repositories/api_interface.dart';
|
||||||
import 'package:pmu_labworks/domain/models/comment.dart';
|
import 'package:pmu_labworks/domain/models/home.dart';
|
||||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||||
|
|
||||||
class PotterDBRepository extends ApiInterface {
|
class PotterDBRepository extends ApiInterface {
|
||||||
@ -15,20 +15,30 @@ class PotterDBRepository extends ApiInterface {
|
|||||||
static const String _baseUrl = 'https://api.potterdb.com';
|
static const String _baseUrl = 'https://api.potterdb.com';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<CommentModel>?> loadData({String? q, OnErrorCallback? onError}) async {
|
Future<HomeData?> loadData({
|
||||||
|
OnErrorCallback? onError,
|
||||||
|
String? q,
|
||||||
|
int page = 1,
|
||||||
|
int pageSize = 25,
|
||||||
|
}) async {
|
||||||
try {
|
try {
|
||||||
const String url = '$_baseUrl/v1/characters';
|
const String url = '$_baseUrl/v1/characters';
|
||||||
|
|
||||||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
||||||
url,
|
url,
|
||||||
queryParameters: q != null ? {'filter[name_cont]': q} : null,
|
queryParameters: {
|
||||||
|
'filter[name_cont]': q,
|
||||||
|
'page[number]': page,
|
||||||
|
'page[size]': pageSize,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
final CharactersDto dto = CharactersDto.fromJson(response.data as Map<String, dynamic>);
|
final CharactersDto dto =
|
||||||
final List<CommentModel>? data = dto.data?.map((e) => e.toDomain()).toList();
|
CharactersDto.fromJson(response.data as Map<String, dynamic>);
|
||||||
|
final HomeData data = dto.toDomain();
|
||||||
return data;
|
return data;
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
onError?.call(e.response?.statusMessage);
|
onError?.call(e.error?.toString());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import 'user.dart';
|
import 'user.dart';
|
||||||
|
|
||||||
class CommentModel {
|
class CommentData {
|
||||||
final String title;
|
final String title;
|
||||||
final String text;
|
final String text;
|
||||||
final UserModel user;
|
final UserData user;
|
||||||
|
|
||||||
CommentModel({
|
CommentData({
|
||||||
required this.title,
|
required this.title,
|
||||||
required this.text,
|
required this.text,
|
||||||
required this.user,
|
required this.user,
|
||||||
|
8
lib/domain/models/home.dart
Normal file
8
lib/domain/models/home.dart
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import 'package:pmu_labworks/domain/models/comment.dart';
|
||||||
|
|
||||||
|
class HomeData {
|
||||||
|
final List<CommentData>? data;
|
||||||
|
final int? nextPage;
|
||||||
|
|
||||||
|
HomeData({this.data, this.nextPage});
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
class UserModel {
|
class UserData {
|
||||||
final String nickname;
|
final String nickname;
|
||||||
final String? avatarUrl;
|
final String? avatarUrl;
|
||||||
|
|
||||||
UserModel({
|
UserData({
|
||||||
required this.nickname,
|
required this.nickname,
|
||||||
this.avatarUrl,
|
this.avatarUrl,
|
||||||
});
|
});
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pmu_labworks/data/repositories/potterdb_repository.dart';
|
||||||
|
import 'package:pmu_labworks/view/home_page/bloc/bloc.dart';
|
||||||
import 'package:pmu_labworks/view/home_page/home_page.dart';
|
import 'package:pmu_labworks/view/home_page/home_page.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@ -17,7 +20,15 @@ class MyApp extends StatelessWidget {
|
|||||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
),
|
),
|
||||||
home: const HomePage(title: 'Comments App'),
|
home: RepositoryProvider<PotterDBRepository>(
|
||||||
|
lazy: true,
|
||||||
|
create: (_) => PotterDBRepository(),
|
||||||
|
child: BlocProvider<HomeBloc>(
|
||||||
|
lazy: false,
|
||||||
|
create: (context) => HomeBloc(context.read<PotterDBRepository>()),
|
||||||
|
child: const HomePage(title: 'Comments App'),
|
||||||
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:pmu_labworks/domain/models/comment.dart';
|
import 'package:pmu_labworks/domain/models/comment.dart';
|
||||||
|
|
||||||
class DetailsPage extends StatelessWidget {
|
class DetailsPage extends StatelessWidget {
|
||||||
final CommentModel model;
|
final CommentData model;
|
||||||
|
|
||||||
const DetailsPage(this.model, {super.key});
|
const DetailsPage(this.model, {super.key});
|
||||||
|
|
||||||
|
39
lib/view/home_page/bloc/bloc.dart
Normal file
39
lib/view/home_page/bloc/bloc.dart
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pmu_labworks/data/repositories/potterdb_repository.dart';
|
||||||
|
import 'package:pmu_labworks/view/home_page/bloc/events.dart';
|
||||||
|
import 'package:pmu_labworks/view/home_page/bloc/state.dart';
|
||||||
|
|
||||||
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||||
|
final PotterDBRepository repo;
|
||||||
|
|
||||||
|
HomeBloc(this.repo) : super(const HomeState()) {
|
||||||
|
on<HomeLoadDataEvent>(_onLoadData);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
||||||
|
if (event.nextPage == null) {
|
||||||
|
emit(state.copyWith(isLoading: true));
|
||||||
|
} else {
|
||||||
|
emit(state.copyWith(isPaginationLoading: true));
|
||||||
|
}
|
||||||
|
|
||||||
|
String? error;
|
||||||
|
|
||||||
|
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(
|
||||||
|
isLoading: false,
|
||||||
|
isPaginationLoading: false,
|
||||||
|
data: data,
|
||||||
|
error: error,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
10
lib/view/home_page/bloc/events.dart
Normal file
10
lib/view/home_page/bloc/events.dart
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
abstract class HomeEvent {
|
||||||
|
const HomeEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
class HomeLoadDataEvent extends HomeEvent {
|
||||||
|
final String? search;
|
||||||
|
final int? nextPage;
|
||||||
|
|
||||||
|
const HomeLoadDataEvent({this.search, this.nextPage});
|
||||||
|
}
|
28
lib/view/home_page/bloc/state.dart
Normal file
28
lib/view/home_page/bloc/state.dart
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import 'package:copy_with_extension/copy_with_extension.dart';
|
||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:pmu_labworks/domain/models/home.dart';
|
||||||
|
|
||||||
|
part 'state.g.dart';
|
||||||
|
|
||||||
|
@CopyWith()
|
||||||
|
class HomeState extends Equatable {
|
||||||
|
final HomeData? data;
|
||||||
|
final bool isLoading;
|
||||||
|
final bool isPaginationLoading;
|
||||||
|
final String? error;
|
||||||
|
|
||||||
|
const HomeState({
|
||||||
|
this.data,
|
||||||
|
this.isLoading = false,
|
||||||
|
this.isPaginationLoading = false,
|
||||||
|
this.error,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [
|
||||||
|
data,
|
||||||
|
isLoading,
|
||||||
|
isPaginationLoading,
|
||||||
|
error,
|
||||||
|
];
|
||||||
|
}
|
92
lib/view/home_page/bloc/state.g.dart
Normal file
92
lib/view/home_page/bloc/state.g.dart
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
|
part of 'state.dart';
|
||||||
|
|
||||||
|
// **************************************************************************
|
||||||
|
// CopyWithGenerator
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
abstract class _$HomeStateCWProxy {
|
||||||
|
HomeState data(HomeData? data);
|
||||||
|
|
||||||
|
HomeState isLoading(bool isLoading);
|
||||||
|
|
||||||
|
HomeState isPaginationLoading(bool isPaginationLoading);
|
||||||
|
|
||||||
|
HomeState error(String? error);
|
||||||
|
|
||||||
|
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `HomeState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||||
|
///
|
||||||
|
/// Usage
|
||||||
|
/// ```dart
|
||||||
|
/// HomeState(...).copyWith(id: 12, name: "My name")
|
||||||
|
/// ````
|
||||||
|
HomeState call({
|
||||||
|
HomeData? data,
|
||||||
|
bool? isLoading,
|
||||||
|
bool? isPaginationLoading,
|
||||||
|
String? error,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Proxy class for `copyWith` functionality. This is a callable class and can be used as follows: `instanceOfHomeState.copyWith(...)`. Additionally contains functions for specific fields e.g. `instanceOfHomeState.copyWith.fieldName(...)`
|
||||||
|
class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
|
||||||
|
const _$HomeStateCWProxyImpl(this._value);
|
||||||
|
|
||||||
|
final HomeState _value;
|
||||||
|
|
||||||
|
@override
|
||||||
|
HomeState data(HomeData? data) => this(data: data);
|
||||||
|
|
||||||
|
@override
|
||||||
|
HomeState isLoading(bool isLoading) => this(isLoading: isLoading);
|
||||||
|
|
||||||
|
@override
|
||||||
|
HomeState isPaginationLoading(bool isPaginationLoading) =>
|
||||||
|
this(isPaginationLoading: isPaginationLoading);
|
||||||
|
|
||||||
|
@override
|
||||||
|
HomeState error(String? error) => this(error: error);
|
||||||
|
|
||||||
|
@override
|
||||||
|
|
||||||
|
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `HomeState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||||
|
///
|
||||||
|
/// Usage
|
||||||
|
/// ```dart
|
||||||
|
/// HomeState(...).copyWith(id: 12, name: "My name")
|
||||||
|
/// ````
|
||||||
|
HomeState call({
|
||||||
|
Object? data = const $CopyWithPlaceholder(),
|
||||||
|
Object? isLoading = const $CopyWithPlaceholder(),
|
||||||
|
Object? isPaginationLoading = const $CopyWithPlaceholder(),
|
||||||
|
Object? error = const $CopyWithPlaceholder(),
|
||||||
|
}) {
|
||||||
|
return HomeState(
|
||||||
|
data: data == const $CopyWithPlaceholder()
|
||||||
|
? _value.data
|
||||||
|
// ignore: cast_nullable_to_non_nullable
|
||||||
|
: data as HomeData?,
|
||||||
|
isLoading: isLoading == const $CopyWithPlaceholder() || isLoading == null
|
||||||
|
? _value.isLoading
|
||||||
|
// ignore: cast_nullable_to_non_nullable
|
||||||
|
: isLoading as bool,
|
||||||
|
isPaginationLoading:
|
||||||
|
isPaginationLoading == const $CopyWithPlaceholder() ||
|
||||||
|
isPaginationLoading == null
|
||||||
|
? _value.isPaginationLoading
|
||||||
|
// ignore: cast_nullable_to_non_nullable
|
||||||
|
: isPaginationLoading as bool,
|
||||||
|
error: error == const $CopyWithPlaceholder()
|
||||||
|
? _value.error
|
||||||
|
// ignore: cast_nullable_to_non_nullable
|
||||||
|
: error as String?,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension $HomeStateCopyWith on HomeState {
|
||||||
|
/// Returns a callable class that can be used as follows: `instanceOfHomeState.copyWith(...)` or like so:`instanceOfHomeState.copyWith.fieldName(...)`.
|
||||||
|
// ignore: library_private_types_in_public_api
|
||||||
|
_$HomeStateCWProxy get copyWith => _$HomeStateCWProxyImpl(this);
|
||||||
|
}
|
@ -6,7 +6,7 @@ typedef OnActionCallback = void Function(
|
|||||||
class _Comment extends StatefulWidget {
|
class _Comment extends StatefulWidget {
|
||||||
final String title;
|
final String title;
|
||||||
final String text;
|
final String text;
|
||||||
final UserModel? user;
|
final UserData? user;
|
||||||
final OnActionCallback onAction;
|
final OnActionCallback onAction;
|
||||||
final VoidCallback? onTap;
|
final VoidCallback? onTap;
|
||||||
|
|
||||||
@ -19,14 +19,14 @@ class _Comment extends StatefulWidget {
|
|||||||
});
|
});
|
||||||
|
|
||||||
factory _Comment.fromData(
|
factory _Comment.fromData(
|
||||||
CommentModel model, {
|
CommentData data, {
|
||||||
OnActionCallback onAction,
|
OnActionCallback onAction,
|
||||||
VoidCallback? onTap,
|
VoidCallback? onTap,
|
||||||
}) =>
|
}) =>
|
||||||
_Comment(
|
_Comment(
|
||||||
title: model.title,
|
title: data.title,
|
||||||
text: model.text,
|
text: data.text,
|
||||||
user: model.user,
|
user: data.user,
|
||||||
onAction: onAction,
|
onAction: onAction,
|
||||||
onTap: onTap,
|
onTap: onTap,
|
||||||
);
|
);
|
||||||
|
@ -2,11 +2,14 @@ import 'dart:math';
|
|||||||
|
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:pmu_labworks/data/repositories/potterdb_repository.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pmu_labworks/components/utils/debounce.dart';
|
||||||
import 'package:pmu_labworks/domain/models/comment.dart';
|
import 'package:pmu_labworks/domain/models/comment.dart';
|
||||||
import 'package:pmu_labworks/domain/models/user.dart';
|
import 'package:pmu_labworks/domain/models/user.dart';
|
||||||
import 'package:pmu_labworks/view/details_page/details_page.dart';
|
import 'package:pmu_labworks/view/details_page/details_page.dart';
|
||||||
import 'package:pmu_labworks/view/dialogs/show_dialog.dart';
|
import 'package:pmu_labworks/view/home_page/bloc/bloc.dart';
|
||||||
|
import 'package:pmu_labworks/view/home_page/bloc/events.dart';
|
||||||
|
import 'package:pmu_labworks/view/home_page/bloc/state.dart';
|
||||||
|
|
||||||
part 'comment.dart';
|
part 'comment.dart';
|
||||||
|
|
||||||
@ -37,11 +40,11 @@ class _HomePageState extends State<HomePage> {
|
|||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text(widget.title,
|
Text(widget.title,
|
||||||
style: TextStyle(
|
style: const TextStyle(
|
||||||
fontSize: 20,
|
fontSize: 20,
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
)),
|
)),
|
||||||
Text(
|
const Text(
|
||||||
'made by Factorino',
|
'made by Factorino',
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
fontSize: 12,
|
fontSize: 12,
|
||||||
@ -51,7 +54,7 @@ class _HomePageState extends State<HomePage> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
body: const Body(),
|
body: const _Body(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,28 +68,32 @@ class _HomePageState extends State<HomePage> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Body extends StatefulWidget {
|
class _Body extends StatefulWidget {
|
||||||
const Body({super.key});
|
const _Body();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<Body> createState() => _BodyState();
|
State<_Body> createState() => _BodyState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _BodyState extends State<Body> {
|
class _BodyState extends State<_Body> {
|
||||||
final searchController = TextEditingController();
|
final searchController = TextEditingController();
|
||||||
late Future<List<CommentModel>?> data;
|
final scrollController = ScrollController();
|
||||||
|
|
||||||
final repo = PotterDBRepository();
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
data = repo.loadData(onError: (e) => showErrorDialog(context, error: e));
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||||
|
});
|
||||||
|
|
||||||
|
scrollController.addListener(_onNextPageListener);
|
||||||
|
|
||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
searchController.dispose();
|
searchController.dispose();
|
||||||
|
scrollController.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,43 +108,63 @@ class _BodyState extends State<Body> {
|
|||||||
child: CupertinoSearchTextField(
|
child: CupertinoSearchTextField(
|
||||||
controller: searchController,
|
controller: searchController,
|
||||||
onChanged: (search) {
|
onChanged: (search) {
|
||||||
setState(() {
|
Debounce.run(() => context
|
||||||
data = repo.loadData(q: search);
|
.read<HomeBloc>()
|
||||||
});
|
.add(HomeLoadDataEvent(search: search)));
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Expanded(
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
child: Center(
|
builder: (context, state) => state.error != null
|
||||||
child: FutureBuilder<List<CommentModel>?>(
|
? Text(
|
||||||
future: data,
|
state.error ?? '',
|
||||||
builder: (context, snapshot) => SingleChildScrollView(
|
style: Theme.of(context)
|
||||||
child: snapshot.hasData
|
.textTheme
|
||||||
? Column(
|
.headlineSmall
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
?.copyWith(color: Colors.red),
|
||||||
children: snapshot.data?.map((data) {
|
)
|
||||||
return _Comment.fromData(
|
: state.isLoading
|
||||||
data,
|
? const CircularProgressIndicator()
|
||||||
onAction: (String nickname, bool isLiked,
|
: Expanded(
|
||||||
bool isDisliked) =>
|
child: RefreshIndicator(
|
||||||
_showSnackBar(context, nickname, isLiked,
|
onRefresh: _onRefresh,
|
||||||
isDisliked),
|
child: ListView.builder(
|
||||||
onTap: () => _navToDetails(context, data),
|
controller: scrollController,
|
||||||
);
|
padding: EdgeInsets.zero,
|
||||||
}).toList() ??
|
itemCount: state.data?.data?.length ?? 0,
|
||||||
[],
|
itemBuilder: (context, index) {
|
||||||
)
|
final data = state.data?.data?[index];
|
||||||
: const CircularProgressIndicator(),
|
return data != null
|
||||||
),
|
? _Comment.fromData(
|
||||||
),
|
data,
|
||||||
),
|
onAction:
|
||||||
|
(nickname, isLiked, isDisliked) =>
|
||||||
|
_showSnackBar(context, nickname,
|
||||||
|
isLiked, isDisliked),
|
||||||
|
onTap: () => _navToDetails(context, data),
|
||||||
|
)
|
||||||
|
: const SizedBox.shrink();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
|
builder: (context, state) => state.isPaginationLoading
|
||||||
|
? const CircularProgressIndicator()
|
||||||
|
: const SizedBox.shrink(),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _navToDetails(BuildContext context, CommentModel data) {
|
Future<void> _onRefresh() {
|
||||||
|
context.read<HomeBloc>().add(HomeLoadDataEvent(search: searchController.text));
|
||||||
|
return Future.value(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _navToDetails(BuildContext context, CommentData data) {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
CupertinoPageRoute(builder: (context) => DetailsPage(data)),
|
CupertinoPageRoute(builder: (context) => DetailsPage(data)),
|
||||||
@ -167,4 +194,17 @@ class _BodyState extends State<Body> {
|
|||||||
));
|
));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _onNextPageListener() {
|
||||||
|
if (scrollController.offset > scrollController.position.maxScrollExtent) {
|
||||||
|
// preventing multiple pagination request on multiple swipes
|
||||||
|
final bloc = context.read<HomeBloc>();
|
||||||
|
if (!bloc.state.isPaginationLoading) {
|
||||||
|
bloc.add(HomeLoadDataEvent(
|
||||||
|
search: searchController.text,
|
||||||
|
nextPage: bloc.state.data?.nextPage,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
56
pubspec.lock
56
pubspec.lock
@ -38,6 +38,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.11.0"
|
version: "2.11.0"
|
||||||
|
bloc:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: bloc
|
||||||
|
sha256: "106842ad6569f0b60297619e9e0b1885c2fb9bf84812935490e6c5275777804e"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "8.1.4"
|
||||||
boolean_selector:
|
boolean_selector:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -158,6 +166,22 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.1.1"
|
version: "3.1.1"
|
||||||
|
copy_with_extension:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: copy_with_extension
|
||||||
|
sha256: fbcf890b0c34aedf0894f91a11a579994b61b4e04080204656b582708b5b1125
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "5.0.4"
|
||||||
|
copy_with_extension_gen:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: copy_with_extension_gen
|
||||||
|
sha256: "51cd11094096d40824c8da629ca7f16f3b7cea5fc44132b679617483d43346b0"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "5.0.4"
|
||||||
crypto:
|
crypto:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -198,6 +222,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0"
|
version: "2.0.0"
|
||||||
|
equatable:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: equatable
|
||||||
|
sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.5"
|
||||||
fake_async:
|
fake_async:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -227,6 +259,14 @@ packages:
|
|||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
flutter_bloc:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: flutter_bloc
|
||||||
|
sha256: b594505eac31a0518bdcb4b5b79573b8d9117b193cc80cc12e17d639b10aa27a
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "8.1.6"
|
||||||
flutter_lints:
|
flutter_lints:
|
||||||
dependency: "direct dev"
|
dependency: "direct dev"
|
||||||
description:
|
description:
|
||||||
@ -392,6 +432,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.6"
|
version: "1.0.6"
|
||||||
|
nested:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: nested
|
||||||
|
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.0"
|
||||||
package_config:
|
package_config:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -424,6 +472,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.4.0"
|
version: "1.4.0"
|
||||||
|
provider:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: provider
|
||||||
|
sha256: c8a055ee5ce3fd98d6fc872478b03823ffdb448699c6ebdbbc71d59b596fd48c
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "6.1.2"
|
||||||
pub_semver:
|
pub_semver:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -36,10 +36,17 @@ dependencies:
|
|||||||
# Use with the CupertinoIcons class for iOS style icons.
|
# Use with the CupertinoIcons class for iOS style icons.
|
||||||
cupertino_icons: ^1.0.8
|
cupertino_icons: ^1.0.8
|
||||||
|
|
||||||
|
# Сетевое взаимодействие
|
||||||
json_annotation: ^4.8.1
|
json_annotation: ^4.8.1
|
||||||
dio: ^5.4.2+1
|
dio: ^5.4.2+1
|
||||||
pretty_dio_logger: ^1.3.1
|
pretty_dio_logger: ^1.3.1
|
||||||
|
|
||||||
|
# BLoC
|
||||||
|
equatable: ^2.0.5
|
||||||
|
flutter_bloc: ^8.1.5
|
||||||
|
|
||||||
|
copy_with_extension_gen: ^5.0.4
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
@ -51,6 +58,7 @@ dev_dependencies:
|
|||||||
# rules and activating additional ones.
|
# rules and activating additional ones.
|
||||||
flutter_lints: ^4.0.0
|
flutter_lints: ^4.0.0
|
||||||
|
|
||||||
|
# Сетевое взаимодействие
|
||||||
build_runner: ^2.4.9
|
build_runner: ^2.4.9
|
||||||
json_serializable: ^6.7.1
|
json_serializable: ^6.7.1
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user