Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
Settings refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jzzocc committed Mar 6, 2021
1 parent 707eef9 commit 94d0b23
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 58 deletions.
3 changes: 1 addition & 2 deletions Extensions/CollectionItem+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ private extension Account {
}
.compactMap(URL.init(string:)))

if !identityContext.appPreferences.shouldReduceMotion
&& identityContext.appPreferences.animateAvatars == .everywhere {
if identityContext.appPreferences.animateAvatars == .everywhere {
urls.insert(avatar)
} else {
urls.insert(avatarStatic)
Expand Down
3 changes: 1 addition & 2 deletions Extensions/NSMutableAttributedString+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ extension NSMutableAttributedString {
let attachment = AnimatedTextAttachment()
let imageURL: URL?

if !identityContext.appPreferences.shouldReduceMotion,
identityContext.appPreferences.animateCustomEmojis,
if identityContext.appPreferences.animateCustomEmojis,
let urlString = emoji.url {
imageURL = URL(stringEscapingPath: urlString)
} else if let staticURLString = emoji.staticUrl {
Expand Down
1 change: 0 additions & 1 deletion Localizations/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@
"preferences.blocked-domains" = "Blocked Domains";
"preferences.blocked-users" = "Blocked Users";
"preferences.media" = "Media";
"preferences.media.use-system-reduce-motion" = "Use system reduce motion setting";
"preferences.media.avatars" = "Avatars";
"preferences.media.avatars.animate" = "Animate avatars";
"preferences.media.avatars.animate.everywhere" = "Everywhere";
Expand Down
3 changes: 2 additions & 1 deletion Notification Service Extension/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ final class NotificationService: UNNotificationServiceExtension {
private extension NotificationService {
private static let environment = AppEnvironment.live(
userNotificationCenter: .current(),
reduceMotion: { false })
reduceMotion: { false },
autoplayVideos: { true })

enum ImageError: Error {
case dataMissing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public struct AppEnvironment {
let userDefaults: UserDefaults
let userNotificationClient: UserNotificationClient
let reduceMotion: () -> Bool
let autoplayVideos: () -> Bool
let uuid: () -> UUID
let inMemoryContent: Bool
let fixtureDatabase: IdentityDatabase?
Expand All @@ -24,6 +25,7 @@ public struct AppEnvironment {
userDefaults: UserDefaults,
userNotificationClient: UserNotificationClient,
reduceMotion: @escaping () -> Bool,
autoplayVideos: @escaping () -> Bool,
uuid: @escaping () -> UUID,
inMemoryContent: Bool,
fixtureDatabase: IdentityDatabase?) {
Expand All @@ -33,6 +35,7 @@ public struct AppEnvironment {
self.userDefaults = userDefaults
self.userNotificationClient = userNotificationClient
self.reduceMotion = reduceMotion
self.autoplayVideos = autoplayVideos
self.uuid = uuid
self.inMemoryContent = inMemoryContent
self.fixtureDatabase = fixtureDatabase
Expand All @@ -42,14 +45,17 @@ public struct AppEnvironment {
public extension AppEnvironment {
static let appGroup = "group.metabolist.metatext"

static func live(userNotificationCenter: UNUserNotificationCenter, reduceMotion: @escaping () -> Bool) -> Self {
static func live(userNotificationCenter: UNUserNotificationCenter,
reduceMotion: @escaping () -> Bool,
autoplayVideos: @escaping () -> Bool) -> Self {
Self(
session: URLSession.shared,
webAuthSessionType: LiveWebAuthSession.self,
keychain: LiveKeychain.self,
userDefaults: UserDefaults(suiteName: appGroup)!,
userNotificationClient: .live(userNotificationCenter),
reduceMotion: reduceMotion,
autoplayVideos: autoplayVideos,
uuid: UUID.init,
inMemoryContent: false,
fixtureDatabase: nil)
Expand Down
22 changes: 7 additions & 15 deletions ServiceLayer/Sources/ServiceLayer/Utilities/AppPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import Mastodon
public struct AppPreferences {
private let userDefaults: UserDefaults
private let systemReduceMotion: () -> Bool
private let systemAutoplayVideos: () -> Bool

public init(environment: AppEnvironment) {
self.userDefaults = environment.userDefaults
self.systemReduceMotion = environment.reduceMotion
self.systemAutoplayVideos = environment.autoplayVideos
}
}

Expand Down Expand Up @@ -45,11 +47,6 @@ public extension AppPreferences {
public var id: String { rawValue }
}

var useSystemReduceMotionForMedia: Bool {
get { self[.useSystemReduceMotionForMedia] ?? true }
set { self[.useSystemReduceMotionForMedia] = newValue }
}

var statusWord: StatusWord {
get {
if let rawValue = self[.statusWord] as String?,
Expand All @@ -69,18 +66,18 @@ public extension AppPreferences {
return value
}

return .everywhere
return systemReduceMotion() ? .never : .everywhere
}
set { self[.animateAvatars] = newValue.rawValue }
}

var animateHeaders: Bool {
get { self[.animateHeaders] ?? true }
get { self[.animateHeaders] ?? !systemReduceMotion() }
set { self[.animateHeaders] = newValue }
}

var animateCustomEmojis: Bool {
get { self[.animateCustomEmojis] ?? true }
get { self[.animateCustomEmojis] ?? !systemReduceMotion() }
set { self[.animateCustomEmojis] = newValue }
}

Expand All @@ -91,7 +88,7 @@ public extension AppPreferences {
return value
}

return .always
return (!systemAutoplayVideos() || systemReduceMotion()) ? .never : .always
}
set { self[.autoplayGIFs] = newValue.rawValue }
}
Expand All @@ -103,7 +100,7 @@ public extension AppPreferences {
return value
}

return .wifi
return (!systemAutoplayVideos() || systemReduceMotion()) ? .never : .wifi
}
set { self[.autoplayVideos] = newValue.rawValue }
}
Expand Down Expand Up @@ -141,10 +138,6 @@ public extension AppPreferences {
set { self[.notificationSounds] = newValue.map { $0.rawValue } }
}

var shouldReduceMotion: Bool {
systemReduceMotion() && useSystemReduceMotionForMedia
}

func positionBehavior(timeline: Timeline) -> PositionBehavior {
switch timeline {
case .home:
Expand Down Expand Up @@ -195,7 +188,6 @@ private extension AppPreferences {
case statusWord
case requireDoubleTapToReblog
case requireDoubleTapToFavorite
case useSystemReduceMotionForMedia
case animateAvatars
case animateHeaders
case animateCustomEmojis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public extension AppEnvironment {
userDefaults: userDefaults,
userNotificationClient: userNotificationClient,
reduceMotion: { false },
autoplayVideos: { true },
uuid: uuid,
inMemoryContent: inMemoryContent,
fixtureDatabase: fixtureDatabase)
Expand Down
3 changes: 2 additions & 1 deletion Share Extension/ShareExtensionNavigationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import ViewModels
class ShareExtensionNavigationViewController: UINavigationController {
private let environment = AppEnvironment.live(
userNotificationCenter: .current(),
reduceMotion: { UIAccessibility.isReduceMotionEnabled })
reduceMotion: { UIAccessibility.isReduceMotionEnabled },
autoplayVideos: { UIAccessibility.isVideoAutoplayEnabled })

override func viewDidLoad() {
super.viewDidLoad()
Expand Down
3 changes: 2 additions & 1 deletion System/MetatextApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ struct MetatextApp: App {
private extension MetatextApp {
static let environment = AppEnvironment.live(
userNotificationCenter: .current(),
reduceMotion: { UIAccessibility.isReduceMotionEnabled })
reduceMotion: { UIAccessibility.isReduceMotionEnabled },
autoplayVideos: { UIAccessibility.isVideoAutoplayEnabled })
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public extension AccountViewModel {
var id: Account.Id { accountService.account.id }

var headerURL: URL {
if !identityContext.appPreferences.shouldReduceMotion, identityContext.appPreferences.animateHeaders {
if identityContext.appPreferences.animateHeaders {
return accountService.account.header
} else {
return accountService.account.headerStatic
Expand Down Expand Up @@ -64,9 +64,8 @@ public extension AccountViewModel {
var isSelf: Bool { accountService.account.id == identityContext.identity.account?.id }

func avatarURL(profile: Bool = false) -> URL {
if !identityContext.appPreferences.shouldReduceMotion,
(identityContext.appPreferences.animateAvatars == .everywhere
|| (identityContext.appPreferences.animateAvatars == .profiles && profile)) {
if identityContext.appPreferences.animateAvatars == .everywhere
|| (identityContext.appPreferences.animateAvatars == .profiles && profile) {
return accountService.account.avatar
} else {
return accountService.account.avatarStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,15 @@ public extension StatusViewModel {
var accountName: String { "@".appending(statusService.status.displayStatus.account.acct) }

var avatarURL: URL {
if !identityContext.appPreferences.shouldReduceMotion,
identityContext.appPreferences.animateAvatars == .everywhere {
if identityContext.appPreferences.animateAvatars == .everywhere {
return statusService.status.displayStatus.account.avatar
} else {
return statusService.status.displayStatus.account.avatarStatic
}
}

var rebloggerAvatarURL: URL {
if !identityContext.appPreferences.shouldReduceMotion,
identityContext.appPreferences.animateAvatars == .everywhere {
if identityContext.appPreferences.animateAvatars == .everywhere {
return statusService.status.account.avatar
} else {
return statusService.status.account.avatarStatic
Expand Down
30 changes: 10 additions & 20 deletions Views/SwiftUI/PreferencesView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright © 2020 Metabolist. All rights reserved.

import Combine
import Mastodon
import SwiftUI
import ViewModels
Expand Down Expand Up @@ -107,40 +108,31 @@ struct PreferencesView: View {
Toggle("preferences.links.use-universal-links",
isOn: $identityContext.appPreferences.useUniversalLinks)
}
if accessibilityReduceMotion {
Toggle("preferences.media.use-system-reduce-motion",
isOn: $identityContext.appPreferences.useSystemReduceMotionForMedia)
}
}
Group {
Picker("preferences.media.autoplay.gifs",
selection: reduceMotion ? .constant(.never) : $identityContext.appPreferences.autoplayGIFs) {
selection: $identityContext.appPreferences.autoplayGIFs) {
ForEach(AppPreferences.Autoplay.allCases) { option in
Text(option.localizedStringKey).tag(option)
}
}
Picker("preferences.media.autoplay.videos",
selection: reduceMotion
? .constant(.never) : $identityContext.appPreferences.autoplayVideos) {
selection: $identityContext.appPreferences.autoplayVideos) {
ForEach(AppPreferences.Autoplay.allCases) { option in
Text(option.localizedStringKey).tag(option)
}
}
Picker("preferences.media.avatars.animate",
selection: reduceMotion
? .constant(.never) : $identityContext.appPreferences.animateAvatars) {
selection: $identityContext.appPreferences.animateAvatars) {
ForEach(AppPreferences.AnimateAvatars.allCases) { option in
Text(option.localizedStringKey).tag(option)
}
}
Toggle("preferences.media.custom-emojis.animate",
isOn: reduceMotion ? .constant(false) : $identityContext.appPreferences.animateCustomEmojis)
.disabled(reduceMotion)
isOn: $identityContext.appPreferences.animateCustomEmojis)
Toggle("preferences.media.headers.animate",
isOn: reduceMotion ? .constant(false) : $identityContext.appPreferences.animateHeaders)
.disabled(reduceMotion)
isOn: $identityContext.appPreferences.animateHeaders)
}
.disabled(reduceMotion)
if viewModel.identityContext.identity.authenticated
&& !viewModel.identityContext.identity.pending {
Picker("preferences.home-timeline-position-on-startup",
Expand All @@ -154,12 +146,10 @@ struct PreferencesView: View {
}
.navigationTitle("preferences")
.alertItem($viewModel.alertItem)
}
}

private extension PreferencesView {
var reduceMotion: Bool {
identityContext.appPreferences.shouldReduceMotion
.onReceive(NotificationCenter.default.publisher(
for: UIAccessibility.videoAutoplayStatusDidChangeNotification)) { _ in
viewModel.objectWillChange.send()
}
}
}

Expand Down
1 change: 0 additions & 1 deletion Views/UIKit/CompositionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ private extension CompositionView {
guard let self = self else { return }

let avatarURL = $0.appPreferences.animateAvatars == .everywhere
&& !$0.appPreferences.shouldReduceMotion
? $0.identity.account?.avatar
: $0.identity.account?.avatarStatic

Expand Down
5 changes: 1 addition & 4 deletions Views/UIKit/Content Views/AutocompleteItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ private extension AutocompleteItemView {
switch autocompleteItemConfiguration.item {
case let .account(account):
let appPreferences = autocompleteItemConfiguration.identityContext.appPreferences
let avatarURL = appPreferences.animateAvatars == .everywhere
&& !appPreferences.shouldReduceMotion
? account.avatar
: account.avatarStatic
let avatarURL = appPreferences.animateAvatars == .everywhere ? account.avatar : account.avatarStatic

imageView.sd_setImage(with: avatarURL)
imageView.isHidden = false
Expand Down
1 change: 0 additions & 1 deletion Views/UIKit/SecondaryNavigationTitleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ private extension SecondaryNavigationTitleView {

func applyViewModel() {
let avatarURL = viewModel.identityContext.appPreferences.animateAvatars == .everywhere
&& !viewModel.identityContext.appPreferences.shouldReduceMotion
? viewModel.identityContext.identity.account?.avatar
: viewModel.identityContext.identity.account?.avatarStatic

Expand Down

0 comments on commit 94d0b23

Please sign in to comment.