Skip to content

Commit

Permalink
feat: background mode setting 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
mooyoung2309 committed May 29, 2023
1 parent 22b201a commit c1a2d1f
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 64 deletions.
84 changes: 49 additions & 35 deletions taskfolio/DynamicWidget/DynamicWidgetLiveActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,35 @@ import SwiftUI
struct DynamicWidgetAttributes: ActivityAttributes {
public struct ContentState: Codable, Hashable {
// Dynamic stateful properties about your activity go here!
var title: String?
var time: Int?
var time: Int
var isTimerActive: Bool = true
}

// Fixed non-changing properties about your activity go here!
var name: String
var id: UUID
var title: String
var colorType: Int16
}

struct DynamicWidgetLiveActivity: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: DynamicWidgetAttributes.self) { context in
// Lock screen/banner UI goes here
HStack {
Text(context.state.title ?? "")
Divider()
.frame(width: 3, height: 15)
.overlay(ColorType.toDomain(int16: context.attributes.colorType).color)

Text(context.attributes.title)
.font(.title2)

Spacer()

VStack {
Image(systemName: "play.circle")
Image(systemName: context.state.isTimerActive ? "pause.circle" : "play.circle")
.font(.title2)

Text(TimeManager.shared.toString(second: context.state.time ?? 0))
Text(TimeManager.shared.toString(second: context.state.time))
.font(.caption)
}
}
Expand All @@ -47,26 +53,34 @@ struct DynamicWidgetLiveActivity: Widget {
// Expanded UI goes here. Compose the expanded UI through
// various regions, like leading/trailing/center/bottom
DynamicIslandExpandedRegion(.leading) {
Text("Leading")
HStack {
Divider()
.frame(width: 3, height: 15)
.overlay(ColorType.toDomain(int16: context.attributes.colorType).color)

Text(context.attributes.title)
}
}
DynamicIslandExpandedRegion(.trailing) {
Text("Trailing")
Text(TimeManager.shared.toString(second: context.state.time))
}
DynamicIslandExpandedRegion(.bottom) {
Text("Bottom")
// more content

}
} compactLeading: {
Text("L")
HStack {
Divider()
.frame(width: 3, height: 15)
.overlay(ColorType.toDomain(int16: context.attributes.colorType).color)

Text(context.attributes.title)
}
} compactTrailing: {
Text("T")
Text(TimeManager.shared.toString(second: context.state.time))
.font(.caption)
} minimal: {
VStack(alignment: .center) {
Image(systemName: "timer")
// Text("ㅇㅇ")
// .multilineTextAlignment(.center)
// .monospacedDigit()
// .font(.caption2)
}
}
.widgetURL(URL(string: "http://www.apple.com"))
Expand All @@ -75,22 +89,22 @@ struct DynamicWidgetLiveActivity: Widget {
}
}

