-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: flesh out settings to a usable state
- Loading branch information
Showing
21 changed files
with
568 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...ages/Channel/Settings/Channel/ChannelPermissionsSettings/ChannelPermissionsSettings.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
Oops, something went wrong.