diff --git a/lib/Bloc/core/queries/auth.dart b/lib/Bloc/core/queries/auth.dart index 299f461..1c44b5c 100644 --- a/lib/Bloc/core/queries/auth.dart +++ b/lib/Bloc/core/queries/auth.dart @@ -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 } } } diff --git a/lib/Bloc/core/queries/group.dart b/lib/Bloc/core/queries/group.dart index 5da5fe1..bc76073 100644 --- a/lib/Bloc/core/queries/group.dart +++ b/lib/Bloc/core/queries/group.dart @@ -171,6 +171,9 @@ query{ _id name } + group{ + _id + } startsAt expiresAt diff --git a/lib/Bloc/data/datasource/local/local_api.dart b/lib/Bloc/data/datasource/local/local_api.dart index d7573d9..6b27fce 100644 --- a/lib/Bloc/data/datasource/local/local_api.dart +++ b/lib/Bloc/data/datasource/local/local_api.dart @@ -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'; @@ -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(userModelbox); - // groupBox = await Hive.openBox(groupModelBox); - // beaconBox = await Hive.openBox(beaconModelBox); - // locationBox = await Hive.openBox(locationModelBox); - // landMarkbox = await Hive.openBox(landMarkModelBox); + groupBox = await Hive.openBox(groupModelBox); + beaconBox = await Hive.openBox(beaconModelBox); + locationBox = await Hive.openBox(locationModelBox); + landMarkbox = await Hive.openBox(landMarkModelBox); } catch (e) { log('error: ${e.toString()}'); } @@ -65,37 +65,118 @@ class LocalApi { await userBox.put('currentUser', user); return true; } catch (e) { + log(e.toString()); return false; } } Future 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 fetchUser() async { - userBox = await Hive.openBox(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 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 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 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 getGroup(String? groupId) async { + try { + final group = await groupBox.get(groupId); + return group; + } catch (e) { + log(e.toString()); + return null; + } + } + + Future 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 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 getBeacon(String? beaconId) async { + try { + final beacon = await beaconBox.get(beaconId); + return beacon; + } catch (e) { + log(e.toString()); + return null; + } } } diff --git a/lib/Bloc/data/datasource/remote/remote_auth_api.dart b/lib/Bloc/data/datasource/remote/remote_auth_api.dart index 2421c40..d40bcb3 100644 --- a/lib/Bloc/data/datasource/remote/remote_auth_api.dart +++ b/lib/Bloc/data/datasource/remote/remote_auth_api.dart @@ -18,39 +18,35 @@ class RemoteAuthApi { Future> 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> register( diff --git a/lib/Bloc/data/datasource/remote/remote_group_api.dart b/lib/Bloc/data/datasource/remote/remote_group_api.dart index 183706a..7203aae 100644 --- a/lib/Bloc/data/datasource/remote/remote_group_api.dart +++ b/lib/Bloc/data/datasource/remote/remote_group_api.dart @@ -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'; @@ -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 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!'); } @@ -32,7 +57,14 @@ class RemoteGroupApi { List 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); @@ -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!)); @@ -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!)); diff --git a/lib/Bloc/data/datasource/remote/remote_home_api.dart b/lib/Bloc/data/datasource/remote/remote_home_api.dart index 3bb402b..06fba35 100644 --- a/lib/Bloc/data/datasource/remote/remote_home_api.dart +++ b/lib/Bloc/data/datasource/remote/remote_home_api.dart @@ -1,8 +1,7 @@ -import 'dart:developer'; - import 'package:beacon/Bloc/core/queries/group.dart'; import 'package:beacon/Bloc/core/resources/data_state.dart'; import 'package:beacon/Bloc/data/models/group/group_model.dart'; +import 'package:beacon/Bloc/data/models/user/user_model.dart'; import 'package:beacon/locator.dart'; import 'package:flutter/material.dart'; import 'package:graphql_flutter/graphql_flutter.dart'; @@ -15,8 +14,40 @@ class RemoteHomeApi { Future>> fetchUserGroups( int page, int pageSize) async { + final isConnected = await utils.checkInternetConnectivity(); + + print(_clientAuth.toString()); + + if (!isConnected) { + // fetching the previous data stored + // here taking all the ids of group from the user model and then fetching the groups locally from the ids + // returning all groups in one go + UserModel? usermodel = await localApi.fetchUser(); + + if (usermodel != null && usermodel.groups != null) { + // taking user groups + + int condition = (page - 1) * pageSize + pageSize; + int groupLen = usermodel.groups!.length; + + if (condition > groupLen) { + condition = groupLen; + } + + List groups = []; + + for (int i = (page - 1) * pageSize; i < condition; i++) { + GroupModel? groupModel = + await localApi.getGroup(usermodel.groups![i]!.id); + groupModel != null ? groups.add(groupModel) : null; + } + + return DataSuccess(groups); + } + } + final clientAuth = await graphqlConfig.authClient(); - log(_clientAuth.toString()); + final result = await clientAuth.query(QueryOptions( document: gql(_groupQueries.fetchUserGroups(page, pageSize)))); @@ -25,6 +56,10 @@ class RemoteHomeApi { List groupsData = result.data!['groups']; for (var groupData in groupsData) { final group = GroupModel.fromJson(groupData); + + // saving locally + await localApi.saveGroup(group); + groups.add(group); } return DataSuccess(groups); @@ -34,6 +69,10 @@ class RemoteHomeApi { } Future> createGroup(String title) async { + final isConnected = await utils.checkInternetConnectivity(); + + if (!isConnected) + return DataFailed('Beacon is trying to connect with internet...'); final _clientAuth = await graphqlConfig.authClient(); final result = await _clientAuth.mutate( MutationOptions(document: gql(_groupQueries.createGroup(title)))); @@ -42,6 +81,9 @@ class RemoteHomeApi { GroupModel group = GroupModel.fromJson( result.data!['createGroup'] as Map); + // storing group + await localApi.saveGroup(group); + return DataSuccess(group); } @@ -49,6 +91,10 @@ class RemoteHomeApi { } Future> joinGroup(String shortCode) async { + final isConnected = await utils.checkInternetConnectivity(); + + if (!isConnected) + return DataFailed('Beacon is trying to connect with internet...'); final _clientAuth = await graphqlConfig.authClient(); final result = await _clientAuth.mutate( MutationOptions(document: gql(_groupQueries.joinGroup(shortCode)))); @@ -57,20 +103,15 @@ class RemoteHomeApi { GroupModel group = GroupModel.fromJson(result.data as Map); + // storing group + await localApi.saveGroup(group); + return DataSuccess(group); } return DataFailed(encounteredExceptionOrError(result.exception!)); } - GraphQLError tryAgainMessage = GraphQLError(message: 'Please try again!'); - GraphQLError groupNotExist = - GraphQLError(message: 'No group exists with that shortcode!'); - GraphQLError alreadymember = - GraphQLError(message: 'Already a member of the group!'); - GraphQLError leaderOfGroup = - GraphQLError(message: 'You are the leader of the group!'); - String encounteredExceptionOrError(OperationException exception) { if (exception.linkException != null) { debugPrint(exception.linkException.toString()); diff --git a/lib/Bloc/data/models/beacon/beacon_model.dart b/lib/Bloc/data/models/beacon/beacon_model.dart index e5a499a..24afdb3 100644 --- a/lib/Bloc/data/models/beacon/beacon_model.dart +++ b/lib/Bloc/data/models/beacon/beacon_model.dart @@ -5,21 +5,32 @@ import 'package:beacon/Bloc/data/models/user/user_model.dart'; import 'package:beacon/Bloc/domain/entities/beacon/beacon_entity.dart'; import 'package:hive/hive.dart'; import 'package:json_annotation/json_annotation.dart'; - part 'beacon_model.g.dart'; +@HiveType(typeId: 20) @JsonSerializable() class BeaconModel implements BeaconEntity { + @HiveField(0) String? id; + @HiveField(1) String? title; + @HiveField(2) UserModel? leader; + @HiveField(3) GroupModel? group; + @HiveField(4) String? shortcode; + @HiveField(5) List? followers; + @HiveField(6) List? landmarks; + @HiveField(7) LocationModel? location; + @HiveField(8) List? route; + @HiveField(9) int? startsAt; + @HiveField(10) int? expiresAt; BeaconModel({ @@ -73,19 +84,3 @@ class BeaconModel implements BeaconEntity { ); } } - -class BeaconModelAdapter extends TypeAdapter { - @override - final int typeId = 20; - - @override - BeaconModel read(BinaryReader reader) { - final fields = reader.readMap().cast(); - return BeaconModel.fromJson(fields); - } - - @override - void write(BinaryWriter writer, BeaconModel obj) { - writer.writeMap(obj.toJson()); - } -} diff --git a/lib/Bloc/data/models/beacon/beacon_model.g.dart b/lib/Bloc/data/models/beacon/beacon_model.g.dart index 25a5f36..e94a13e 100644 --- a/lib/Bloc/data/models/beacon/beacon_model.g.dart +++ b/lib/Bloc/data/models/beacon/beacon_model.g.dart @@ -2,6 +2,74 @@ part of 'beacon_model.dart'; +// ************************************************************************** +// TypeAdapterGenerator +// ************************************************************************** + +class BeaconModelAdapter extends TypeAdapter { + @override + final int typeId = 20; + + @override + BeaconModel read(BinaryReader reader) { + final numOfFields = reader.readByte(); + final fields = { + for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), + }; + return BeaconModel( + id: fields[0] as String?, + title: fields[1] as String?, + leader: fields[2] as UserModel?, + group: fields[3] as GroupModel?, + shortcode: fields[4] as String?, + followers: (fields[5] as List?)?.cast(), + landmarks: (fields[6] as List?)?.cast(), + location: fields[7] as LocationModel?, + route: (fields[8] as List?)?.cast(), + startsAt: fields[9] as int?, + expiresAt: fields[10] as int?, + ); + } + + @override + void write(BinaryWriter writer, BeaconModel obj) { + writer + ..writeByte(11) + ..writeByte(0) + ..write(obj.id) + ..writeByte(1) + ..write(obj.title) + ..writeByte(2) + ..write(obj.leader) + ..writeByte(3) + ..write(obj.group) + ..writeByte(4) + ..write(obj.shortcode) + ..writeByte(5) + ..write(obj.followers) + ..writeByte(6) + ..write(obj.landmarks) + ..writeByte(7) + ..write(obj.location) + ..writeByte(8) + ..write(obj.route) + ..writeByte(9) + ..write(obj.startsAt) + ..writeByte(10) + ..write(obj.expiresAt); + } + + @override + int get hashCode => typeId.hashCode; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is BeaconModelAdapter && + runtimeType == other.runtimeType && + typeId == other.typeId; +} + // ************************************************************************** // JsonSerializableGenerator // ************************************************************************** @@ -33,8 +101,8 @@ BeaconModel _$BeaconModelFromJson(Map json) => BeaconModel( ? null : LocationModel.fromJson(e as Map)) .toList(), - startsAt: json['startsAt'] as int?, - expiresAt: json['expiresAt'] as int?, + startsAt: (json['startsAt'] as num?)?.toInt(), + expiresAt: (json['expiresAt'] as num?)?.toInt(), ); Map _$BeaconModelToJson(BeaconModel instance) => diff --git a/lib/Bloc/data/models/group/group_model.dart b/lib/Bloc/data/models/group/group_model.dart index 01b4850..4b5db15 100644 --- a/lib/Bloc/data/models/group/group_model.dart +++ b/lib/Bloc/data/models/group/group_model.dart @@ -6,13 +6,20 @@ import 'package:json_annotation/json_annotation.dart'; part 'group_model.g.dart'; +@HiveType(typeId: 30) @JsonSerializable() class GroupModel implements GroupEntity { + @HiveField(0) String? id; + @HiveField(1) String? title; + @HiveField(2) UserModel? leader; + @HiveField(3) List? members; + @HiveField(4) String? shortcode; + @HiveField(5) List? beacons; GroupModel({ @@ -50,19 +57,3 @@ class GroupModel implements GroupEntity { ); } } - -class GroupModelAdapter extends TypeAdapter { - @override - final int typeId = 30; - - @override - GroupModel read(BinaryReader reader) { - final fields = reader.readMap().cast(); - return GroupModel.fromJson(fields); - } - - @override - void write(BinaryWriter writer, GroupModel obj) { - writer.writeMap(obj.toJson()); - } -} diff --git a/lib/Bloc/data/models/group/group_model.g.dart b/lib/Bloc/data/models/group/group_model.g.dart index 3b9c8e8..64d110e 100644 --- a/lib/Bloc/data/models/group/group_model.g.dart +++ b/lib/Bloc/data/models/group/group_model.g.dart @@ -2,6 +2,59 @@ part of 'group_model.dart'; +// ************************************************************************** +// TypeAdapterGenerator +// ************************************************************************** + +class GroupModelAdapter extends TypeAdapter { + @override + final int typeId = 30; + + @override + GroupModel read(BinaryReader reader) { + final numOfFields = reader.readByte(); + final fields = { + for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), + }; + return GroupModel( + id: fields[0] as String?, + title: fields[1] as String?, + leader: fields[2] as UserModel?, + members: (fields[3] as List?)?.cast(), + shortcode: fields[4] as String?, + beacons: (fields[5] as List?)?.cast(), + ); + } + + @override + void write(BinaryWriter writer, GroupModel obj) { + writer + ..writeByte(6) + ..writeByte(0) + ..write(obj.id) + ..writeByte(1) + ..write(obj.title) + ..writeByte(2) + ..write(obj.leader) + ..writeByte(3) + ..write(obj.members) + ..writeByte(4) + ..write(obj.shortcode) + ..writeByte(5) + ..write(obj.beacons); + } + + @override + int get hashCode => typeId.hashCode; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is GroupModelAdapter && + runtimeType == other.runtimeType && + typeId == other.typeId; +} + // ************************************************************************** // JsonSerializableGenerator // ************************************************************************** diff --git a/lib/Bloc/data/models/landmark/landmark_model.dart b/lib/Bloc/data/models/landmark/landmark_model.dart index aef1e56..779c3c2 100644 --- a/lib/Bloc/data/models/landmark/landmark_model.dart +++ b/lib/Bloc/data/models/landmark/landmark_model.dart @@ -5,9 +5,12 @@ import 'package:json_annotation/json_annotation.dart'; part 'landmark_model.g.dart'; +@HiveType(typeId: 50) @JsonSerializable() class LandMarkModel implements LandMarkEntity { + @HiveField(0) String? title; + @HiveField(1) LocationModel? location; LandMarkModel({this.title, this.location}); @@ -31,19 +34,3 @@ class LandMarkModel implements LandMarkEntity { ); } } - -class LandMarkModelAdapter extends TypeAdapter { - @override - final int typeId = 50; - - @override - LandMarkModel read(BinaryReader reader) { - final fields = reader.readMap().cast(); - return LandMarkModel.fromJson(fields); - } - - @override - void write(BinaryWriter writer, LandMarkModel obj) { - writer.writeMap(obj.toJson()); - } -} diff --git a/lib/Bloc/data/models/landmark/landmark_model.g.dart b/lib/Bloc/data/models/landmark/landmark_model.g.dart index a2d8233..272a500 100644 --- a/lib/Bloc/data/models/landmark/landmark_model.g.dart +++ b/lib/Bloc/data/models/landmark/landmark_model.g.dart @@ -2,6 +2,47 @@ part of 'landmark_model.dart'; +// ************************************************************************** +// TypeAdapterGenerator +// ************************************************************************** + +class LandMarkModelAdapter extends TypeAdapter { + @override + final int typeId = 50; + + @override + LandMarkModel read(BinaryReader reader) { + final numOfFields = reader.readByte(); + final fields = { + for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), + }; + return LandMarkModel( + title: fields[0] as String?, + location: fields[1] as LocationModel?, + ); + } + + @override + void write(BinaryWriter writer, LandMarkModel obj) { + writer + ..writeByte(2) + ..writeByte(0) + ..write(obj.title) + ..writeByte(1) + ..write(obj.location); + } + + @override + int get hashCode => typeId.hashCode; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is LandMarkModelAdapter && + runtimeType == other.runtimeType && + typeId == other.typeId; +} + // ************************************************************************** // JsonSerializableGenerator // ************************************************************************** diff --git a/lib/Bloc/data/models/location/location_model.dart b/lib/Bloc/data/models/location/location_model.dart index d8625b2..79701b6 100644 --- a/lib/Bloc/data/models/location/location_model.dart +++ b/lib/Bloc/data/models/location/location_model.dart @@ -4,14 +4,17 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:beacon/Bloc/domain/entities/location/location_entity.dart'; part 'location_model.g.dart'; +@HiveType(typeId: 40) @JsonSerializable() class LocationModel implements LocationEntity { + @HiveField(0) final String? lat; - final String? long; + @HiveField(1) + final String? lon; LocationModel({ this.lat, - this.long, + this.lon, }); factory LocationModel.fromJson(Map json) => @@ -25,7 +28,7 @@ class LocationModel implements LocationEntity { }) { return LocationModel( lat: lat ?? this.lat, - long: long ?? this.long, + lon: lon ?? this.lon, ); } @@ -33,19 +36,3 @@ class LocationModel implements LocationEntity { $LocationEntityCopyWith get copyWith => throw UnimplementedError(); } - -class LocationModelAdapter extends TypeAdapter { - @override - final int typeId = 40; - - @override - LocationModel read(BinaryReader reader) { - final fields = reader.readMap().cast(); - return LocationModel.fromJson(fields); - } - - @override - void write(BinaryWriter writer, LocationModel obj) { - writer.writeMap(obj.toJson()); - } -} diff --git a/lib/Bloc/data/models/location/location_model.g.dart b/lib/Bloc/data/models/location/location_model.g.dart index 5010dba..174fae3 100644 --- a/lib/Bloc/data/models/location/location_model.g.dart +++ b/lib/Bloc/data/models/location/location_model.g.dart @@ -2,6 +2,47 @@ part of 'location_model.dart'; +// ************************************************************************** +// TypeAdapterGenerator +// ************************************************************************** + +class LocationModelAdapter extends TypeAdapter { + @override + final int typeId = 40; + + @override + LocationModel read(BinaryReader reader) { + final numOfFields = reader.readByte(); + final fields = { + for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), + }; + return LocationModel( + lat: fields[0] as String?, + lon: fields[1] as String?, + ); + } + + @override + void write(BinaryWriter writer, LocationModel obj) { + writer + ..writeByte(2) + ..writeByte(0) + ..write(obj.lat) + ..writeByte(1) + ..write(obj.lon); + } + + @override + int get hashCode => typeId.hashCode; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is LocationModelAdapter && + runtimeType == other.runtimeType && + typeId == other.typeId; +} + // ************************************************************************** // JsonSerializableGenerator // ************************************************************************** @@ -9,11 +50,11 @@ part of 'location_model.dart'; LocationModel _$LocationModelFromJson(Map json) => LocationModel( lat: json['lat'] as String?, - long: json['lon'] as String?, + lon: json['lon'] as String?, ); Map _$LocationModelToJson(LocationModel instance) => { 'lat': instance.lat, - 'lon': instance.long, + 'lon': instance.lon, }; diff --git a/lib/Bloc/data/models/user/user_model.dart b/lib/Bloc/data/models/user/user_model.dart index 51ca2b8..41bcd7b 100644 --- a/lib/Bloc/data/models/user/user_model.dart +++ b/lib/Bloc/data/models/user/user_model.dart @@ -7,15 +7,31 @@ import 'package:json_annotation/json_annotation.dart'; part 'user_model.g.dart'; +@HiveType(typeId: 10) @JsonSerializable() class UserModel implements UserEntity { + @HiveField(0) String? id; + + @HiveField(1) String? name; - String? authToken; + + @HiveField(2) String? email; + + @HiveField(3) + String? authToken; + + @HiveField(4) bool? isGuest; + + @HiveField(5) List? groups; + + @HiveField(6) List? beacons; + + @HiveField(7) LocationModel? location; UserModel( @@ -58,18 +74,3 @@ class UserModel implements UserEntity { ); } } - -class UserModelAdapter extends TypeAdapter { - @override - final int typeId = 10; - @override - UserModel read(BinaryReader reader) { - final fields = reader.readMap().cast(); - return UserModel.fromJson(fields); - } - - @override - void write(BinaryWriter writer, UserModel obj) { - writer.writeMap(obj.toJson()); - } -} diff --git a/lib/Bloc/data/models/user/user_model.g.dart b/lib/Bloc/data/models/user/user_model.g.dart index 84f7f53..e52a890 100644 --- a/lib/Bloc/data/models/user/user_model.g.dart +++ b/lib/Bloc/data/models/user/user_model.g.dart @@ -2,6 +2,65 @@ part of 'user_model.dart'; +// ************************************************************************** +// TypeAdapterGenerator +// ************************************************************************** + +class UserModelAdapter extends TypeAdapter { + @override + final int typeId = 10; + + @override + UserModel read(BinaryReader reader) { + final numOfFields = reader.readByte(); + final fields = { + for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), + }; + return UserModel( + authToken: fields[3] as String?, + beacons: (fields[6] as List?)?.cast(), + email: fields[2] as String?, + groups: (fields[5] as List?)?.cast(), + id: fields[0] as String?, + isGuest: fields[4] as bool?, + location: fields[7] as LocationModel?, + name: fields[1] as String?, + ); + } + + @override + void write(BinaryWriter writer, UserModel obj) { + writer + ..writeByte(8) + ..writeByte(0) + ..write(obj.id) + ..writeByte(1) + ..write(obj.name) + ..writeByte(2) + ..write(obj.email) + ..writeByte(3) + ..write(obj.authToken) + ..writeByte(4) + ..write(obj.isGuest) + ..writeByte(5) + ..write(obj.groups) + ..writeByte(6) + ..write(obj.beacons) + ..writeByte(7) + ..write(obj.location); + } + + @override + int get hashCode => typeId.hashCode; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is UserModelAdapter && + runtimeType == other.runtimeType && + typeId == other.typeId; +} + // ************************************************************************** // JsonSerializableGenerator // ************************************************************************** @@ -29,8 +88,8 @@ UserModel _$UserModelFromJson(Map json) => UserModel( Map _$UserModelToJson(UserModel instance) => { '_id': instance.id, 'name': instance.name, - 'authToken': instance.authToken, 'email': instance.email, + 'authToken': instance.authToken, 'isGuest': instance.isGuest, 'groups': instance.groups, 'beacons': instance.beacons, diff --git a/lib/Bloc/domain/entities/group/group_entity.freezed.dart b/lib/Bloc/domain/entities/group/group_entity.freezed.dart index f64d3db..c601ea0 100644 --- a/lib/Bloc/domain/entities/group/group_entity.freezed.dart +++ b/lib/Bloc/domain/entities/group/group_entity.freezed.dart @@ -217,7 +217,7 @@ class _$GroupEntityImpl implements _GroupEntity { @override String toString() { - return 'GroupEntity(_id: $id, beacons: $beacons, members: $members, leader: $leader, title: $title, shortcode: $shortcode)'; + return 'GroupEntity(id: $id, beacons: $beacons, members: $members, leader: $leader, title: $title, shortcode: $shortcode)'; } @override diff --git a/lib/Bloc/domain/entities/location/location_entity.dart b/lib/Bloc/domain/entities/location/location_entity.dart index 00a7174..6f7448a 100644 --- a/lib/Bloc/domain/entities/location/location_entity.dart +++ b/lib/Bloc/domain/entities/location/location_entity.dart @@ -3,5 +3,5 @@ part 'location_entity.freezed.dart'; @freezed class LocationEntity with _$LocationEntity { - factory LocationEntity({String? lat, String? long}) = _LocationEntity; + factory LocationEntity({String? lat, String? lon}) = _LocationEntity; } diff --git a/lib/Bloc/domain/entities/location/location_entity.freezed.dart b/lib/Bloc/domain/entities/location/location_entity.freezed.dart index 4121b70..01117b3 100644 --- a/lib/Bloc/domain/entities/location/location_entity.freezed.dart +++ b/lib/Bloc/domain/entities/location/location_entity.freezed.dart @@ -17,7 +17,7 @@ final _privateConstructorUsedError = UnsupportedError( /// @nodoc mixin _$LocationEntity { String? get lat => throw _privateConstructorUsedError; - String? get long => throw _privateConstructorUsedError; + String? get lon => throw _privateConstructorUsedError; @JsonKey(ignore: true) $LocationEntityCopyWith get copyWith => @@ -30,7 +30,7 @@ abstract class $LocationEntityCopyWith<$Res> { LocationEntity value, $Res Function(LocationEntity) then) = _$LocationEntityCopyWithImpl<$Res, LocationEntity>; @useResult - $Res call({String? lat, String? long}); + $Res call({String? lat, String? lon}); } /// @nodoc @@ -47,16 +47,16 @@ class _$LocationEntityCopyWithImpl<$Res, $Val extends LocationEntity> @override $Res call({ Object? lat = freezed, - Object? long = freezed, + Object? lon = freezed, }) { return _then(_value.copyWith( lat: freezed == lat ? _value.lat : lat // ignore: cast_nullable_to_non_nullable as String?, - long: freezed == long - ? _value.long - : long // ignore: cast_nullable_to_non_nullable + lon: freezed == lon + ? _value.lon + : lon // ignore: cast_nullable_to_non_nullable as String?, ) as $Val); } @@ -70,7 +70,7 @@ abstract class _$$LocationEntityImplCopyWith<$Res> __$$LocationEntityImplCopyWithImpl<$Res>; @override @useResult - $Res call({String? lat, String? long}); + $Res call({String? lat, String? lon}); } /// @nodoc @@ -85,16 +85,16 @@ class __$$LocationEntityImplCopyWithImpl<$Res> @override $Res call({ Object? lat = freezed, - Object? long = freezed, + Object? lon = freezed, }) { return _then(_$LocationEntityImpl( lat: freezed == lat ? _value.lat : lat // ignore: cast_nullable_to_non_nullable as String?, - long: freezed == long - ? _value.long - : long // ignore: cast_nullable_to_non_nullable + lon: freezed == lon + ? _value.lon + : lon // ignore: cast_nullable_to_non_nullable as String?, )); } @@ -103,16 +103,16 @@ class __$$LocationEntityImplCopyWithImpl<$Res> /// @nodoc class _$LocationEntityImpl implements _LocationEntity { - _$LocationEntityImpl({this.lat, this.long}); + _$LocationEntityImpl({this.lat, this.lon}); @override final String? lat; @override - final String? long; + final String? lon; @override String toString() { - return 'LocationEntity(lat: $lat, long: $long)'; + return 'LocationEntity(lat: $lat, lon: $lon)'; } @override @@ -121,11 +121,11 @@ class _$LocationEntityImpl implements _LocationEntity { (other.runtimeType == runtimeType && other is _$LocationEntityImpl && (identical(other.lat, lat) || other.lat == lat) && - (identical(other.long, long) || other.long == long)); + (identical(other.lon, lon) || other.lon == lon)); } @override - int get hashCode => Object.hash(runtimeType, lat, long); + int get hashCode => Object.hash(runtimeType, lat, lon); @JsonKey(ignore: true) @override @@ -136,13 +136,13 @@ class _$LocationEntityImpl implements _LocationEntity { } abstract class _LocationEntity implements LocationEntity { - factory _LocationEntity({final String? lat, final String? long}) = + factory _LocationEntity({final String? lat, final String? lon}) = _$LocationEntityImpl; @override String? get lat; @override - String? get long; + String? get lon; @override @JsonKey(ignore: true) _$$LocationEntityImplCopyWith<_$LocationEntityImpl> get copyWith => diff --git a/lib/Bloc/presentation/screens/group_screen.dart b/lib/Bloc/presentation/screens/group_screen.dart index 1a36eac..c1d050f 100644 --- a/lib/Bloc/presentation/screens/group_screen.dart +++ b/lib/Bloc/presentation/screens/group_screen.dart @@ -9,6 +9,7 @@ import 'package:beacon/old/components/shape_painter.dart'; import 'package:beacon/locator.dart'; import 'package:beacon/old/components/models/beacon/beacon.dart'; import 'package:beacon/old/components/utilities/constants.dart'; +import 'package:beacon/router.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart'; @@ -129,8 +130,13 @@ class _GroupScreenState extends State HikeButton( buttonHeight: 2.5.h, buttonWidth: 8.w, - onTap: () { + onTap: () async { AutoRouter.of(context).maybePop(); + AutoRouter.of(context).pushAndPopUntil( + AuthScreenRoute(), + predicate: (route) => true, + ); + await localApi.deleteUser(); }, text: 'Yes', textSize: 18.0, diff --git a/lib/Bloc/presentation/screens/splash_screen.dart b/lib/Bloc/presentation/screens/splash_screen.dart index 10b6a20..0d7764a 100644 --- a/lib/Bloc/presentation/screens/splash_screen.dart +++ b/lib/Bloc/presentation/screens/splash_screen.dart @@ -13,74 +13,8 @@ class SplashScreen extends StatefulWidget { } class _SplashScreenState extends State { - // Uri? _initialUri; - // Uri? _latestUri; - // late StreamSubscription _sub; bool isCheckingUrl = false; - // Future _handleInitialUri() async { - // // _sub = uriLinkStream.listen((Uri? uri) { - // // if (!mounted) return; - // // setState(() { - // // _latestUri = uri; - // // }); - // // }, onError: (Object err) { - // // if (!mounted) return; - // // setState(() { - // // _latestUri = null; - // // }); - // // }); - // // try { - // // final uri = await getInitialUri(); - // // if (!mounted) return; - // // setState(() => _initialUri = uri); - // // } on PlatformException { - // // if (!mounted) return; - // // setState(() => _initialUri = null); - // // } on FormatException catch (err) { - // // debugPrint(err.toString()); - // // if (!mounted) return; - // // setState(() => _initialUri = null); - // // } - - // // checking if user is login or not ? - - // // await userConfig!.userLoggedIn().then((value) async { - // // if (_latestUri == null && _initialUri == null) { - // // if (value || localApi.userBox.containsKey('currentUser')) { - // // AutoRouter.of(context).replaceNamed('/home'); - // // } else { - // // AutoRouter.of(context).replaceNamed('/auth'); - // // } - // // } else { - // // if (_initialUri != null) { - // // var shortcode = _initialUri!.queryParameters['shortcode']; - // // if (value) { - // // await databaseFunctions!.joinBeacon(shortcode).then((val) { - // // if (val != null) { - // // // navigationService!.pushScreen('/hikeScreen', - // // // arguments: HikeScreen(val, isLeader: false)); - - // // AutoRouter.of(context).pushNamed('/hike'); - // // } else { - // // // navigationService!.pushReplacementScreen('/main'); - // // AutoRouter.of(context).replaceNamed('/hike'); - // // } - // // }); - // // } else { - // // // login in anonymously and join hike - // // await databaseFunctions!.signup(name: "Anonymous"); - // // await databaseFunctions!.joinBeacon(shortcode).then((val) async { - // // // navigationService!.pushScreen('/hikeScreen', - // // // arguments: HikeScreen(val, isLeader: false)); - // // AutoRouter.of(context).pushNamed('/hike'); - // // }); - // // } - // // } - // // } - // // }); - // } - @override void initState() { _handleNavigation(); @@ -102,7 +36,7 @@ class _SplashScreenState extends State { AutoRouter.of(context).replaceNamed('/auth'); } } else { - AutoRouter.of(context).replaceNamed('/auth'); + AutoRouter.of(context).replaceNamed('/home'); } } else { AutoRouter.of(context).replaceNamed('/auth'); diff --git a/lib/old/components/beacon_card.dart b/lib/old/components/beacon_card.dart index 8d26d66..6129ba8 100644 --- a/lib/old/components/beacon_card.dart +++ b/lib/old/components/beacon_card.dart @@ -71,10 +71,10 @@ class BeaconCustomWidgets { group: beacon.group == null ? '' : beacon.group!.id, landmarks: [], location: Location( - lat: beacon.location!.lat, lon: beacon.location!.long), + lat: beacon.location!.lat, lon: beacon.location!.lon), route: []); - log('location: ${beacon.location!.long} ${beacon.location!.lat}'); + log('location: ${beacon.location!.lon} ${beacon.location!.lat}'); AutoRouter.of(context) .push(HikeScreenRoute(beacon: refrencedBeacon, isLeader: false)); diff --git a/pubspec.lock b/pubspec.lock index 72abc5a..d198aec 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -49,6 +49,14 @@ packages: url: "https://pub.dev" source: hosted version: "8.1.3" + auto_route_generator: + dependency: "direct dev" + description: + name: auto_route_generator + sha256: ba28133d3a3bf0a66772bcc98dade5843753cd9f1a8fb4802b842895515b67d3 + url: "https://pub.dev" + source: hosted + version: "8.0.0" bloc: dependency: "direct main" description: @@ -1502,5 +1510,5 @@ packages: source: hosted version: "3.1.2" sdks: - dart: ">=3.2.3 <4.0.0" + dart: ">=3.3.0 <4.0.0" flutter: ">=3.16.0" diff --git a/pubspec.yaml b/pubspec.yaml index 4732e16..646adf4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -69,6 +69,7 @@ dev_dependencies: freezed: ^2.5.2 hive_generator: test: ^1.16.5 + auto_route_generator: flutter_icons: android: "launcher_icon"