struct DynamicWidgetLiveActivity_Previews: PreviewProvider {
static let attributes = DynamicWidgetAttributes(name: "Me")
static let contentState = DynamicWidgetAttributes.ContentState(title: "Task", time: 00)
static var previews: some View {
attributes
.previewContext(contentState, viewKind: .dynamicIsland(.compact))
.previewDisplayName("Island Compact")
attributes
.previewContext(contentState, viewKind: .dynamicIsland(.expanded))
.previewDisplayName("Island Expanded")
attributes
.previewContext(contentState, viewKind: .dynamicIsland(.minimal))
.previewDisplayName("Minimal")
attributes
.previewContext(contentState, viewKind: .content)
.previewDisplayName("Notification")
}
}
//struct DynamicWidgetLiveActivity_Previews: PreviewProvider {
// static let attributes = DynamicWidgetAttributes(name: "Me")
// static let contentState = DynamicWidgetAttributes.ContentState(title: "Task", time: 00)
//
// static var previews: some View {
// attributes
// .previewContext(contentState, viewKind: .dynamicIsland(.compact))
// .previewDisplayName("Island Compact")
// attributes
// .previewContext(contentState, viewKind: .dynamicIsland(.expanded))
// .previewDisplayName("Island Expanded")
// attributes
// .previewContext(contentState, viewKind: .dynamicIsland(.minimal))
// .previewDisplayName("Minimal")
// attributes
// .previewContext(contentState, viewKind: .content)
// .previewDisplayName("Notification")
// }
//}
10 changes: 5 additions & 5 deletions taskfolio/taskfolio.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
703D55B82A1F455C003BB6D7 /* SettingStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 703D55B72A1F455C003BB6D7 /* SettingStore.swift */; };
703D55BA2A1F53FE003BB6D7 /* Date+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 703D55B92A1F53FE003BB6D7 /* Date+Extension.swift */; };
703D55BC2A1F57DA003BB6D7 /* TaskClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 703D55BB2A1F57DA003BB6D7 /* TaskClient.swift */; };
703D55C02A1F59C0003BB6D7 /* Color+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 703D55BF2A1F59C0003BB6D7 /* Color+Extension.swift */; };
703D55C22A1F59D6003BB6D7 /* ColorType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 703D55C12A1F59D6003BB6D7 /* ColorType.swift */; };
703D55C42A1F5AC8003BB6D7 /* Array+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 703D55C32A1F5AC8003BB6D7 /* Array+Extension.swift */; };
70A1F6E32A2491ED00F0524A /* TimeManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 70BCDFC22A234909002AE1CC /* TimeManager.swift */; };
70A1F6E42A24A47500F0524A /* ColorType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 703D55C12A1F59D6003BB6D7 /* ColorType.swift */; };
70A1F6E52A24A52B00F0524A /* Array+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 703D55C32A1F5AC8003BB6D7 /* Array+Extension.swift */; };
70BCDFC32A234909002AE1CC /* TimeManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 70BCDFC22A234909002AE1CC /* TimeManager.swift */; };
70BCDFC62A2471D8002AE1CC /* EditTaskStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 70BCDFC52A2471D8002AE1CC /* EditTaskStore.swift */; };
70BCDFC82A2471E1002AE1CC /* EditTaskView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 70BCDFC72A2471E1002AE1CC /* EditTaskView.swift */; };
Expand Down Expand Up @@ -80,7 +81,6 @@
703D55B72A1F455C003BB6D7 /* SettingStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingStore.swift; sourceTree = "<group>"; };
703D55B92A1F53FE003BB6D7 /* Date+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+Extension.swift"; sourceTree = "<group>"; };
703D55BB2A1F57DA003BB6D7 /* TaskClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskClient.swift; sourceTree = "<group>"; };
703D55BF2A1F59C0003BB6D7 /* Color+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Color+Extension.swift"; sourceTree = "<group>"; };
703D55C12A1F59D6003BB6D7 /* ColorType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorType.swift; sourceTree = "<group>"; };
703D55C32A1F5AC8003BB6D7 /* Array+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Array+Extension.swift"; sourceTree = "<group>"; };
70BCDFC22A234909002AE1CC /* TimeManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeManager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -164,6 +164,7 @@
703D55982A1F14F4003BB6D7 /* Shared */ = {
isa = PBXGroup;
children = (
703D55C12A1F59D6003BB6D7 /* ColorType.swift */,
703D55B92A1F53FE003BB6D7 /* Date+Extension.swift */,
703D55C32A1F5AC8003BB6D7 /* Array+Extension.swift */,
70BCDFC22A234909002AE1CC /* TimeManager.swift */,
Expand All @@ -183,8 +184,6 @@
703D559E2A1F15D0003BB6D7 /* Model */ = {
isa = PBXGroup;
children = (
703D55BF2A1F59C0003BB6D7 /* Color+Extension.swift */,
703D55C12A1F59D6003BB6D7 /* ColorType.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -414,8 +413,10 @@
buildActionMask = 2147483647;
files = (
70BCDFFA2A248118002AE1CC /* DynamicWidget.intentdefinition in Sources */,
70A1F6E42A24A47500F0524A /* ColorType.swift in Sources */,
70BCDFF32A248117002AE1CC /* DynamicWidgetLiveActivity.swift in Sources */,
70BCDFF12A248117002AE1CC /* DynamicWidgetBundle.swift in Sources */,
70A1F6E52A24A52B00F0524A /* Array+Extension.swift in Sources */,
70A1F6E32A2491ED00F0524A /* TimeManager.swift in Sources */,
70BCDFF52A248117002AE1CC /* DynamicWidget.swift in Sources */,
);
Expand All @@ -429,7 +430,6 @@
70BCDFC82A2471E1002AE1CC /* EditTaskView.swift in Sources */,
703D55C22A1F59D6003BB6D7 /* ColorType.swift in Sources */,
70BCDFC62A2471D8002AE1CC /* EditTaskStore.swift in Sources */,
703D55C02A1F59C0003BB6D7 /* Color+Extension.swift in Sources */,
70BCDFC32A234909002AE1CC /* TimeManager.swift in Sources */,
703D55B62A1F4557003BB6D7 /* SettingView.swift in Sources */,
703D55AC2A1F4093003BB6D7 /* TaskCellStore.swift in Sources */,
Expand Down
Binary file not shown.
8 changes: 0 additions & 8 deletions taskfolio/taskfolio/Domain/Model/Color+Extension.swift

This file was deleted.

31 changes: 18 additions & 13 deletions taskfolio/taskfolio/Feature/Home/HomeStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ struct HomeStore: ReducerProtocol {
case editTask(EditTaskStore.Action)

//MARK: ActivityKit
case showActivity
case activityRequest(ActivityContent<DynamicWidgetAttributes.ContentState>)
case activityUpdateRequest(ActivityContent<DynamicWidgetAttributes.ContentState>)
case activityRequest(id: UUID, title: String, colorType: Int16)
case activityUpdateRequest(time: Int, isTimerActive: Bool)
case activityUpdateResponse
}

Expand All @@ -94,26 +93,29 @@ struct HomeStore: ReducerProtocol {
case .binding:
return .none

case .showActivity:
return .send(.activityRequest(.init(state: .init(title: "Task", time: 0), staleDate: Calendar.current.date(byAdding: .minute, value: 50, to: Date())!)))
case let .activityRequest(id, title, colorType):
if state.activity?.attributes.id == id { return .none }

case let .activityRequest(content):
let activityAttributes = DynamicWidgetAttributes(name: "not use")
let initialContentState = DynamicWidgetAttributes.ContentState(time: 0)
let activityAttributes = DynamicWidgetAttributes(id: id, title: title, colorType: colorType)

let content = ActivityContent(state: initialContentState, staleDate: Calendar.current.date(byAdding: .hour, value: 7, to: Date())!)
do {
state.activity = try Activity.request(attributes: activityAttributes, content: content)
} catch {
print(error)
}
return .none

case let .activityUpdateRequest(content):
return .task { [activity = state.activity] in
await activity?.update(content)
case let .activityUpdateRequest(time, isTimerActive):
let content = ActivityContent<DynamicWidgetAttributes.ContentState>.init(state: .init(time: time, isTimerActive: isTimerActive), staleDate: nil)
guard let activity = state.activity else { return .none }
return .task { [activity = activity] in
await activity.update(content)
return .activityUpdateResponse
}

case .activityUpdateResponse:
print("[D] activity updated")
return .none

case .addButtonTapped:
Expand Down Expand Up @@ -166,7 +168,7 @@ struct HomeStore: ReducerProtocol {
return .init(id: $0.id, task: $0.task, isTimerActive: $0.isTimerActive)
}
})))),
.send(.activityUpdateRequest(.init(state: .init(title: task?.title, time: Int(task?.time ?? 0)), staleDate: nil)))
.send(.activityUpdateRequest(time: Int(task?.time ?? 0), isTimerActive: state.isTimerActive))
])

