Skip to content

Commit

Permalink
organize
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Mar 12, 2024
1 parent 2dff2a8 commit ec8b34c
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 162 deletions.
31 changes: 15 additions & 16 deletions Sources/LiveKitComponents/Buttons/CameraToggleButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,33 @@ import SwiftUI

/// The Camera Toggle Button is a button that toggles the camera on and off.
public struct CameraToggleButton<Label: View, PublishedLabel: View>: View {
@EnvironmentObject var room: Room
@State var isBusy = false
@EnvironmentObject private var _room: Room
@State private var _isBusy = false
private let _label: ComponentBuilder<Label>
private let _publishedLabel: ComponentBuilder<PublishedLabel>

let label: ComponentBuilder<Label>
let publishedLabel: ComponentBuilder<PublishedLabel>

public init(@ViewBuilder label: @escaping ComponentBuilder<Label>, @ViewBuilder published: @escaping ComponentBuilder<PublishedLabel>) {
self.label = label
publishedLabel = published
private var isCameraEnabled: Bool {
_room.localParticipant.isCameraEnabled()
}

var isCameraEnabled: Bool {
room.localParticipant.isCameraEnabled()
public init(@ViewBuilder label: @escaping ComponentBuilder<Label>, @ViewBuilder published: @escaping ComponentBuilder<PublishedLabel>) {
_label = label
_publishedLabel = published
}

public var body: some View {
Button {
Task {
isBusy = true
defer { Task { @MainActor in isBusy = false } }
try await room.localParticipant.setCamera(enabled: !isCameraEnabled)
_isBusy = true
defer { Task { @MainActor in _isBusy = false } }
try await _room.localParticipant.setCamera(enabled: !isCameraEnabled)
}
} label: {
if isCameraEnabled {
publishedLabel()
_publishedLabel()
} else {
label()
_label()
}
}.disabled(isBusy)
}.disabled(_isBusy)
}
}
11 changes: 5 additions & 6 deletions Sources/LiveKitComponents/Buttons/DisconnectRoomButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@ import LiveKit
import SwiftUI

public struct DisconnectRoomButton<Label: View>: View {
@EnvironmentObject var room: Room

let label: ComponentBuilder<Label>
@EnvironmentObject private var _room: Room
private let _label: ComponentBuilder<Label>

public init(@ViewBuilder label: @escaping ComponentBuilder<Label>) {
self.label = label
_label = label
}

public var body: some View {
Button {
Task {
await room.disconnect()
await _room.disconnect()
}
} label: {
label()
_label()
}
}
}
32 changes: 15 additions & 17 deletions Sources/LiveKitComponents/Buttons/MicrophoneToggleButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,33 @@ import LiveKit
import SwiftUI

public struct MicrophoneToggleButton<Label: View, PublishedLabel: View>: View {
@EnvironmentObject var room: Room
@EnvironmentObject private var _room: Room
@State private var _isBusy = false
private let _label: ComponentBuilder<Label>
private let _publishedLabel: ComponentBuilder<PublishedLabel>

@State var isBusy = false

let label: ComponentBuilder<Label>
let publishedLabel: ComponentBuilder<PublishedLabel>

public init(@ViewBuilder label: @escaping ComponentBuilder<Label>, @ViewBuilder published: @escaping ComponentBuilder<PublishedLabel>) {
self.label = label
publishedLabel = published
private var isMicrophoneEnabled: Bool {
_room.localParticipant.isMicrophoneEnabled()
}

var isMicrophoneEnabled: Bool {
room.localParticipant.isMicrophoneEnabled()
public init(@ViewBuilder label: @escaping ComponentBuilder<Label>, @ViewBuilder published: @escaping ComponentBuilder<PublishedLabel>) {
_label = label
_publishedLabel = published
}

public var body: some View {
Button {
Task {
isBusy = true
defer { Task { @MainActor in isBusy = false } }
try await room.localParticipant.setMicrophone(enabled: !isMicrophoneEnabled)
_isBusy = true
defer { Task { @MainActor in _isBusy = false } }
try await _room.localParticipant.setMicrophone(enabled: !isMicrophoneEnabled)
}
} label: {
if isMicrophoneEnabled {
publishedLabel()
_publishedLabel()
} else {
label()
_label()
}
}.disabled(isBusy)
}.disabled(_isBusy)
}
}
28 changes: 15 additions & 13 deletions Sources/LiveKitComponents/ForEach/ForEachParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import SwiftUI
///
/// > Note: References `Room` environment object.
public struct ForEachParticipant<Content: View>: View {
@EnvironmentObject var room: Room
// MARK: - Public types

public enum Filter {
case all
Expand All @@ -31,26 +31,28 @@ public struct ForEachParticipant<Content: View>: View {
case isPublishingAudio
}

@EnvironmentObject private var _room: Room

/// Whether to include the local participant in the enumeration
let includeLocalParticipant: Bool
let filterMode: Filter
let content: ParticipantComponentBuilder<Content>
private let _includeLocalParticipant: Bool
private let _filterMode: Filter
private let _content: ParticipantComponentBuilder<Content>

public init(includeLocalParticipant: Bool = true,
filter: Filter = .all,
@ViewBuilder content: @escaping ParticipantComponentBuilder<Content>)
{
self.includeLocalParticipant = includeLocalParticipant
filterMode = filter
self.content = content
_includeLocalParticipant = includeLocalParticipant
_filterMode = filter
_content = content
}

private func sortedParticipants() -> [Participant] {
private func _sortedParticipants() -> [Participant] {
// Include LocalParticipant or not
let participants: [Participant] = Array(room.allParticipants.values).filter { participant in
let participants: [Participant] = Array(_room.allParticipants.values).filter { participant in
// Filter out LocalParticipant if not required
if !includeLocalParticipant, participant is LocalParticipant { return false }
if case .canPublishVideoOrAudio = filterMode, !participant.permissions.canPublish { return false }
if !_includeLocalParticipant, participant is LocalParticipant { return false }
if case .canPublishVideoOrAudio = _filterMode, !participant.permissions.canPublish { return false }
return true
}

Expand All @@ -62,8 +64,8 @@ public struct ForEachParticipant<Content: View>: View {
}

public var body: some View {
ForEach(sortedParticipants()) { participant in
content(participant)
ForEach(_sortedParticipants()) { participant in
_content(participant)
.environmentObject(participant)
}
}
Expand Down
24 changes: 13 additions & 11 deletions Sources/LiveKitComponents/ForEach/ForEachTrack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,40 @@ import SwiftUI
///
/// > Note: References `Participant` environment object.
public struct ForEachTrack<Content: View>: View {
// MARK: - Public types

public enum Filter {
case all
case video
case audio
}

@EnvironmentObject var participant: Participant
@EnvironmentObject private var _participant: Participant

let filter: Filter
let content: TrackReferenceComponentBuilder<Content>
private let _filter: Filter
private let _content: TrackReferenceComponentBuilder<Content>

public init(filter: Filter = .video,
@ViewBuilder content: @escaping TrackReferenceComponentBuilder<Content>)
{
self.filter = filter
self.content = content
_filter = filter
_content = content
}

private func computedTrackPublications() -> [TrackPublication] {
let trackPublications = Array(participant.trackPublications.values)
switch filter {
private func _computedTrackPublications() -> [TrackPublication] {
let trackPublications = Array(_participant.trackPublications.values)
switch _filter {
case .all: return trackPublications
case .video: return trackPublications.filter { $0.kind == .video }
case .audio: return trackPublications.filter { $0.kind == .audio }
}
}

public var body: some View {
ForEach(computedTrackPublications()) { trackPublication in
let trackReference = TrackReference(participant: participant,
ForEach(_computedTrackPublications()) { trackPublication in
let trackReference = TrackReference(participant: _participant,
publication: trackPublication)
content(trackReference)
_content(trackReference)
.environmentObject(trackReference)
}
}
Expand Down
40 changes: 19 additions & 21 deletions Sources/LiveKitComponents/Layouts/ParticipantLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,28 @@

import SwiftUI

struct ParticipantLayout<Data: RandomAccessCollection, Content: View>: View where Data.Element: Identifiable, Data.Index: Hashable {
@Environment(\.liveKitUIOptions) var ui: UIOptions
public struct ParticipantLayout<Data: RandomAccessCollection, Content: View>: View where Data.Element: Identifiable, Data.Index: Hashable {
private let _data: Data
private let _spacing: CGFloat?
private let _viewBuilder: (Data.Element) -> Content

private let data: Data
private let spacing: CGFloat?
private let viewBuilder: (Data.Element) -> Content

private func data(at index: Int) -> Data.Element {
let dataIndex = data.index(data.startIndex, offsetBy: index)
return data[dataIndex]
private func _data(at index: Int) -> Data.Element {
let dataIndex = _data.index(_data.startIndex, offsetBy: index)
return _data[dataIndex]
}

public init(_ data: Data,
spacing: CGFloat? = nil,
content: @escaping (Data.Element) -> Content)
{
self.data = data
viewBuilder = content
self.spacing = spacing
_data = data
_viewBuilder = content
_spacing = spacing
}

private func computeColumn() -> (columns: [Int], rows: Int) {
let baseCount = Int(ceil(Double(data.count).squareRoot()))
let remainder = data.count % baseCount
private func _computeColumn() -> (columns: [Int], rows: Int) {
let baseCount = Int(ceil(Double(_data.count).squareRoot()))
let remainder = _data.count % baseCount
let firstRowCount = remainder > 0 ? remainder : baseCount
let rows = remainder > 0 ? baseCount : baseCount

Expand All @@ -49,16 +47,16 @@ struct ParticipantLayout<Data: RandomAccessCollection, Content: View>: View wher
return (columns: columns, rows: rows)
}

var body: some View {
if data.count > 0 {
public var body: some View {
if _data.count > 0 {
GeometryReader { _ in
let computed = computeColumn()
VStack(spacing: spacing) {
let computed = _computeColumn()
VStack(spacing: _spacing) {
ForEach(0 ..< computed.rows, id: \.self) { row in
HStack(spacing: spacing) {
HStack(spacing: _spacing) {
ForEach(0 ..< computed.columns[row], id: \.self) { column in
let index = computed.columns.prefix(row).reduce(0, +) + column
if index < data.count {
if index < _data.count {
ZStack(alignment: .center) {
Color.white
Text("\(index)")
Expand Down
12 changes: 6 additions & 6 deletions Sources/LiveKitComponents/Scopes/ComponentsScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ public extension EnvironmentValues {
}

public struct ComponentsScope<Content: View>: View {
var content: () -> Content
let preference: UIOptions
private let _content: () -> Content
private let _options: UIOptions

public init(uiOptions: UIOptions? = nil,
@ViewBuilder _ content: @escaping () -> Content)
{
preference = uiOptions ?? UIOptions()
self.content = content
_options = uiOptions ?? UIOptions()
_content = content
}

public var body: some View {
content()
.environment(\.liveKitUIOptions, preference)
_content()
.environment(\.liveKitUIOptions, _options)
}
}
12 changes: 6 additions & 6 deletions Sources/LiveKitComponents/Scopes/RoomScope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ import LiveKit
import SwiftUI

public struct RoomScope<Content: View>: View {
var content: () -> Content
let room: Room
private let _content: () -> Content
private let _room: Room

public init(room: Room? = nil,
@ViewBuilder _ content: @escaping () -> Content)
{
self.room = room ?? Room()
self.content = content
_room = room ?? Room()
_content = content
}

public var body: some View {
content()
.environmentObject(room)
_content()
.environmentObject(_room)
}
}
18 changes: 9 additions & 9 deletions Sources/LiveKitComponents/Support/HorVGrid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@
import SwiftUI

public struct HorVGrid<Content: View>: View {
let axis: Axis
let spacing: CGFloat?
let content: () -> Content
private let _axis: Axis
private let _spacing: CGFloat?
private let _content: () -> Content

public init(axis: Axis = .horizontal,
spacing: CGFloat? = nil,
@ViewBuilder content: @escaping () -> Content)
{
self.axis = axis
self.spacing = spacing
self.content = content
_axis = axis
_spacing = spacing
_content = content
}

public var body: some View {
Group {
if axis == .vertical {
LazyVGrid(columns: [GridItem(.flexible())], spacing: spacing, content: content)
if _axis == .vertical {
LazyVGrid(columns: [GridItem(.flexible())], spacing: _spacing, content: _content)
} else {
LazyHGrid(rows: [GridItem(.flexible())], spacing: spacing, content: content)
LazyHGrid(rows: [GridItem(.flexible())], spacing: _spacing, content: _content)
}
}
}
Expand Down
Loading

0 comments on commit ec8b34c

Please sign in to comment.