Skip to content

Commit

Permalink
setting up the auth model for user
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-which-qp committed Jun 9, 2024
1 parent c960d46 commit bd14b5e
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 19 deletions.
8 changes: 4 additions & 4 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions lib/app/helpers/hive_manager.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,6 +18,8 @@ class HiveManager {

static late Box<NoteModel> _noteModelBox;
static late Box<NoteModel> _deleteNoteModelBox;
static late Box<CloudNoteModel> _cloudNoteModelBox;
static late Box<UserModel> _userModelBox;

Future<void> init() async {
Directory directory = await getApplicationDocumentsDirectory();
Expand All @@ -24,10 +28,23 @@ class HiveManager {
..registerAdapter(NoteModelAdapter());
_noteModelBox = await Hive.openBox<NoteModel>(noteBox);
_deleteNoteModelBox = await Hive.openBox<NoteModel>(deletedNotes);

Hive
..init(directory.path)
..registerAdapter(CloudNoteModelAdapter());
_cloudNoteModelBox = await Hive.openBox<CloudNoteModel>(cloudNoteBox);

Hive
..init(directory.path)
..registerAdapter(UserModelAdapter());
_userModelBox = await Hive.openBox<UserModel>(userBox);

}


Box<NoteModel> get noteModelBox => _noteModelBox;
Box<NoteModel> get deleteNoteModelBox => _deleteNoteModelBox;
Box<CloudNoteModel> get cloudNoteModelBox => _cloudNoteModelBox;
Box<UserModel> get userModelBox => _userModelBox;

}
34 changes: 19 additions & 15 deletions lib/app/resources/home/views/cloud_notes/auth/wrapper.dart
Original file line number Diff line number Diff line change
@@ -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});
Expand All @@ -10,20 +16,18 @@ class Wrapper extends StatefulWidget {
class _WrapperState extends State<Wrapper> {
@override
Widget build(BuildContext context) {
return const Placeholder();

// final hiveData = HiveManager().userModelBox;
// return ValueListenableBuilder(
// valueListenable: hiveData.listenable(),
// builder: (context, Box<UserModel> 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<UserModel> userData, _) {
if (userData.get(tokenKey) == null) {
return const LoginScreen();
} else {
// return const HomeScreen();
// return const PTabControl();
return const CloudNotesScreen();
}
},
);
}
}
80 changes: 80 additions & 0 deletions lib/app/resources/home/views/cloud_notes/models/user_model.dart
Original file line number Diff line number Diff line change
@@ -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'],
);
}
}
74 changes: 74 additions & 0 deletions lib/app/resources/home/views/cloud_notes/models/user_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/utils/const_values.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
48 changes: 48 additions & 0 deletions lib/utils/message_dialog.dart
Original file line number Diff line number Diff line change
@@ -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();
}
9 changes: 9 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit bd14b5e

Please sign in to comment.