Skip to content

Commit

Permalink
feat: flesh out settings to a usable state
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomatree committed Sep 30, 2024
1 parent f61ae28 commit 736ea73
Show file tree
Hide file tree
Showing 21 changed files with 568 additions and 139 deletions.
32 changes: 30 additions & 2 deletions Revolt.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
17B7128D2B03E9D700CFF61C /* FriendsList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17B7128C2B03E9D700CFF61C /* FriendsList.swift */; };
17BADE012B7019270021BB62 /* EmojiPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17BADE002B7019270021BB62 /* EmojiPicker.swift */; };
17BB26772C8E923600D70AFC /* SnapKit in Frameworks */ = {isa = PBXBuildFile; productRef = 17BB26762C8E923600D70AFC /* SnapKit */; };
17BB78B52CA4C45C000CAD87 /* ChannelPermissionsSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17BB78B42CA4C45C000CAD87 /* ChannelPermissionsSettings.swift */; };
17BB78B82CA4C64E000CAD87 /* ChannelRolePermissionsSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17BB78B72CA4C64E000CAD87 /* ChannelRolePermissionsSettings.swift */; };
17BB78BA2CA4DF25000CAD87 /* OptionSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17BB78B92CA4DF25000CAD87 /* OptionSet.swift */; };
17BC476B2B9CB51200A593DA /* ShareInviteSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17BC476A2B9CB51200A593DA /* ShareInviteSheet.swift */; };
17BF54CD2B1785E200178866 /* HomeWelcome.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17BF54CC2B1785E200178866 /* HomeWelcome.swift */; };
17BF54D12B17B24200178866 /* LanguageSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17BF54D02B17B24200178866 /* LanguageSettings.swift */; };
Expand Down Expand Up @@ -254,6 +257,9 @@
17B1A0462BFC471C005D4664 /* Types.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Types.swift; sourceTree = "<group>"; };
17B7128C2B03E9D700CFF61C /* FriendsList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FriendsList.swift; sourceTree = "<group>"; };
17BADE002B7019270021BB62 /* EmojiPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiPicker.swift; sourceTree = "<group>"; };
17BB78B42CA4C45C000CAD87 /* ChannelPermissionsSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelPermissionsSettings.swift; sourceTree = "<group>"; };
17BB78B72CA4C64E000CAD87 /* ChannelRolePermissionsSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelRolePermissionsSettings.swift; sourceTree = "<group>"; };
17BB78B92CA4DF25000CAD87 /* OptionSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OptionSet.swift; sourceTree = "<group>"; };
17BBFE102C77949E000A8FB1 /* ci_post_xcodebuild.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = ci_post_xcodebuild.sh; sourceTree = "<group>"; };
17BC476A2B9CB51200A593DA /* ShareInviteSheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareInviteSheet.swift; sourceTree = "<group>"; };
17BF54CC2B1785E200178866 /* HomeWelcome.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeWelcome.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -363,6 +369,7 @@
17A7FE362C49DE6F0083B22F /* Member.swift */,
17A7FE382C49EBD20083B22F /* Image.swift */,
176485722CA3882E00AF8141 /* Binding.swift */,
17BB78B92CA4DF25000CAD87 /* OptionSet.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -445,6 +452,7 @@
children = (
17429D962B4C91170036105A /* ChannelSettings.swift */,
17429D982B4C931D0036105A /* ChannelOverviewSettings.swift */,
17BB78B62CA4C636000CAD87 /* ChannelPermissionsSettings */,
);
path = Channel;
sourceTree = "<group>";
Expand All @@ -462,6 +470,7 @@
175E01DC2ADAB3EA004F6431 /* Components */ = {
isa = PBXGroup;
children = (
17BB78BB2CA4E140000CAD87 /* Settings */,
17BC47692B9CB4F500A593DA /* Sheets */,
17CE783D2B129E0A006C1D2C /* Home */,
178BB1152B02E626001143A4 /* CommonElements */,
Expand All @@ -481,7 +490,6 @@
17BADE002B7019270021BB62 /* EmojiPicker.swift */,
17863A582C8094840051A52C /* Tile.swift */,
17CC0CB32C937D4F005BA3A6 /* ServerBadges.swift */,
176485742CA3947B00AF8141 /* PermissionToggle.swift */,
);
path = Components;
sourceTree = "<group>";
Expand All @@ -491,7 +499,6 @@
children = (
1764856D2CA383BD00AF8141 /* ServerRolesSettings.swift */,
176485702CA3858100AF8141 /* RoleSettings.swift */,
170410962CA478D5002C1445 /* AllPermissionsSettings.swift */,
170410982CA478F6002C1445 /* DefaultRoleSettings.swift */,
);
path = ServerRoles;
Expand Down Expand Up @@ -534,6 +541,24 @@
path = CommonElements;
sourceTree = "<group>";
};
17BB78B62CA4C636000CAD87 /* ChannelPermissionsSettings */ = {
isa = PBXGroup;
children = (
17BB78B42CA4C45C000CAD87 /* ChannelPermissionsSettings.swift */,
17BB78B72CA4C64E000CAD87 /* ChannelRolePermissionsSettings.swift */,
);
path = ChannelPermissionsSettings;
sourceTree = "<group>";
};
17BB78BB2CA4E140000CAD87 /* Settings */ = {
isa = PBXGroup;
children = (
176485742CA3947B00AF8141 /* PermissionToggle.swift */,
170410962CA478D5002C1445 /* AllPermissionsSettings.swift */,
);
path = Settings;
sourceTree = "<group>";
};
17BC47692B9CB4F500A593DA /* Sheets */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -988,6 +1013,7 @@
17E019D72AF1998300AB4663 /* MessageAttachment.swift in Sources */,
DAAA4BFB29F2518900F41E52 /* MessageView.swift in Sources */,
173A336A2C422EF800B8A58E /* DeveloperSettings.swift in Sources */,
17BB78BA2CA4DF25000CAD87 /* OptionSet.swift in Sources */,
1759C39A2B291A75006E6BBE /* MessageContentsView.swift in Sources */,
17E019D12AF14EC000AB4663 /* ServerIcon.swift in Sources */,
176485752CA3947B00AF8141 /* PermissionToggle.swift in Sources */,
Expand All @@ -1006,6 +1032,7 @@
17CE78412B12AA51006C1D2C /* ServerChannelScrollView.swift in Sources */,
17E019C92AF1279100AB4663 /* AppearanceSettings.swift in Sources */,
1782F5E62B08F60B00759D40 /* Discovery.swift in Sources */,
17BB78B52CA4C45C000CAD87 /* ChannelPermissionsSettings.swift in Sources */,
17772C182C30AF83000D1EDA /* AppDelegate.swift in Sources */,
17C444582B06CC5400F97E4A /* ForgotPassword.swift in Sources */,
17CE2E142AE709CE0059FB3A /* UserSheet.swift in Sources */,
Expand All @@ -1015,6 +1042,7 @@
172754C92B399092002223FE /* Contents.swift in Sources */,
170C23CE2C224B460057E399 /* UserSettingsStore.swift in Sources */,
1777DD892ADC3C31003D6C72 /* Markdown.swift in Sources */,
17BB78B82CA4C64E000CAD87 /* ChannelRolePermissionsSettings.swift in Sources */,
17E019D52AF1937400AB4663 /* Theme.swift in Sources */,
1788F96E2C3CA19800A385A8 /* MessageEmbed.swift in Sources */,
175E01E02ADAB554004F6431 /* Avatar.swift in Sources */,
Expand Down
86 changes: 86 additions & 0 deletions Revolt/Components/Settings/AllPermissionsSettings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// AllPermissionsSettings.swift
// Revolt
//
// Created by Angelo on 25/09/2024.
//

