Skip to content

Commit

Permalink
Implement end time editing & tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dummyx committed Apr 11, 2021
1 parent ff1023b commit d8d530d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 60 deletions.
94 changes: 54 additions & 40 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'src/qr.dart';
import 'src/db.dart';
import 'src/entry.dart';
import 'package:intl/intl.dart';

var db = DatabaseProvider();
final DateFormat formatter = DateFormat('yyyy-MM-dd HH:mm');
final DateFormat hourminuteFormatter = DateFormat('HH:mm');

void main() async {
WidgetsFlutterBinding.ensureInitialized();

await db.open();
List<Entry> entries = await getData();
runApp(Home(entries: entries));
Expand All @@ -26,61 +30,70 @@ class _HomeState extends State {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
/*leading: IconButton(
home: Scaffold(
appBar: AppBar(
/*leading: IconButton(
icon: Icon(Icons.menu),
tooltip: 'Navigation menu',
onPressed: null,
),*/
title: Text("HistoryKiroku"),
/*actions: <Widget>[
title: Text("HistoryKiroku"),
/*actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
tooltip: 'Search',
onPressed: null,
),
],*/
),
body: ListView.builder(
itemCount: entries.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
'id:${entries[index].id} Classroom:${entries[index].classroom} Seat:${entries[index].seat} Period: ${entries[index].period} Time: ${DateTime.fromMillisecondsSinceEpoch(entries[index].timestampStart*1000).toString()}'),
);
},
),
floatingActionButton: Builder (builder: (context) =>FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {_navigateToNewEntry(context, generateNewEntry(''));} ,
))
/*Builder(
builder: (context) => PopupMenuButton<String>(
tooltip: 'Add',
child: Icon(Icons.add_circle_outline_outlined),
itemBuilder: (BuildContext context) =>
<PopupMenuItem<String>>[
PopupMenuItem<String>(
value: 'QRScan',
child: Text('Scan QR code'),
),
PopupMenuItem<String>(
value: 'EnterManually',
child: Text('Enter manually')),
],
onSelected: (index) {
_navigate(index, context);
),
body: ListView.builder(
itemCount: entries.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${entries[index].classroom}-${entries[index].seat} Period: ${entries[index].period}\n' +
'${formatter.format(DateTime.fromMillisecondsSinceEpoch(entries[index].timestampStart * 1000))} ~ ' +
'${hourminuteFormatter.format(DateTime.fromMillisecondsSinceEpoch(entries[index].timestampEnd * 1000))}'),
onTap: () {
_navigateToEditEntry(context, entries[index]);
},
))),*/
));
trailing: Text('id:${entries[index].id}'));
},
),
floatingActionButton: Builder(
builder: (context) => FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
_navigateToNewEntry(context, generateNewEntry(''));
},
))));
}

_navigateToEditEntry(BuildContext context, Entry entry) async {
var returnedEntry = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NewEntryScreen(
newEntry: entry,
isEdit: true,
)));
if (returnedEntry != null) {
entry = returnedEntry;
}
await db.update(entry);
entries = await db.getEntries();
setState(() {
entries = entries;
});
}

_navigateToNewEntry(BuildContext context, Entry newEntry) async {
var returnedEntry = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NewEntryScreen(newEntry: newEntry)));
builder: (context) => NewEntryScreen(
newEntry: newEntry,
isEdit: false,
)));
if (returnedEntry != null) {
newEntry = returnedEntry;
}
Expand All @@ -91,7 +104,8 @@ class _HomeState extends State {
});
}
}

getData() async {
List<Entry> entries = await db.getEntries();
return entries;
}
}
12 changes: 6 additions & 6 deletions lib/src/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ create table 'entries' (
for (var record in records) {
entries.add(getEntryFromRecord(record));
}
return entries;
return entries.reversed.toList();
}

