-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
...re/src/main/java/com/easyhz/noffice/core/datastore/datasource/auth/AuthLocalDataSource.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.easyhz.noffice.core.datastore.datasource.auth | ||
|
||
|
||
interface AuthLocalDataSource { | ||
suspend fun getAccessToken(): Result<String> | ||
suspend fun getRefreshToken(): Result<String> | ||
suspend fun deleteToken() | ||
suspend fun updateAccessToken(access: String) | ||
suspend fun updateTokens(access: String, refresh: String) | ||
} |
59 changes: 59 additions & 0 deletions
59
...rc/main/java/com/easyhz/noffice/core/datastore/datasource/auth/AuthLocalDataSourceImpl.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.easyhz.noffice.core.datastore.datasource.auth | ||
|
||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import com.easyhz.noffice.core.common.di.Dispatcher | ||
import com.easyhz.noffice.core.common.di.NofficeDispatchers.IO | ||
import com.easyhz.noffice.core.datastore.util.AuthKey | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
|
||
class AuthLocalDataSourceImpl @Inject constructor( | ||
@Dispatcher(IO) private val dispatcher: CoroutineDispatcher, | ||
private val dataStore: DataStore<Preferences> | ||
): AuthLocalDataSource { | ||
private val accessToken = stringPreferencesKey(AuthKey.ACCESS_TOKEN.key) | ||
private val refreshToken = stringPreferencesKey(AuthKey.REFRESH_TOKEN.key) | ||
|
||
override suspend fun getAccessToken(): Result<String> = withContext(dispatcher) { | ||
val preferences = dataStore.data.first() | ||
return@withContext preferences[accessToken]?.let { | ||
Result.success(it) | ||
} ?: Result.failure(generateNullException(AuthKey.ACCESS_TOKEN)) | ||
} | ||
|
||
override suspend fun getRefreshToken(): Result<String> = withContext(dispatcher) { | ||
val preferences = dataStore.data.first() | ||
return@withContext preferences[refreshToken]?.let { | ||
Result.success(it) | ||
} ?: Result.failure(generateNullException(AuthKey.REFRESH_TOKEN)) | ||
} | ||
|
||
override suspend fun deleteToken() { | ||
dataStore.edit { preferences -> | ||
preferences.remove(accessToken) | ||
preferences.remove(refreshToken) | ||
} | ||
} | ||
|
||
override suspend fun updateAccessToken(access: String) { | ||
dataStore.edit { preferences -> | ||
preferences[accessToken] = access | ||
} | ||
} | ||
|
||
override suspend fun updateTokens(access: String, refresh: String) { | ||
dataStore.edit { preferences -> | ||
preferences[accessToken] = access | ||
preferences[refreshToken] = refresh | ||
} | ||
} | ||
|
||
private fun generateNullException(authKey: AuthKey): Exception { | ||
return Exception("${authKey.key} is null") | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
core/datastore/src/main/java/com/easyhz/noffice/core/datastore/di/DataSourceModule.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.easyhz.noffice.core.datastore.di | ||
|
||
import com.easyhz.noffice.core.datastore.datasource.auth.AuthLocalDataSource | ||
import com.easyhz.noffice.core.datastore.datasource.auth.AuthLocalDataSourceImpl | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
interface DataSourceModule { | ||
@Binds | ||
fun bindAuthLocalDataSource( | ||
authLocalDataSourceImpl: AuthLocalDataSourceImpl | ||
): AuthLocalDataSource | ||
} |
27 changes: 27 additions & 0 deletions
27
core/datastore/src/main/java/com/easyhz/noffice/core/datastore/di/DataStoreModule.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.easyhz.noffice.core.datastore.di | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.dataStoreFile | ||
import androidx.datastore.preferences.core.Preferences | ||
import com.easyhz.noffice.core.datastore.util.authDataStore | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@InstallIn(SingletonComponent::class) | ||
@Module | ||
abstract class DataStoreModule { | ||
companion object { | ||
|
||
@Singleton | ||
@Provides | ||
fun provideUserDataStorePreferences( | ||
@ApplicationContext context: Context | ||
): DataStore<Preferences> = | ||
context.authDataStore | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
core/datastore/src/main/java/com/easyhz/noffice/core/datastore/util/DataStoreConfig.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.easyhz.noffice.core.datastore.util | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.preferencesDataStore | ||
|
||
val Context.authDataStore: DataStore<Preferences> by preferencesDataStore( | ||
name = "noffice-auth" | ||
) |
11 changes: 11 additions & 0 deletions
11
core/datastore/src/main/java/com/easyhz/noffice/core/datastore/util/DataStoreKey.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.easyhz.noffice.core.datastore.util | ||
|
||
internal enum class AuthKey( | ||
val key: String | ||
) { | ||
ACCESS_TOKEN( | ||
key = "ACCESS_TOKEN" | ||
), REFRESH_TOKEN( | ||
key = "REFRESH_TOKEN" | ||
) | ||
} |