Skip to content

Commit

Permalink
create, delete edit metadata for cues
Browse files Browse the repository at this point in the history
  • Loading branch information
RedyAu committed Nov 23, 2024
1 parent 7354472 commit 7a46947
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 24 deletions.
27 changes: 6 additions & 21 deletions lib/services/cue/write_cue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:uuid/v4.dart';

import '../../data/cue/cue.dart';
import '../../data/cue/slide.dart';
import '../../data/database.dart';

part 'write_cue.g.dart';

@riverpod
Future insertNewCue(Ref ref, {required String title, String? description}) async {
await db.into(db.cues).insert(
Future<Cue> insertNewCue({required String title, required String description}) async {
return await db.into(db.cues).insertReturning(
CuesCompanion(
id: Value.absent(),
uuid: Value(UuidV4().generate()),
title: Value(title),
description: description != null ? Value(description) : Value.absent(),
description: Value(description),
cueVersion: Value(currentCueVersion),
content: Value([]),
),
);
}

@riverpod
Future updateCueMetadataFor(Ref ref, int id, {String? title, String? description}) async {
Future updateCueMetadataFor(int id, {String? title, String? description}) async {
await (db.update(db.cues)..where((c) => c.id.equals(id))).write(
CuesCompanion(
title: Value.absentIfNull(title),
Expand All @@ -33,16 +28,6 @@ Future updateCueMetadataFor(Ref ref, int id, {String? title, String? description
);
}

@riverpod
Future updateSlidesOfCue(Ref ref, Cue cue) async {
//cue.updateContentJson();

throw UnimplementedError();
}

/*
@riverpod
Future updateCueWith(Ref ref, int id, CuesCompanion data) async {
await (db.update(db.cues)..where((e) => e.id.equals(id))).write(data);
Future deleteCueWithUuid(String uuid) {
return db.cues.deleteWhere((c) => c.uuid.equals(uuid));
}
*/
145 changes: 145 additions & 0 deletions lib/ui/base/cues/dialogs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:lyric/services/cue/write_cue.dart';

import '../../../data/cue/cue.dart';

class EditCueDialog extends StatefulWidget {
/// If cue is null, dialog adds new cue
const EditCueDialog({
this.cue,
super.key,
});

final Cue? cue;

@override
EditCueDialogState createState() => EditCueDialogState();
}

class EditCueDialogState extends State<EditCueDialog> {
@override
void initState() {
_titleController = TextEditingController(text: widget.cue?.title);
_descriptionController = TextEditingController(text: widget.cue?.description);
super.initState();
}

@override
void dispose() {
_titleController.dispose();
_descriptionController.dispose();
super.dispose();
}

final _formKey = GlobalKey<FormState>();
late final TextEditingController _titleController;
late final TextEditingController _descriptionController;

bool isSaving = false;

@override
Widget build(BuildContext context) {
onSubmit() async {
if (_formKey.currentState!.validate()) {
setState(() => isSaving = true);

if (widget.cue != null) {
updateCueMetadataFor(widget.cue!.id,
title: _titleController.text, description: _descriptionController.text);
// ignore: use_build_context_synchronously
context.pop();
} else {
final createdCue =
await insertNewCue(title: _titleController.text, description: _descriptionController.text);
// ignore: use_build_context_synchronously
context.pop();
// ignore: use_build_context_synchronously
context.push('/cue/${createdCue.uuid}');
}
}
}

return AlertDialog.adaptive(
title: widget.cue != null ? Text('Lista szerkesztése') : Text('Lista létrehozása'),
content: SingleChildScrollView(
child: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
controller: _titleController,
autofocus: true,
decoration: InputDecoration(
hintText: 'Cím',
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Kötelező címet adni a listának!';
}
return null;
},
onFieldSubmitted: (_) => onSubmit(),
),
TextFormField(
controller: _descriptionController,
maxLines: null,
decoration: InputDecoration(
hintText: 'Leírás',
),
),
],
),
),
),
actions: [
TextButton(
onPressed: isSaving ? null : () => context.pop(),
child: Text('Mégse'),
),
FilledButton(
onPressed: onSubmit,
child: AnimatedSize(
duration: Durations.medium1,
child: isSaving
? SizedBox(width: 50, child: LinearProgressIndicator())
: Text(widget.cue != null ? 'Mentés' : 'Létrehozás')),
),
],
);
}
}

class DeleteCueDialog extends StatelessWidget {
const DeleteCueDialog({
super.key,
required this.cue,
});

final Cue cue;

@override
Widget build(BuildContext context) {
return AlertDialog.adaptive(
title: Text('${cue.title} törlése - biztos vagy benne?'),
actions: [
TextButton.icon(
label: Text(
'Törlés',
style: TextStyle(color: Theme.of(context).textTheme.bodySmall!.color),
),
icon: IconTheme(
data: IconThemeData(color: Colors.red),
child: Icon(Icons.delete_forever),
),
onPressed: () {
deleteCueWithUuid(cue.uuid);
context.pop();
},
),
FilledButton(onPressed: () => context.pop(), child: Text('Mégse')),
],
);
}
}
29 changes: 26 additions & 3 deletions lib/ui/base/cues/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:lyric/services/cue/cues.dart';
import 'package:lyric/ui/common/error.dart';

import '../../../data/cue/cue.dart';
import 'dialogs.dart';

class SetsPage extends ConsumerStatefulWidget {
const SetsPage({super.key});
Expand All @@ -23,7 +24,10 @@ class _SetsPageState extends ConsumerState<SetsPage> {
title: Text('Listáim'),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () {},
onPressed: () => showAdaptiveDialog(
context: context,
builder: (context) => EditCueDialog(),
),
label: Text('Új lista'),
icon: Icon(Icons.add_box_outlined),
),
Expand Down Expand Up @@ -58,8 +62,27 @@ class CueTile extends StatelessWidget {
Widget build(BuildContext context) {
return ListTile(
title: Text(cue.title),
subtitle: Text(cue.description),
onTap: () => context.push('/cue/${cue.id}'),
subtitle: cue.description.isNotEmpty ? Text(cue.description) : null,
onTap: () => context.push('/cue/${cue.uuid}'),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: Icon(Icons.edit_outlined),
onPressed: () => showAdaptiveDialog(
context: context,
builder: (context) => EditCueDialog(cue: cue),
),
),
IconButton(
icon: Icon(Icons.delete_outline),
onPressed: () => showAdaptiveDialog(
context: context,
builder: (context) => DeleteCueDialog(cue: cue),
),
),
],
),
);
}
}

0 comments on commit 7a46947

Please sign in to comment.