Adds Refreshing token if previous token expires, #3
Merged
vikas2-sharma merged 7 commits intomainfrom Sep 30, 2025
Merged
Conversation
This commit introduces the `kotlinx-coroutines-play-services` dependency to the project. This library provides integration with Google Play Services Tasks API, enabling the use of coroutines with Play Services. The dependency is added to `gradle/libs.versions.toml` and `core/build.gradle.kts`.
This commit introduces functionality to refresh Google Drive access tokens and manage their expiration within the `AuthManager`. Key changes: - A new `getNewToken()` suspend function is added to `AuthManager`. This function requests a new authorization token from Google Identity Services and stores the new access token and its expiration time (5 minutes from now) in `DataStorePref`. - The `getDrivePermission` function now also saves the token's expiration time (5 minutes from issue) to `DataStorePref` upon successful authorization. - The `AuthManager` constructor now accepts a `Context` parameter, which is used by the new `getNewToken()` function. - Logging has been added to trace the token refresh process.
This commit changes the `getExpiresIn` function in `DataStorePref` from returning a `Flow<Long>` to a suspend function returning a `Long`. The implementation now uses `.first()` on the mapped `DataStore` data flow to retrieve the `EXPIRES_IN_KEY` value directly, simplifying the call site by removing the need to collect the Flow.
This commit modifies the `NetworkModule` to provide the `ApplicationContext` to the `AuthManager`. The `providesAuthManager` function in `NetworkModule.kt` now accepts `@ApplicationContext context: Context` as a parameter and passes it to the `AuthManager` constructor. This change allows `AuthManager` to access application-level context if needed.
This commit refactors `DriveManager` to enhance token handling and error management when interacting with the Google Drive API.
**Key changes:**
- **Token Refresh Logic:**
- `getDriveService` now checks the access token's expiry time before making API calls.
- If the token is expired, it requests a new token via `authManager.getNewToken()`.
- This ensures that API calls are always made with a valid access token.
- **Error Handling:**
- `getDriveService`, `createFolder`, `rootFolderId`, and `getEventFolderFromDrive` now include `try-catch` blocks to handle potential exceptions during API interactions.
- Methods that return a value now return `null` or an empty string/list in case of an error, providing more robust error propagation.
- `getDriveService` can now return `null` if the Drive service cannot be initialized.
- **Dependency Injection:**
- `DriveManager` now accepts an `AuthManager` instance in its constructor to facilitate token refresh.
- **Suspend Functions:**
- `createFolder`, `rootFolderId`, and `getEventFolderFromDrive` are now `suspend` functions to align with the asynchronous nature of network operations and token refresh.
- **Logging:**
- Added more detailed logging for token status, API calls, and errors.
- **Removed Unused Code:**
- Commented out `cancelDriveManagerCoroutine` as it's not currently used.
- Removed unused `ROOT_FOLDER_KEY` import.
- **Null Safety:**
- `createFolder` now handles potential `null` responses from the Drive API more safely.
These changes improve the reliability and robustness of Drive interactions by proactively managing token lifecycles and gracefully handling API errors.
This commit updates the `DriveManager` to accept an `AuthManager` instance as a dependency. The `providesDriveManager` function in the Dagger module (`Provider.kt`) has been modified to inject `AuthManager` into `DriveManager` during its construction. This change likely facilitates authentication-related operations within `DriveManager`.
This commit enhances the `EventFolderRepository.getOrCreateProjectFolder` function. The function now checks if the `rootFolderIfFromPref` retrieved from `dataStorePref` is null or blank. Previously, it only checked for null. Additionally, debug logs have been added to trace the different execution paths within the function, such as retrieving the folder ID from local storage, Google Drive, or creating a new root folder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implemented feature to obtain new token for Google Drive API before driveService execute, considering previous token expires in 5 mins