Refactoring

This commit is contained in:
Justin Mazzocchi 2020-08-07 03:59:48 -07:00
parent 56f1b63fb9
commit f1ef5def5b
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
11 changed files with 87 additions and 86 deletions

View file

@ -138,8 +138,12 @@ extension IdentitiesViewModel {
static let development = SecondaryNavigationViewModel.development.identitiesViewModel()
}
extension IdentityPreferencesViewModel {
static let development = SecondaryNavigationViewModel.development.identityPreferencesViewModel()
extension PreferencesViewModel {
static let development = SecondaryNavigationViewModel.development.preferencesViewModel()
}
extension PostingReadingPreferencesViewModel {
static let development = PreferencesViewModel.development.postingReadingPreferencesViewModel()
}
// swiftlint:enable force_try

View file

@ -7,10 +7,14 @@
objects = {
/* Begin PBXBuildFile section */
D0091B6824DC10B30040E8D2 /* IdentityPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B6724DC10B30040E8D2 /* IdentityPreferencesView.swift */; };
D0091B6924DC10B30040E8D2 /* IdentityPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B6724DC10B30040E8D2 /* IdentityPreferencesView.swift */; };
D0091B6B24DC10CE0040E8D2 /* IdentityPreferencesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B6A24DC10CE0040E8D2 /* IdentityPreferencesViewModel.swift */; };
D0091B6C24DC10CE0040E8D2 /* IdentityPreferencesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B6A24DC10CE0040E8D2 /* IdentityPreferencesViewModel.swift */; };
D0091B6824DC10B30040E8D2 /* PostingReadingPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B6724DC10B30040E8D2 /* PostingReadingPreferencesView.swift */; };
D0091B6924DC10B30040E8D2 /* PostingReadingPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B6724DC10B30040E8D2 /* PostingReadingPreferencesView.swift */; };
D0091B6B24DC10CE0040E8D2 /* PostingReadingPreferencesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B6A24DC10CE0040E8D2 /* PostingReadingPreferencesViewModel.swift */; };
D0091B6C24DC10CE0040E8D2 /* PostingReadingPreferencesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B6A24DC10CE0040E8D2 /* PostingReadingPreferencesViewModel.swift */; };
D0091B6E24DD68090040E8D2 /* PreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B6D24DD68090040E8D2 /* PreferencesView.swift */; };
D0091B6F24DD68090040E8D2 /* PreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B6D24DD68090040E8D2 /* PreferencesView.swift */; };
D0091B7124DD68220040E8D2 /* PreferencesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B7024DD68220040E8D2 /* PreferencesViewModel.swift */; };
D0091B7224DD68220040E8D2 /* PreferencesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0091B7024DD68220040E8D2 /* PreferencesViewModel.swift */; };
D047FAAE24C3E21200AF17C5 /* MetatextApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D047FA8524C3E21000AF17C5 /* MetatextApp.swift */; };
D047FAAF24C3E21200AF17C5 /* MetatextApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D047FA8524C3E21000AF17C5 /* MetatextApp.swift */; };
D047FAB224C3E21200AF17C5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D047FA8724C3E21200AF17C5 /* Assets.xcassets */; };
@ -163,8 +167,10 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
D0091B6724DC10B30040E8D2 /* IdentityPreferencesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IdentityPreferencesView.swift; sourceTree = "<group>"; };
D0091B6A24DC10CE0040E8D2 /* IdentityPreferencesViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IdentityPreferencesViewModel.swift; sourceTree = "<group>"; };
D0091B6724DC10B30040E8D2 /* PostingReadingPreferencesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostingReadingPreferencesView.swift; sourceTree = "<group>"; };
D0091B6A24DC10CE0040E8D2 /* PostingReadingPreferencesViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostingReadingPreferencesViewModel.swift; sourceTree = "<group>"; };
D0091B6D24DD68090040E8D2 /* PreferencesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesView.swift; sourceTree = "<group>"; };
D0091B7024DD68220040E8D2 /* PreferencesViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesViewModel.swift; sourceTree = "<group>"; };
D047FA8524C3E21000AF17C5 /* MetatextApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetatextApp.swift; sourceTree = "<group>"; };
D047FA8724C3E21200AF17C5 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
D047FA8C24C3E21200AF17C5 /* Metatext.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Metatext.app; sourceTree = BUILT_PRODUCTS_DIR; };
@ -385,7 +391,8 @@
children = (
D0DB6EF324C5228A00D965FE /* AddIdentityView.swift */,
D06BAB5024D942CF0081B8FD /* IdentitiesView.swift */,
D0091B6724DC10B30040E8D2 /* IdentityPreferencesView.swift */,
D0091B6724DC10B30040E8D2 /* PostingReadingPreferencesView.swift */,
D0091B6D24DD68090040E8D2 /* PreferencesView.swift */,
D0BEC93A24C96FD500E864C4 /* RootView.swift */,
D04FD73224D48F37007D572D /* SecondaryNavigationView.swift */,
D0BEC94924CA231200E864C4 /* TimelineView.swift */,
@ -412,7 +419,8 @@
D0DB6F0824C65AC000D965FE /* AddIdentityViewModel.swift */,
D06BAB4D24D942BC0081B8FD /* IdentitiesViewModel.swift */,
D052BBDF24D805E300A80A7A /* MainNavigationViewModel.swift */,
D0091B6A24DC10CE0040E8D2 /* IdentityPreferencesViewModel.swift */,
D0091B6A24DC10CE0040E8D2 /* PostingReadingPreferencesViewModel.swift */,
D0091B7024DD68220040E8D2 /* PreferencesViewModel.swift */,
D0BEC93724C9632800E864C4 /* RootViewModel.swift */,
D04FD73524D49506007D572D /* SecondaryNavigationViewModel.swift */,
D0BEC94624CA22C400E864C4 /* TimelineViewModel.swift */,
@ -714,9 +722,11 @@
D0DC177724D0CF2600A75C65 /* FakeKeychain.swift in Sources */,
D0C963FB24CC359D003BD330 /* AlertItem.swift in Sources */,
D0DC174624CFEC2000A75C65 /* StubbingURLProtocol.swift in Sources */,
D0091B7124DD68220040E8D2 /* PreferencesViewModel.swift in Sources */,
D0DC174D24CFF1F100A75C65 /* Stubbing.swift in Sources */,
D0091B6B24DC10CE0040E8D2 /* IdentityPreferencesViewModel.swift in Sources */,
D0091B6B24DC10CE0040E8D2 /* PostingReadingPreferencesViewModel.swift in Sources */,
D0666A5724C6C63400F3F04B /* MastodonDecoder.swift in Sources */,
D0091B6E24DD68090040E8D2 /* PreferencesView.swift in Sources */,
D0DB6EF424C5228A00D965FE /* AddIdentityView.swift in Sources */,
D0DC177424D0B58800A75C65 /* Keychain.swift in Sources */,
D06BAB5124D942CF0081B8FD /* IdentitiesView.swift in Sources */,
@ -736,7 +746,7 @@
D0CD847C24DBEA9F00CF380C /* Unknowable.swift in Sources */,
D0666A6F24C6DFB300F3F04B /* AccessToken.swift in Sources */,
D0ED1BCB24CF744200B4899C /* MastodonClient.swift in Sources */,
D0091B6824DC10B30040E8D2 /* IdentityPreferencesView.swift in Sources */,
D0091B6824DC10B30040E8D2 /* PostingReadingPreferencesView.swift in Sources */,
D0CD847624DBDF3C00CF380C /* Status.swift in Sources */,
D052BBE024D805E300A80A7A /* MainNavigationViewModel.swift in Sources */,
);
@ -786,9 +796,11 @@
D0DC177824D0CF2600A75C65 /* FakeKeychain.swift in Sources */,
D0C963FC24CC359D003BD330 /* AlertItem.swift in Sources */,
D0DC174724CFEC2000A75C65 /* StubbingURLProtocol.swift in Sources */,
D0091B7224DD68220040E8D2 /* PreferencesViewModel.swift in Sources */,
D0DC174E24CFF1F100A75C65 /* Stubbing.swift in Sources */,
D0091B6C24DC10CE0040E8D2 /* IdentityPreferencesViewModel.swift in Sources */,
D0091B6C24DC10CE0040E8D2 /* PostingReadingPreferencesViewModel.swift in Sources */,
D0666A5824C6C63400F3F04B /* MastodonDecoder.swift in Sources */,
D0091B6F24DD68090040E8D2 /* PreferencesView.swift in Sources */,
D0DB6EF524C5233E00D965FE /* AddIdentityView.swift in Sources */,
D0DC177524D0B58800A75C65 /* Keychain.swift in Sources */,
D06BAB5224D942CF0081B8FD /* IdentitiesView.swift in Sources */,
@ -808,7 +820,7 @@
D0CD847D24DBEA9F00CF380C /* Unknowable.swift in Sources */,
D0666A7024C6DFB300F3F04B /* AccessToken.swift in Sources */,
D0ED1BCC24CF744200B4899C /* MastodonClient.swift in Sources */,
D0091B6924DC10B30040E8D2 /* IdentityPreferencesView.swift in Sources */,
D0091B6924DC10B30040E8D2 /* PostingReadingPreferencesView.swift in Sources */,
D0CD847724DBDF3C00CF380C /* Status.swift in Sources */,
D052BBE124D805E300A80A7A /* MainNavigationViewModel.swift in Sources */,
);

