Refactor sidebar styling to remove duplicate code

This commit is contained in:
Maurice Parker
2020-07-06 01:32:43 -05:00
parent bcbb38c120
commit 6695fcfca7
6 changed files with 87 additions and 60 deletions

View File

@@ -17,15 +17,10 @@ struct SceneNavigationView: View {
var body: some View {
NavigationView {
#if os(macOS)
RegularSidebarContainerView()
SidebarContainerView()
.frame(minWidth: 100, idealWidth: 150, maxHeight: .infinity)
#else
if horizontalSizeClass == .compact {
CompactSidebarContainerView()
} else {
RegularSidebarContainerView()
}
SidebarContainerView()
#endif
#if os(iOS)

View File

@@ -1,37 +0,0 @@
//
// CompactSidebarContainerView.swift
// Multiplatform iOS
//
// Created by Stuart Breckenridge on 29/6/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import SwiftUI
struct CompactSidebarContainerView: View {
@EnvironmentObject private var sceneModel: SceneModel
@StateObject private var sidebarModel = SidebarModel()
@State private var showSettings: Bool = false
var body: some View {
SidebarView()
.modifier(SidebarToolbarModifier())
.environmentObject(sidebarModel)
.navigationBarTitle(Text("Feeds"))
.listStyle(PlainListStyle())
.onAppear {
sceneModel.sidebarModel = sidebarModel
sidebarModel.delegate = sceneModel
sidebarModel.rebuildSidebarItems()
}
}
}
struct CompactSidebarContainerView_Previews: PreviewProvider {
static var previews: some View {
CompactSidebarContainerView()
.environmentObject(SceneModel())
}
}

View File

@@ -1,5 +1,5 @@
//
// RegularSidebarContainerView.swift
// SidebarContainerView.swift
// NetNewsWire
//
// Created by Maurice Parker on 6/28/20.
@@ -8,7 +8,7 @@
import SwiftUI
struct RegularSidebarContainerView: View {
struct SidebarContainerView: View {
@EnvironmentObject private var sceneModel: SceneModel
@StateObject private var sidebarModel = SidebarModel()
@@ -18,9 +18,9 @@ struct RegularSidebarContainerView: View {
@ViewBuilder var body: some View {
SidebarView()
.modifier(SidebarToolbarModifier())
.modifier(SidebarListStyleModifier())
.environmentObject(sidebarModel)
.navigationTitle(Text("Feeds"))
.listStyle(SidebarListStyle())
.onAppear {
sceneModel.sidebarModel = sidebarModel
sidebarModel.delegate = sceneModel
@@ -30,9 +30,9 @@ struct RegularSidebarContainerView: View {
}
struct RegularSidebarContainerView_Previews: PreviewProvider {
struct SidebarContainerView_Previews: PreviewProvider {
static var previews: some View {
RegularSidebarContainerView()
SidebarContainerView()
.environmentObject(SceneModel())
}
}

View File

@@ -0,0 +1,33 @@
//
// SidebarListStyleModifier.swift
// NetNewsWire
//
// Created by Maurice Parker on 7/6/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import SwiftUI
struct SidebarListStyleModifier: ViewModifier {
#if os(iOS)
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
#endif
@ViewBuilder func body(content: Content) -> some View {
#if os(macOS)
content
.listStyle(SidebarListStyle())
#else
if horizontalSizeClass == .compact {
content
.listStyle(PlainListStyle())
} else {
content
.listStyle(SidebarListStyle())
}
#endif
}
}

View File

@@ -0,0 +1,34 @@
//
// SidebarStyleModifier.swift
// NetNewsWire
//
// Created by Maurice Parker on 7/6/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import SwiftUI
struct SidebarListStyleModifier: ViewModifier {
#if os(iOS)
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
#endif
@ViewBuilder func body(content: Content) -> some View {
content
#if os(macOS)
content
.listStyle(SidebarListStyle())
#else
if horizontalSizeClass == .compact {
content
.listStyle(PlainListStyle())
} else {
content
.listStyle(SidebarListStyle())
}
#endif
}
}