Skip to content

Commit

Permalink
bug fixes and feature updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nixrajput committed May 16, 2022
1 parent e3e1218 commit a191485
Show file tree
Hide file tree
Showing 24 changed files with 565 additions and 11 deletions.
3 changes: 3 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

<application
android:label="Rippl!"
Expand Down
5 changes: 5 additions & 0 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationTemporaryUsageDescriptionDictionary</key>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
Expand Down
30 changes: 30 additions & 0 deletions lib/apis/models/entities/login_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:json_annotation/json_annotation.dart';

part 'login_info.g.dart';

@JsonSerializable()
class LoginInfo {
LoginInfo({
required this.id,
this.user,
this.devices,
required this.createdAt,
});

factory LoginInfo.fromJson(Map<String, dynamic> json) =>
_$LoginInfoFromJson(json);

Map<String, dynamic> toJson() => _$LoginInfoToJson(this);

@JsonKey(name: '_id')
String id;

@JsonKey(name: 'user')
String? user;

@JsonKey(name: 'devices')
List<Map<String, dynamic>>? devices;

@JsonKey(name: 'createdAt')
DateTime createdAt;
}
23 changes: 23 additions & 0 deletions lib/apis/models/entities/login_info.g.dart

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

4 changes: 2 additions & 2 deletions lib/apis/models/entities/notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class ApiNotification {
});

factory ApiNotification.fromJson(Map<String, dynamic> json) =>
_$NotificationFromJson(json);
_$ApiNotificationFromJson(json);

Map<String, dynamic> toJson() => _$NotificationToJson(this);
Map<String, dynamic> toJson() => _$ApiNotificationToJson(this);

@JsonKey(name: '_id')
String id;
Expand Down
4 changes: 2 additions & 2 deletions lib/apis/models/entities/notification.g.dart

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

23 changes: 23 additions & 0 deletions lib/apis/models/responses/login_info_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:social_media_app/apis/models/entities/login_info.dart';

part 'login_info_response.g.dart';

@JsonSerializable()
class LoginInfoResponse {
LoginInfoResponse({
this.success,
this.result,
});

factory LoginInfoResponse.fromJson(Map<String, dynamic> json) =>
_$LoginInfoResponseFromJson(json);

Map<String, dynamic> toJson() => _$LoginInfoResponseToJson(this);

@JsonKey(name: 'success')
bool? success;

@JsonKey(name: 'result')
LoginInfo? result;
}
21 changes: 21 additions & 0 deletions lib/apis/models/responses/login_info_response.g.dart

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

27 changes: 27 additions & 0 deletions lib/apis/providers/api_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,33 @@ class ApiProvider {
},
);

return response;
}

// LOGIN INFO
Future<http.Response> saveLoginInfo(
String token, Map<String, dynamic> body) async {
final response = await _client.post(
Uri.parse('${baseUrl!}${AppUrls.saveLoginInfo}'),
headers: {
"content-type": "application/json",
"authorization": "Bearer $token",
},
body: jsonEncode(body),
);

return response;
}

Future<http.Response> getLoginInfo(String token) async {
final response = await _client.get(
Uri.parse('${baseUrl!}${AppUrls.getLoginInfo}'),
headers: {
"content-type": "application/json",
"authorization": "Bearer $token",
},
);

return response;
}
}
109 changes: 109 additions & 0 deletions lib/apis/services/auth_service.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:connectivity/connectivity.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
import 'package:social_media_app/apis/models/responses/login_response.dart';
import 'package:social_media_app/apis/providers/api_provider.dart';
import 'package:social_media_app/constants/strings.dart';
import 'package:social_media_app/helpers/permissions.dart';
import 'package:social_media_app/helpers/utils.dart';
import 'package:social_media_app/routes/route_management.dart';

class AuthService extends GetxService {
static AuthService get find => Get.find();

final _apiProvider = ApiProvider(http.Client());

StreamSubscription<dynamic>? _streamSubscription;

String _token = '';
String _expiresAt = '';
String _deviceId = '';
LoginResponse _loginData = LoginResponse();

String get token => _token;

String get deviceId => _deviceId;

String get expiresAt => _expiresAt;

LoginResponse get loginData => _loginData;
Expand Down Expand Up @@ -50,6 +63,102 @@ class AuthService extends GetxService {
);
}

Future<dynamic> getCurrentLocation() async {
var hasPerm = await AppPermissions.checkLocationPermission();
if (!hasPerm) {
return;
}
var position = await Geolocator.getCurrentPosition();
var loc =
await placemarkFromCoordinates(position.latitude, position.longitude);
var locationInfo = <String, dynamic>{
"street": loc.first.street,
"locality": loc.first.locality,
"city": loc.first.subAdministrativeArea,
"state": loc.first.administrativeArea,
"country": loc.first.country,
"countryCode": loc.first.isoCountryCode,
"pincode": loc.first.postalCode
};
AppUtils.printLog("Location Info: $locationInfo");
return locationInfo;
}

