Skip to content

Commit

Permalink
Use grouped notifications beta setting in UserDefaults
Browse files Browse the repository at this point in the history
  • Loading branch information
shannon committed Jan 14, 2025
1 parent 1ec772d commit 12d737c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Mastodon/Scene/Donation/DonationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ struct DonationButtonStyle: ButtonStyle {
struct DefaultDonationViewModel: DonationCampaignViewModel {
var id: String = "default"
var paymentBaseURL: URL? {
if Mastodon.API.isTestingDonations {
if UserDefaults.standard.useStagingForDonations {
URL(string: "https://sponsor.staging.joinmastodon.org/donation/new")
} else {
URL(string: "https://sponsor.joinmastodon.org/donation/new")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ final class NotificationTimelineViewModel {
) {
self.authenticationBox = authenticationBox
self.scope = scope
let useGroupedNotifications = false
self.feedLoader = MastodonFeedLoader(authenticationBox: authenticationBox, kind: scope.feedKind, dedupePolicy: useGroupedNotifications ? .removeOldest : .omitNewest)
let useGroupedNotifications = UserDefaults.standard.useGroupedNotifications
self.feedLoader = MastodonFeedLoader(authenticationBox: authenticationBox, kind: scope.feedKind)
self.notificationPolicy = notificationPolicy

NotificationCenter.default.addObserver(self, selector: #selector(Self.notificationFilteringChanged(_:)), name: .notificationFilteringChanged, object: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ struct BetaTestSettingsViewModel {
let testGroupedNotifications: Bool

init() {
useStagingForDonations = Mastodon.API.isTestingDonations
testGroupedNotifications = UserDefaults.standard.bool(forKey: BetaTestSetting.useGroupedNotifications.userDefaultsKey)
useStagingForDonations = UserDefaults.standard.useStagingForDonations
testGroupedNotifications = UserDefaults.standard.useGroupedNotifications
}

func byToggling(_ setting: BetaTestSetting) -> BetaTestSettingsViewModel {
switch setting {
case .useStagingForDonations:
Mastodon.API.toggleTestingDonations()
UserDefaults.standard.toggleUseStagingForDonations()
case .useGroupedNotifications:
UserDefaults.standard.set(!testGroupedNotifications, forKey: setting.userDefaultsKey)
UserDefaults.standard.toggleUseGroupedNotifications()
case .clearPreviousDonationCampaigns:
assertionFailure("this is an action, not a setting")
break
Expand Down Expand Up @@ -55,15 +55,6 @@ enum BetaTestSetting: Hashable {
return "Test grouped notifications"
}
}

var userDefaultsKey: String {
switch self {
case .useGroupedNotifications:
return "use_grouped_notifications"
case .useStagingForDonations, .clearPreviousDonationCampaigns:
return "UNEXPECTED_KEY"
}
}
}

fileprivate typealias BasicCell = UITableViewCell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SettingsViewController: UIViewController {
if Mastodon.Entity.DonationCampaign.isEligibleForDonationsSettingsSection(domain: domain) {
baseSections.insert(.init(entries: [.makeDonation, .manageDonations]), at: baseSections.count - 1)
}
if isDebugOrTestflightOrSimulator {
if UserDefaults.isDebugOrTestflightOrSimulator {
baseSections.append(.init(entries: [.manageBetaFeatures]))
}
sections = baseSections
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// BetaTestSetting.swift
// MastodonSDK
//
// Created by Shannon Hughes on 2025-01-13.
//

import Foundation

extension UserDefaults {

public static var isDebugOrTestflightOrSimulator: Bool {
#if DEBUG
return true
#else
guard let path = Bundle.main.appStoreReceiptURL?.path else {
return false
}
return path.contains("CoreSimulator") || path.contains("sandboxReceipt")
#endif
}

@objc public dynamic var useStagingForDonations: Bool {
get {
register(defaults: [#function: true])
return bool(forKey: #function) && UserDefaults.isDebugOrTestflightOrSimulator
}
set { self[#function] = newValue }
}

@objc public dynamic var useGroupedNotifications: Bool {
get {
register(defaults: [#function: true])
return bool(forKey: #function) && UserDefaults.isDebugOrTestflightOrSimulator
}
set { self[#function] = newValue }
}

public func toggleUseStagingForDonations() {
let useStaging = UserDefaults.standard.useStagingForDonations
UserDefaults.standard.useStagingForDonations = !useStaging
}

public func toggleUseGroupedNotifications() {
let useGrouped = UserDefaults.standard.useGroupedNotifications
UserDefaults.standard.useGroupedNotifications = !useGrouped
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,19 @@ import os.log
@MainActor
final public class MastodonFeedLoader {

public enum DeduplicationPolicy {
case omitNewest
case removeOldest
}

private let logger = Logger(subsystem: "MastodonFeedLoader", category: "Data")
private static let entryNotFoundMessage = "Failed to find suitable record. Depending on the context this might result in errors (data not being updated) or can be discarded (e.g. when there are mixed data sources where an entry might or might not exist)."

@Published public private(set) var records: [MastodonFeedItemIdentifier] = []

private let authenticationBox: MastodonAuthenticationBox
private let kind: MastodonFeedKind
private let dedupePolicy: DeduplicationPolicy

private var subscriptions = Set<AnyCancellable>()

public init(authenticationBox: MastodonAuthenticationBox, kind: MastodonFeedKind, dedupePolicy: DeduplicationPolicy = .omitNewest) {
public init(authenticationBox: MastodonAuthenticationBox, kind: MastodonFeedKind) {
self.authenticationBox = authenticationBox
self.kind = kind
self.dedupePolicy = dedupePolicy

StatusFilterService.shared.$activeFilterBox
.sink { filterBox in
Expand All @@ -56,13 +49,7 @@ final public class MastodonFeedLoader {
private func appendRecordsAfterFiltering(_ additionalRecords: [MastodonFeedItemIdentifier]) async {
guard let filterBox = StatusFilterService.shared.activeFilterBox else { self.records += additionalRecords; return }
let newRecords = await self.filter(additionalRecords, forFeed: kind, with: filterBox)
switch dedupePolicy {
case .omitNewest:
self.records = (self.records + newRecords).removingDuplicates()
case .removeOldest:
assertionFailure("not implemented")
self.records = (self.records + newRecords).removingDuplicates()
}
self.records = (self.records + newRecords).removingDuplicates()
}

public func loadInitial(kind: MastodonFeedKind) {
Expand Down Expand Up @@ -244,7 +231,7 @@ private extension MastodonFeedLoader {
}

private func loadNotifications(withScope scope: APIService.MastodonNotificationScope, olderThan maxID: String? = nil) async throws -> [MastodonFeedItemIdentifier] {
let useGroupedNotifications = false
let useGroupedNotifications = UserDefaults.standard.useGroupedNotifications
if useGroupedNotifications {
return try await _getGroupedNotifications(withScope: scope, olderThan: maxID)
} else {
Expand All @@ -253,7 +240,7 @@ private extension MastodonFeedLoader {
}

private func loadNotifications(withAccountID accountID: String, olderThan maxID: String? = nil) async throws -> [MastodonFeedItemIdentifier] {
let useGroupedNotifications = false
let useGroupedNotifications = UserDefaults.standard.useGroupedNotifications
if useGroupedNotifications {
return try await _getGroupedNotifications(accountID: accountID, olderThan: maxID)
} else {
Expand Down
32 changes: 1 addition & 31 deletions MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Donation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,7 @@

import Foundation

public var isDebugOrTestflightOrSimulator: Bool {
#if DEBUG
return true
#else
guard let path = Bundle.main.appStoreReceiptURL?.path else {
return false
}
return path.contains("CoreSimulator") || path.contains("sandboxReceipt")
#endif
}

extension Mastodon.API {
public static var isTestingDonations: Bool {
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 {
URL(
Expand All @@ -56,7 +26,7 @@ extension Mastodon.API {
URLQueryItem(name: "locale", value: locale),
URLQueryItem(name: "seed", value: "\(seed)"),
]
if isTestingDonations {
if UserDefaults.standard.useStagingForDonations {
queryItems.append(
URLQueryItem(name: "environment", value: "staging"))
}
Expand Down

0 comments on commit 12d737c

Please sign in to comment.