refactor: split views from DamusPurpleView into separate files
This refactoring commit splits several view blocks from DamusPurpleView into separate files. - New view structs were defined within the DamusPurpleView namespace, to avoid polluting the global namespace - No logical changes were made. The functionality should have stayed equivalent - Changes were made conservatively, and as semantically as possible, to make the code easier to work with. Signed-off-by: Daniel D’Aquino <daniel@daquino.me> Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
committed by
William Casarin
parent
b3b6fdc29e
commit
2525799c8a
@@ -34,7 +34,7 @@ struct DamusPurpleVerifyNpubView: View {
|
||||
.background(Color.black)
|
||||
|
||||
VStack {
|
||||
DamusPurpleLogoView()
|
||||
DamusPurpleView.LogoView()
|
||||
|
||||
VStack(alignment: .center, spacing: 30) {
|
||||
|
||||
|
||||
@@ -12,23 +12,6 @@ fileprivate let damus_products = ["purpleyearly","purple"]
|
||||
|
||||
// MARK: - Helper structures
|
||||
|
||||
enum ProductState {
|
||||
case loading
|
||||
case loaded([Product])
|
||||
case failed
|
||||
|
||||
var products: [Product]? {
|
||||
switch self {
|
||||
case .loading:
|
||||
return nil
|
||||
case .loaded(let ps):
|
||||
return ps
|
||||
case .failed:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum AccountInfoState {
|
||||
case loading
|
||||
case loaded(account: DamusPurple.Account)
|
||||
@@ -45,11 +28,6 @@ enum DamusPurpleType: String {
|
||||
case monthly = "purple"
|
||||
}
|
||||
|
||||
struct PurchasedProduct {
|
||||
let tx: StoreKit.Transaction
|
||||
let product: Product
|
||||
}
|
||||
|
||||
// MARK: - Main view
|
||||
|
||||
struct DamusPurpleView: View {
|
||||
@@ -125,7 +103,7 @@ struct DamusPurpleView: View {
|
||||
|
||||
var MainContent: some View {
|
||||
VStack {
|
||||
DamusPurpleLogoView()
|
||||
DamusPurpleView.LogoView()
|
||||
|
||||
switch my_account_info_state {
|
||||
case .loading:
|
||||
@@ -148,74 +126,7 @@ struct DamusPurpleView: View {
|
||||
|
||||
var MarketingContent: some View {
|
||||
VStack {
|
||||
VStack(alignment: .leading, spacing: 30) {
|
||||
PurpleViewPrimitives.SubtitleView(text: NSLocalizedString("Help us stay independent in our mission for Freedom tech with our Purple subscription, and look cool doing it!", comment: "Damus purple subscription pitch"))
|
||||
.multilineTextAlignment(.center)
|
||||
|
||||
HStack(spacing: 20) {
|
||||
PurpleViewPrimitives.IconOnBoxView(name: "heart.fill")
|
||||
|
||||
VStack(alignment: .leading) {
|
||||
PurpleViewPrimitives.TitleView(text: NSLocalizedString("Help Build The Future", comment: "Title for funding future damus development"))
|
||||
|
||||
PurpleViewPrimitives.SubtitleView(text: NSLocalizedString("Support Damus development to help build the future of decentralized communication on the web.", comment: "Reason for supporting damus development"))
|
||||
}
|
||||
}
|
||||
|
||||
HStack(spacing: 20) {
|
||||
PurpleViewPrimitives.IconOnBoxView(name: "ai-3-stars.fill")
|
||||
|
||||
VStack(alignment: .leading) {
|
||||
PurpleViewPrimitives.TitleView(text: NSLocalizedString("Exclusive features", comment: "Features only available on subscription service"))
|
||||
.padding(.bottom, -3)
|
||||
|
||||
HStack(spacing: 3) {
|
||||
Image("calendar")
|
||||
.resizable()
|
||||
.frame(width: 15, height: 15)
|
||||
|
||||
Text(NSLocalizedString("Coming soon", comment: "Feature is still in development and will be available soon"))
|
||||
.font(.caption)
|
||||
.bold()
|
||||
}
|
||||
.foregroundColor(DamusColors.pink)
|
||||
.padding(.vertical, 3)
|
||||
.padding(.horizontal, 8)
|
||||
.background(DamusColors.lightBackgroundPink)
|
||||
.cornerRadius(30.0)
|
||||
|
||||
PurpleViewPrimitives.SubtitleView(text: NSLocalizedString("Be the first to access upcoming premium features: Automatic translations, longer note storage, and more", comment: "Description of new features to be expected"))
|
||||
.padding(.top, 3)
|
||||
}
|
||||
}
|
||||
|
||||
HStack(spacing: 20) {
|
||||
PurpleViewPrimitives.IconOnBoxView(name: "badge")
|
||||
|
||||
VStack(alignment: .leading) {
|
||||
PurpleViewPrimitives.TitleView(text: NSLocalizedString("Supporter Badge", comment: "Title for supporter badge"))
|
||||
|
||||
PurpleViewPrimitives.SubtitleView(text: NSLocalizedString("Get a special badge on your profile to show everyone your contribution to Freedom tech", comment: "Supporter badge description"))
|
||||
}
|
||||
}
|
||||
|
||||
HStack {
|
||||
Spacer()
|
||||
Link(
|
||||
damus_state.purple.enable_purple_iap_support ?
|
||||
NSLocalizedString("Learn more about the features", comment: "Label for a link to the Damus website, to allow the user to learn more about the features of Purple")
|
||||
:
|
||||
NSLocalizedString("Coming soon! Visit our website to learn more", comment: "Label announcing Purple, and inviting the user to learn more on the website"),
|
||||
destination: damus_state.purple.environment.damus_website_url()
|
||||
)
|
||||
.foregroundColor(DamusColors.pink)
|
||||
.padding()
|
||||
Spacer()
|
||||
}
|
||||
|
||||
}
|
||||
.padding([.trailing, .leading], 30)
|
||||
.padding(.bottom, 20)
|
||||
DamusPurpleView.MarketingContentView(purple: damus_state.purple)
|
||||
|
||||
VStack(alignment: .center) {
|
||||
ProductStateView
|
||||
@@ -227,103 +138,11 @@ struct DamusPurpleView: View {
|
||||
var ProductStateView: some View {
|
||||
Group {
|
||||
if damus_state.purple.enable_purple_iap_support {
|
||||
switch self.products {
|
||||
case .failed:
|
||||
PurpleViewPrimitives.ProductLoadErrorView()
|
||||
case .loaded(let products):
|
||||
if let purchased {
|
||||
PurchasedView(purchased)
|
||||
} else {
|
||||
ProductsView(products)
|
||||
}
|
||||
case .loading:
|
||||
ProgressView()
|
||||
.progressViewStyle(.circular)
|
||||
}
|
||||
DamusPurpleView.IAPProductStateView(products: products, purchased: purchased, subscribe: subscribe)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func PurchasedView(_ purchased: PurchasedProduct) -> some View {
|
||||
VStack(spacing: 10) {
|
||||
Text(NSLocalizedString("Purchased!", comment: "User purchased a subscription"))
|
||||
.font(.title2)
|
||||
.foregroundColor(.white)
|
||||
price_description(product: purchased.product)
|
||||
.foregroundColor(.white)
|
||||
.opacity(0.65)
|
||||
.frame(width: 200)
|
||||
Text(NSLocalizedString("Purchased on", comment: "Indicating when the user purchased the subscription"))
|
||||
.font(.title2)
|
||||
.foregroundColor(.white)
|
||||
Text(format_date(date: purchased.tx.purchaseDate))
|
||||
.foregroundColor(.white)
|
||||
.opacity(0.65)
|
||||
if let expiry = purchased.tx.expirationDate {
|
||||
Text(NSLocalizedString("Renews on", comment: "Indicating when the subscription will renew"))
|
||||
.font(.title2)
|
||||
.foregroundColor(.white)
|
||||
Text(format_date(date: expiry))
|
||||
.foregroundColor(.white)
|
||||
.opacity(0.65)
|
||||
}
|
||||
Button(action: {
|
||||
show_manage_subscriptions = true
|
||||
}, label: {
|
||||
Text(NSLocalizedString("Manage", comment: "Manage the damus subscription"))
|
||||
})
|
||||
.buttonStyle(GradientButtonStyle())
|
||||
}
|
||||
}
|
||||
|
||||
func ProductsView(_ products: [Product]) -> some View {
|
||||
VStack(spacing: 10) {
|
||||
Text(NSLocalizedString("Save 20% off on an annual subscription", comment: "Savings for purchasing an annual subscription"))
|
||||
.font(.callout.bold())
|
||||
.foregroundColor(.white)
|
||||
ForEach(products) { product in
|
||||
Button(action: {
|
||||
Task { @MainActor in
|
||||
do {
|
||||
try await subscribe(product)
|
||||
} catch {
|
||||
print(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
}, label: {
|
||||
price_description(product: product)
|
||||
})
|
||||
.buttonStyle(GradientButtonStyle())
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 20)
|
||||
}
|
||||
|
||||
func price_description(product: Product) -> some View {
|
||||
if product.id == "purpleyearly" {
|
||||
return (
|
||||
AnyView(
|
||||
HStack(spacing: 10) {
|
||||
Text(NSLocalizedString("Annually", comment: "Annual renewal of purple subscription"))
|
||||
Spacer()
|
||||
Text(verbatim: non_discounted_price(product)).strikethrough().foregroundColor(DamusColors.white.opacity(0.5))
|
||||
Text(verbatim: product.displayPrice).fontWeight(.bold)
|
||||
}
|
||||
)
|
||||
)
|
||||
} else {
|
||||
return (
|
||||
AnyView(
|
||||
HStack(spacing: 10) {
|
||||
Text(NSLocalizedString("Monthly", comment: "Monthly renewal of purple subscription"))
|
||||
Spacer()
|
||||
Text(verbatim: product.displayPrice).fontWeight(.bold)
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - State management
|
||||
|
||||
func load_account() async {
|
||||
@@ -408,42 +227,6 @@ struct DamusPurpleView: View {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - More helper views
|
||||
|
||||
struct DamusPurpleLogoView: View {
|
||||
var body: some View {
|
||||
HStack(spacing: 20) {
|
||||
Image("damus-dark-logo")
|
||||
.resizable()
|
||||
.frame(width: 60, height: 60)
|
||||
.clipShape(RoundedRectangle(cornerRadius: 15.0))
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: 15)
|
||||
.stroke(LinearGradient(
|
||||
colors: [DamusColors.lighterPink.opacity(0.8), .white.opacity(0), DamusColors.deepPurple.opacity(0.6)],
|
||||
startPoint: .topLeading,
|
||||
endPoint: .bottomTrailing), lineWidth: 1)
|
||||
)
|
||||
.shadow(radius: 5)
|
||||
|
||||
VStack(alignment: .leading) {
|
||||
Text(NSLocalizedString("Purple", comment: "Subscription service name"))
|
||||
.font(.system(size: 60.0).weight(.bold))
|
||||
.foregroundStyle(
|
||||
LinearGradient(
|
||||
colors: [DamusColors.lighterPink, DamusColors.deepPurple],
|
||||
startPoint: .bottomLeading,
|
||||
endPoint: .topTrailing
|
||||
)
|
||||
)
|
||||
.foregroundColor(.white)
|
||||
.tracking(-2)
|
||||
}
|
||||
}
|
||||
.padding(.bottom, 30)
|
||||
}
|
||||
}
|
||||
|
||||
struct DamusPurpleView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
/*
|
||||
|
||||
139
damus/Views/Purple/Detail/IAPProductStateView.swift
Normal file
139
damus/Views/Purple/Detail/IAPProductStateView.swift
Normal file
@@ -0,0 +1,139 @@
|
||||
//
|
||||
// PurchasedProductView.swift
|
||||
// damus
|
||||
//
|
||||
// Created by Daniel D’Aquino on 2024-02-09.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import StoreKit
|
||||
|
||||
// MARK: - IAPProductStateView
|
||||
|
||||
extension DamusPurpleView {
|
||||
struct IAPProductStateView: View {
|
||||
let products: ProductState
|
||||
let purchased: PurchasedProduct?
|
||||
let subscribe: (Product) async throws -> Void
|
||||
|
||||
var body: some View {
|
||||
switch self.products {
|
||||
case .failed:
|
||||
PurpleViewPrimitives.ProductLoadErrorView()
|
||||
case .loaded(let products):
|
||||
if let purchased {
|
||||
PurchasedView(purchased)
|
||||
} else {
|
||||
ProductsView(products)
|
||||
}
|
||||
case .loading:
|
||||
ProgressView()
|
||||
.progressViewStyle(.circular)
|
||||
}
|
||||
}
|
||||
|
||||
func PurchasedView(_ purchased: PurchasedProduct) -> some View {
|
||||
VStack(spacing: 10) {
|
||||
Text(NSLocalizedString("Purchased!", comment: "User purchased a subscription"))
|
||||
.font(.title2)
|
||||
.foregroundColor(.white)
|
||||
price_description(product: purchased.product)
|
||||
.foregroundColor(.white)
|
||||
.opacity(0.65)
|
||||
.frame(width: 200)
|
||||
Text(NSLocalizedString("Purchased on", comment: "Indicating when the user purchased the subscription"))
|
||||
.font(.title2)
|
||||
.foregroundColor(.white)
|
||||
Text(format_date(date: purchased.tx.purchaseDate))
|
||||
.foregroundColor(.white)
|
||||
.opacity(0.65)
|
||||
if let expiry = purchased.tx.expirationDate {
|
||||
Text(NSLocalizedString("Renews on", comment: "Indicating when the subscription will renew"))
|
||||
.font(.title2)
|
||||
.foregroundColor(.white)
|
||||
Text(format_date(date: expiry))
|
||||
.foregroundColor(.white)
|
||||
.opacity(0.65)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ProductsView(_ products: [Product]) -> some View {
|
||||
VStack(spacing: 10) {
|
||||
Text(NSLocalizedString("Save 20% off on an annual subscription", comment: "Savings for purchasing an annual subscription"))
|
||||
.font(.callout.bold())
|
||||
.foregroundColor(.white)
|
||||
ForEach(products) { product in
|
||||
Button(action: {
|
||||
Task { @MainActor in
|
||||
do {
|
||||
try await subscribe(product)
|
||||
} catch {
|
||||
print(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
}, label: {
|
||||
price_description(product: product)
|
||||
})
|
||||
.buttonStyle(GradientButtonStyle())
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 20)
|
||||
}
|
||||
|
||||
func price_description(product: Product) -> some View {
|
||||
if product.id == "purpleyearly" {
|
||||
return (
|
||||
AnyView(
|
||||
HStack(spacing: 10) {
|
||||
Text(NSLocalizedString("Annually", comment: "Annual renewal of purple subscription"))
|
||||
Spacer()
|
||||
Text(verbatim: non_discounted_price(product)).strikethrough().foregroundColor(DamusColors.white.opacity(0.5))
|
||||
Text(verbatim: product.displayPrice).fontWeight(.bold)
|
||||
}
|
||||
)
|
||||
)
|
||||
} else {
|
||||
return (
|
||||
AnyView(
|
||||
HStack(spacing: 10) {
|
||||
Text(NSLocalizedString("Monthly", comment: "Monthly renewal of purple subscription"))
|
||||
Spacer()
|
||||
Text(verbatim: product.displayPrice).fontWeight(.bold)
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helper structures
|
||||
|
||||
extension DamusPurpleView {
|
||||
enum ProductState {
|
||||
case loading
|
||||
case loaded([Product])
|
||||
case failed
|
||||
|
||||
var products: [Product]? {
|
||||
switch self {
|
||||
case .loading:
|
||||
return nil
|
||||
case .loaded(let ps):
|
||||
return ps
|
||||
case .failed:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct PurchasedProduct {
|
||||
let tx: StoreKit.Transaction
|
||||
let product: Product
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
DamusPurpleView.IAPProductStateView(products: .loaded([]), purchased: nil, subscribe: {_ in })
|
||||
}
|
||||
50
damus/Views/Purple/Detail/LogoView.swift
Normal file
50
damus/Views/Purple/Detail/LogoView.swift
Normal file
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// LogoView.swift
|
||||
// damus
|
||||
//
|
||||
// Created by Daniel D’Aquino on 2024-02-09.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
// MARK: - More helper views
|
||||
|
||||
extension DamusPurpleView {
|
||||
struct LogoView: View {
|
||||
var body: some View {
|
||||
HStack(spacing: 20) {
|
||||
Image("damus-dark-logo")
|
||||
.resizable()
|
||||
.frame(width: 60, height: 60)
|
||||
.clipShape(RoundedRectangle(cornerRadius: 15.0))
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: 15)
|
||||
.stroke(LinearGradient(
|
||||
colors: [DamusColors.lighterPink.opacity(0.8), .white.opacity(0), DamusColors.deepPurple.opacity(0.6)],
|
||||
startPoint: .topLeading,
|
||||
endPoint: .bottomTrailing), lineWidth: 1)
|
||||
)
|
||||
.shadow(radius: 5)
|
||||
|
||||
VStack(alignment: .leading) {
|
||||
Text(NSLocalizedString("Purple", comment: "Subscription service name"))
|
||||
.font(.system(size: 60.0).weight(.bold))
|
||||
.foregroundStyle(
|
||||
LinearGradient(
|
||||
colors: [DamusColors.lighterPink, DamusColors.deepPurple],
|
||||
startPoint: .bottomLeading,
|
||||
endPoint: .topTrailing
|
||||
)
|
||||
)
|
||||
.foregroundColor(.white)
|
||||
.tracking(-2)
|
||||
}
|
||||
}
|
||||
.padding(.bottom, 30)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
DamusPurpleView.LogoView()
|
||||
}
|
||||
89
damus/Views/Purple/Detail/MarketingContentView.swift
Normal file
89
damus/Views/Purple/Detail/MarketingContentView.swift
Normal file
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// DamusPurpleMarketingContentView.swift
|
||||
// damus
|
||||
//
|
||||
// Created by Daniel D’Aquino on 2024-02-09.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
extension DamusPurpleView {
|
||||
struct MarketingContentView: View {
|
||||
let purple: DamusPurple
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 30) {
|
||||
PurpleViewPrimitives.SubtitleView(text: NSLocalizedString("Help us stay independent in our mission for Freedom tech with our Purple subscription, and look cool doing it!", comment: "Damus purple subscription pitch"))
|
||||
.multilineTextAlignment(.center)
|
||||
|
||||
HStack(spacing: 20) {
|
||||
PurpleViewPrimitives.IconOnBoxView(name: "heart.fill")
|
||||
|
||||
VStack(alignment: .leading) {
|
||||
PurpleViewPrimitives.TitleView(text: NSLocalizedString("Help Build The Future", comment: "Title for funding future damus development"))
|
||||
|
||||
PurpleViewPrimitives.SubtitleView(text: NSLocalizedString("Support Damus development to help build the future of decentralized communication on the web.", comment: "Reason for supporting damus development"))
|
||||
}
|
||||
}
|
||||
|
||||
HStack(spacing: 20) {
|
||||
PurpleViewPrimitives.IconOnBoxView(name: "ai-3-stars.fill")
|
||||
|
||||
VStack(alignment: .leading) {
|
||||
PurpleViewPrimitives.TitleView(text: NSLocalizedString("Exclusive features", comment: "Features only available on subscription service"))
|
||||
.padding(.bottom, -3)
|
||||
|
||||
HStack(spacing: 3) {
|
||||
Image("calendar")
|
||||
.resizable()
|
||||
.frame(width: 15, height: 15)
|
||||
|
||||
Text(NSLocalizedString("Coming soon", comment: "Feature is still in development and will be available soon"))
|
||||
.font(.caption)
|
||||
.bold()
|
||||
}
|
||||
.foregroundColor(DamusColors.pink)
|
||||
.padding(.vertical, 3)
|
||||
.padding(.horizontal, 8)
|
||||
.background(DamusColors.lightBackgroundPink)
|
||||
.cornerRadius(30.0)
|
||||
|
||||
PurpleViewPrimitives.SubtitleView(text: NSLocalizedString("Be the first to access upcoming premium features: Automatic translations, longer note storage, and more", comment: "Description of new features to be expected"))
|
||||
.padding(.top, 3)
|
||||
}
|
||||
}
|
||||
|
||||
HStack(spacing: 20) {
|
||||
PurpleViewPrimitives.IconOnBoxView(name: "badge")
|
||||
|
||||
VStack(alignment: .leading) {
|
||||
PurpleViewPrimitives.TitleView(text: NSLocalizedString("Supporter Badge", comment: "Title for supporter badge"))
|
||||
|
||||
PurpleViewPrimitives.SubtitleView(text: NSLocalizedString("Get a special badge on your profile to show everyone your contribution to Freedom tech", comment: "Supporter badge description"))
|
||||
}
|
||||
}
|
||||
|
||||
HStack {
|
||||
Spacer()
|
||||
Link(
|
||||
purple.enable_purple_iap_support ?
|
||||
NSLocalizedString("Learn more about the features", comment: "Label for a link to the Damus website, to allow the user to learn more about the features of Purple")
|
||||
:
|
||||
NSLocalizedString("Coming soon! Visit our website to learn more", comment: "Label announcing Purple, and inviting the user to learn more on the website"),
|
||||
destination: purple.environment.damus_website_url()
|
||||
)
|
||||
.foregroundColor(DamusColors.pink)
|
||||
.padding()
|
||||
Spacer()
|
||||
}
|
||||
|
||||
}
|
||||
.padding([.trailing, .leading], 30)
|
||||
.padding(.bottom, 20)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
DamusPurpleView.MarketingContentView(purple: test_damus_state.purple)
|
||||
}
|
||||
Reference in New Issue
Block a user