case .refresh:
Expand Down Expand Up @@ -209,7 +211,9 @@ struct HomeStore: ReducerProtocol {
case .toggleTimerButtonTapped:
state.timerTaskCellID = nil
state.isTimerActive = false
let task = state.taskListCells.first(where: { $0.id == id })?.task
return .concatenate([
.send(.activityRequest(id: id, title: task?.title ?? "Task", colorType: task?.colorType ?? 0)),
.send(.updateTaskListCells(.init(uniqueElements: state.taskListCells.map({
let isTimerActive = $0.isTimerActive ? false : (id == $0.id)
state.timerTaskCellID = isTimerActive ? id : state.timerTaskCellID
Expand All @@ -222,7 +226,8 @@ struct HomeStore: ReducerProtocol {
await send(.timerTicked)
}
}
.cancellable(id: TimerID.self, cancelInFlight: true)
.cancellable(id: TimerID.self, cancelInFlight: true),
.send(.activityUpdateRequest(time: Int(task?.time ?? 0), isTimerActive: state.isTimerActive))
])
}
case .editTask:
Expand Down
14 changes: 11 additions & 3 deletions taskfolio/taskfolio/Feature/Home/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,21 @@ struct HomeView: View {
Spacer()

HStack {
Button("Start") {
//TODO: User 선택 사항으로 변경
/*
Button(action: {
viewStore.send(.showActivity)
}
}, label: {
Image(systemName: "iphone.badge.play")
.imageScale(.large)
.foregroundColor(Color(.label))
})
.padding(.horizontal)
*/

Spacer()

Button(action:{
Button(action: {
viewStore.send(.addButtonTapped)
}) {
Image(systemName: "square.and.pencil")
Expand Down
1 change: 1 addition & 0 deletions taskfolio/taskfolio/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<dict>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>remote-notification</string>
</array>
</dict>
Expand Down
File renamed without changes.

0 comments on commit c1a2d1f

Please sign in to comment.