Skip to content

Commit

Permalink
Settings: Prepare sidebar settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimillian committed May 17, 2021
1 parent 4647f76 commit 72d8fda
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 49 deletions.
16 changes: 16 additions & 0 deletions RedditOs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
697E324924E3EDE70006F00F /* CommentVoteView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 697E324824E3EDE70006F00F /* CommentVoteView.swift */; };
697E324B24E3EFCB0006F00F /* CommentActionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 697E324A24E3EFCB0006F00F /* CommentActionsView.swift */; };
697E324D24E3F2900006F00F /* SharingPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 697E324C24E3F2900006F00F /* SharingPicker.swift */; };
69ACD1E126529C05008CC42C /* GeneralTabView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69ACD1E026529C05008CC42C /* GeneralTabView.swift */; };
69ACD1E326529C0D008CC42C /* SidebarTabView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69ACD1E226529C0D008CC42C /* SidebarTabView.swift */; };
69ACD1E52652A025008CC42C /* SidebarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69ACD1E42652A025008CC42C /* SidebarItem.swift */; };
69ACD1E72652A390008CC42C /* Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69ACD1E62652A390008CC42C /* Array.swift */; };
69CCB3EA24E2BEAC003FAAD7 /* SubredditAboutPopoverView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69CCB3E924E2BEAC003FAAD7 /* SubredditAboutPopoverView.swift */; };
69D076C824B9E871001619AC /* Color.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69D076C724B9E871001619AC /* Color.swift */; };
69D459FE264BBB0000A98C6F /* Kingfisher in Frameworks */ = {isa = PBXBuildFile; productRef = 69D459FD264BBB0000A98C6F /* Kingfisher */; };
Expand Down Expand Up @@ -106,6 +110,10 @@
697E324824E3EDE70006F00F /* CommentVoteView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentVoteView.swift; sourceTree = "<group>"; };
697E324A24E3EFCB0006F00F /* CommentActionsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentActionsView.swift; sourceTree = "<group>"; };
697E324C24E3F2900006F00F /* SharingPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SharingPicker.swift; sourceTree = "<group>"; };
69ACD1E026529C05008CC42C /* GeneralTabView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralTabView.swift; sourceTree = "<group>"; };
69ACD1E226529C0D008CC42C /* SidebarTabView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarTabView.swift; sourceTree = "<group>"; };
69ACD1E42652A025008CC42C /* SidebarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarItem.swift; sourceTree = "<group>"; };
69ACD1E62652A390008CC42C /* Array.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Array.swift; sourceTree = "<group>"; };
69CCB3E924E2BEAC003FAAD7 /* SubredditAboutPopoverView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubredditAboutPopoverView.swift; sourceTree = "<group>"; };
69D076C724B9E871001619AC /* Color.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = "<group>"; };
69D8663324E568060052A2B0 /* Route.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Route.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -204,6 +212,8 @@
isa = PBXGroup;
children = (
6924D54C24CDF92A005487CA /* SettingsView.swift */,
69ACD1E026529C05008CC42C /* GeneralTabView.swift */,
69ACD1E226529C0D008CC42C /* SidebarTabView.swift */,
);
path = Settings;
sourceTree = "<group>";
Expand Down Expand Up @@ -267,6 +277,7 @@
children = (
69D076C724B9E871001619AC /* Color.swift */,
693F85D024D0690500224ADB /* NSTextField.swift */,
69ACD1E62652A390008CC42C /* Array.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -343,6 +354,7 @@
69EACF1224B668F200303A16 /* SidebarView.swift */,
694C634E24C0AA6D0017897D /* SidebarSubredditRow.swift */,
9F19B57926505DCF00FBEEDA /* SidebarMultiView.swift */,
69ACD1E42652A025008CC42C /* SidebarItem.swift */,
);
path = Sidebar;
sourceTree = "<group>";
Expand Down Expand Up @@ -467,8 +479,11 @@
6918A8CB24C1FEDC008A74E1 /* FlairView.swift in Sources */,
69D076C824B9E871001619AC /* Color.swift in Sources */,
697E324924E3EDE70006F00F /* CommentVoteView.swift in Sources */,
69ACD1E72652A390008CC42C /* Array.swift in Sources */,
6970A0AE24B74A9D00B11031 /* PostInfoView.swift in Sources */,
69ACD1E126529C05008CC42C /* GeneralTabView.swift in Sources */,
6924D54D24CDF92A005487CA /* SettingsView.swift in Sources */,
69ACD1E326529C0D008CC42C /* SidebarTabView.swift in Sources */,
693BD7732518C4FB00CA5214 /* PostDetailToolbar.swift in Sources */,
6970A0B624B783FE00B11031 /* LinkPresentationRepresentable.swift in Sources */,
6924D54324CDD049005487CA /* UIState.swift in Sources */,
Expand Down Expand Up @@ -510,6 +525,7 @@
6970A0BD24B82E1C00B11031 /* LoadingRow.swift in Sources */,
69222AAA24CD7518009F31B4 /* CommentRow.swift in Sources */,
6970A0B324B77D1200B11031 /* PostVoteView.swift in Sources */,
69ACD1E52652A025008CC42C /* SidebarItem.swift in Sources */,
6970A0C124B88BA200B11031 /* PostViewModel.swift in Sources */,
69222AA524CD6A89009F31B4 /* UserHeaderView.swift in Sources */,
);
Expand Down
1 change: 1 addition & 0 deletions RedditOs/Environements/SettingsKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ struct SettingsKey {
static let subreddit_display_mode = "postDisplayMode"
static let subreddit_defaut_sort_order = "defaultSortOrder"
static let comments_default_sort_order = "defaultCommentsSortOrder"
static let sidebar_enabled_section = "sidebarEnabledSections"
}
21 changes: 21 additions & 0 deletions RedditOs/Extensions/Array.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Foundation

