Skip to content

Commit

Permalink
Add donations testing helpers to Settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
whattherestimefor committed Nov 6, 2024
1 parent ad8561c commit e0dfb63
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
19 changes: 18 additions & 1 deletion Mastodon/Scene/Settings/Settings Overview/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import UIKit
import MastodonLocalization
import MastodonSDK

struct SettingsSection: Hashable {
let entries: [SettingsEntry]
Expand All @@ -16,6 +17,8 @@ enum SettingsEntry: Hashable {
case makeDonation
case manageDonations
case logout(accountName: String)
case toggleTestDonations
case clearPreviousDonationCampaigns

var title: String {
switch self {
Expand All @@ -35,14 +38,20 @@ enum SettingsEntry: Hashable {
return L10n.Scene.Settings.Overview.aboutMastodon
case .logout(let accountName):
return L10n.Scene.Settings.Overview.logout(accountName)
case .toggleTestDonations:
return Mastodon.API.isTestingDonations ? "Donations use staging: ON" : "Donations use staging: OFF"
case .clearPreviousDonationCampaigns:
return "Clear Donation History"
}
}

var secondaryTitle: String? {
switch self {
case .serverDetails(domain: let domain):
return domain
case .general, .notifications, .privacySafety, .makeDonation, .manageDonations, .aboutMastodon, .logout(_):
case .general, .notifications, .privacySafety, .makeDonation, .manageDonations, .aboutMastodon, .logout(_):
return nil
case .toggleTestDonations, .clearPreviousDonationCampaigns:
return nil
}
}
Expand All @@ -51,6 +60,8 @@ enum SettingsEntry: Hashable {
switch self {
case .general, .notifications, .privacySafety, .serverDetails(_), .makeDonation, .manageDonations, .aboutMastodon, .logout(_):
return .disclosureIndicator
case .toggleTestDonations, .clearPreviousDonationCampaigns:
return .none
}
}

Expand All @@ -72,6 +83,8 @@ enum SettingsEntry: Hashable {
return UIImage(systemName: "info.circle.fill")
case .logout(_):
return nil
case .toggleTestDonations, .clearPreviousDonationCampaigns:
return nil
}
}

Expand All @@ -91,6 +104,8 @@ enum SettingsEntry: Hashable {
return .systemPurple
case .logout(_):
return nil
case .toggleTestDonations, .clearPreviousDonationCampaigns:
return nil
}

}
Expand All @@ -101,6 +116,8 @@ enum SettingsEntry: Hashable {
return .label
case .logout(_):
return .red
case .toggleTestDonations, .clearPreviousDonationCampaigns:
return .systemIndigo
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class SettingsViewController: UIViewController {
if Mastodon.Entity.DonationCampaign.isEligibleForDonationsSettingsSection(domain: domain) {
baseSections.insert(.init(entries: [.makeDonation, .manageDonations]), at: baseSections.count - 1)
}
if isDebugOrTestflightOrSimulator {
baseSections.append(.init(entries: [.toggleTestDonations, .clearPreviousDonationCampaigns]))
}
sections = baseSections

tableView = UITableView(frame: .zero, style: .insetGrouped)
Expand Down
5 changes: 5 additions & 0 deletions Mastodon/Scene/Settings/SettingsCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ extension SettingsCoordinator: SettingsViewControllerDelegate {
navigationController.pushViewController(aboutViewController, animated: true)
case .logout(_):
delegate?.logout(self)
case .toggleTestDonations:
Mastodon.API.toggleTestingDonations()
settingsViewController.tableView.reloadData()
case .clearPreviousDonationCampaigns:
Mastodon.Entity.DonationCampaign.forgetPreviousCampaigns()
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Donation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import Foundation

private var isDebugOrTestflightOrSimulator: Bool {
public var isDebugOrTestflightOrSimulator: Bool {
#if DEBUG
return true
#else
Expand All @@ -15,7 +15,23 @@ private var isDebugOrTestflightOrSimulator: Bool {

extension Mastodon.API {
public static var isTestingDonations: Bool {
return isDebugOrTestflightOrSimulator
return isDebugOrTestflightOrSimulator && useStaging
}
public static func toggleTestingDonations() {
useStaging = !useStaging
}
private static let stagingKey = "use_staging_for_donations_testing"
private static var useStaging: Bool {
get {
if UserDefaults.standard.value(forKey: stagingKey) != nil {
return UserDefaults.standard.bool(forKey: stagingKey)
} else {
return true
}
}
set {
UserDefaults.standard.set(newValue, forKey: stagingKey)
}
}

public static var donationsEndpoint: URL {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,9 @@ extension Mastodon.Entity {
ids.append(campaign)
UserDefaults.standard.setValue(ids, forKey: contributedCampaignsKey)
}
static public func forgetPreviousCampaigns() {
UserDefaults.standard.removeObject(forKey: contributedCampaignsKey)
UserDefaults.standard.removeObject(forKey: dismissedCampaignsKey)
}
}
}

0 comments on commit e0dfb63

Please sign in to comment.