Skip to content

Commit

Permalink
Ordenação das listagens, listagem de eventos
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasVinicius314 committed Apr 26, 2023
1 parent 11cfa44 commit 34afb8d
Show file tree
Hide file tree
Showing 22 changed files with 545 additions and 181 deletions.
29 changes: 29 additions & 0 deletions app/lib/blocs/event/event_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:baby_guard/blocs/event/event_event.dart';
import 'package:baby_guard/blocs/event/event_state.dart';
import 'package:baby_guard/repositories/event_repository.dart';
import 'package:flutter/foundation.dart';

import 'package:flutter_bloc/flutter_bloc.dart';

class EventBloc extends Bloc<EventEvent, EventState> {
EventBloc({required this.eventRepository}) : super(EventInitialState()) {
on<ListEventsEvent>((event, emit) async {
try {
emit(ListEventsLoadingState());

final events =
(await eventRepository.list(sensorId: event.sensorId)).events ?? [];

emit(ListEventsDoneState(events: events));
} catch (e) {
if (kDebugMode) {
print(e);
}

emit(ListEventsErrorState(message: e.toString()));
}
});
}

final EventRepository eventRepository;
}
9 changes: 9 additions & 0 deletions app/lib/blocs/event/event_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
abstract class EventEvent {}

class ListEventsEvent extends EventEvent {
ListEventsEvent({
required this.sensorId,
});

final int sensorId;
}
25 changes: 25 additions & 0 deletions app/lib/blocs/event/event_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:baby_guard/models/event.dart';

abstract class EventState {}

class EventInitialState extends EventState {}

// ListEvents

class ListEventsLoadingState extends EventState {}

class ListEventsDoneState extends EventState {
ListEventsDoneState({
required this.events,
});

final List<Event> events;
}

class ListEventsErrorState extends EventState {
ListEventsErrorState({
required this.message,
});

final String message;
}
2 changes: 2 additions & 0 deletions app/lib/core/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:baby_guard/blocs/theme/theme_bloc.dart';
import 'package:baby_guard/blocs/theme/theme_state.dart';
import 'package:baby_guard/core/bloc_providers.dart';
import 'package:baby_guard/core/repository_providers.dart';
import 'package:baby_guard/pages/events_page.dart';
import 'package:baby_guard/pages/home_page.dart';
import 'package:baby_guard/pages/login_page.dart';
import 'package:baby_guard/pages/main_page.dart';
Expand Down Expand Up @@ -62,6 +63,7 @@ class App extends StatelessWidget {
LoginPage.route: (context) => const LoginPage(),
RegisterPage.route: (context) => const RegisterPage(),
SensorsPage.route: (context) => const SensorsPage(),
EventsPage.route: (context) => const EventsPage(),
},
);
},
Expand Down
4 changes: 4 additions & 0 deletions app/lib/core/repository_providers.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:baby_guard/repositories/auth_repository.dart';
import 'package:baby_guard/repositories/event_repository.dart';
import 'package:baby_guard/repositories/sensor_repository.dart';
import 'package:baby_guard/repositories/theme_repository.dart';
import 'package:baby_guard/repositories/user_repository.dart';
Expand Down Expand Up @@ -33,6 +34,9 @@ class RepositoryProviders extends StatelessWidget {
RepositoryProvider<UserRepository>(
create: (context) => UserRepository(api: _api),
),
RepositoryProvider<EventRepository>(
create: (context) => EventRepository(api: _api),
),
],
child: child,
);
Expand Down
2 changes: 2 additions & 0 deletions app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ import 'package:flutter/material.dart';
void main() {
runApp(const App());
}

// TODO: fix, app bar logo
22 changes: 22 additions & 0 deletions app/lib/models/event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:json_annotation/json_annotation.dart';

part 'event.g.dart';

@JsonSerializable(fieldRename: FieldRename.none, explicitToJson: true)
class Event {
Event({
required this.id,
required this.updatedAt,
required this.createdAt,
required this.sensorId,
});

int? id;
String? updatedAt;
String? createdAt;
int? sensorId;

factory Event.fromJson(Map<String, dynamic> json) => _$EventFromJson(json);

Map<String, dynamic> toJson() => _$EventToJson(this);
}
21 changes: 21 additions & 0 deletions app/lib/models/event.g.dart

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

18 changes: 18 additions & 0 deletions app/lib/models/responses/list_events_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:baby_guard/models/event.dart';
import 'package:json_annotation/json_annotation.dart';

part 'list_events_response.g.dart';

