Skip to content

Commit

Permalink
Refactor E Tiket (Using model)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaffailhami committed Jun 25, 2024
1 parent 7af75cc commit c0177d9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
59 changes: 59 additions & 0 deletions lib/models/e_tiket.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:intl/intl.dart';

class ETiket {
final DateTime tanggal;
ETiket({
required this.tanggal,
});

ETiket copyWith({
DateTime? tanggal,
}) {
return ETiket(
tanggal: tanggal ?? this.tanggal,
);
}

String get nomor => 'LPG-${DateFormat('ddMMyyyy').format(tanggal)}';

String get tanggalKedaluwarsa => DateFormat('dd/MM/yyyy')
.format(tanggal.add(Duration(days: 6)))
.toString();

String generateUrl(String nik) =>
'${dotenv.env['SERVER_URL']}/konfirmasi-e-tiket/${nik}/${sha1.convert(utf8.encode('${nik}(${DateFormat('dd-MM-yyyy').format(tanggal).toString()})')).toString()}';

Map<String, dynamic> toMap() {
return <String, dynamic>{
'tanggal': tanggal.millisecondsSinceEpoch,
};
}

factory ETiket.fromMap(Map<String, dynamic> map) {
return ETiket(
tanggal: DateTime.fromMillisecondsSinceEpoch(map['tanggal'] as int),
);
}

String toJson() => json.encode(toMap());

factory ETiket.fromJson(String source) =>
ETiket.fromMap(json.decode(source) as Map<String, dynamic>);

@override
String toString() => 'ETiket(tanggal: $tanggal)';

@override
bool operator ==(covariant ETiket other) {
if (identical(this, other)) return true;

return other.tanggal == tanggal;
}

@override
int get hashCode => tanggal.hashCode;
}
25 changes: 8 additions & 17 deletions lib/pages/e_tiket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:gasku/cubits/pengguna_masuk.dart';
import 'package:gasku/models/e_tiket.dart';
import 'package:gasku/models/pengguna.dart';
import 'package:intl/intl.dart';
import 'package:qr_flutter/qr_flutter.dart';
Expand All @@ -18,21 +19,11 @@ class MyETiketPage extends StatelessWidget {
builder: (context, state) {
if (state == null) return SizedBox.shrink();

final DateTime dateTime = DateTime.now();
final DateTime monday =
dateTime.subtract(Duration(days: dateTime.weekday - 1));
final DateTime currentDateTime = DateTime.now();
final DateTime senin = currentDateTime
.subtract(Duration(days: currentDateTime.weekday - 1));

final String formattedMonday =
DateFormat('dd-MM-yyyy').format(monday).toString();
final String formattedMonday2 =
DateFormat('ddMMyyyy').format(monday).toString();

final DateTime expirationDate = monday.add(Duration(days: 6));
final String formattedExpirationDate =
DateFormat('dd/MM/yyyy').format(expirationDate).toString();

final String eTiketUrl =
'${dotenv.env['SERVER_URL']}/konfirmasi-e-tiket/${state.nik}/${sha1.convert(utf8.encode('${state.nik}($formattedMonday)')).toString()}';
final eTiket = ETiket(tanggal: senin);

return Center(
child: Container(
Expand Down Expand Up @@ -119,7 +110,7 @@ class MyETiketPage extends StatelessWidget {
),
SizedBox(height: 20),
QrImageView(
data: eTiketUrl,
data: eTiket.generateUrl(state.nik),
version: QrVersions.auto,
size: MediaQuery.of(context).size.width * 0.55,
backgroundColor: Colors.white,
Expand All @@ -133,7 +124,7 @@ class MyETiketPage extends StatelessWidget {
?.copyWith(color: Colors.white),
),
Text(
'LPG-$formattedMonday2',
eTiket.nomor,
style: Theme.of(context)
.textTheme
.headlineMedium
Expand All @@ -142,7 +133,7 @@ class MyETiketPage extends StatelessWidget {
color: Colors.white),
),
Text(
'Berlaku sampai $formattedExpirationDate',
'Berlaku sampai ${eTiket.tanggalKedaluwarsa}',
style: TextStyle(color: Colors.white),
),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/material.dart';
import 'package:qr_flutter/qr_flutter.dart';

class ETiket extends StatelessWidget {
const ETiket({super.key});
class ETiketListTile extends StatelessWidget {
const ETiketListTile({super.key});

@override
Widget build(BuildContext context) {
Expand Down

0 comments on commit c0177d9

Please sign in to comment.