extension Array: RawRepresentable where Element: Codable {
public init?(rawValue: String) {
guard let data = rawValue.data(using: .utf8),
let result = try? JSONDecoder().decode([Element].self, from: data)
else {
return nil
}
self = result
}

public var rawValue: String {
guard let data = try? JSONEncoder().encode(self),
let result = String(data: data, encoding: .utf8)
else {
return "[]"
}
return result
}
}
42 changes: 42 additions & 0 deletions RedditOs/Features/Settings/GeneralTabView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// GeneralTabView.swift
// RedditOs
//
// Created by Thomas Ricouard on 17/05/2021.
//

import SwiftUI

struct GeneralTabView: View {
@AppStorage(SettingsKey.subreddit_display_mode) private var displayMode: SubredditPostRow.DisplayMode = .large
@AppStorage(SettingsKey.subreddit_defaut_sort_order) private var sortOrder: SubredditViewModel.SortOrder = .hot

var body: some View {
Form {
Section(header: Text("Default Subreddit settings")) {

Picker("Display layout style", selection: $displayMode) {
ForEach(SubredditPostRow.DisplayMode.allCases, id: \.self) { mode in
Label(mode.rawValue, systemImage: mode.symbol()).tag(mode)
}
}

Picker(selection: $sortOrder, label: Text("Sort order")) {
ForEach(SubredditViewModel.SortOrder.allCases, id: \.self) { sort in
Text(sort.rawValue.capitalized).tag(sort)
}
}

}
}
.padding()
.frame(width: 400, height: 150)
}
}

struct GeneralTabView_Previews: PreviewProvider {
static var previews: some View {
GeneralTabView()
}
}

48 changes: 11 additions & 37 deletions RedditOs/Features/Settings/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,39 @@
import SwiftUI

struct SettingsView: View {
@AppStorage(SettingsKey.subreddit_display_mode) private var displayMode: SubredditPostRow.DisplayMode = .large
@AppStorage(SettingsKey.subreddit_defaut_sort_order) private var sortOrder: SubredditViewModel.SortOrder = .hot

var body: some View {
TabView {
generalView
GeneralTabView()
.tabItem {
Image(systemName: "gearshape").imageScale(.large)
Text("General")
}
}

Text("Apperance")
SidebarTabView()
.tabItem {
Image(systemName: "textformat.alt").imageScale(.large)
Text("Apperance")
}
Image(systemName: "sidebar.left").imageScale(.large)
Text("Sidebar")
}

Text("Filters")
.tabItem {
Image(systemName: "stop.circle").imageScale(.large)
Text("Filters")
}
}

Text("Search")
.tabItem {
Image(systemName: "magnifyingglass").imageScale(.large)
Text("Search")
}
}

Text("Accounts")
.tabItem {
Image(systemName: "person").imageScale(.large)
Text("Accounts")
}
}.frame(width: 1000, height: 500)
}

private var generalView: some View {
VStack {
Form {
Section(header: Text("Default Subreddit settings")) {

Picker("Display layout style", selection: $displayMode) {
ForEach(SubredditPostRow.DisplayMode.allCases, id: \.self) { mode in
Label(mode.rawValue, systemImage: mode.symbol()).tag(mode)
}
}

Picker(selection: $sortOrder, label: Text("Sort order")) {
ForEach(SubredditViewModel.SortOrder.allCases, id: \.self) { sort in
Text(sort.rawValue.capitalized).tag(sort)
}
}

Image(systemName: "person").imageScale(.large)
Text("Accounts")
}
}
Spacer()
}
.frame(width: 500)
.padding()
}
}