@JsonSerializable(fieldRename: FieldRename.none, explicitToJson: true)
class ListEventsResponse {
ListEventsResponse({
required this.events,
});

List<Event>? events;

factory ListEventsResponse.fromJson(Map<String, dynamic> json) =>
_$ListEventsResponseFromJson(json);

Map<String, dynamic> toJson() => _$ListEventsResponseToJson(this);
}
19 changes: 19 additions & 0 deletions app/lib/models/responses/list_events_response.g.dart

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

Empty file removed app/lib/modules/.gitkeep
Empty file.
27 changes: 27 additions & 0 deletions app/lib/modules/events/event_list_tile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:baby_guard/models/event.dart';
import 'package:baby_guard/utils/formatting.dart';
import 'package:baby_guard/utils/utils.dart';
import 'package:flutter/material.dart';

class EventListTile extends StatelessWidget {
const EventListTile({
super.key,
required this.event,
});

final Event event;

@override
Widget build(BuildContext context) {
final date = Formatting.date(
Utils.dateTimeFromString(event.createdAt),
mode: DateFormattingMode.slashSeparatedLong,
);

return ListTile(
title: const Text('Detecção'),
leading: const Icon(Icons.sensors),
subtitle: Text('Disparado em $date'),
);
}
}
79 changes: 79 additions & 0 deletions app/lib/modules/sensors/create_sensor_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'package:baby_guard/blocs/sensor/sensor_bloc.dart';
import 'package:baby_guard/blocs/sensor/sensor_event.dart';
import 'package:baby_guard/blocs/sensor/sensor_state.dart';
import 'package:baby_guard/models/sensor.dart';
import 'package:baby_guard/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class CreateSensorDialog extends StatelessWidget {
CreateSensorDialog({super.key});

final _formKey = GlobalKey<FormState>();

final _identifierController = TextEditingController();

@override
Widget build(BuildContext context) {
return BlocConsumer<SensorBloc, SensorState>(
listener: (context, state) async {
if (state is CreateSensorDoneState) {
await Utils.popNavigation(context);
}
},
builder: (context, state) {
return AlertDialog(
title: const Text('Criar sensor'),
content: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextFormField(
controller: _identifierController,
decoration: const InputDecoration(
labelText: 'Identificador',
),
validator: (value) {
value ??= '';

if (value.length < 3) {
return 'Identificador muito curto.';
}

return null;
},
),
],
),
),
actions: [
TextButton(
onPressed: state is CreateSensorLoadingState
? null
: () {
if (_formKey.currentState?.validate() != true) {
return;
}

final sensor = Sensor(
id: null,
updatedAt: null,
createdAt: null,
userId: null,
alias: null,
identifier: _identifierController.text,
);

BlocProvider.of<SensorBloc>(context)
.add(CreateSensorEvent(sensor: sensor));
},
child: const Text('OK'),
),
],
);
},
);
}
}
82 changes: 82 additions & 0 deletions app/lib/modules/sensors/delete_sensor_modal_bottom_sheet.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'package:baby_guard/blocs/sensor/sensor_bloc.dart';
import 'package:baby_guard/blocs/sensor/sensor_event.dart';
import 'package:baby_guard/blocs/sensor/sensor_state.dart';
import 'package:baby_guard/models/sensor.dart';
import 'package:baby_guard/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class DeleteSensorModalBottomSheet extends StatelessWidget {
const DeleteSensorModalBottomSheet({
super.key,
required this.sensor,
});

final Sensor sensor;

@override
Widget build(BuildContext context) {
return BlocConsumer<SensorBloc, SensorState>(
listener: (context, state) async {
if (state is DeleteSensorDoneState) {
await Utils.popNavigation(context);
}
},
builder: (context, state) {
return Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Remover sensor',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 16),
Text(
'Deseja remover o sensor ${sensor.identifier ?? ''}? Esta ação não pode ser desfeita.',
),
const SizedBox(height: 16),
IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: OutlinedButton(
style: OutlinedButton.styleFrom(
foregroundColor: Colors.redAccent,
),
onPressed: state is DeleteSensorLoadingState
? null
: () async {
await Utils.popNavigation(context);
},
child: const Icon(Icons.close),
),
),
const SizedBox(width: 16),
Expanded(
child: OutlinedButton(
style: OutlinedButton.styleFrom(
foregroundColor: Colors.greenAccent,
),
onPressed: state is DeleteSensorLoadingState
? null
: () {
BlocProvider.of<SensorBloc>(context)
.add(DeleteSensorEvent(sensor: sensor));
},
child: const Icon(Icons.check),
),
),
],
),
),
],
),
);
},
);
}
}
Loading

0 comments on commit 34afb8d

Please sign in to comment.