Cotton code
117 lines · 4.4 KB · SwiftRaw
// Cotton Meetings' widget extension: the recording's Live Activity (lock// screen and Dynamic Island: what it is doing, its elapsed time, Pause or// Resume, Mark, Stop) and the control that starts a recording from Control// Centre, the lock screen or the Action Button. It draws and hands acts to// the app (`CottonMeetingsLive`); it never records and never opens the core.// Every word comes in the Activity's content, resolved by the app from the// kit's lines; the control's title is the intent's own.import ActivityKitimport AppIntentsimport CottonMeetingsLiveimport SwiftUIimport WidgetKit@mainstruct MeetingsWidgets: WidgetBundle {    var body: some Widget {        MeetingRecordingActivity()        MeetingRecordControl()    }}struct MeetingRecordingActivity: Widget {    var body: some WidgetConfiguration {        ActivityConfiguration(for: MeetingActivityAttributes.self) { context in            LockScreenRecording(state: context.state)                .padding(16)                .activityBackgroundTint(Color.black.opacity(0.85))                .activitySystemActionForegroundColor(.white)        } dynamicIsland: { context in            DynamicIsland {                DynamicIslandExpandedRegion(.leading) {                    Image(systemName: context.state.paused ? "pause.circle.fill" : "record.circle")                        .foregroundStyle(.red)                        .font(.title2)                }                DynamicIslandExpandedRegion(.trailing) {                    Elapsed(state: context.state).font(.title3.monospacedDigit())                }                DynamicIslandExpandedRegion(.center) {                    Text(context.state.headline).font(.subheadline).lineLimit(1)                }                DynamicIslandExpandedRegion(.bottom) {                    Controls(state: context.state)                }            } compactLeading: {                Image(systemName: context.state.paused ? "pause.fill" : "record.circle")                    .foregroundStyle(.red)            } compactTrailing: {                Elapsed(state: context.state).monospacedDigit().frame(maxWidth: 52)            } minimal: {                Image(systemName: "record.circle").foregroundStyle(.red)            }        }    }}struct LockScreenRecording: View {    let state: MeetingActivityAttributes.ContentState    var body: some View {        VStack(alignment: .leading, spacing: 10) {            HStack {                Image(systemName: state.paused ? "pause.circle.fill" : "record.circle")                    .foregroundStyle(.red)                Text(state.headline).font(.headline).foregroundStyle(.white).lineLimit(1)                Spacer()                Elapsed(state: state).font(.title2.monospacedDigit()).foregroundStyle(.white)            }            Controls(state: state)        }    }}/// The elapsed time: counting while it records, still while it is paused.struct Elapsed: View {    let state: MeetingActivityAttributes.ContentState    var body: some View {        if state.paused {            Text(state.bankedClock)        } else {            Text(timerInterval: state.clockZero...Date.distantFuture, countsDown: false)        }    }}struct Controls: View {    let state: MeetingActivityAttributes.ContentState    var body: some View {        HStack(spacing: 10) {            if state.paused {                Button(intent: ResumeMeetingRecording()) { Label(state.resume, systemImage: "play.fill") }            } else {                Button(intent: PauseMeetingRecording()) { Label(state.pause, systemImage: "pause.fill") }            }            Button(intent: MarkMeetingMoment()) { Label(state.mark, systemImage: "flag.fill") }            Button(intent: StopMeetingRecording()) { Label(state.stop, systemImage: "stop.fill") }                .tint(.red)        }        .buttonStyle(.bordered)        .font(.footnote)    }}/// Start a meeting from Control Centre, the lock screen or the Action Button.struct MeetingRecordControl: ControlWidget {    var body: some ControlWidgetConfiguration {        StaticControlConfiguration(kind: "com.cottondb.meetings.record") {            ControlWidgetButton(action: StartMeetingRecording()) {                Label(StartMeetingRecording.title, systemImage: "record.circle")            }        }        .displayName(StartMeetingRecording.title)    }}