import Foundation
import SwiftUI
import Types

struct PermissionSetting: View {
var title: String
var description: String
var value: Permissions

@Binding var permissions: Overwrite

var customBinding: Binding<Bool?> {
Binding {
if permissions.a.contains(value) {
return true
} else if permissions.d.contains(value) {
return false
} else {
return nil
}
} set: {
switch $0 {
case .some(true):
permissions.a.insert(value)
permissions.d.remove(value)
case .some(false):
permissions.d.insert(value)
permissions.a.remove(value)
case .none:
permissions.a.remove(value)
permissions.d.remove(value)
}
}
}

var body: some View {
PermissionToggle(value: customBinding) {
VStack(alignment: .leading) {
Text(title)
Text(description)
.font(.caption)
}
}
}
}


struct AllPermissionSettings: View {
enum RolePermissions {
case role(Binding<Overwrite>)
case defaultRole(Binding<Permissions>)
}

var permissions: RolePermissions
var filter: Permissions = .all

var body: some View {
ForEach(Array(filter.makeIterator()), id: \.self) { perm in
switch permissions {
case .role(let binding):
PermissionSetting(title: perm.name, description: perm.description, value: perm, permissions: binding)

case .defaultRole(let binding):
Toggle(
perm.name,
isOn: Binding {
binding.wrappedValue.contains(perm)
} set: { b in
if b {
binding.wrappedValue.insert(perm)
} else {
binding.wrappedValue.remove(perm)
}
}
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ struct PermissionToggle<Label: View>: View {

Spacer()

Picker("select permission", selection: $value) {
Picker("Select permission", selection: $value) {
Image(systemName: "xmark")
.foregroundStyle(.red)
.foregroundStyle(.red, .red)
.tag(Optional.some(false))

Image(systemName: "square")
.tag(nil as Bool?)

Image(systemName: "checkmark")
.foregroundStyle(.green)
.foregroundStyle(.green, .green)
.tag(Optional.some(true))
}
.tint(value == true ? .green : value == false ? .red : nil)
Expand Down
36 changes: 36 additions & 0 deletions Revolt/Extensions/OptionSet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// OptionSet.swift
// Revolt
//
// Created by Angelo on 26/09/2024.
//

import Foundation

public struct OptionSetIterator<Element: OptionSet>: IteratorProtocol, Sequence where Element.RawValue == Int {
private let value: Element

public init(element: Element) {
self.value = element
}

private lazy var remainingBits = value.rawValue
private var bitMask = 1

public mutating func next() -> Element? {
while remainingBits != 0 {
defer { bitMask = bitMask &* 2 }
if remainingBits & bitMask != 0 {
remainingBits = remainingBits & ~bitMask
return Element(rawValue: bitMask)
}
}
return nil
}
}

extension OptionSet where Self.RawValue == Int {
public func makeIterator() -> OptionSetIterator<Self> {
return OptionSetIterator(element: self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ struct ChannelOverviewSettings: View {

struct ChannelSettingsValues: Equatable {
var icon: Icon
var name: String
var description: String
var nsfw: Bool
}

@State var currentValues: ChannelSettingsValues
Expand All @@ -34,7 +36,9 @@ struct ChannelOverviewSettings: View {
static func fromState(viewState: ViewState, channel c: Binding<Channel>) -> Self {
let settings = ChannelSettingsValues(
icon: .remote(c.wrappedValue.icon),
description: c.wrappedValue.description ?? ""
name: c.wrappedValue.getName(viewState),
description: c.wrappedValue.description ?? "",
nsfw: c.wrappedValue.nsfw
)

return .init(currentValues: settings, channel: c)
Expand Down Expand Up @@ -70,13 +74,23 @@ struct ChannelOverviewSettings: View {

}
.listRowBackground(viewState.theme.background)


Section("Name") {
TextField("Channel Name", text: $currentValues.name)
}
.listRowBackground(viewState.theme.background)

Section("Server Description") {
TextField(text: $currentValues.description) {
Text("Add a topic...")
}
}
.listRowBackground(viewState.theme.background2)

Section("NSFW") {
CheckboxListItem(title: "Set this channel to NSFW", isOn: $currentValues.nsfw)
}
.listRowBackground(viewState.theme.background2)
}
.toolbar {
ToolbarItem(placement: .principal) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// ChannelPermissionsSettings.swift
// Revolt
//
// Created by Angelo on 25/09/2024.
//

import Foundation
import SwiftUI
import Types

struct ChannelPermissionsSettings: View {
@EnvironmentObject var viewState: ViewState

@Binding var server: Server?
@Binding var channel: Channel

var body: some View {
List {
switch channel {
case .saved_messages(let savedMessages):
EmptyView()
case .dm_channel(let dMChannel):
EmptyView()
case .group_dm_channel(var groupDMChannel):
AllPermissionSettings(
permissions: .defaultRole(Binding {
groupDMChannel.permissions ?? .defaultDirectMessages
} set: {
groupDMChannel.permissions = $0
}),
filter: [.readMessageHistory, .sendMessages, .manageMessages, .inviteOthers, .sendEmbeds, .uploadFiles, .masquerade, .react, .manageChannel, .managePermissions]
)
.listRowBackground(viewState.theme.background2)
case .text_channel, .voice_channel:
Section {
ForEach(Array(server!.roles ?? [:]).sorted(by: { a, b in a.value.rank < b.value.rank }), id: \.key) { pair in
let roleColour = pair.value.colour.map { parseCSSColor(currentTheme: viewState.theme, input: $0) } ?? AnyShapeStyle(viewState.theme.foreground)

NavigationLink {
let overwrite = channel.role_permissions?[pair.key] ?? Overwrite(a: .none, d: .none)
ChannelRolePermissionsSettings(server: Binding($server)!, channel: $channel, roleId: pair.key, permissions: .overwrite(overwrite))
.toolbar {
ToolbarItem(placement: .principal) {
Text(verbatim: pair.value.name)
.bold()
.foregroundStyle(roleColour)
}
}
} label: {
Text(verbatim: pair.value.name)
.foregroundStyle(roleColour)
}
}

NavigationLink {
ChannelRolePermissionsSettings(server: Binding($server)!, channel: $channel, roleId: "default", permissions: .overwrite(channel.default_permissions ?? Overwrite(a: .none, d: .none)))
.navigationTitle("Default")
} label: {
Text("Default")
}
}
.listRowBackground(viewState.theme.background2)
}
}
.scrollContentBackground(.hidden)
.background(viewState.theme.background)
.navigationTitle("Permissions")
}
}
Loading

0 comments on commit 736ea73

Please sign in to comment.