Future<String> localFile() async {
Expand Down Expand Up @@ -156,15 +156,15 @@ create table 'entries' (
}

Map<String, String> parseScannedData(scannedData) {
var classroom = '';
var seat = '';
var classroom;
var seat;
try {
var data = scannedData.split('/')[1].split('-');
classroom = data[0];
seat = data[1];
}
catch (e) {

} catch (e) {
classroom = '';
seat = '';
}
return ({'classroom': classroom, 'seat': seat});
}
61 changes: 47 additions & 14 deletions lib/src/entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,43 @@ import 'qr.dart';

class NewEntryScreen extends StatefulWidget {
final Entry newEntry;
NewEntryScreen({Key? key, required this.newEntry}) : super(key: key);
final bool isEdit;
NewEntryScreen({Key? key, required this.newEntry, required this.isEdit})
: super(key: key);
@override
_NewEntryScreenState createState() => _NewEntryScreenState(newEntry);
_NewEntryScreenState createState() => _NewEntryScreenState(newEntry, isEdit);
}

class _NewEntryScreenState extends State<NewEntryScreen> {
final _formKey = GlobalKey<FormState>();
Entry newEntry;
_NewEntryScreenState(this.newEntry);
bool isEdit;
_NewEntryScreenState(this.newEntry, this.isEdit);
final classroomTextController = TextEditingController();
final seatTextController = TextEditingController();

@override
void initState() {
super.initState();
classroomTextController.text = newEntry.classroom;
seatTextController.text = newEntry.seat;
if (isEdit) {
newEntry.timestampEnd = DateTime.now().millisecondsSinceEpoch ~/ 1000;
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('New entry'),
title: Text(isEdit ? 'Edit entry' : 'New entry'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.camera_alt),
tooltip: 'Scan QR code',
onPressed: () {_scanQRCode(context);},
onPressed: () {
_scanQRCode(context);
},
),
],
),
Expand All @@ -43,15 +59,20 @@ class _NewEntryScreenState extends State<NewEntryScreen> {
children: [
...[
DateTimePicker(
initialValue: DateTime.fromMillisecondsSinceEpoch(newEntry.timestampStart*1000).toString(),
initialValue: DateTime.fromMillisecondsSinceEpoch(
newEntry.timestampStart * 1000)
.toString(),
firstDate: DateTime(2019),
lastDate: DateTime(2100),
type: DateTimePickerType.dateTimeSeparate,
dateLabelText: 'Date',
timeLabelText: 'Time',
onChanged: (datetime) {newEntry.timestampStart = DateTime.parse(datetime).millisecondsSinceEpoch~/1000;},
),

onChanged: (datetime) {
newEntry.timestampStart =
DateTime.parse(datetime).millisecondsSinceEpoch ~/
1000;
},
),
TextFormField(
controller: classroomTextController,
onChanged: (text) {
Expand Down Expand Up @@ -96,7 +117,7 @@ class _NewEntryScreenState extends State<NewEntryScreen> {
child: Text(value),
);
}).toList(),
)
),
])
].expand(
(widget) => [
Expand All @@ -105,6 +126,20 @@ class _NewEntryScreenState extends State<NewEntryScreen> {
height: 24,
)
],
),
DateTimePicker(
initialValue: DateTime.fromMillisecondsSinceEpoch(
newEntry.timestampEnd * 1000)
.toString(),
firstDate: DateTime(2019),
lastDate: DateTime(2100),
type: DateTimePickerType.dateTimeSeparate,
dateLabelText: 'Date',
timeLabelText: 'Time',
onChanged: (datetime) {
newEntry.timestampEnd =
DateTime.parse(datetime).millisecondsSinceEpoch ~/ 1000;
},
)
],
),
Expand All @@ -121,14 +156,12 @@ class _NewEntryScreenState extends State<NewEntryScreen> {
_scanQRCode(BuildContext context) async {
var scannedData = await Navigator.push(
context,
// Create the SelectionScreen in the next step.
MaterialPageRoute(builder: (context) => QRScanScreen()),
);
var parsedData = parseScannedData(scannedData);

classroomTextController.text = parsedData['classroom']!;
seatTextController.text = parsedData['seat']!;

classroomTextController.text = parsedData['classroom']!;
seatTextController.text = parsedData['seat']!;
}

@override
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies:
provider: ^5.0.0
path_provider: ^2.0.1
date_time_picker: "^2.0.0"
intl: ^0.17.0

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down

0 comments on commit d8d530d

Please sign in to comment.