Direct subscribe to sub.club

This commit is contained in:
Thomas Ricouard 2024-08-21 18:22:38 +02:00
parent f80fca91e2
commit e0eed97bcf
2 changed files with 34 additions and 6 deletions

View file

@ -4,21 +4,25 @@ import Env
import Models
import NukeUI
import SwiftUI
import AppAccount
@MainActor
struct AccountDetailHeaderView: View {
enum Constants {
static let headerHeight: CGFloat = 200
}
@Environment(\.openWindow) private var openWindow
@Environment(Theme.self) private var theme
@Environment(QuickLook.self) private var quickLook
@Environment(RouterPath.self) private var routerPath
@Environment(CurrentAccount.self) private var currentAccount
@Environment(StreamWatcher.self) private var watcher
@Environment(AppAccountsManager.self) private var appAccount
@Environment(\.redactionReasons) private var reasons
@Environment(\.isSupporter) private var isSupporter: Bool
@Environment(\.openURL) private var openURL
@Environment(\.colorScheme) private var colorScheme
var viewModel: AccountDetailViewModel
let account: Account
@ -241,7 +245,7 @@ struct AccountDetailHeaderView: View {
movedToView
joinedAtView
if viewModel.account?.isPremiumAccount == true && viewModel.relationship?.following == false || viewModel.account?.isLinkedToPremiumAccount == true && viewModel.premiumRelationship?.following == false {
tipView
subscribeButton
}
}
.accessibilityElement(children: .contain)
@ -342,18 +346,31 @@ struct AccountDetailHeaderView: View {
}
@ViewBuilder
private var tipView: some View {
private var subscribeButton: some View {
Button {
isTipSheetPresented = true
shouldListenToPremiumTimer = true
if let subscription = viewModel.subClubUser?.subscription,
let accountName = appAccount.currentAccount.accountName,
let premiumUsername = account.premiumUsername,
let url = URL(string: "https://\(AppInfo.premiumInstance)/@\(premiumUsername)/subscribe?callback=icecubesapp://subclub&id=@\(accountName)&amount=\(subscription.unitAmount)&currency=\(subscription.currency)&theme=\(colorScheme)") {
openURL(url)
} else {
isTipSheetPresented = true
shouldListenToPremiumTimer = true
}
Task {
if viewModel.account?.isLinkedToPremiumAccount == true {
try? await viewModel.followPremiumAccount()
}
try? await viewModel.followButtonViewModel?.follow()
}
} label: {
Text("$ Subscribe")
if let subscription = viewModel.subClubUser?.subscription {
Text("Subscribe \(subscription.formattedAmount) / month")
} else {
Text("$ Subscribe")
}
}
.buttonStyle(.bordered)
.padding(.top, 8)

View file

@ -79,8 +79,11 @@ import SwiftUI
var fields: [Account.Field] = []
var familiarFollowers: [Account] = []
// Sub.club stuff
var premiumAccount: Account?
var premiumRelationship: Relationship?
var subClubUser: SubClubUser?
private let subClubClient = SubClubClient()
var selectedTab = Tab.statuses {
didSet {
@ -333,9 +336,12 @@ extension AccountDetailViewModel {
forceVersion: .v2)
if let premiumAccount = results?.accounts.first {
self.premiumAccount = premiumAccount
await fetchSubClubAccount(premiumUsername: premiumAccount.username)
let relationships: [Relationship] = try await client.get(endpoint: Accounts.relationships(ids: [premiumAccount.id]))
self.premiumRelationship = relationships.first
}
} else if fromAccount.isPremiumAccount {
await fetchSubClubAccount(premiumUsername: fromAccount.username)
}
}
@ -346,4 +352,9 @@ extension AccountDetailViewModel {
reblogs: true))
}
}
private func fetchSubClubAccount(premiumUsername: String) async {
let user = await subClubClient.getUser(username: premiumUsername)
subClubUser = user
}
}