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

View File

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