Removing the dependency on an application class from AccountViewModel

This commit is contained in:
Vitor Pamplona
2025-09-08 15:50:54 -04:00
parent e0764da095
commit ed1ea4d289
6 changed files with 40 additions and 24 deletions
@@ -62,7 +62,7 @@ fun VideoViewInner(
callbackUri = nostrUriCallback, callbackUri = nostrUriCallback,
mimeType = mimeType, mimeType = mimeType,
aspectRatio = aspectRatio, aspectRatio = aspectRatio,
proxyPort = accountViewModel.proxyPortFor(videoUri), proxyPort = accountViewModel.proxyPortForVideo(videoUri),
keepPlaying = true, keepPlaying = true,
waveformData = waveform, waveformData = waveform,
) { mediaItem -> ) { mediaItem ->
@@ -26,7 +26,11 @@ import com.vitorpamplona.amethyst.service.relayClient.KeyDataSourceSubscription
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
@Composable @Composable
fun AccountFilterAssemblerSubscription(accountViewModel: AccountViewModel) = AccountFilterAssemblerSubscription(accountViewModel, accountViewModel.dataSources().account) fun AccountFilterAssemblerSubscription(accountViewModel: AccountViewModel) =
AccountFilterAssemblerSubscription(
accountViewModel,
accountViewModel.dataSources().account,
)
@Composable @Composable
fun AccountFilterAssemblerSubscription( fun AccountFilterAssemblerSubscription(
@@ -121,7 +121,7 @@ fun VoiceHeader(
callbackUri = callbackUri, callbackUri = callbackUri,
mimeType = null, mimeType = null,
aspectRatio = null, aspectRatio = null,
proxyPort = accountViewModel.proxyPortFor(media), proxyPort = accountViewModel.proxyPortForVideo(media),
keepPlaying = false, keepPlaying = false,
waveformData = waveform, waveformData = waveform,
) { mediaItem -> ) { mediaItem ->
@@ -38,7 +38,6 @@ import coil3.asDrawable
import coil3.imageLoader import coil3.imageLoader
import coil3.request.ImageRequest import coil3.request.ImageRequest
import com.vitorpamplona.amethyst.AccountInfo import com.vitorpamplona.amethyst.AccountInfo
import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.commons.compose.GenericBaseCache 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.checkNotInMainThread
import com.vitorpamplona.amethyst.service.lnurl.LightningAddressResolver import com.vitorpamplona.amethyst.service.lnurl.LightningAddressResolver
import com.vitorpamplona.amethyst.service.location.LocationState 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.relayClient.reqCommand.nwc.NWCPaymentFilterAssembler
import com.vitorpamplona.amethyst.service.uploads.CompressorQuality import com.vitorpamplona.amethyst.service.uploads.CompressorQuality
import com.vitorpamplona.amethyst.service.uploads.UploadOrchestrator import com.vitorpamplona.amethyst.service.uploads.UploadOrchestrator
@@ -164,13 +166,13 @@ import java.util.Locale
class AccountViewModel( class AccountViewModel(
val account: Account, val account: Account,
val settings: SharedSettingsState, val settings: SharedSettingsState,
val app: Amethyst, val dataSources: RelaySubscriptionsCoordinator,
val okHttpClient: IHttpClientManager,
) : ViewModel(), ) : ViewModel(),
Dao { Dao {
var firstRoute: Route? = null var firstRoute: Route? = null
val toastManager = ToastManager() val toastManager = ToastManager()
val feedStates = AccountFeedContentStates(this) val feedStates = AccountFeedContentStates(this)
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
@@ -905,7 +907,7 @@ class AccountViewModel(
.verifyNip05( .verifyNip05(
nip05, nip05,
okHttpClient = { okHttpClient = {
app.okHttpClients.getHttpClient(account.privacyState.shouldUseTorForNIP05(it)) okHttpClient.getHttpClient(account.privacyState.shouldUseTorForNIP05(it))
}, },
onSuccess = { onSuccess = {
// Marks user as verified // Marks user as verified
@@ -1120,10 +1122,17 @@ class AccountViewModel(
class Factory( class Factory(
val account: Account, val account: Account,
val settings: SharedSettingsState, val settings: SharedSettingsState,
val app: Amethyst, val dataSources: RelaySubscriptionsCoordinator,
val okHttpClient: IHttpClientManager,
) : ViewModelProvider.Factory { ) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T = AccountViewModel(account, settings, app) as T override fun <T : ViewModel> create(modelClass: Class<T>): T =
AccountViewModel(
account,
settings,
dataSources,
okHttpClient,
) as T
} }
init { 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) suspend fun createTempDraftNote(noteEvent: DraftWrapEvent): Note? = draftNoteCache.update(noteEvent)
@@ -1697,7 +1706,8 @@ fun mockAccountViewModel(): AccountViewModel {
return AccountViewModel( return AccountViewModel(
account = account, account = account,
settings = sharedPreferencesViewModel.sharedPrefs, settings = sharedPreferencesViewModel.sharedPrefs,
app = Amethyst(), okHttpClient = EmptyHttpClientManager,
dataSources = RelaySubscriptionsCoordinator(LocalCache, client, scope),
).also { ).also {
mockedCache = it mockedCache = it
} }
@@ -1737,7 +1747,8 @@ fun mockVitorAccountViewModel(): AccountViewModel {
return AccountViewModel( return AccountViewModel(
account = account, account = account,
settings = sharedPreferencesViewModel.sharedPrefs, settings = sharedPreferencesViewModel.sharedPrefs,
app = Amethyst(), okHttpClient = EmptyHttpClientManager,
dataSources = RelaySubscriptionsCoordinator(LocalCache, client, scope),
).also { ).also {
vitorCache = it vitorCache = it
} }
@@ -71,7 +71,8 @@ fun LoggedInPage(
AccountViewModel.Factory( AccountViewModel.Factory(
account = account, account = account,
settings = sharedPreferencesViewModel.sharedPrefs, settings = sharedPreferencesViewModel.sharedPrefs,
app = Amethyst.instance, dataSources = Amethyst.instance.sources,
okHttpClient = Amethyst.instance.okHttpClients,
), ),
) )
@@ -157,7 +158,7 @@ fun NotificationRegistration(accountViewModel: AccountViewModel) {
scope.launch { scope.launch {
PushNotificationUtils.checkAndInit( PushNotificationUtils.checkAndInit(
LocalPreferences.allSavedAccounts(), LocalPreferences.allSavedAccounts(),
accountViewModel::okHttpClientForTrustedRelays, accountViewModel::okHttpClientForPushRegistration,
) )
} }
@@ -172,7 +173,7 @@ fun NotificationRegistration(accountViewModel: AccountViewModel) {
scope.launch { scope.launch {
PushNotificationUtils.checkAndInit( PushNotificationUtils.checkAndInit(
LocalPreferences.allSavedAccounts(), LocalPreferences.allSavedAccounts(),
accountViewModel::okHttpClientForTrustedRelays, accountViewModel::okHttpClientForPushRegistration,
) )
} }
@@ -297,7 +297,7 @@ fun UrlVideoView(
callbackUri = content.uri, callbackUri = content.uri,
mimeType = content.mimeType, mimeType = content.mimeType,
aspectRatio = ratio, aspectRatio = ratio,
proxyPort = accountViewModel.proxyPortFor(content.url), proxyPort = accountViewModel.proxyPortForVideo(content.url),
) { mediaItem -> ) { mediaItem ->
GetVideoController( GetVideoController(
mediaItem = mediaItem, mediaItem = mediaItem,