Добавлен Yandex
This commit is contained in:
@@ -67,6 +67,8 @@ dependencies {
|
|||||||
implementation(libs.androidx.core.ktx)
|
implementation(libs.androidx.core.ktx)
|
||||||
implementation(libs.androidx.lifecycle.runtime.ktx)
|
implementation(libs.androidx.lifecycle.runtime.ktx)
|
||||||
implementation(libs.androidx.activity.compose)
|
implementation(libs.androidx.activity.compose)
|
||||||
|
implementation(platform(libs.androidx.compose.bom))
|
||||||
|
implementation(libs.androidx.ui)
|
||||||
|
|
||||||
testImplementation(libs.junit)
|
testImplementation(libs.junit)
|
||||||
androidTestImplementation(libs.androidx.junit)
|
androidTestImplementation(libs.androidx.junit)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
|
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
|
||||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||||
|
|||||||
@@ -9,30 +9,37 @@ import androidx.activity.compose.setContent
|
|||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
import com.github.nullptroma.wallenc.app.auth.YandexSignInService
|
||||||
import com.github.nullptroma.wallenc.presentation.WallencUi
|
import com.github.nullptroma.wallenc.presentation.WallencUi
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var yandexSignInService: YandexSignInService
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
yandexSignInService.registerWith(this)
|
||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
requestNotificationPermissionIfNeeded()
|
requestNotificationPermissionIfNeeded()
|
||||||
|
|
||||||
Timber.plant(Timber.DebugTree())
|
Timber.plant(Timber.DebugTree())
|
||||||
// val sdk = YandexAuthSdk.create(YandexAuthOptions(applicationContext, true))
|
|
||||||
// val launcher =
|
|
||||||
// registerForActivityResult(sdk.contract) { result -> handleResult(result) }
|
|
||||||
// val loginOptions = YandexAuthLoginOptions(LoginType.CHROME_TAB)
|
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
WallencUi()
|
WallencUi()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
yandexSignInService.unregister(this)
|
||||||
|
super.onDestroy()
|
||||||
|
}
|
||||||
|
|
||||||
private fun requestNotificationPermissionIfNeeded() {
|
private fun requestNotificationPermissionIfNeeded() {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return
|
||||||
val granted = ContextCompat.checkSelfPermission(
|
val granted = ContextCompat.checkSelfPermission(
|
||||||
@@ -50,12 +57,4 @@ class MainActivity : ComponentActivity() {
|
|||||||
companion object {
|
companion object {
|
||||||
private const val NOTIFICATION_PERMISSION_REQUEST_CODE = 100
|
private const val NOTIFICATION_PERMISSION_REQUEST_CODE = 100
|
||||||
}
|
}
|
||||||
|
|
||||||
// private fun handleResult(result: YandexAuthResult) {
|
|
||||||
// when (result) {
|
|
||||||
// is YandexAuthResult.Success -> Toast.makeText(applicationContext, "Success: ${result.token}", Toast.LENGTH_SHORT).show()
|
|
||||||
// is YandexAuthResult.Failure -> Toast.makeText(applicationContext, "Success: ${result.exception}", Toast.LENGTH_SHORT).show()
|
|
||||||
// YandexAuthResult.Cancelled -> Toast.makeText(applicationContext, "Cancel", Toast.LENGTH_SHORT).show()
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package com.github.nullptroma.wallenc.app.auth
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.activity.ComponentActivity
|
||||||
|
import androidx.activity.result.ActivityResultLauncher
|
||||||
|
import com.github.nullptroma.wallenc.domain.auth.RemoteYandexAuthResult
|
||||||
|
import com.github.nullptroma.wallenc.domain.auth.RemoteYandexSignInLauncher
|
||||||
|
import com.yandex.authsdk.YandexAuthLoginOptions
|
||||||
|
import com.yandex.authsdk.YandexAuthOptions
|
||||||
|
import com.yandex.authsdk.YandexAuthResult
|
||||||
|
import com.yandex.authsdk.YandexAuthSdk
|
||||||
|
import com.yandex.authsdk.internal.strategy.LoginType
|
||||||
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Императивный вход через Yandex Auth SDK: [registerWith] в [ComponentActivity.onCreate],
|
||||||
|
* [unregister] в [ComponentActivity.onDestroy], чтобы синглтон не держал [ActivityResultLauncher]
|
||||||
|
* дольше жизни Activity.
|
||||||
|
*
|
||||||
|
* При смене конфигурации новая Activity может вызвать [registerWith] до [unregister] старой —
|
||||||
|
* тогда владелец и launcher обновляются; [unregister] для уже неактуального экземпляра — no-op.
|
||||||
|
*/
|
||||||
|
@Singleton
|
||||||
|
class YandexSignInService @Inject constructor(
|
||||||
|
@ApplicationContext appContext: Context,
|
||||||
|
) : RemoteYandexSignInLauncher {
|
||||||
|
|
||||||
|
private val sdk = YandexAuthSdk.create(YandexAuthOptions(appContext, true))
|
||||||
|
|
||||||
|
private var launcher: ActivityResultLauncher<YandexAuthLoginOptions>? = null
|
||||||
|
|
||||||
|
private var registrationOwner: ComponentActivity? = null
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private var pending: ((RemoteYandexAuthResult) -> Unit)? = null
|
||||||
|
|
||||||
|
fun registerWith(activity: ComponentActivity) {
|
||||||
|
if (registrationOwner === activity && launcher != null) return
|
||||||
|
launcher = activity.registerForActivityResult(sdk.contract) { result ->
|
||||||
|
val mapped = mapYandexResult(result)
|
||||||
|
val cb = synchronized(this) {
|
||||||
|
val p = pending
|
||||||
|
pending = null
|
||||||
|
p
|
||||||
|
}
|
||||||
|
cb?.invoke(mapped)
|
||||||
|
}
|
||||||
|
registrationOwner = activity
|
||||||
|
}
|
||||||
|
|
||||||
|
fun unregister(activity: ComponentActivity) {
|
||||||
|
if (registrationOwner !== activity) return
|
||||||
|
launcher = null
|
||||||
|
registrationOwner = null
|
||||||
|
val cb = synchronized(this) {
|
||||||
|
val p = pending
|
||||||
|
pending = null
|
||||||
|
p
|
||||||
|
}
|
||||||
|
cb?.invoke(RemoteYandexAuthResult.Cancelled)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun launch(onResult: (RemoteYandexAuthResult) -> Unit) {
|
||||||
|
val l = launcher
|
||||||
|
?: error("YandexSignInService: call registerWith(activity) from MainActivity.onCreate first")
|
||||||
|
synchronized(this) {
|
||||||
|
pending = onResult
|
||||||
|
}
|
||||||
|
l.launch(YandexAuthLoginOptions(LoginType.WEBVIEW))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun mapYandexResult(result: YandexAuthResult): RemoteYandexAuthResult = when (result) {
|
||||||
|
is YandexAuthResult.Success ->
|
||||||
|
RemoteYandexAuthResult.Success(result.token.value)
|
||||||
|
is YandexAuthResult.Failure ->
|
||||||
|
RemoteYandexAuthResult.Failure(
|
||||||
|
result.exception.message ?: result.exception.toString(),
|
||||||
|
)
|
||||||
|
YandexAuthResult.Cancelled -> RemoteYandexAuthResult.Cancelled
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.github.nullptroma.wallenc.app.di.modules.auth
|
||||||
|
|
||||||
|
import com.github.nullptroma.wallenc.app.auth.YandexSignInService
|
||||||
|
import com.github.nullptroma.wallenc.domain.auth.RemoteYandexSignInLauncher
|
||||||
|
import dagger.Binds
|
||||||
|
import dagger.Module
|
||||||
|
import dagger.hilt.InstallIn
|
||||||
|
import dagger.hilt.components.SingletonComponent
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Module
|
||||||
|
@InstallIn(SingletonComponent::class)
|
||||||
|
abstract class YandexAuthModule {
|
||||||
|
|
||||||
|
@Binds
|
||||||
|
@Singleton
|
||||||
|
abstract fun bindRemoteYandexSignInLauncher(
|
||||||
|
impl: YandexSignInService,
|
||||||
|
): RemoteYandexSignInLauncher
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import com.github.nullptroma.wallenc.data.db.RoomFactory
|
|||||||
import com.github.nullptroma.wallenc.data.db.app.IAppDb
|
import com.github.nullptroma.wallenc.data.db.app.IAppDb
|
||||||
import com.github.nullptroma.wallenc.data.db.app.dao.StorageKeyMapDao
|
import com.github.nullptroma.wallenc.data.db.app.dao.StorageKeyMapDao
|
||||||
import com.github.nullptroma.wallenc.data.db.app.dao.StorageMetaInfoDao
|
import com.github.nullptroma.wallenc.data.db.app.dao.StorageMetaInfoDao
|
||||||
|
import com.github.nullptroma.wallenc.data.db.app.dao.YandexAccountDao
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import dagger.hilt.InstallIn
|
import dagger.hilt.InstallIn
|
||||||
@@ -32,6 +33,12 @@ class RoomModule {
|
|||||||
return database.storageMetaInfoDao
|
return database.storageMetaInfoDao
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun provideYandexAccountDao(database: IAppDb): YandexAccountDao {
|
||||||
|
return database.yandexAccountDao
|
||||||
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideAppDb(
|
fun provideAppDb(
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ import android.content.Context
|
|||||||
import com.github.nullptroma.wallenc.app.di.modules.app.IoDispatcher
|
import com.github.nullptroma.wallenc.app.di.modules.app.IoDispatcher
|
||||||
import com.github.nullptroma.wallenc.data.db.app.dao.StorageKeyMapDao
|
import com.github.nullptroma.wallenc.data.db.app.dao.StorageKeyMapDao
|
||||||
import com.github.nullptroma.wallenc.data.db.app.dao.StorageMetaInfoDao
|
import com.github.nullptroma.wallenc.data.db.app.dao.StorageMetaInfoDao
|
||||||
|
import com.github.nullptroma.wallenc.data.db.app.dao.YandexAccountDao
|
||||||
|
import com.github.nullptroma.wallenc.data.network.YandexUserInfoApi
|
||||||
|
import com.github.nullptroma.wallenc.data.network.YandexUserInfoApiFactory
|
||||||
import com.github.nullptroma.wallenc.data.db.app.repository.StorageKeyMapRepository
|
import com.github.nullptroma.wallenc.data.db.app.repository.StorageKeyMapRepository
|
||||||
import com.github.nullptroma.wallenc.data.db.app.repository.StorageMetaInfoRepository
|
import com.github.nullptroma.wallenc.data.db.app.repository.StorageMetaInfoRepository
|
||||||
import com.github.nullptroma.wallenc.data.tasks.TaskOrchestrator
|
import com.github.nullptroma.wallenc.data.tasks.TaskOrchestrator
|
||||||
@@ -28,14 +31,20 @@ class SingletonModule {
|
|||||||
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||||
): ITaskOrchestrator = TaskOrchestrator(ioDispatcher)
|
): ITaskOrchestrator = TaskOrchestrator(ioDispatcher)
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
fun provideYandexUserInfoApi(): YandexUserInfoApi = YandexUserInfoApiFactory.create()
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideVaultsManager(
|
fun provideVaultsManager(
|
||||||
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
@IoDispatcher ioDispatcher: CoroutineDispatcher,
|
||||||
@ApplicationContext context: Context,
|
@ApplicationContext context: Context,
|
||||||
keyRepo: StorageKeyMapRepository,
|
keyRepo: StorageKeyMapRepository,
|
||||||
|
yandexAccountDao: YandexAccountDao,
|
||||||
|
yandexUserInfoApi: YandexUserInfoApi,
|
||||||
): IVaultsManager {
|
): IVaultsManager {
|
||||||
return VaultsManager(ioDispatcher, context, keyRepo)
|
return VaultsManager(ioDispatcher, context, keyRepo, yandexAccountDao, yandexUserInfoApi)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
|
|||||||
@@ -4,16 +4,24 @@ import androidx.room.Database
|
|||||||
import androidx.room.RoomDatabase
|
import androidx.room.RoomDatabase
|
||||||
import com.github.nullptroma.wallenc.data.db.app.dao.StorageKeyMapDao
|
import com.github.nullptroma.wallenc.data.db.app.dao.StorageKeyMapDao
|
||||||
import com.github.nullptroma.wallenc.data.db.app.dao.StorageMetaInfoDao
|
import com.github.nullptroma.wallenc.data.db.app.dao.StorageMetaInfoDao
|
||||||
|
import com.github.nullptroma.wallenc.data.db.app.dao.YandexAccountDao
|
||||||
import com.github.nullptroma.wallenc.data.db.app.model.DbStorageKeyMap
|
import com.github.nullptroma.wallenc.data.db.app.model.DbStorageKeyMap
|
||||||
import com.github.nullptroma.wallenc.data.db.app.model.DbStorageMetaInfo
|
import com.github.nullptroma.wallenc.data.db.app.model.DbStorageMetaInfo
|
||||||
|
import com.github.nullptroma.wallenc.data.db.app.model.DbYandexAccount
|
||||||
|
|
||||||
interface IAppDb {
|
interface IAppDb {
|
||||||
val storageKeyMapDao: StorageKeyMapDao
|
val storageKeyMapDao: StorageKeyMapDao
|
||||||
val storageMetaInfoDao: StorageMetaInfoDao
|
val storageMetaInfoDao: StorageMetaInfoDao
|
||||||
|
val yandexAccountDao: YandexAccountDao
|
||||||
}
|
}
|
||||||
|
|
||||||
@Database(entities = [DbStorageKeyMap::class, DbStorageMetaInfo::class], version = 3, exportSchema = false)
|
@Database(
|
||||||
|
entities = [DbStorageKeyMap::class, DbStorageMetaInfo::class, DbYandexAccount::class],
|
||||||
|
version = 4,
|
||||||
|
exportSchema = false,
|
||||||
|
)
|
||||||
abstract class AppDb : IAppDb, RoomDatabase() {
|
abstract class AppDb : IAppDb, RoomDatabase() {
|
||||||
abstract override val storageKeyMapDao: StorageKeyMapDao
|
abstract override val storageKeyMapDao: StorageKeyMapDao
|
||||||
abstract override val storageMetaInfoDao: StorageMetaInfoDao
|
abstract override val storageMetaInfoDao: StorageMetaInfoDao
|
||||||
|
abstract override val yandexAccountDao: YandexAccountDao
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.github.nullptroma.wallenc.data.db.app.dao
|
||||||
|
|
||||||
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.Query
|
||||||
|
import com.github.nullptroma.wallenc.data.db.app.model.DbYandexAccount
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
interface YandexAccountDao {
|
||||||
|
@Query("SELECT * FROM yandex_accounts WHERE yandexUserId = :id LIMIT 1")
|
||||||
|
suspend fun getByYandexUserId(id: String): DbYandexAccount?
|
||||||
|
|
||||||
|
@Insert
|
||||||
|
suspend fun insert(account: DbYandexAccount)
|
||||||
|
|
||||||
|
@Query(
|
||||||
|
"UPDATE yandex_accounts SET oauthToken = :token, email = :email WHERE vaultUuid = :vaultUuid",
|
||||||
|
)
|
||||||
|
suspend fun updateCredentials(vaultUuid: String, email: String, token: String)
|
||||||
|
|
||||||
|
@Query("SELECT * FROM yandex_accounts ORDER BY email COLLATE NOCASE ASC")
|
||||||
|
fun observeAll(): Flow<List<DbYandexAccount>>
|
||||||
|
|
||||||
|
@Query("DELETE FROM yandex_accounts WHERE vaultUuid = :vaultUuid")
|
||||||
|
suspend fun deleteByVaultUuid(vaultUuid: String)
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.github.nullptroma.wallenc.data.db.app.model
|
||||||
|
|
||||||
|
import androidx.room.Entity
|
||||||
|
import androidx.room.Index
|
||||||
|
import androidx.room.PrimaryKey
|
||||||
|
|
||||||
|
@Entity(
|
||||||
|
tableName = "yandex_accounts",
|
||||||
|
indices = [Index(value = ["yandexUserId"], unique = true)],
|
||||||
|
)
|
||||||
|
data class DbYandexAccount(
|
||||||
|
@PrimaryKey val vaultUuid: String,
|
||||||
|
val yandexUserId: String,
|
||||||
|
val email: String,
|
||||||
|
val oauthToken: String,
|
||||||
|
)
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.github.nullptroma.wallenc.data.network
|
||||||
|
|
||||||
|
import retrofit2.http.GET
|
||||||
|
import retrofit2.http.Header
|
||||||
|
import retrofit2.http.Query
|
||||||
|
|
||||||
|
interface YandexUserInfoApi {
|
||||||
|
@GET("info")
|
||||||
|
suspend fun userInfo(
|
||||||
|
@Query("format") format: String,
|
||||||
|
@Header("Authorization") authorization: String,
|
||||||
|
): YandexUserInfoDto
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.github.nullptroma.wallenc.data.network
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
|
import retrofit2.Retrofit
|
||||||
|
import retrofit2.converter.jackson.JacksonConverterFactory
|
||||||
|
|
||||||
|
object YandexUserInfoApiFactory {
|
||||||
|
fun create(): YandexUserInfoApi {
|
||||||
|
val retrofit = Retrofit.Builder()
|
||||||
|
.baseUrl("https://login.yandex.ru/")
|
||||||
|
.addConverterFactory(
|
||||||
|
JacksonConverterFactory.create(jacksonObjectMapper().findAndRegisterModules()),
|
||||||
|
)
|
||||||
|
.build()
|
||||||
|
return retrofit.create(YandexUserInfoApi::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.github.nullptroma.wallenc.data.network
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty
|
||||||
|
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
data class YandexUserInfoDto(
|
||||||
|
val id: String,
|
||||||
|
val login: String,
|
||||||
|
@JsonProperty("default_email") val defaultEmail: String? = null,
|
||||||
|
)
|
||||||
@@ -12,7 +12,7 @@ import kotlinx.coroutines.CoroutineDispatcher
|
|||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
@@ -33,7 +33,7 @@ class UnlockManager(
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
CoroutineScope(ioDispatcher).launch {
|
CoroutineScope(ioDispatcher).launch {
|
||||||
vaultsManager.allStorages.collectLatest {
|
vaultsManager.allStorages.collect {
|
||||||
mutex.withLock {
|
mutex.withLock {
|
||||||
val allKeys = keymapRepository.getAll()
|
val allKeys = keymapRepository.getAll()
|
||||||
val keysToRemove = mutableListOf<StorageKeyMap>()
|
val keysToRemove = mutableListOf<StorageKeyMap>()
|
||||||
|
|||||||
@@ -1,33 +1,84 @@
|
|||||||
package com.github.nullptroma.wallenc.data.vaults
|
package com.github.nullptroma.wallenc.data.vaults
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import com.github.nullptroma.wallenc.data.db.app.dao.YandexAccountDao
|
||||||
|
import com.github.nullptroma.wallenc.data.db.app.model.DbYandexAccount
|
||||||
import com.github.nullptroma.wallenc.data.db.app.repository.StorageKeyMapRepository
|
import com.github.nullptroma.wallenc.data.db.app.repository.StorageKeyMapRepository
|
||||||
|
import com.github.nullptroma.wallenc.data.network.YandexUserInfoApi
|
||||||
import com.github.nullptroma.wallenc.data.storages.UnlockManager
|
import com.github.nullptroma.wallenc.data.storages.UnlockManager
|
||||||
import com.github.nullptroma.wallenc.domain.interfaces.IStorage
|
import com.github.nullptroma.wallenc.domain.interfaces.IStorage
|
||||||
import com.github.nullptroma.wallenc.domain.interfaces.IUnlockManager
|
import com.github.nullptroma.wallenc.domain.interfaces.IUnlockManager
|
||||||
import com.github.nullptroma.wallenc.domain.interfaces.IVault
|
import com.github.nullptroma.wallenc.domain.interfaces.IVault
|
||||||
import com.github.nullptroma.wallenc.domain.interfaces.IVaultsManager
|
import com.github.nullptroma.wallenc.domain.interfaces.IVaultsManager
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.coroutines.flow.stateIn
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
class VaultsManager(
|
||||||
|
private val ioDispatcher: CoroutineDispatcher,
|
||||||
|
context: Context,
|
||||||
|
keyRepo: StorageKeyMapRepository,
|
||||||
|
private val yandexAccountDao: YandexAccountDao,
|
||||||
|
private val yandexUserInfoApi: YandexUserInfoApi,
|
||||||
|
) : IVaultsManager {
|
||||||
|
private val scope = CoroutineScope(SupervisorJob() + ioDispatcher)
|
||||||
|
|
||||||
class VaultsManager(ioDispatcher: CoroutineDispatcher, context: Context, keyRepo: StorageKeyMapRepository) : IVaultsManager {
|
|
||||||
override val localVault = LocalVault(ioDispatcher, context)
|
override val localVault = LocalVault(ioDispatcher, context)
|
||||||
|
|
||||||
|
// До unlockManager: UnlockManager в init подписывается на allStorages.
|
||||||
|
override val allStorages: StateFlow<List<IStorage>> = localVault.storages
|
||||||
|
|
||||||
override val unlockManager: IUnlockManager = UnlockManager(
|
override val unlockManager: IUnlockManager = UnlockManager(
|
||||||
keymapRepository = keyRepo,
|
keymapRepository = keyRepo,
|
||||||
ioDispatcher = ioDispatcher,
|
ioDispatcher = ioDispatcher,
|
||||||
vaultsManager = this
|
vaultsManager = this
|
||||||
)
|
)
|
||||||
override val remoteVaults: StateFlow<List<IVault>>
|
|
||||||
get() = TODO("Not yet implemented")
|
|
||||||
override val allStorages: StateFlow<List<IStorage>>
|
|
||||||
get() = localVault.storages
|
|
||||||
override val allVaults: StateFlow<List<IVault>>
|
|
||||||
get() = MutableStateFlow(listOf(localVault))
|
|
||||||
|
|
||||||
|
private val _remoteVaults = yandexAccountDao.observeAll()
|
||||||
|
.map { rows ->
|
||||||
|
rows.map { row ->
|
||||||
|
YandexVault(
|
||||||
|
uuid = UUID.fromString(row.vaultUuid),
|
||||||
|
accountEmail = row.email,
|
||||||
|
oauthToken = row.oauthToken,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.stateIn(scope, SharingStarted.Eagerly, emptyList())
|
||||||
|
|
||||||
override fun addYandexVault(email: String, token: String) {
|
override val remoteVaults: StateFlow<List<IVault>> = _remoteVaults
|
||||||
TODO("Not yet implemented")
|
|
||||||
|
override val allVaults: StateFlow<List<IVault>> = _remoteVaults
|
||||||
|
.map { listOf(localVault) + it }
|
||||||
|
.stateIn(scope, SharingStarted.Eagerly, listOf(localVault))
|
||||||
|
|
||||||
|
override suspend fun addYandexVault(accessToken: String) = withContext(ioDispatcher) {
|
||||||
|
val info = yandexUserInfoApi.userInfo("json", "OAuth $accessToken")
|
||||||
|
val email = info.defaultEmail?.takeIf { it.isNotBlank() }
|
||||||
|
?: "${info.login}@yandex.ru"
|
||||||
|
val existing = yandexAccountDao.getByYandexUserId(info.id)
|
||||||
|
val vaultUuid = existing?.vaultUuid ?: UUID.randomUUID().toString()
|
||||||
|
if (existing != null) {
|
||||||
|
yandexAccountDao.updateCredentials(vaultUuid, email, accessToken)
|
||||||
|
} else {
|
||||||
|
yandexAccountDao.insert(
|
||||||
|
DbYandexAccount(
|
||||||
|
vaultUuid = vaultUuid,
|
||||||
|
yandexUserId = info.id,
|
||||||
|
email = email,
|
||||||
|
oauthToken = accessToken,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun removeRemoteVault(vaultUuid: UUID) = withContext(ioDispatcher) {
|
||||||
|
yandexAccountDao.deleteByVaultUuid(vaultUuid.toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package com.github.nullptroma.wallenc.data.vaults
|
||||||
|
|
||||||
|
import com.github.nullptroma.wallenc.domain.datatypes.StorageEncryptionInfo
|
||||||
|
import com.github.nullptroma.wallenc.domain.enums.VaultType
|
||||||
|
import com.github.nullptroma.wallenc.domain.interfaces.IStorage
|
||||||
|
import com.github.nullptroma.wallenc.domain.interfaces.IYandexVault
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Удалённый vault Яндекс: OAuth и профиль есть; Yandex.Disk и хранилища — заглушки.
|
||||||
|
*/
|
||||||
|
class YandexVault(
|
||||||
|
override val uuid: UUID,
|
||||||
|
override val accountEmail: String,
|
||||||
|
@Suppress("unused") val oauthToken: String,
|
||||||
|
) : IYandexVault {
|
||||||
|
override val type: VaultType = VaultType.YANDEX
|
||||||
|
|
||||||
|
private val _storages = MutableStateFlow<List<IStorage>>(emptyList())
|
||||||
|
override val storages: StateFlow<List<IStorage>?> = _storages
|
||||||
|
|
||||||
|
private val _isAvailable = MutableStateFlow(true)
|
||||||
|
override val isAvailable: StateFlow<Boolean> = _isAvailable
|
||||||
|
|
||||||
|
private val _totalSpace = MutableStateFlow<Int?>(null)
|
||||||
|
override val totalSpace: StateFlow<Int?> = _totalSpace
|
||||||
|
|
||||||
|
private val _availableSpace = MutableStateFlow<Int?>(null)
|
||||||
|
override val availableSpace: StateFlow<Int?> = _availableSpace
|
||||||
|
|
||||||
|
override suspend fun createStorage(): IStorage {
|
||||||
|
throw UnsupportedOperationException("Yandex.Disk ещё не подключён")
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun createStorage(enc: StorageEncryptionInfo): IStorage {
|
||||||
|
throw UnsupportedOperationException("Yandex.Disk ещё не подключён")
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun remove(storage: IStorage) {
|
||||||
|
// заглушка до интеграции API Диска
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.github.nullptroma.wallenc.domain.auth
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Результат входа через Яндекс (без зависимости от Yandex SDK).
|
||||||
|
*/
|
||||||
|
sealed interface RemoteYandexAuthResult {
|
||||||
|
data class Success(val accessToken: String) : RemoteYandexAuthResult
|
||||||
|
data class Failure(val message: String) : RemoteYandexAuthResult
|
||||||
|
data object Cancelled : RemoteYandexAuthResult
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Запуск OAuth Яндекса. Реализация только в модуле app (Yandex Auth SDK).
|
||||||
|
*/
|
||||||
|
fun interface RemoteYandexSignInLauncher {
|
||||||
|
fun launch(onResult: (RemoteYandexAuthResult) -> Unit)
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.github.nullptroma.wallenc.domain.interfaces
|
package com.github.nullptroma.wallenc.domain.interfaces
|
||||||
|
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
interface IVaultsManager {
|
interface IVaultsManager {
|
||||||
val localVault: IVault
|
val localVault: IVault
|
||||||
@@ -8,5 +9,7 @@ interface IVaultsManager {
|
|||||||
val remoteVaults: StateFlow<List<IVault>>
|
val remoteVaults: StateFlow<List<IVault>>
|
||||||
val allStorages: StateFlow<List<IStorage>>
|
val allStorages: StateFlow<List<IStorage>>
|
||||||
val allVaults: StateFlow<List<IVault>>
|
val allVaults: StateFlow<List<IVault>>
|
||||||
fun addYandexVault(email: String, token: String)
|
suspend fun addYandexVault(accessToken: String)
|
||||||
|
|
||||||
|
suspend fun removeRemoteVault(vaultUuid: UUID)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.github.nullptroma.wallenc.domain.interfaces
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Удалённый vault Яндекс с привязанным аккаунтом (почта для UI).
|
||||||
|
*/
|
||||||
|
interface IYandexVault : IVault {
|
||||||
|
val accountEmail: String
|
||||||
|
}
|
||||||
@@ -19,7 +19,7 @@ import com.github.nullptroma.wallenc.domain.tasks.TaskLogLevel
|
|||||||
import com.github.nullptroma.wallenc.presentation.ViewModelBase
|
import com.github.nullptroma.wallenc.presentation.ViewModelBase
|
||||||
import com.github.nullptroma.wallenc.presentation.extensions.toPrintable
|
import com.github.nullptroma.wallenc.presentation.extensions.toPrintable
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
import kotlinx.coroutines.flow.SharedFlow
|
import kotlinx.coroutines.flow.SharedFlow
|
||||||
@@ -70,8 +70,9 @@ class LocalVaultViewModel @Inject constructor(
|
|||||||
private fun collectFlows() {
|
private fun collectFlows() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
manageLocalVaultUseCase.localStorages.combine(getOpenedStoragesUseCase.openedStorages) { local, opened ->
|
manageLocalVaultUseCase.localStorages.combine(getOpenedStoragesUseCase.openedStorages) { local, opened ->
|
||||||
if(local == null)
|
if (local == null) {
|
||||||
return@combine null
|
return@combine Pair(true, emptyList<Tree<IStorageInfo>>())
|
||||||
|
}
|
||||||
val list = mutableListOf<Tree<IStorageInfo>>()
|
val list = mutableListOf<Tree<IStorageInfo>>()
|
||||||
for (storage in local) {
|
for (storage in local) {
|
||||||
var tree = Tree(storage)
|
var tree = Tree(storage)
|
||||||
@@ -83,13 +84,10 @@ class LocalVaultViewModel @Inject constructor(
|
|||||||
tree = nextTree
|
tree = nextTree
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return@combine list
|
return@combine Pair(false, list)
|
||||||
}.collectLatest {
|
}.collect { (loading, trees) ->
|
||||||
isLoading = it == null
|
isLoading = loading
|
||||||
val newState = state.value.copy(
|
updateState(state.value.copy(storagesList = trees))
|
||||||
storagesList = it ?: listOf()
|
|
||||||
)
|
|
||||||
updateState(newState)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,241 @@
|
|||||||
package com.github.nullptroma.wallenc.presentation.screens.main.screens.remotes
|
package com.github.nullptroma.wallenc.presentation.screens.main.screens.remotes
|
||||||
|
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import android.widget.Toast
|
||||||
|
import androidx.compose.foundation.clickable
|
||||||
|
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.PaddingValues
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.Spacer
|
||||||
|
import androidx.compose.foundation.layout.WindowInsets
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
|
import androidx.compose.foundation.lazy.items
|
||||||
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.filled.Add
|
||||||
|
import androidx.compose.material.icons.filled.Delete
|
||||||
|
import androidx.compose.material3.AlertDialog
|
||||||
|
import androidx.compose.material3.CardDefaults
|
||||||
|
import androidx.compose.material3.CircularProgressIndicator
|
||||||
|
import androidx.compose.material3.ElevatedCard
|
||||||
|
import androidx.compose.material3.FilledTonalButton
|
||||||
|
import androidx.compose.material3.FloatingActionButton
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
|
import androidx.compose.material3.IconButton
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Scaffold
|
||||||
|
import androidx.compose.material3.Surface
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.material3.TextButton
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.compose.ui.window.Dialog
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.hilt.navigation.compose.hiltViewModel
|
import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
|
import com.github.nullptroma.wallenc.domain.auth.RemoteYandexAuthResult
|
||||||
|
import com.github.nullptroma.wallenc.domain.enums.VaultType
|
||||||
|
import com.github.nullptroma.wallenc.presentation.R
|
||||||
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
@Composable
|
||||||
fun RemoteVaultsScreen(modifier: Modifier = Modifier,
|
fun RemoteVaultsScreen(
|
||||||
viewModel: RemoteVaultsViewModel = hiltViewModel()) {
|
modifier: Modifier = Modifier,
|
||||||
Text("Remote vault screen", modifier = modifier)
|
viewModel: RemoteVaultsViewModel = hiltViewModel(),
|
||||||
|
) {
|
||||||
|
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
|
val context = LocalContext.current
|
||||||
|
|
||||||
|
Box {
|
||||||
|
Scaffold(
|
||||||
|
modifier = modifier,
|
||||||
|
contentWindowInsets = WindowInsets(0.dp),
|
||||||
|
floatingActionButton = {
|
||||||
|
FloatingActionButton(
|
||||||
|
onClick = {
|
||||||
|
if (!uiState.isBusy) viewModel.setAddChoiceVisible(true)
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
Icons.Filled.Add,
|
||||||
|
contentDescription = stringResource(R.string.remote_vaults_add_cd),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
) { innerPadding ->
|
||||||
|
if (uiState.vaults.isEmpty()) {
|
||||||
|
Text(
|
||||||
|
text = stringResource(R.string.remote_vaults_empty_hint),
|
||||||
|
modifier = Modifier
|
||||||
|
.padding(innerPadding)
|
||||||
|
.padding(24.dp),
|
||||||
|
style = MaterialTheme.typography.bodyLarge,
|
||||||
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
LazyColumn(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(innerPadding),
|
||||||
|
contentPadding = PaddingValues(16.dp),
|
||||||
|
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||||
|
) {
|
||||||
|
items(uiState.vaults, key = { it.uuid }) { item ->
|
||||||
|
ElevatedCard(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
shape = RoundedCornerShape(16.dp),
|
||||||
|
elevation = CardDefaults.elevatedCardElevation(defaultElevation = 2.dp),
|
||||||
|
colors = CardDefaults.elevatedCardColors(
|
||||||
|
containerColor = MaterialTheme.colorScheme.surfaceContainerHigh,
|
||||||
|
),
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(16.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
|
) {
|
||||||
|
Column(modifier = Modifier.weight(1f)) {
|
||||||
|
Text(
|
||||||
|
text = item.label,
|
||||||
|
style = MaterialTheme.typography.titleMedium,
|
||||||
|
color = MaterialTheme.colorScheme.onSurface,
|
||||||
|
)
|
||||||
|
Spacer(modifier = Modifier.height(4.dp))
|
||||||
|
Text(
|
||||||
|
text = when (item.type) {
|
||||||
|
VaultType.YANDEX ->
|
||||||
|
stringResource(R.string.remote_vault_type_yandex)
|
||||||
|
else -> item.type.name
|
||||||
|
},
|
||||||
|
style = MaterialTheme.typography.bodyMedium,
|
||||||
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
IconButton(
|
||||||
|
onClick = { viewModel.requestDeleteVault(item) },
|
||||||
|
enabled = !uiState.isBusy,
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
Icons.Filled.Delete,
|
||||||
|
contentDescription = stringResource(R.string.remote_vault_delete_cd),
|
||||||
|
tint = MaterialTheme.colorScheme.error,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uiState.isBusy) {
|
||||||
|
CircularProgressIndicator(
|
||||||
|
modifier = Modifier
|
||||||
|
.align(Alignment.Center)
|
||||||
|
.padding(24.dp),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uiState.addChoiceVisible) {
|
||||||
|
Dialog(
|
||||||
|
onDismissRequest = { if (!uiState.isBusy) viewModel.setAddChoiceVisible(false) },
|
||||||
|
) {
|
||||||
|
Surface(
|
||||||
|
shape = RoundedCornerShape(28.dp),
|
||||||
|
color = MaterialTheme.colorScheme.surfaceContainerHigh,
|
||||||
|
tonalElevation = 3.dp,
|
||||||
|
) {
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.padding(24.dp)
|
||||||
|
.fillMaxWidth(),
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = stringResource(R.string.remote_vaults_add_title),
|
||||||
|
style = MaterialTheme.typography.headlineSmall,
|
||||||
|
color = MaterialTheme.colorScheme.onSurface,
|
||||||
|
)
|
||||||
|
Spacer(modifier = Modifier.height(20.dp))
|
||||||
|
FilledTonalButton(
|
||||||
|
onClick = {
|
||||||
|
viewModel.setAddChoiceVisible(false)
|
||||||
|
viewModel.yandexSignIn.launch { outcome ->
|
||||||
|
when (outcome) {
|
||||||
|
is RemoteYandexAuthResult.Success ->
|
||||||
|
viewModel.onYandexAuthSuccess(outcome.accessToken)
|
||||||
|
is RemoteYandexAuthResult.Failure ->
|
||||||
|
Toast.makeText(context, outcome.message, Toast.LENGTH_LONG)
|
||||||
|
.show()
|
||||||
|
RemoteYandexAuthResult.Cancelled -> { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
enabled = !uiState.isBusy,
|
||||||
|
) {
|
||||||
|
Text(stringResource(R.string.remote_vaults_provider_yandex))
|
||||||
|
}
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
Row(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
horizontalArrangement = Arrangement.End,
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = stringResource(R.string.remote_vaults_add_cancel),
|
||||||
|
style = MaterialTheme.typography.labelLarge,
|
||||||
|
color = MaterialTheme.colorScheme.primary,
|
||||||
|
modifier = Modifier
|
||||||
|
.clickable(
|
||||||
|
enabled = !uiState.isBusy,
|
||||||
|
interactionSource = remember { MutableInteractionSource() },
|
||||||
|
indication = null,
|
||||||
|
) { viewModel.setAddChoiceVisible(false) }
|
||||||
|
.padding(vertical = 8.dp, horizontal = 4.dp),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uiState.vaultPendingDelete?.let { pending ->
|
||||||
|
AlertDialog(
|
||||||
|
onDismissRequest = { if (!uiState.isBusy) viewModel.dismissDeleteVault() },
|
||||||
|
title = {
|
||||||
|
Text(stringResource(R.string.remote_vault_remove_title))
|
||||||
|
},
|
||||||
|
text = {
|
||||||
|
Text(stringResource(R.string.remote_vault_remove_message, pending.label))
|
||||||
|
},
|
||||||
|
confirmButton = {
|
||||||
|
TextButton(
|
||||||
|
onClick = { viewModel.confirmDeleteVault() },
|
||||||
|
enabled = !uiState.isBusy,
|
||||||
|
) {
|
||||||
|
Text(stringResource(R.string.remove))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
dismissButton = {
|
||||||
|
TextButton(
|
||||||
|
onClick = { viewModel.dismissDeleteVault() },
|
||||||
|
enabled = !uiState.isBusy,
|
||||||
|
) {
|
||||||
|
Text(stringResource(R.string.remote_vaults_add_cancel))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,18 @@
|
|||||||
package com.github.nullptroma.wallenc.presentation.screens.main.screens.remotes
|
package com.github.nullptroma.wallenc.presentation.screens.main.screens.remotes
|
||||||
|
|
||||||
class RemoteVaultsScreenState
|
import com.github.nullptroma.wallenc.domain.enums.VaultType
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
data class RemoteVaultListItem(
|
||||||
|
val uuid: UUID,
|
||||||
|
val type: VaultType,
|
||||||
|
val label: String,
|
||||||
|
)
|
||||||
|
|
||||||
|
data class RemoteVaultsScreenState(
|
||||||
|
val vaults: List<RemoteVaultListItem> = emptyList(),
|
||||||
|
val isBusy: Boolean = false,
|
||||||
|
val addChoiceVisible: Boolean = false,
|
||||||
|
/** Карточка, для которой показан диалог удаления */
|
||||||
|
val vaultPendingDelete: RemoteVaultListItem? = null,
|
||||||
|
)
|
||||||
@@ -1,9 +1,84 @@
|
|||||||
package com.github.nullptroma.wallenc.presentation.screens.main.screens.remotes
|
package com.github.nullptroma.wallenc.presentation.screens.main.screens.remotes
|
||||||
|
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import com.github.nullptroma.wallenc.domain.auth.RemoteYandexSignInLauncher
|
||||||
|
import com.github.nullptroma.wallenc.domain.interfaces.IYandexVault
|
||||||
|
import com.github.nullptroma.wallenc.domain.interfaces.IVaultsManager
|
||||||
import com.github.nullptroma.wallenc.presentation.ViewModelBase
|
import com.github.nullptroma.wallenc.presentation.ViewModelBase
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
|
import kotlinx.coroutines.flow.combine
|
||||||
|
import kotlinx.coroutines.flow.stateIn
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class RemoteVaultsViewModel @Inject constructor() :
|
class RemoteVaultsViewModel @Inject constructor(
|
||||||
ViewModelBase<RemoteVaultsScreenState>(RemoteVaultsScreenState())
|
private val vaultsManager: IVaultsManager,
|
||||||
|
val yandexSignIn: RemoteYandexSignInLauncher,
|
||||||
|
) : ViewModelBase<RemoteVaultsScreenState>(RemoteVaultsScreenState()) {
|
||||||
|
|
||||||
|
val uiState = combine(
|
||||||
|
vaultsManager.remoteVaults,
|
||||||
|
state,
|
||||||
|
) { remotes, base ->
|
||||||
|
base.copy(
|
||||||
|
vaults = remotes.map { v ->
|
||||||
|
val label = when (v) {
|
||||||
|
is IYandexVault -> v.accountEmail
|
||||||
|
else -> v.uuid.toString()
|
||||||
|
}
|
||||||
|
RemoteVaultListItem(
|
||||||
|
uuid = v.uuid,
|
||||||
|
type = v.type,
|
||||||
|
label = label,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}.stateIn(
|
||||||
|
viewModelScope,
|
||||||
|
SharingStarted.Eagerly,
|
||||||
|
RemoteVaultsScreenState(),
|
||||||
|
)
|
||||||
|
|
||||||
|
fun setAddChoiceVisible(visible: Boolean) {
|
||||||
|
updateState(state.value.copy(addChoiceVisible = visible))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setBusy(busy: Boolean) {
|
||||||
|
updateState(state.value.copy(isBusy = busy))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onYandexAuthSuccess(accessToken: String) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
setBusy(true)
|
||||||
|
try {
|
||||||
|
vaultsManager.addYandexVault(accessToken)
|
||||||
|
} finally {
|
||||||
|
setBusy(false)
|
||||||
|
setAddChoiceVisible(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun requestDeleteVault(item: RemoteVaultListItem) {
|
||||||
|
updateState(state.value.copy(vaultPendingDelete = item))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun dismissDeleteVault() {
|
||||||
|
updateState(state.value.copy(vaultPendingDelete = null))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun confirmDeleteVault() {
|
||||||
|
val pending = state.value.vaultPendingDelete ?: return
|
||||||
|
viewModelScope.launch {
|
||||||
|
setBusy(true)
|
||||||
|
try {
|
||||||
|
vaultsManager.removeRemoteVault(pending.uuid)
|
||||||
|
} finally {
|
||||||
|
setBusy(false)
|
||||||
|
dismissDeleteVault()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -34,4 +34,15 @@
|
|||||||
<string name="task_state_cancelled">Cancelled</string>
|
<string name="task_state_cancelled">Cancelled</string>
|
||||||
<string name="task_state_failed">Failed: %1$s</string>
|
<string name="task_state_failed">Failed: %1$s</string>
|
||||||
|
|
||||||
|
<string name="remote_vaults_add_cd">Add remote vault</string>
|
||||||
|
<string name="remote_vaults_empty_hint">No remote vaults yet. Tap + to add Yandex.</string>
|
||||||
|
<string name="remote_vaults_add_title">Add vault</string>
|
||||||
|
<string name="remote_vaults_add_pick_provider">Choose provider:</string>
|
||||||
|
<string name="remote_vaults_provider_yandex">Yandex</string>
|
||||||
|
<string name="remote_vaults_add_cancel">Cancel</string>
|
||||||
|
<string name="remote_vault_type_yandex">Yandex</string>
|
||||||
|
<string name="remote_vault_delete_cd">Remove remote vault</string>
|
||||||
|
<string name="remote_vault_remove_title">Remove remote vault?</string>
|
||||||
|
<string name="remote_vault_remove_message">Remove \"%1$s\" from this device? The account data on the server is not deleted.</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
Reference in New Issue
Block a user