View file

@ -4,8 +4,9 @@
"add-identity.instance-url" = "Instance URL";
"oauth.error.code-not-found" = "OAuth error: code not found";
"secondary-navigation.accounts" = "Accounts";
"secondary-navigation.identity-preferences" = "Preferences";
"preferences.title.%@" = "Preferences for %@";
"secondary-navigation.preferences" = "Preferences";
"preferences" = "Preferences";
"preferences.posting-reading" = "Posting and Reading";
"preferences.posting" = "Posting";
"preferences.use-preferences-from-server" = "Use preferences from server";
"preferences.posting-default-visiblility" = "Default visibility";

View file

@ -31,11 +31,10 @@ extension Identity {
}
struct Preferences: Codable, Hashable {
var useServerPostingPreferences = true
var useServerPostingReadingPreferences = true
var postingDefaultVisibility = Status.Visibility.public
var postingDefaultSensitive = false
var postingDefaultLanguage: String?
var useServerReadingPreferences = true
var readingExpandMedia = MastodonPreferences.ExpandMedia.default
var readingExpandSpoilers = false
}
@ -54,18 +53,12 @@ extension Identity {
}
extension Identity.Preferences {
var shouldUseAnyServerPreferences: Bool {
useServerPostingPreferences || useServerReadingPreferences
}
func updated(from serverPreferences: MastodonPreferences) -> Self {
var mutable = self
if useServerPostingPreferences {
if useServerPostingReadingPreferences {
mutable.postingDefaultVisibility = serverPreferences.postingDefaultVisibility
mutable.postingDefaultSensitive = serverPreferences.postingDefaultSensitive
}
if useServerReadingPreferences {
mutable.readingExpandMedia = serverPreferences.readingExpandMedia
mutable.readingExpandSpoilers = serverPreferences.readingExpandSpoilers
}

View file

@ -37,7 +37,7 @@ extension MainNavigationViewModel {
.sink(receiveValue: {})
.store(in: &cancellables)
if identity.preferences.shouldUseAnyServerPreferences {
if identity.preferences.useServerPostingReadingPreferences {
let capturedPreferences = identity.preferences
environment.networkClient.request(PreferencesEndpoint.preferences)

View file

@ -3,7 +3,7 @@
import Foundation
import Combine
class IdentityPreferencesViewModel: ObservableObject {
class PostingReadingPreferencesViewModel: ObservableObject {
@Published var preferences: Identity.Preferences
@Published var alertItem: AlertItem?
let handle: String
@ -20,7 +20,7 @@ class IdentityPreferencesViewModel: ObservableObject {
.dropFirst()
.removeDuplicates()
.handleEvents(receiveOutput: { [weak self] in
if $0.shouldUseAnyServerPreferences {
if $0.useServerPostingReadingPreferences {
self?.refreshPreferences()
}
})
@ -37,7 +37,7 @@ class IdentityPreferencesViewModel: ObservableObject {
}
}
extension IdentityPreferencesViewModel {
extension PostingReadingPreferencesViewModel {
func refreshPreferences() {
let id = environment.identity.id
let capturedPreferences = preferences

View file

@ -0,0 +1,20 @@
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
class PreferencesViewModel: ObservableObject {
let handle: String
private let environment: IdentifiedEnvironment
init(environment: IdentifiedEnvironment) {
self.environment = environment
handle = environment.identity.handle
}
}
extension PreferencesViewModel {
func postingReadingPreferencesViewModel() -> PostingReadingPreferencesViewModel {
PostingReadingPreferencesViewModel(environment: environment)
}
}

View file

@ -18,7 +18,7 @@ extension SecondaryNavigationViewModel {
IdentitiesViewModel(environment: environment)
}
func identityPreferencesViewModel() -> IdentityPreferencesViewModel {
IdentityPreferencesViewModel(environment: environment)
func preferencesViewModel() -> PreferencesViewModel {
PreferencesViewModel(environment: environment)
}
}

View file

@ -2,14 +2,16 @@
import SwiftUI
struct IdentityPreferencesView: View {
@StateObject var viewModel: IdentityPreferencesViewModel
struct PostingReadingPreferencesView: View {
@StateObject var viewModel: PostingReadingPreferencesViewModel
var body: some View {
Form {
Section(header: Text("preferences.posting")) {
Section {
Toggle("preferences.use-preferences-from-server",
isOn: $viewModel.preferences.useServerPostingPreferences)
isOn: $viewModel.preferences.useServerPostingReadingPreferences)
}
Section(header: Text("preferences.posting")) {
VStack(alignment: .leading) {
Text("preferences.posting-default-visiblility")
Picker("", selection: $viewModel.preferences.postingDefaultVisibility,
@ -20,14 +22,11 @@ struct IdentityPreferencesView: View {
})
.pickerStyle(SegmentedPickerStyle())
}
.disabled(viewModel.preferences.useServerPostingPreferences)
Toggle("preferences.posting-default-sensitive",
isOn: $viewModel.preferences.postingDefaultSensitive)
.disabled(viewModel.preferences.useServerPostingPreferences)
}
.disabled(viewModel.preferences.useServerPostingReadingPreferences)
Section(header: Text("preferences.reading")) {
Toggle("preferences.use-preferences-from-server",
isOn: $viewModel.preferences.useServerReadingPreferences)
VStack(alignment: .leading) {
Text("preferences.reading-expand-media")
Picker("", selection: $viewModel.preferences.readingExpandMedia,
@ -38,19 +37,18 @@ struct IdentityPreferencesView: View {
})
.pickerStyle(SegmentedPickerStyle())
}
.disabled(viewModel.preferences.useServerReadingPreferences)
Toggle("preferences.reading-expand-spoilers",
isOn: $viewModel.preferences.readingExpandSpoilers)
.disabled(viewModel.preferences.useServerReadingPreferences)
}
.disabled(viewModel.preferences.useServerPostingReadingPreferences)
}
.navigationTitle("preferences.title.\(viewModel.handle)")
.navigationTitle("preferences.posting-reading")
.alertItem($viewModel.alertItem)
}
}
struct PreferencesView_Previews: PreviewProvider {
struct PostingReadingPreferencesViewView_Previews: PreviewProvider {
static var previews: some View {
IdentityPreferencesView(viewModel: .development)
PostingReadingPreferencesView(viewModel: .development)
}
}

View file

@ -2,53 +2,23 @@
import SwiftUI
struct IdentityPreferencesView: View {
@StateObject var viewModel: IdentityPreferencesViewModel
struct PreferencesView: View {
@StateObject var viewModel: PreferencesViewModel
var body: some View {
Form {
Section(header: Text("preferences.posting")) {
Toggle("preferences.use-preferences-from-server",
isOn: $viewModel.preferences.useServerPostingPreferences)
VStack(alignment: .leading) {
Text("preferences.posting-default-visiblility")
Picker("", selection: $viewModel.preferences.postingDefaultVisibility,
content: {
Text("status.visibility.public").tag(Status.Visibility.public)
Text("status.visibility.unlisted").tag(Status.Visibility.unlisted)
Text("status.visibility.private").tag(Status.Visibility.private)
})
.pickerStyle(SegmentedPickerStyle())
}
.disabled(viewModel.preferences.useServerPostingPreferences)
Toggle("preferences.posting-default-sensitive",
isOn: $viewModel.preferences.postingDefaultSensitive)
.disabled(viewModel.preferences.useServerPostingPreferences)
}
Section(header: Text("preferences.reading")) {
Toggle("preferences.use-preferences-from-server",
isOn: $viewModel.preferences.useServerReadingPreferences)
VStack(alignment: .leading) {
Text("preferences.reading-expand-media")
Picker("", selection: $viewModel.preferences.readingExpandMedia,
content: {
Text("preferences.expand-media.default").tag(MastodonPreferences.ExpandMedia.default)
Text("preferences.expand-media.show-all").tag(MastodonPreferences.ExpandMedia.showAll)
Text("preferences.expand-media.hide-all").tag(MastodonPreferences.ExpandMedia.hideAll)
})
.pickerStyle(SegmentedPickerStyle())
}
.disabled(viewModel.preferences.useServerReadingPreferences)
Toggle("preferences.reading-expand-spoilers",
isOn: $viewModel.preferences.readingExpandSpoilers)
.disabled(viewModel.preferences.useServerReadingPreferences)
Section(header: Text(viewModel.handle)) {
NavigationLink("preferences.posting-reading",
destination: PostingReadingPreferencesView(
viewModel: viewModel.postingReadingPreferencesViewModel()))
}
}
.navigationTitle("preferences.title.\(viewModel.handle)")
.navigationTitle("preferences")
}
}
struct PreferencesView_Previews: PreviewProvider {
static var previews: some View {
IdentityPreferencesView(viewModel: .development)
PreferencesView(viewModel: .development)
}
}

View file

@ -34,9 +34,12 @@ struct SecondaryNavigationView: View {
.padding()
}
})
}
Section {
NavigationLink(
"secondary-navigation.identity-preferences",
destination: IdentityPreferencesView(viewModel: viewModel.identityPreferencesViewModel()))
"secondary-navigation.preferences",
destination: PreferencesView(
viewModel: viewModel.preferencesViewModel()))
}
}
.navigationItems(presentationMode: presentationMode)