Skip to content

Commit

Permalink
Did improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-pratik-k committed Dec 11, 2024
1 parent 91bbbdb commit 3bdc5fb
Show file tree
Hide file tree
Showing 11 changed files with 402 additions and 424 deletions.
20 changes: 10 additions & 10 deletions .idea/libraries/Flutter_Plugins.xml

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

2 changes: 1 addition & 1 deletion data/.flutter-plugins-dependencies

Large diffs are not rendered by default.

19 changes: 4 additions & 15 deletions data/lib/errors/app_error.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:io';
import 'dart:math';
import 'l10n_error_codes.dart';
import 'package:dio/dio.dart' show DioException, DioExceptionType;
import 'package:dio/dio.dart' show DioException;

class AppError implements Exception {
final String? message;
Expand All @@ -21,9 +20,6 @@ class AppError implements Exception {
} else if (error is SocketException) {
return const NoConnectionError();
} else if (error is DioException) {
if (error.type == DioExceptionType.cancel) {
return const RequestCancelledByUser();
}
return SomethingWentWrongError(
message: error.message,
statusCode: error.response?.statusCode,
Expand Down Expand Up @@ -51,13 +47,6 @@ class UserGoogleSignInAccountNotFound extends AppError {
);
}

class RequestCancelledByUser extends AppError {
const RequestCancelledByUser()
: super(
message: "Request cancelled",
);
}

class BackUpFolderNotFound extends AppError {
const BackUpFolderNotFound()
: super(
Expand All @@ -82,12 +71,12 @@ class SomethingWentWrongError extends AppError {
);
}

class AuthSessionExpiredError extends AppError {
const AuthSessionExpiredError()
class DropboxAuthSessionExpiredError extends AppError {
const DropboxAuthSessionExpiredError()
: super(
l10nCode: AppErrorL10nCodes.authSessionExpiredError,
message:
"User authentication session expired. Unable to get access token.",
"User authentication session expired. Unable to get dropbox access token.",
statusCode: 401,
);
}
32 changes: 28 additions & 4 deletions data/lib/handlers/connectivity_handler.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import 'dart:io';
import 'package:flutter_riverpod/flutter_riverpod.dart';

///
Future<void> internetLookUp() async {
await InternetAddress.lookup('google.com');
}
import '../apis/network/urls.dart';

final connectivityHandlerProvider = Provider<ConnectivityHandler>((ref) {
return ConnectivityHandler();
});

class ConnectivityHandler {
Future<void> lookUpDropbox() async {
await InternetAddress.lookup(BaseURL.dropboxV2);
}

Future<void> lookUpGoogleDrive() async {
await InternetAddress.lookup(BaseURL.googleDriveV3);
}

Future<bool> hasInternetAccess() async {
try {
final result = await InternetAddress.lookup('example.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
return true; // Internet access
}
} on SocketException catch (_) {
return false; // No Internet access
}
return false;
}
}
8 changes: 4 additions & 4 deletions data/lib/repositories/media_process_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ class MediaProcessRepo extends ChangeNotifier {

await clearUploadProcessResponse(id: process.id);
} catch (e) {
if (e is RequestCancelledByUser) {
if (e is DioException && e.type == DioExceptionType.cancel) {
showNotification('Upload to Google Drive cancelled');
return;
}
Expand Down Expand Up @@ -593,7 +593,7 @@ class MediaProcessRepo extends ChangeNotifier {

await clearUploadProcessResponse(id: process.id);
} catch (e) {
if (e is RequestCancelledByUser) {
if (e is DioException && e.type == DioExceptionType.cancel) {
showNotification('Upload to Dropbox cancelled');
return;
}
Expand Down Expand Up @@ -820,7 +820,7 @@ class MediaProcessRepo extends ChangeNotifier {

await clearDownloadProcessResponse(id: process.id);
} catch (error) {
if (error is RequestCancelledByUser) {
if (error is DioException && error.type == DioExceptionType.cancel) {
showNotification('Download from Google Drive cancelled');
return;
}
Expand Down Expand Up @@ -938,7 +938,7 @@ class MediaProcessRepo extends ChangeNotifier {

await clearDownloadProcessResponse(id: process.id);
} catch (error) {
if (error is RequestCancelledByUser) {
if (error is DioException && error.type == DioExceptionType.cancel) {
showNotification('Download from Dropbox cancelled');
return;
}
Expand Down
132 changes: 56 additions & 76 deletions data/lib/services/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,84 +74,66 @@ class AuthService {
}

Future<void> signInSilently() async {
try {
await _googleSignIn.signInSilently(suppressErrors: true);
} catch (e) {
throw AppError.fromError(e);
}
await _googleSignIn.signInSilently(suppressErrors: true);
}

Future<void> signInWithGoogle() async {
try {
final googleSignInAccount = await _googleSignIn.signIn();
if (googleSignInAccount != null) {
await googleSignInAccount.authentication;
}
} catch (e) {
throw AppError.fromError(e);
final googleSignInAccount = await _googleSignIn.signIn();
if (googleSignInAccount != null) {
await googleSignInAccount.authentication;
}
}

Future<void> signOutWithGoogle() async {
try {
await _googleSignIn.signOut();
_googleDriveAutoBackUpController.state = false;
} catch (e) {
throw AppError.fromError(e);
}
await _googleSignIn.signOut();
_googleDriveAutoBackUpController.state = false;
}

/// Launches the URL in the browser for OAuth 2 authentication with Dropbox.
/// Retrieves the access token using the Proof of Key Code Exchange (PKCE) flow.
/// Retrieves the code to fetch access token using the Proof of Key Code Exchange (PKCE) flow.
Future<void> signInWithDropBox() async {
try {
final codeVerifier = _oauth2.generateCodeVerifier;
_dropboxCodeVerifierPrefProvider.state = codeVerifier;
final authorizationUrl = _oauth2.getAuthorizationUrl(
clientId: AppSecretes.dropBoxAppKey,
authorizationEndpoint:
Uri.parse('${BaseURL.dropboxOAuth2Web}/authorize'),
additionalParameters: {'token_access_type': 'offline'},
redirectUri: RedirectURL.auth,
codeVerifier: codeVerifier,
);
await launchUrl(authorizationUrl);
} catch (e) {
throw AppError.fromError(e);
}
final codeVerifier = _oauth2.generateCodeVerifier;
_dropboxCodeVerifierPrefProvider.state = codeVerifier;
final authorizationUrl = _oauth2.getAuthorizationUrl(
clientId: AppSecretes.dropBoxAppKey,
authorizationEndpoint: Uri.parse('${BaseURL.dropboxOAuth2Web}/authorize'),
additionalParameters: {'token_access_type': 'offline'},
redirectUri: RedirectURL.auth,
codeVerifier: codeVerifier,
);
await launchUrl(authorizationUrl);
}

/// Fetch dropbox access token using the code using the Proof of Key Code Exchange (PKCE) flow.
Future<void> setDropboxTokenFromCode({required String code}) async {
try {
if (_dropboxCodeVerifierPrefProvider.state == null) {
throw const SomethingWentWrongError(
message: "Dropbox code verifier is missing",
);
}
final res = await _dio.req(
DropboxTokenEndpoint(
code: code,
codeVerifier: _dropboxCodeVerifierPrefProvider.state!,
clientId: AppSecretes.dropBoxAppKey,
redirectUrl: RedirectURL.auth,
clientSecret: AppSecretes.dropBoxAppSecret,
),
if (_dropboxCodeVerifierPrefProvider.state == null) {
throw const SomethingWentWrongError(
message: "Dropbox code verifier is missing",
);
if (res.data != null) {
_dropboxTokenController.state = DropboxToken(
access_token: res.data['access_token'],
token_type: res.data['token_type'],
refresh_token: res.data['refresh_token'],
expires_in:
DateTime.now().add(Duration(seconds: res.data['expires_in'])),
account_id: res.data['account_id'],
scope: res.data['scope'],
uid: res.data['uid'],
);
_dropboxCodeVerifierPrefProvider.state = null;
}
} catch (e) {
throw AppError.fromError(e);
}
final res = await _dio.req(
DropboxTokenEndpoint(
code: code,
codeVerifier: _dropboxCodeVerifierPrefProvider.state!,
clientId: AppSecretes.dropBoxAppKey,
redirectUrl: RedirectURL.auth,
clientSecret: AppSecretes.dropBoxAppSecret,
),
);
if (res.statusCode == 200) {
_dropboxTokenController.state = DropboxToken(
access_token: res.data['access_token'],
token_type: res.data['token_type'],
refresh_token: res.data['refresh_token'],
expires_in:
DateTime.now().add(Duration(seconds: res.data['expires_in'])),
account_id: res.data['account_id'],
scope: res.data['scope'],
uid: res.data['uid'],
);
_dropboxCodeVerifierPrefProvider.state = null;
} else {
throw const DropboxAuthSessionExpiredError();
}
}

Expand All @@ -163,15 +145,15 @@ class AuthService {
}

Future<void> refreshDropboxToken() async {
try {
if (_dropboxTokenController.state != null) {
final res = await _dio.req(
DropboxRefreshTokenEndpoint(
refreshToken: _dropboxTokenController.state!.refresh_token,
clientId: AppSecretes.dropBoxAppKey,
clientSecret: AppSecretes.dropBoxAppSecret,
),
);
if (_dropboxTokenController.state != null) {
final res = await _dio.req(
DropboxRefreshTokenEndpoint(
refreshToken: _dropboxTokenController.state!.refresh_token,
clientId: AppSecretes.dropBoxAppKey,
clientSecret: AppSecretes.dropBoxAppSecret,
),
);
if (res.statusCode == 200) {
_dropboxTokenController.state = _dropboxTokenController.state!.copyWith(
access_token: res.data['access_token'],
expires_in: DateTime.now().add(
Expand All @@ -181,12 +163,10 @@ class AuthService {
),
token_type: res.data['token_type'],
);
} else {
throw const AuthSessionExpiredError();
return;
}
} catch (e) {
throw AppError.fromError(e);
}
throw DropboxAuthSessionExpiredError();
}

bool get signedInWithGoogle => _googleSignIn.currentUser != null;
Expand Down
2 changes: 1 addition & 1 deletion data/lib/services/base/cloud_provider_service.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:dio/dio.dart';
import '../models/media/media.dart';
import '../../models/media/media.dart';

abstract class CloudProviderService {
const CloudProviderService();
Expand Down
Loading

0 comments on commit 3bdc5fb

Please sign in to comment.