Expand All @@ -76,3 +49,4 @@ struct SettingsView_Previews: PreviewProvider {
SettingsView()
}
}

39 changes: 39 additions & 0 deletions RedditOs/Features/Settings/SidebarTabView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// SidebarTabView.swift
// RedditOs
//
// Created by Thomas Ricouard on 17/05/2021.
//

import SwiftUI

struct SidebarTabView: View {
@State private var items = ["Home", "Favorites", "Subscriptions", "Recently", "Multireddits"]
@State private var enabled = true
var body: some View {
Form {
Section(header: Text("Sidebar items")) {
ForEach(items, id: \.self) { item in
HStack {
Toggle(isOn: $enabled) {
Text(item)
}
}
}
}
}
.padding(20)
.frame(width: 400, height: 300)
}

func move(from source: IndexSet, to destination: Int) {
items.move(fromOffsets: source, toOffset: destination)
}
}

struct SidebarTabView_Previews: PreviewProvider {
static var previews: some View {
SidebarTabView()
}
}

32 changes: 32 additions & 0 deletions RedditOs/Features/Sidebar/SidebarItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// SidebarViewModel.swift
// RedditOs
//
// Created by Thomas Ricouard on 17/05/2021.
//

import Foundation
import SwiftUI

enum SidebarItem: String, CaseIterable, Identifiable {
case home, account, favorites, subscription, multi

var id: String {
rawValue
}

func title() -> String {
switch self {
case .home:
return "Home"
case .account:
return "Account"
case .favorites:
return "Favorites"
case .subscription:
return "Subscriptions"
case .multi:
return "Multireddits"
}
}
}
40 changes: 28 additions & 12 deletions RedditOs/Features/Sidebar/SidebarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ struct SidebarView: View {
@EnvironmentObject private var localData: LocalDataStore
@EnvironmentObject private var currentUser: CurrentUserStore

@AppStorage(SettingsKey.sidebar_enabled_section) var enabledSections = SidebarItem.allCases.map{ $0.rawValue }

@State private var isSearchPopoverPresented = false
@State private var isHovered = false
@State private var isInEditMode = false

var body: some View {
List(selection: $uiState.sidebarSelection) {
mainSection
accountSection
favoritesSection
subscriptionSection
multiSection
ForEach(enabledSections.map{ SidebarItem(rawValue: $0)! }) { section in
makeSection(item: section)
}
}
.listStyle(SidebarListStyle())
.frame(minWidth: 200, idealWidth: 200, maxWidth: 200, maxHeight: .infinity)
Expand All @@ -40,9 +40,25 @@ struct SidebarView: View {
}
}

@ViewBuilder
private func makeSection(item: SidebarItem) -> some View {
switch item {
case .home:
mainSection
case .account:
accountSection
case .favorites:
favoritesSection
case .subscription:
subscriptionSection
case .multi:
multiSection
}
}

private var subscriptionsHeader: some View {
HStack(spacing: 8) {
Text("Subscriptions")
Text(SidebarItem.subscription.title())
if currentUser.isRefreshingSubscriptions {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
Expand All @@ -52,9 +68,9 @@ struct SidebarView: View {
}
}

private var subredditsHeader: some View {
private var favoritesHeader: some View {
HStack(spacing: 8) {
Text("Favorites")
Text(SidebarItem.favorites.title())
if isHovered {
Button {
isSearchPopoverPresented = true
Expand Down Expand Up @@ -82,7 +98,7 @@ struct SidebarView: View {
}

private var mainSection: some View {
Section(header: Text("Home")) {
Section(header: Text(SidebarItem.home.title())) {
NavigationLink(destination: SearchMainContentView(),
isActive: uiState.isSearchActive,
label: {
Expand All @@ -101,7 +117,7 @@ struct SidebarView: View {
}

private var accountSection: some View {
Section(header: Text("Account")) {
Section(header: Text(SidebarItem.account.title())) {
NavigationLink(destination: ProfileView()) {
if let user = currentUser.user {
Label(user.name, systemImage: "person.crop.circle")
Expand All @@ -121,7 +137,7 @@ struct SidebarView: View {
}

private var favoritesSection: some View {
Section(header: subredditsHeader) {
Section(header: favoritesHeader) {
ForEach(localData.favorites) { reddit in
HStack {
SidebarSubredditRow(name: reddit.name,
Expand Down Expand Up @@ -180,7 +196,7 @@ struct SidebarView: View {
@ViewBuilder
private var multiSection: some View {
if currentUser.user != nil && !currentUser.multi.isEmpty {
Section(header: Text("Multireddits")) {
Section(header: Text(SidebarItem.multi.title())) {
ForEach(currentUser.multi) { multi in
SidebarMultiView(multi: multi)
}
Expand Down

0 comments on commit 72d8fda

Please sign in to comment.