From ed1ea4d289bf345213ee21a3803bfc233d518604 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 8 Sep 2025 15:50:54 -0400 Subject: [PATCH] Removing the dependency on an application class from AccountViewModel --- .../playback/composable/VideoViewInner.kt | 2 +- .../AccountFilterAssemblerSubscription.kt | 6 ++- .../amethyst/ui/note/types/VoiceTrack.kt | 2 +- .../ui/screen/loggedIn/AccountViewModel.kt | 45 ++++++++++++------- .../ui/screen/loggedIn/LoggedInPage.kt | 7 +-- .../loggedIn/profile/gallery/GalleryThumb.kt | 2 +- 6 files changed, 40 insertions(+), 24 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/composable/VideoViewInner.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/composable/VideoViewInner.kt index 0614fef0d..76c75b9d2 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/composable/VideoViewInner.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/composable/VideoViewInner.kt @@ -62,7 +62,7 @@ fun VideoViewInner( callbackUri = nostrUriCallback, mimeType = mimeType, aspectRatio = aspectRatio, - proxyPort = accountViewModel.proxyPortFor(videoUri), + proxyPort = accountViewModel.proxyPortForVideo(videoUri), keepPlaying = true, waveformData = waveform, ) { mediaItem -> diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/account/AccountFilterAssemblerSubscription.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/account/AccountFilterAssemblerSubscription.kt index cae7379f6..2745f17e7 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/account/AccountFilterAssemblerSubscription.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/account/AccountFilterAssemblerSubscription.kt @@ -26,7 +26,11 @@ import com.vitorpamplona.amethyst.service.relayClient.KeyDataSourceSubscription import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel @Composable -fun AccountFilterAssemblerSubscription(accountViewModel: AccountViewModel) = AccountFilterAssemblerSubscription(accountViewModel, accountViewModel.dataSources().account) +fun AccountFilterAssemblerSubscription(accountViewModel: AccountViewModel) = + AccountFilterAssemblerSubscription( + accountViewModel, + accountViewModel.dataSources().account, + ) @Composable fun AccountFilterAssemblerSubscription( diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/VoiceTrack.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/VoiceTrack.kt index 9588f4a3e..a63c56721 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/VoiceTrack.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/VoiceTrack.kt @@ -121,7 +121,7 @@ fun VoiceHeader( callbackUri = callbackUri, mimeType = null, aspectRatio = null, - proxyPort = accountViewModel.proxyPortFor(media), + proxyPort = accountViewModel.proxyPortForVideo(media), keepPlaying = false, waveformData = waveform, ) { mediaItem -> diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index 3905df8fb..04d4f5d51 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -38,7 +38,6 @@ import coil3.asDrawable import coil3.imageLoader import coil3.request.ImageRequest import com.vitorpamplona.amethyst.AccountInfo -import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.compose.GenericBaseCache @@ -67,6 +66,9 @@ import com.vitorpamplona.amethyst.service.cashu.melt.MeltProcessor import com.vitorpamplona.amethyst.service.checkNotInMainThread import com.vitorpamplona.amethyst.service.lnurl.LightningAddressResolver import com.vitorpamplona.amethyst.service.location.LocationState +import com.vitorpamplona.amethyst.service.okhttp.EmptyHttpClientManager +import com.vitorpamplona.amethyst.service.okhttp.IHttpClientManager +import com.vitorpamplona.amethyst.service.relayClient.reqCommand.RelaySubscriptionsCoordinator import com.vitorpamplona.amethyst.service.relayClient.reqCommand.nwc.NWCPaymentFilterAssembler import com.vitorpamplona.amethyst.service.uploads.CompressorQuality import com.vitorpamplona.amethyst.service.uploads.UploadOrchestrator @@ -164,13 +166,13 @@ import java.util.Locale class AccountViewModel( val account: Account, val settings: SharedSettingsState, - val app: Amethyst, + val dataSources: RelaySubscriptionsCoordinator, + val okHttpClient: IHttpClientManager, ) : ViewModel(), Dao { var firstRoute: Route? = null val toastManager = ToastManager() - val feedStates = AccountFeedContentStates(this) @OptIn(ExperimentalCoroutinesApi::class) @@ -905,7 +907,7 @@ class AccountViewModel( .verifyNip05( nip05, okHttpClient = { - app.okHttpClients.getHttpClient(account.privacyState.shouldUseTorForNIP05(it)) + okHttpClient.getHttpClient(account.privacyState.shouldUseTorForNIP05(it)) }, onSuccess = { // Marks user as verified @@ -1120,10 +1122,17 @@ class AccountViewModel( class Factory( val account: Account, val settings: SharedSettingsState, - val app: Amethyst, + val dataSources: RelaySubscriptionsCoordinator, + val okHttpClient: IHttpClientManager, ) : ViewModelProvider.Factory { @Suppress("UNCHECKED_CAST") - override fun create(modelClass: Class): T = AccountViewModel(account, settings, app) as T + override fun create(modelClass: Class): T = + AccountViewModel( + account, + settings, + dataSources, + okHttpClient, + ) as T } init { @@ -1395,23 +1404,23 @@ class AccountViewModel( } } - fun proxyPortFor(url: String): Int? = app.okHttpClients.getCurrentProxyPort(account.privacyState.shouldUseTorForVideoDownload(url)) + fun proxyPortForVideo(url: String): Int? = okHttpClient.getCurrentProxyPort(account.privacyState.shouldUseTorForVideoDownload(url)) - fun okHttpClientForNip96(url: String): OkHttpClient = app.okHttpClients.getHttpClient(account.privacyState.shouldUseTorForUploads(url)) + fun okHttpClientForNip96(url: String): OkHttpClient = okHttpClient.getHttpClient(account.privacyState.shouldUseTorForUploads(url)) - fun okHttpClientForImage(url: String): OkHttpClient = app.okHttpClients.getHttpClient(account.privacyState.shouldUseTorForImageDownload(url)) + fun okHttpClientForImage(url: String): OkHttpClient = okHttpClient.getHttpClient(account.privacyState.shouldUseTorForImageDownload(url)) - fun okHttpClientForVideo(url: String): OkHttpClient = app.okHttpClients.getHttpClient(account.privacyState.shouldUseTorForVideoDownload(url)) + fun okHttpClientForVideo(url: String): OkHttpClient = okHttpClient.getHttpClient(account.privacyState.shouldUseTorForVideoDownload(url)) - fun okHttpClientForMoney(url: String): OkHttpClient = app.okHttpClients.getHttpClient(account.privacyState.shouldUseTorForMoneyOperations(url)) + fun okHttpClientForMoney(url: String): OkHttpClient = okHttpClient.getHttpClient(account.privacyState.shouldUseTorForMoneyOperations(url)) - fun okHttpClientForPreview(url: String): OkHttpClient = app.okHttpClients.getHttpClient(account.privacyState.shouldUseTorForPreviewUrl(url)) + fun okHttpClientForPreview(url: String): OkHttpClient = okHttpClient.getHttpClient(account.privacyState.shouldUseTorForPreviewUrl(url)) - fun okHttpClientForClean(url: NormalizedRelayUrl): OkHttpClient = app.okHttpClients.getHttpClient(account.torRelayState.shouldUseTorForClean(url)) + fun okHttpClientForClean(url: NormalizedRelayUrl): OkHttpClient = okHttpClient.getHttpClient(account.torRelayState.shouldUseTorForClean(url)) - fun okHttpClientForTrustedRelays(url: String): OkHttpClient = app.okHttpClients.getHttpClient(account.privacyState.shouldUseTorForTrustedRelays()) + fun okHttpClientForPushRegistration(url: String): OkHttpClient = okHttpClient.getHttpClient(account.privacyState.shouldUseTorForTrustedRelays()) - fun dataSources() = app.sources + fun dataSources() = dataSources suspend fun createTempDraftNote(noteEvent: DraftWrapEvent): Note? = draftNoteCache.update(noteEvent) @@ -1697,7 +1706,8 @@ fun mockAccountViewModel(): AccountViewModel { return AccountViewModel( account = account, settings = sharedPreferencesViewModel.sharedPrefs, - app = Amethyst(), + okHttpClient = EmptyHttpClientManager, + dataSources = RelaySubscriptionsCoordinator(LocalCache, client, scope), ).also { mockedCache = it } @@ -1737,7 +1747,8 @@ fun mockVitorAccountViewModel(): AccountViewModel { return AccountViewModel( account = account, settings = sharedPreferencesViewModel.sharedPrefs, - app = Amethyst(), + okHttpClient = EmptyHttpClientManager, + dataSources = RelaySubscriptionsCoordinator(LocalCache, client, scope), ).also { vitorCache = it } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/LoggedInPage.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/LoggedInPage.kt index 3c50e37e6..299f4df11 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/LoggedInPage.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/LoggedInPage.kt @@ -71,7 +71,8 @@ fun LoggedInPage( AccountViewModel.Factory( account = account, settings = sharedPreferencesViewModel.sharedPrefs, - app = Amethyst.instance, + dataSources = Amethyst.instance.sources, + okHttpClient = Amethyst.instance.okHttpClients, ), ) @@ -157,7 +158,7 @@ fun NotificationRegistration(accountViewModel: AccountViewModel) { scope.launch { PushNotificationUtils.checkAndInit( LocalPreferences.allSavedAccounts(), - accountViewModel::okHttpClientForTrustedRelays, + accountViewModel::okHttpClientForPushRegistration, ) } @@ -172,7 +173,7 @@ fun NotificationRegistration(accountViewModel: AccountViewModel) { scope.launch { PushNotificationUtils.checkAndInit( LocalPreferences.allSavedAccounts(), - accountViewModel::okHttpClientForTrustedRelays, + accountViewModel::okHttpClientForPushRegistration, ) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/gallery/GalleryThumb.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/gallery/GalleryThumb.kt index 6c6898852..f2296805c 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/gallery/GalleryThumb.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/profile/gallery/GalleryThumb.kt @@ -297,7 +297,7 @@ fun UrlVideoView( callbackUri = content.uri, mimeType = content.mimeType, aspectRatio = ratio, - proxyPort = accountViewModel.proxyPortFor(content.url), + proxyPort = accountViewModel.proxyPortForVideo(content.url), ) { mediaItem -> GetVideoController( mediaItem = mediaItem,