Skip to content

Commit

Permalink
local storage implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
RunTerror committed Jun 8, 2024
1 parent 0226c95 commit e74e82e
Show file tree
Hide file tree
Showing 24 changed files with 582 additions and 280 deletions.
29 changes: 0 additions & 29 deletions lib/Bloc/core/queries/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,9 @@ class AuthQueries {
name
groups{
_id
title
shortcode
leader {
_id
name
}
members {
_id
name
}
beacons{
_id
}
}
beacons{
_id
title
shortcode
leader {
_id
name
}
followers{
_id
name
}
location {
lat
lon
}
startsAt
expiresAt
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Bloc/core/queries/group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ query{
_id
name
}
group{
_id
}
startsAt
expiresAt
Expand Down
149 changes: 115 additions & 34 deletions lib/Bloc/data/datasource/local/local_api.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';
import 'dart:developer';

import 'dart:io';
import 'package:beacon/Bloc/data/models/beacon/beacon_model.dart';
import 'package:beacon/Bloc/data/models/group/group_model.dart';
Expand Down Expand Up @@ -35,25 +34,26 @@ class LocalApi {
!Hive.isAdapterRegistered(10)
? Hive.registerAdapter(UserModelAdapter())
: null;
// !Hive.isAdapterRegistered(20)
// ? Hive.registerAdapter(BeaconModelAdapter())
// : null;
// !Hive.isAdapterRegistered(30)
// ? Hive.registerAdapter(GroupModelAdapter())
// : null;
// !Hive.isAdapterRegistered(40)
// ? Hive.registerAdapter(LocationModelAdapter())
// : null;
// !Hive.isAdapterRegistered(50)
// ? Hive.registerAdapter(LandMarkModelAdapter())
// : null;
!Hive.isAdapterRegistered(20)
? Hive.registerAdapter(BeaconModelAdapter())
: null;
!Hive.isAdapterRegistered(30)
? Hive.registerAdapter(GroupModelAdapter())
: null;

!Hive.isAdapterRegistered(40)
? Hive.registerAdapter(LocationModelAdapter())
: null;
!Hive.isAdapterRegistered(50)
? Hive.registerAdapter(LandMarkModelAdapter())
: null;

try {
userBox = await Hive.openBox<UserModel>(userModelbox);
// groupBox = await Hive.openBox<GroupModel>(groupModelBox);
// beaconBox = await Hive.openBox<BeaconModel>(beaconModelBox);
// locationBox = await Hive.openBox<LocationModel>(locationModelBox);
// landMarkbox = await Hive.openBox<LandMarkModel>(landMarkModelBox);
groupBox = await Hive.openBox<GroupModel>(groupModelBox);
beaconBox = await Hive.openBox<BeaconModel>(beaconModelBox);
locationBox = await Hive.openBox<LocationModel>(locationModelBox);
landMarkbox = await Hive.openBox<LandMarkModel>(landMarkModelBox);
} catch (e) {
log('error: ${e.toString()}');
}
Expand All @@ -65,37 +65,118 @@ class LocalApi {
await userBox.put('currentUser', user);
return true;
} catch (e) {
log(e.toString());
return false;
}
}

Future<void> deleteUser() async {
// clearing the info
_userModel.copyWithModel(
authToken: null,
beacons: null,
email: null,
groups: null,
id: null,
isGuest: null,
location: null,
name: null);
await userBox.delete('currentUser');

try {
_userModel.copyWithModel(
authToken: null,
beacons: null,
email: null,
groups: null,
id: null,
isGuest: null,
location: null,
name: null);
await userBox.clear();
await groupBox.clear();
await beaconBox.clear();
} catch (e) {
log(e.toString());
}
}

Future<UserModel?> fetchUser() async {
userBox = await Hive.openBox<UserModel>(userModelbox);
final user = await userBox.get('currentUser');
return user;
try {
final user = await userBox.get('currentUser');
return user;
} catch (e) {
log(e.toString());
return null;
}
}

Future<bool?> userloggedIn() async {
UserModel? user = await userBox.get('currentUser');
try {
UserModel? user = await userBox.get('currentUser');

if (user == null) {
if (user == null) {
return false;
}
_userModel = user;
return true;
} catch (e) {
log(e.toString());
return false;
}
}

Future<bool?> saveGroup(GroupModel group) async {
await deleteGroup(group.id);
try {
await groupBox.put(group.id, group);
return true;
} catch (e) {
log(e.toString());
return false;
}
_userModel = user;
return true;
}

Future<bool?> deleteGroup(String? groupId) async {
try {
bool doesExist = await groupBox.containsKey(groupId);
doesExist ? await groupBox.delete(groupId) : null;
return true;
} catch (e) {
log(e.toString());
return false;
}
}

Future<GroupModel?> getGroup(String? groupId) async {
try {
final group = await groupBox.get(groupId);
return group;
} catch (e) {
log(e.toString());
return null;
}
}

Future<bool?> saveBeacon(BeaconModel beacon) async {
try {
await deleteBeacon(beacon.id);
await beaconBox.put(beacon.id, beacon);
return true;
} catch (e) {
log(e.toString());
return false;
}
}

Future<bool?> deleteBeacon(String? beaconId) async {
try {
bool doesExist = await beaconBox.containsKey(beaconId);
doesExist ? await beaconBox.delete(beaconId) : null;
return true;
} catch (e) {
log(e.toString());
return false;
}
}

Future<BeaconModel?> getBeacon(String? beaconId) async {
try {
final beacon = await beaconBox.get(beaconId);
return beacon;
} catch (e) {
log(e.toString());
return null;
}
}
}
46 changes: 21 additions & 25 deletions lib/Bloc/data/datasource/remote/remote_auth_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,35 @@ class RemoteAuthApi {

Future<DataState<UserModel>> fetchUserInfo() async {
clientAuth = await graphqlConfig.authClient();
try {
final isConnected = await utils.checkInternetConnectivity();

if (!isConnected)
return DataFailed('Beacon is trying to connect with internet...');
final isConnected = await utils.checkInternetConnectivity();

// api call
final result = await clientAuth
.mutate(MutationOptions(document: gql(_authQueries.fetchUserInfo())));
if (!isConnected)
return DataFailed('Beacon is trying to connect with internet...');

if (result.data != null && result.isConcrete) {
final json = result.data!['me'];
final user = UserModel.fromJson(json);
// api call
final result = await clientAuth
.mutate(MutationOptions(document: gql(_authQueries.fetchUserInfo())));

final currentUser = await localApi.fetchUser();
if (result.data != null && result.isConcrete) {
final json = result.data!['me'];
final user = UserModel.fromJson(json);

// checking if user is login
if (currentUser == null) return DataFailed('Please login first');
final newUser = user.copyWithModel(
authToken: currentUser.authToken,
isGuest: user.email == '' ? true : false);
final currentUser = await localApi.fetchUser();

// saving user details locally
await localApi.saveUser(newUser);
// checking if user is login
if (currentUser == null) return DataFailed('Please login first');
final newUser = user.copyWithModel(
authToken: currentUser.authToken,
isGuest: user.email == '' ? true : false);

// returning
return DataSuccess(newUser);
} else {
return DataFailed('Something went wrong!');
}
} catch (e) {
return DataFailed(e.toString());
// saving user details locally
await localApi.saveUser(newUser);

// returning
return DataSuccess(newUser);
}
return DataFailed(encounteredExceptionOrError(result.exception!));
}

Future<DataState<UserModel>> register(
Expand Down
40 changes: 39 additions & 1 deletion lib/Bloc/data/datasource/remote/remote_group_api.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'dart:developer';

import 'package:beacon/Bloc/core/queries/beacon.dart';
import 'package:beacon/Bloc/core/queries/group.dart';
import 'package:beacon/Bloc/core/resources/data_state.dart';
import 'package:beacon/Bloc/data/models/beacon/beacon_model.dart';
import 'package:beacon/Bloc/data/models/group/group_model.dart';
import 'package:beacon/locator.dart';
import 'package:graphql_flutter/graphql_flutter.dart';

Expand All @@ -19,6 +22,28 @@ class RemoteGroupApi {
bool isConnected = await utils.checkInternetConnectivity();

if (!isConnected) {
GroupModel? group = await localApi.getGroup(groupId);

if (group != null && group.beacons != null) {
int condition = (page - 1) * pageSize + pageSize;
int beaconLen = group.beacons!.length;

if (condition > beaconLen) {
condition = beaconLen;
}

List<BeaconModel> beacons = [];

for (int i = (page - 1) * pageSize; i < condition; i++) {
BeaconModel? beaconModel =
await localApi.getBeacon(group.beacons![i]!.id);

beaconModel != null ? beacons.add(beaconModel) : null;
}

return DataSuccess(beacons);
}

return DataFailed('Please check your internet connection!');
}

Expand All @@ -32,7 +57,14 @@ class RemoteGroupApi {
List<BeaconModel> hikes = [];

for (var hikeJson in hikesJson) {
hikes.add(BeaconModel.fromJson(hikeJson));
BeaconModel hike = BeaconModel.fromJson(hikeJson);
hikes.add(hike);

// storing beacon
if (1 == 1) {
log('called');
await localApi.saveBeacon(hike);
}
}

return DataSuccess(hikes);
Expand All @@ -57,6 +89,9 @@ class RemoteGroupApi {
final hikeJson = result.data!['createBeacon'];

final beacon = BeaconModel.fromJson(hikeJson);

// storing beacon
await localApi.saveBeacon(beacon);
return DataSuccess(beacon);
}
return DataFailed(encounteredExceptionOrError(result.exception!));
Expand All @@ -77,6 +112,9 @@ class RemoteGroupApi {

final beacon = BeaconModel.fromJson(hikeJosn);

// storing beacon
await localApi.saveBeacon(beacon);

return DataSuccess(beacon);
}
return DataFailed(encounteredExceptionOrError(result.exception!));
Expand Down
Loading

0 comments on commit e74e82e

Please sign in to comment.