Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form Refactored to not lose its state on scroll #6

Merged
merged 21 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Putting everything together, missing tests
  • Loading branch information
baumths committed Nov 20, 2020
commit 92b8745e37be3daa1b64f68496662b395dd53b61
5 changes: 2 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:provider/provider.dart';

// import 'src/database/hive_database.dart';
import 'src/features/features.dart';
import 'src/repositories/hive_repository.dart';

Future<void> main() async {
// await HiveDatabase.instance.initDatabase();
final HiveRepository repository = HiveRepository()..initDatabase();
final HiveRepository repository = HiveRepository();
await repository.initDatabase();
runApp(
MultiProvider(
providers: [
Expand Down
76 changes: 0 additions & 76 deletions lib/src/database/hive_database.dart

This file was deleted.

22 changes: 19 additions & 3 deletions lib/src/entities/classe/classe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:hive/hive.dart';
import '../../repositories/hive_repository.dart';

part 'metadado.dart';
// part 'classe.g.dart';
part 'classe.g.dart';

// Todo: RUN BUILD_RUNNER

Expand All @@ -15,12 +15,22 @@ class Classe with HiveObject {
@required this.code,
@required this.parentId,
@required this.metadados,
@required this.referenceCode,
}) : children = HiveList<Classe>(HiveRepository.classesBox);

factory Classe.root() => Classe(
name: '',
code: '',
parentId: -1,
referenceCode: '',
metadados: <Metadado>[],
);

factory Classe.fromParent(int parentId) => Classe(
name: '',
code: '',
parentId: parentId,
referenceCode: '',
metadados: <Metadado>[],
);

Expand All @@ -37,18 +47,24 @@ class Classe with HiveObject {
String code;

@HiveField(4)
List<Metadado> metadados;
String referenceCode;

@HiveField(5)
List<Metadado> metadados;

@HiveField(6)
HiveList<Classe> children;

bool get hasChildren => children.isNotEmpty;

@override
String toString() {
final String metadadosStr = metadados.map((m) => m.toCsv()).join('');
return 'id ➜ { $id } '
'parentId ➜ { $parentId } '
'Nome ➜ { $name } '
'Código ➜ { $code }'
'Código ➜ { $code } '
'Código de Referência ➜ { $referenceCode }'
'\n $metadadosStr';
}
}
59 changes: 59 additions & 0 deletions lib/src/entities/classe/classe.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/src/entities/classe/metadado.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Metadado {

String get label => type.asString();

String toCsv() => '$label: $content\n\n';
String toCsv() => '$label: $content';
}

extension MetadadosX on Metadados {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/features/app/misc/app_routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import '../../../entities/entities.dart';
import '../../features.dart';

abstract class ElPCDRouter {
static const home = HomeView.routeName;
static const compose = ComposeView.routeName;

static MaterialPageRoute onGenerateRoute(RouteSettings settings) {
switch (settings.name) {
case ComposeView.routeName:
Expand Down
20 changes: 9 additions & 11 deletions lib/src/features/app/view/app_view.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:provider/provider.dart';

import '../../../database/hive_database.dart';
import '../../../repositories/hive_repository.dart';
import '../../features.dart';
import '../misc/app_theme.dart';

class ElPCDApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final _repository = context.watch<HiveRepository>();
return ValueListenableBuilder(
valueListenable: HiveDatabase.settingsBox.listenable(keys: ['darkMode']),
builder: (_, settingsBox, __) {
final darkMode =
settingsBox.get('darkMode', defaultValue: true) as bool;
valueListenable: _repository.listenToSettings(keys: ['darkMode']),
builder: (_, __, ___) {
return MaterialApp(
title: 'ElPCD',
debugShowCheckedModeBanner: false,
themeMode: darkMode ? ThemeMode.dark : ThemeMode.light,
theme: AppTheme.themeData(darkMode: darkMode),
// initialRoute: HomeView.routeName,
initialRoute: ComposeView.routeName,
// onGenerateRoute: ElPCDRouter.onGenerateRoute,
themeMode: _repository.isDarkMode ? ThemeMode.dark : ThemeMode.light,
theme: AppTheme.themeData(darkMode: _repository.isDarkMode),
initialRoute: HomeView.routeName,
onGenerateRoute: ElPCDRouter.onGenerateRoute,
routes: {
HomeView.routeName: (_) => HomeView(),
ComposeView.routeName: (_) => const ComposeView(classe: null),
Expand Down
3 changes: 3 additions & 0 deletions lib/src/features/compose/bloc/compose_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class ComposeBloc extends Bloc<ComposeEvent, ComposeState> {

final HiveRepository _repository;

//! BLOC exiting with null error

@override
Stream<ComposeState> mapEventToState(ComposeEvent event) async* {
if (event is ComposeStarted) {
Expand Down Expand Up @@ -53,6 +55,7 @@ class ComposeBloc extends Bloc<ComposeEvent, ComposeState> {
final classe = state.classe
..name = state.name
..code = state.code
..referenceCode = _repository.buildReferenceCode(state.classe)
..metadados = clearEmptyMetadados(state.metadados);

state.isEditing
Expand Down
2 changes: 1 addition & 1 deletion lib/src/features/compose/misc/form_metadados.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import '../../../entities/entities.dart';
class FormMetadados extends ChangeNotifier {
static const int max = 14;

List<Metadado> metadados = const [];
List<Metadado> metadados = <Metadado>[];

bool get canAddMetadados => metadados.length < max;

Expand Down
80 changes: 46 additions & 34 deletions lib/src/features/compose/view/compose_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ class ComposeView extends StatelessWidget {
}
},
buildWhen: (p, c) => p.isSaving != c.isSaving,
builder: (_, state) => _ComposeViewScaffold(isSaving: state.isSaving),
builder: (_, state) {
return ChangeNotifierProvider(
create: (_) => FormMetadados(),
child: _ComposeViewScaffold(isSaving: state.isSaving),
);
},
),
);
}
Expand All @@ -52,42 +57,49 @@ class _ComposeViewScaffold extends StatelessWidget {

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => FormMetadados(),
child: Scaffold(
appBar: AppBar(
title: BlocBuilder<ComposeBloc, ComposeState>(
buildWhen: (p, c) => p.isEditing != c.isEditing,
builder: (_, state) {
return Text(state.isEditing ? 'Editando Classe' : 'Nova Classe');
},
),
actions: [
IconButton(
tooltip: 'Salvar',
icon: const Icon(Icons.check),
onPressed: () => context.read<ComposeBloc>().add(SavePressed(
metadados: context.read<FormMetadados>().metadados)),
)
],
return Scaffold(
appBar: AppBar(
title: BlocBuilder<ComposeBloc, ComposeState>(
buildWhen: (p, c) => p.isEditing != c.isEditing,
builder: (_, state) {
return Text(state.isEditing ? 'Editando Classe' : 'Nova Classe');
},
),
body: Form(
autovalidateMode: AutovalidateMode.onUserInteraction,
child: IgnorePointer(
ignoring: isSaving,
child: Scrollbar(
radius: const Radius.circular(8),
child: CustomScrollView(
slivers: [
if (isSaving) const LinearProgressIndicator(),
SliverToBoxAdapter(child: RequiredFields()),
SliverToBoxAdapter(child: MetadadosList()),
SliverToBoxAdapter(child: AddMetadados()),
],
actions: [
IconButton(
tooltip: 'Salvar',
icon: const Icon(Icons.check),
onPressed: () => context.read<ComposeBloc>().add(SavePressed(
metadados: context.read<FormMetadados>().metadados)),
)
],
),
body: BlocBuilder<ComposeBloc, ComposeState>(
builder: (_, state) {
return Form(
autovalidateMode:
state.successOrFailure == ComposeSuccessOrFailure.none
? AutovalidateMode.always
: AutovalidateMode.disabled,
child: IgnorePointer(
ignoring: isSaving,
child: Scrollbar(
radius: const Radius.circular(8),
child: CustomScrollView(
slivers: [
if (isSaving)
const SliverToBoxAdapter(
child: LinearProgressIndicator(),
),
SliverToBoxAdapter(child: RequiredFields()),
SliverToBoxAdapter(child: MetadadosList()),
SliverToBoxAdapter(child: AddMetadados()),
],
),
),
),
),
),
);
},
),
);
}
Expand Down
2 changes: 0 additions & 2 deletions lib/src/features/description/description.dart

This file was deleted.

Loading