Future<dynamic> getDeviceInfo() async {
var deviceInfoPlugin = DeviceInfoPlugin();
Map<String, dynamic> deviceInfo;
if (GetPlatform.isIOS) {
var iosInfo = await deviceInfoPlugin.iosInfo;
var deviceBrand = iosInfo.utsname.machine;
var deviceModel = iosInfo.model;
var deviceSystemVersion = iosInfo.utsname.release;
var deviceId = iosInfo.identifierForVendor;

deviceInfo = <String, dynamic>{
"deviceId": deviceId,
"model": deviceModel,
"brand": deviceBrand,
"osVersion": deviceSystemVersion
};
} else {
var androidInfo = await deviceInfoPlugin.androidInfo;
var deviceBrand = androidInfo.brand;
var deviceModel = androidInfo.model;
var deviceSystemVersion = androidInfo.version.release;
var deviceId = androidInfo.androidId;

deviceInfo = <String, dynamic>{
"deviceId": deviceId,
"model": deviceModel,
"brand": deviceBrand,
"osVersion": deviceSystemVersion
};
}

return deviceInfo;
}

Future<void> saveLoginInfo() async {
var deviceInfo = await getDeviceInfo();
_deviceId = deviceInfo['deviceId'];
var locationInfo = await getCurrentLocation();

final body = {
'deviceInfo': deviceInfo,
'locationInfo': locationInfo,
'lastActive': DateTime.now().toIso8601String(),
};

AppUtils.printLog("Save LoginInfo Request...");

try {
final response = await _apiProvider.saveLoginInfo(_token, body);

final decodedData = jsonDecode(utf8.decode(response.bodyBytes));

if (response.statusCode == 200) {
AppUtils.printLog(decodedData[StringValues.message]);
} else {
AppUtils.printLog(decodedData[StringValues.message]);
}
} on SocketException {
AppUtils.printLog(StringValues.internetConnError);
AppUtils.showSnackBar(StringValues.internetConnError, StringValues.error);
} on TimeoutException {
AppUtils.printLog(StringValues.connTimedOut);
AppUtils.printLog(StringValues.connTimedOut);
AppUtils.showSnackBar(StringValues.connTimedOut, StringValues.error);
} on FormatException catch (e) {
AppUtils.printLog(StringValues.formatExcError);
AppUtils.printLog(e);
AppUtils.showSnackBar(StringValues.errorOccurred, StringValues.error);
} catch (exc) {
AppUtils.printLog(StringValues.errorOccurred);
AppUtils.printLog(exc);
AppUtils.showSnackBar(StringValues.errorOccurred, StringValues.error);
}
}

void autoLogout() async {
if (_expiresAt.isNotEmpty) {
var currentTimestamp =
Expand Down
21 changes: 20 additions & 1 deletion lib/helpers/permissions.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:permission_handler/permission_handler.dart';
import 'package:social_media_app/helpers/utils.dart';

abstract class Permissions {
abstract class AppPermissions {
static Future<bool> checkStoragePermission() async {
var status = await Permission.storage.status;
if (status.isDenied) {
Expand Down Expand Up @@ -40,4 +40,23 @@ abstract class Permissions {
}
return true;
}

static Future<bool> checkLocationPermission() async {
var status = await Permission.location.status;
if (status.isDenied) {
if (await Permission.location.request().isGranted) {
return true;
}
return false;
}
if (status.isRestricted) {
AppUtils.showError('Location Permission Error');
return false;
}
if (status.isPermanentlyDenied) {
await openAppSettings();
return false;
}
return true;
}
}
2 changes: 1 addition & 1 deletion lib/helpers/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ abstract class AppUtils {
static void printLog(message) {
debugPrint(
"=======================================================================");
debugPrint(message.toString());
debugPrint(message.toString(), wrapWidth: 1024);
debugPrint(
"=======================================================================");
}
Expand Down
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Future<void> initServices() async {
var hasData = await Get.find<ProfileController>().getProfileDetails();
if (hasData) {
isLogin = true;
await Get.find<AuthService>().saveLoginInfo();
}
}
isLogin
Expand Down
1 change: 1 addition & 0 deletions lib/modules/auth/controllers/login_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class LoginController extends GetxController {
_auth.setExpiresAt = expiresAt;
_auth.autoLogout();
await _profile.fetchProfileDetails();
await _auth.saveLoginInfo();

_clearLoginTextControllers();

Expand Down
Loading

0 comments on commit a191485

Please sign in to comment.