Skip to content

Commit

Permalink
Use User Session
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaffailhami committed Jun 28, 2024
1 parent 0b333a1 commit 2f8b3b2
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/cubits/pengguna_masuk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class PenggunaMasukCubit extends Cubit<Pengguna?> {
emit(pengguna);
}

Future<void> masukTanpaSandi({required String nik}) async {
final pengguna = await Pengguna.get(nik);

emit(pengguna);
}

Future<void> edit(Pengguna pengguna) async {
await Pengguna.edit(pengguna.nik, pengguna);

Expand Down
33 changes: 32 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:gasku/cubits/daftar_pangkalan.dart';
import 'package:gasku/cubits/pengguna_masuk.dart';
import 'package:gasku/cubits/posisi_pengguna.dart';
import 'package:gasku/pages/main.dart';
import 'package:gasku/pages/masuk.dart';
import 'package:gasku/utils/show_loading_screen.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() async {
await dotenv.load(fileName: ".env");
Expand Down Expand Up @@ -43,7 +46,35 @@ class MyApp extends StatelessWidget {
dragHandleColor: Colors.white,
backgroundColor: Colors.transparent,
)),
home: const MyMasukPage(),
home: FutureBuilder(
future: SharedPreferences.getInstance(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const SizedBox.shrink();
} else {
final nikPengguna = snapshot.data!.getString('nik_pengguna_masuk');

if (nikPengguna == null) return const MyMasukPage();

return FutureBuilder(
future: context
.read<PenggunaMasukCubit>()
.masukTanpaSandi(nik: nikPengguna),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (context.read<PenggunaMasukCubit>().state == null) {
return const MyMasukPage();
}

return const MyMainPage();
} else {
return const SizedBox.shrink();
}
},
);
}
},
),
);
}
}
4 changes: 4 additions & 0 deletions lib/pages/masuk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:gasku/utils/show_loading_screen.dart';
import 'package:gasku/widgets/filled_button.dart';
import 'package:gasku/widgets/text_form_field.dart';
import 'package:gasku/pages/daftar.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MyMasukPage extends StatefulWidget {
const MyMasukPage({super.key});
Expand All @@ -32,6 +33,9 @@ class _MyMasukPageState extends State<MyMasukPage> {
.read<PenggunaMasukCubit>()
.masuk(nik: _nik, kataSandi: _kataSandi);

(await SharedPreferences.getInstance())
.setString('nik_pengguna_masuk', _nik);

Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => MyMainPage()),
Expand Down
37 changes: 30 additions & 7 deletions lib/pages/profil.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import 'package:gasku/pages/edit_profil.dart';
import 'package:gasku/pages/faq.dart';
import 'package:gasku/pages/kontak_kami.dart';
import 'package:gasku/pages/masuk.dart';
import 'package:gasku/utils/show_loading_screen.dart';
import 'package:gasku/widgets/divider.dart';
import 'package:gasku/widgets/list_tile_button.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MyProfilPage extends StatelessWidget {
const MyProfilPage({super.key});
Expand All @@ -26,14 +28,35 @@ class MyProfilPage extends StatelessWidget {
child: const Text('Batal'),
),
TextButton(
onPressed: () {
context.read<PenggunaMasukCubit>().keluar();
onPressed: () async {
showLoadingScreen(context);

Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => const MyMasukPage()),
(_) => false,
);
try {
(await SharedPreferences.getInstance())
.remove('nik_pengguna_masuk');

if (!context.mounted) throw 'Terjadi kesalahan';

context.read<PenggunaMasukCubit>().keluar();

Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => const MyMasukPage()),
(_) => false,
);
} catch (e) {
Navigator.pop(context);

ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(e.toString()),
backgroundColor: Theme.of(context).colorScheme.primary,
action: SnackBarAction(
label: 'Tutup',
onPressed: () =>
ScaffoldMessenger.of(context).hideCurrentSnackBar(),
),
));
}
},
child: const Text('Keluar'),
),
Expand Down
64 changes: 64 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.2"
file:
dependency: transitive
description:
name: file
sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c"
url: "https://pub.dev"
source: hosted
version: "7.0.0"
file_selector_linux:
dependency: transitive
description:
Expand Down Expand Up @@ -568,6 +576,62 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.1.0"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
sha256: d3bbe5553a986e83980916ded2f0b435ef2e1893dfaa29d5a7a790d0eca12180
url: "https://pub.dev"
source: hosted
version: "2.2.3"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
sha256: "93d0ec9dd902d85f326068e6a899487d1f65ffcd5798721a95330b26c8131577"
url: "https://pub.dev"
source: hosted
version: "2.2.3"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
sha256: "0a8a893bf4fd1152f93fec03a415d11c27c74454d96e2318a7ac38dd18683ab7"
url: "https://pub.dev"
source: hosted
version: "2.4.0"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
sha256: "9f2cbcf46d4270ea8be39fa156d86379077c8a5228d9dfdb1164ae0bb93f1faa"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
sha256: "22e2ecac9419b4246d7c22bfbbda589e3acf5c0351137d87dd2939d984d37c3b"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
sha256: "9aee1089b36bd2aafe06582b7d7817fd317ef05fc30e6ba14bff247d0933042a"
url: "https://pub.dev"
source: hosted
version: "2.3.0"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
sha256: "841ad54f3c8381c480d0c9b508b89a34036f512482c407e6df7a9c4aa2ef8f59"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
sky_engine:
dependency: transitive
description: flutter
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies:
image_picker: ^1.1.2
url_launcher: ^6.3.0
geolocator: ^12.0.0
shared_preferences: ^2.2.3

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 2f8b3b2

Please sign in to comment.