From bd14b5e994b4a3bc7cc357a4479a55fe95e3ee6b Mon Sep 17 00:00:00 2001 From: Godsend Joseph Date: Sun, 9 Jun 2024 11:01:14 +0100 Subject: [PATCH] setting up the auth model for user --- ios/Podfile.lock | 8 +- lib/app/helpers/hive_manager.dart | 17 ++++ .../home/views/cloud_notes/auth/wrapper.dart | 34 ++++---- .../views/cloud_notes/models/user_model.dart | 80 +++++++++++++++++++ .../cloud_notes/models/user_model.g.dart | 74 +++++++++++++++++ lib/utils/const_values.dart | 6 ++ lib/utils/message_dialog.dart | 48 +++++++++++ pubspec.lock | 9 +++ pubspec.yaml | 4 + 9 files changed, 261 insertions(+), 19 deletions(-) create mode 100644 lib/app/resources/home/views/cloud_notes/models/user_model.dart create mode 100644 lib/app/resources/home/views/cloud_notes/models/user_model.g.dart create mode 100644 lib/utils/message_dialog.dart diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 9d6423a..02aa196 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -15,9 +15,9 @@ PODS: - FirebaseCoreInternal (~> 10.0) - GoogleUtilities/Environment (~> 7.12) - GoogleUtilities/Logger (~> 7.12) - - FirebaseCoreInternal (10.25.0): + - FirebaseCoreInternal (10.27.0): - "GoogleUtilities/NSData+zlib (~> 7.8)" - - FirebaseInstallations (10.25.0): + - FirebaseInstallations (10.27.0): - FirebaseCore (~> 10.0) - GoogleUtilities/Environment (~> 7.8) - GoogleUtilities/UserDefaults (~> 7.8) @@ -133,8 +133,8 @@ SPEC CHECKSUMS: firebase_core: 0b39f4f424e02eecabb2356ddf331fa07b772af8 firebase_messaging: 8999827b6efc9c3ab4b1f9dc246deaa7f13dbf88 FirebaseCore: 7ec4d0484817f12c3373955bc87762d96842d483 - FirebaseCoreInternal: 910a81992c33715fec9263ca7381d59ab3a750b7 - FirebaseInstallations: 91950fe859846fff0fbd296180909dd273103b09 + FirebaseCoreInternal: 4b297a2d56063dbea2c1d0d04222d44a8d058862 + FirebaseInstallations: 766dabca09fd94aef922538aaf144cc4a6fb6869 FirebaseMessaging: 88950ba9485052891ebe26f6c43a52bb62248952 Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7 flutter_paystack: 50021637b71e7712780744f69d2843e71b65175d diff --git a/lib/app/helpers/hive_manager.dart b/lib/app/helpers/hive_manager.dart index 8bcbc30..360a0e6 100644 --- a/lib/app/helpers/hive_manager.dart +++ b/lib/app/helpers/hive_manager.dart @@ -1,6 +1,8 @@ import 'dart:io'; import 'package:hive_flutter/hive_flutter.dart'; +import 'package:note_app/app/resources/home/views/cloud_notes/models/cloud_note_model.dart'; +import 'package:note_app/app/resources/home/views/cloud_notes/models/user_model.dart'; import 'package:note_app/models/note_model.dart'; import 'package:note_app/utils/const_values.dart'; import 'package:path_provider/path_provider.dart'; @@ -16,6 +18,8 @@ class HiveManager { static late Box _noteModelBox; static late Box _deleteNoteModelBox; + static late Box _cloudNoteModelBox; + static late Box _userModelBox; Future init() async { Directory directory = await getApplicationDocumentsDirectory(); @@ -24,10 +28,23 @@ class HiveManager { ..registerAdapter(NoteModelAdapter()); _noteModelBox = await Hive.openBox(noteBox); _deleteNoteModelBox = await Hive.openBox(deletedNotes); + + Hive + ..init(directory.path) + ..registerAdapter(CloudNoteModelAdapter()); + _cloudNoteModelBox = await Hive.openBox(cloudNoteBox); + + Hive + ..init(directory.path) + ..registerAdapter(UserModelAdapter()); + _userModelBox = await Hive.openBox(userBox); + } Box get noteModelBox => _noteModelBox; Box get deleteNoteModelBox => _deleteNoteModelBox; + Box get cloudNoteModelBox => _cloudNoteModelBox; + Box get userModelBox => _userModelBox; } \ No newline at end of file diff --git a/lib/app/resources/home/views/cloud_notes/auth/wrapper.dart b/lib/app/resources/home/views/cloud_notes/auth/wrapper.dart index b9ae482..ce7c6cb 100644 --- a/lib/app/resources/home/views/cloud_notes/auth/wrapper.dart +++ b/lib/app/resources/home/views/cloud_notes/auth/wrapper.dart @@ -1,4 +1,10 @@ import 'package:flutter/material.dart'; +import 'package:hive_flutter/hive_flutter.dart'; +import 'package:note_app/app/helpers/hive_manager.dart'; +import 'package:note_app/app/resources/home/views/cloud_notes/auth/login_screen.dart'; +import 'package:note_app/app/resources/home/views/cloud_notes/controller/cloud_notes.dart'; +import 'package:note_app/app/resources/home/views/cloud_notes/models/user_model.dart'; +import 'package:note_app/utils/const_values.dart'; class Wrapper extends StatefulWidget { const Wrapper({super.key}); @@ -10,20 +16,18 @@ class Wrapper extends StatefulWidget { class _WrapperState extends State { @override Widget build(BuildContext context) { - return const Placeholder(); - - // final hiveData = HiveManager().userModelBox; - // return ValueListenableBuilder( - // valueListenable: hiveData.listenable(), - // builder: (context, Box userData, _) { - // if (userData.get(tokenKey) == null) { - // return OnBoardingScreen(); - // } else { - // // return const HomeScreen(); - // // return const PTabControl(); - // return const MTabControl(); - // } - // }, - // ); + final hiveData = HiveManager().userModelBox; + return ValueListenableBuilder( + valueListenable: hiveData.listenable(), + builder: (context, Box userData, _) { + if (userData.get(tokenKey) == null) { + return const LoginScreen(); + } else { + // return const HomeScreen(); + // return const PTabControl(); + return const CloudNotesScreen(); + } + }, + ); } } diff --git a/lib/app/resources/home/views/cloud_notes/models/user_model.dart b/lib/app/resources/home/views/cloud_notes/models/user_model.dart new file mode 100644 index 0000000..7e5de71 --- /dev/null +++ b/lib/app/resources/home/views/cloud_notes/models/user_model.dart @@ -0,0 +1,80 @@ +import 'package:hive/hive.dart'; + +part 'user_model.g.dart'; + +@HiveType(typeId: 2) +class UserModel { + @HiveField(1) + final int? id; + + @HiveField(2) + final String? uuid; + + @HiveField(3) + final String? firstName; + + @HiveField(4) + final String? lastName; + + @HiveField(5) + final String? userName; + + @HiveField(6) + final String? email; + + @HiveField(7) + final String? emailVerified; + + @HiveField(8) + final int? hasSubscription; + + @HiveField(9) + final int? hasExceedTier; + + @HiveField(10) + final String? planDuration; + + @HiveField(11) + final String? planSubDate; + + @HiveField(12) + final String? accessToken; + + UserModel({ + this.id, + this.uuid, + this.firstName, + this.lastName, + this.userName, + this.email, + this.emailVerified, + this.hasSubscription, + this.hasExceedTier, + this.planDuration, + this.planSubDate, + this.accessToken, + }); + + + factory UserModel.fromJsonLocalToken(responseData) { + return UserModel( + accessToken: responseData, + ); + } + + factory UserModel.fromJsonUserDetails(responseData) { + return UserModel( + id: responseData['data']['id'], + uuid: responseData['data']['uuid'], + firstName: responseData['data']['first_name'], + lastName: responseData['data']['last_name'], + userName: responseData['data']['username'], + email: responseData['data']['email'], + emailVerified: responseData['data']['email_verified_at'], + hasSubscription: responseData['data']['has_subscription'], + hasExceedTier: responseData['data']['has_exceed_tier'], + planDuration: responseData['data']['plan_duration'], + planSubDate: responseData['data']['plan_sub_date'], + ); + } +} \ No newline at end of file diff --git a/lib/app/resources/home/views/cloud_notes/models/user_model.g.dart b/lib/app/resources/home/views/cloud_notes/models/user_model.g.dart new file mode 100644 index 0000000..dffa83a --- /dev/null +++ b/lib/app/resources/home/views/cloud_notes/models/user_model.g.dart @@ -0,0 +1,74 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'user_model.dart'; + +// ************************************************************************** +// TypeAdapterGenerator +// ************************************************************************** + +class UserModelAdapter extends TypeAdapter { + @override + final int typeId = 2; + + @override + UserModel read(BinaryReader reader) { + final numOfFields = reader.readByte(); + final fields = { + for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), + }; + return UserModel( + id: fields[1] as int?, + uuid: fields[2] as String?, + firstName: fields[3] as String?, + lastName: fields[4] as String?, + userName: fields[5] as String?, + email: fields[6] as String?, + emailVerified: fields[7] as String?, + hasSubscription: fields[8] as int?, + hasExceedTier: fields[9] as int?, + planDuration: fields[10] as String?, + planSubDate: fields[11] as String?, + accessToken: fields[12] as String?, + ); + } + + @override + void write(BinaryWriter writer, UserModel obj) { + writer + ..writeByte(12) + ..writeByte(1) + ..write(obj.id) + ..writeByte(2) + ..write(obj.uuid) + ..writeByte(3) + ..write(obj.firstName) + ..writeByte(4) + ..write(obj.lastName) + ..writeByte(5) + ..write(obj.userName) + ..writeByte(6) + ..write(obj.email) + ..writeByte(7) + ..write(obj.emailVerified) + ..writeByte(8) + ..write(obj.hasSubscription) + ..writeByte(9) + ..write(obj.hasExceedTier) + ..writeByte(10) + ..write(obj.planDuration) + ..writeByte(11) + ..write(obj.planSubDate) + ..writeByte(12) + ..write(obj.accessToken); + } + + @override + int get hashCode => typeId.hashCode; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is UserModelAdapter && + runtimeType == other.runtimeType && + typeId == other.typeId; +} diff --git a/lib/utils/const_values.dart b/lib/utils/const_values.dart index ac18595..71ee6cf 100644 --- a/lib/utils/const_values.dart +++ b/lib/utils/const_values.dart @@ -30,6 +30,12 @@ Color? cardColor = Colors.grey[850]; // Hive details const String noteBox = 'notebox'; +const String cloudNoteBox = 'cloudNoteBox'; +const String userBox = 'userBox'; const String deletedNotes = 'deletedNotes'; const String appHiveKey = 'state'; const String deleteNote = 'deleteNote'; + + +const String userKey = 'userKey'; +const String tokenKey = 'tokenKey'; diff --git a/lib/utils/message_dialog.dart b/lib/utils/message_dialog.dart new file mode 100644 index 0000000..ce216f5 --- /dev/null +++ b/lib/utils/message_dialog.dart @@ -0,0 +1,48 @@ +import 'package:flashy_flushbar/flashy_flushbar.dart'; +import 'package:flutter/material.dart'; + +void showError(String errorMsg) { + FlashyFlushbar( + leadingWidget: const Icon( + Icons.error_outline, + color: Colors.orange, + size: 24, + ), + message: errorMsg, + duration: const Duration(seconds: 5), + trailingWidget: IconButton( + icon: const Icon( + Icons.close, + color: Colors.black, + size: 24, + ), + onPressed: () { + FlashyFlushbar.cancel(); + }, + ), + isDismissible: true, + ).show(); +} + +void showSuccess(String msg) { + FlashyFlushbar( + leadingWidget: const Icon( + Icons.check_circle_outline_outlined, + color: Colors.green, + size: 24, + ), + message: msg, + duration: const Duration(seconds: 5), + trailingWidget: IconButton( + icon: const Icon( + Icons.close, + color: Colors.black, + size: 24, + ), + onPressed: () { + FlashyFlushbar.cancel(); + }, + ), + isDismissible: true, + ).show(); +} \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 9aa1fa6..b678275 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -281,6 +281,15 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.0" + flashy_flushbar: + dependency: "direct main" + description: + path: "." + ref: c9af9f8 + resolved-ref: c9af9f8b58dc8cd0b4ea9c6d21b91e99548da6a7 + url: "https://github.com/quiet-programmer/flashy_flushbar.git" + source: git + version: "1.1.0" flutter: dependency: "direct main" description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index 91ace6b..81c7b7f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -42,6 +42,10 @@ dependencies: firebase_messaging: ^14.6.7 firebase_core_web: ^2.6.0 go_router: ^14.1.1 + flashy_flushbar: + git: + url: https://github.com/quiet-programmer/flashy_flushbar.git + ref: c9af9f8 dev_dependencies: flutter_test: