mirror of https://github.com/evanferrao/mynotes
105 lines
3.5 KiB
Dart
105 lines
3.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:mynotes/constants/routes.dart';
|
|
import 'package:mynotes/enums/menu_action.dart';
|
|
import 'package:mynotes/services/auth/auth_service.dart';
|
|
import 'package:mynotes/services/crud/notes_service.dart';
|
|
import 'package:mynotes/utilities/dialogs/logout_dialog.dart';
|
|
import 'package:mynotes/views/notes/notes_list_view.dart';
|
|
|
|
class NotesView extends StatefulWidget {
|
|
const NotesView({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
_NotesViewState createState() => _NotesViewState();
|
|
}
|
|
|
|
class _NotesViewState extends State<NotesView> {
|
|
late final NotesService _notesService;
|
|
String get userEmail => AuthService.firebase().currentUser!.email!;
|
|
|
|
@override
|
|
void initState() {
|
|
_notesService = NotesService();
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Your Notes', style: TextStyle(color: Colors.white)),
|
|
backgroundColor: Colors.blue,
|
|
actions: [
|
|
IconButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pushNamed(createOrUpdateNoteRoute);
|
|
},
|
|
icon: const Icon(Icons.add),
|
|
),
|
|
PopupMenuButton<MenuAction>(
|
|
onSelected: (value) async {
|
|
switch (value) {
|
|
case MenuAction.logout:
|
|
final shouldLogout = await showLogOutDialog(context);
|
|
if (shouldLogout) {
|
|
await AuthService.firebase().logOut();
|
|
Navigator.of(context).pushNamedAndRemoveUntil(
|
|
loginRoute,
|
|
(_) => false,
|
|
);
|
|
}
|
|
}
|
|
},
|
|
itemBuilder: (context) {
|
|
return const [
|
|
PopupMenuItem<MenuAction>(
|
|
value: MenuAction.logout,
|
|
child: Text('Log out'),
|
|
),
|
|
];
|
|
},
|
|
)
|
|
],
|
|
),
|
|
body: FutureBuilder(
|
|
future: _notesService.getOrCreateUser(email: userEmail),
|
|
builder: (context, snapshot) {
|
|
switch (snapshot.connectionState) {
|
|
case ConnectionState.done:
|
|
return StreamBuilder(
|
|
stream: _notesService.allNotes,
|
|
builder: (context, snapshot) {
|
|
switch (snapshot.connectionState) {
|
|
case ConnectionState.waiting:
|
|
case ConnectionState.active:
|
|
if (snapshot.hasData) {
|
|
final allNotes = snapshot.data as List<DatabaseNote>;
|
|
return NotesListView(
|
|
notes: allNotes,
|
|
onDeleteNote: (note) async {
|
|
await _notesService.deleteNote(id: note.id);
|
|
},
|
|
onTap: (note) {
|
|
Navigator.of(context).pushNamed(
|
|
createOrUpdateNoteRoute,
|
|
arguments: note,
|
|
);
|
|
},
|
|
);
|
|
} else {
|
|
return const CircularProgressIndicator();
|
|
}
|
|
default:
|
|
return const CircularProgressIndicator();
|
|
}
|
|
},
|
|
);
|
|
default:
|
|
return const CircularProgressIndicator();
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|