Skip to content

Commit

Permalink
위젯 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JustHm committed Jun 29, 2024
1 parent 0843bee commit 3df2c8f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,15 @@
//

import Foundation

import WidgetKit
import SwiftUI

@main
struct widget_hmBundle: WidgetBundle {
var body: some Widget {
// widget_hm()
widget_hmControl()
// widget_hmLiveActivity()
}
}
14 changes: 13 additions & 1 deletion DippingTone/Projects/App/WidgetExtension/Sources/widget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,16 @@
// Created by 안정흠 on 6/29/24.
//

import Foundation
import WidgetKit
import SwiftUI
import ComposableArchitecture
import Presentation

@main
struct widget: WidgetBundle {
var body: some Widget {
widgetControl(store: Store(initialState: ListFeature.State(), reducer: {
ListFeature()
}))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,50 @@
// Created by 안정흠 on 6/29/24.
//

import Foundation
import AppIntents
import SwiftUI
import WidgetKit
import ComposableArchitecture
import Presentation

public struct widgetControl: ControlWidget {
public init() {

}
// @Bindable var store: StoreOf<ListFeature>
//
// public init(store: StoreOf<ListFeature>) {
// self.store = store
// }

static let kind: String = "io.DippingTone.DippingTone"

public var body: some ControlWidgetConfiguration {
StaticControlConfiguration(kind: Self.kind) {
ControlWidgetButton(action: LaunchAppIntent()) {
VStack {

Image(systemName: "pencil.line")
Text("😀").font(.title)
Text("I am so happy").font(.body)
}
}
}
}
}

struct LaunchAppIntent: AppIntent {
static var title: LocalizedStringResource { "Open App" }
@Environment(\.openURL) var openURL

init() {}

func perform() async throws -> some IntentResult {
if let url = URL(string: "myapp://") { //myapp://open
DispatchQueue.main.async {
openURL(url)
}
}
return .result()
}
}
2 changes: 1 addition & 1 deletion DippingTone/Projects/App/widget/widgetBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
@main
struct widgetBundle: WidgetBundle {
var body: some Widget {
widget()
// widget()
widgetControl()
}
}
64 changes: 34 additions & 30 deletions DippingTone/Projects/App/widget/widgetControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,54 @@
import AppIntents
import SwiftUI
import WidgetKit
import Presentation
import ComposableArchitecture
import Networkings
import Model
import SwiftData

struct widgetControl: ControlWidget {
static let kind: String = "io.DippingTone.DippingTone.widget"

static let kind: String = "io.DippingTone.DippingTone"
@Environment(\.modelContext) var context

var body: some ControlWidgetConfiguration {
StaticControlConfiguration(
kind: Self.kind,
provider: Provider()
) { value in
ControlWidgetToggle(
"Start Timer",
isOn: value,
action: StartTimerIntent(),
valueLabel: { isRunning in
Label(isRunning ? "On" : "Off", systemImage: "timer")
StaticControlConfiguration(kind: Self.kind) {
ControlWidgetButton(action: LaunchAppIntent()) {
VStack {
Image(systemName: "pencil.line")
Text(fetchRecentlyData()).font(.title)
// Text("").font(.body)
}
)
}
}
.displayName("Timer")
.description("A an example control that runs a timer.")
}
}

extension widgetControl {
struct Provider: ControlValueProvider {
var previewValue: Bool {
false
}

func currentValue() async throws -> Bool {
let isRunning = true // Check if the timer is running
return isRunning
func fetchRecentlyData() -> String {
do {
let descriptor = FetchDescriptor<LIstModel>()
let temp = try context.fetch(descriptor)
return temp.first!.title!
} catch {
return ""
}
}
}

struct StartTimerIntent: SetValueIntent {
static var title: LocalizedStringResource { "Start a timer" }

@Parameter(title: "Timer is running")
var value: Bool

struct LaunchAppIntent: AppIntent {
static var title: LocalizedStringResource { "Open App" }
@Environment(\.openURL) var openURL

init() {}
func perform() async throws -> some IntentResult {
// Start / stop the timer based on `value`.
if let url = URL(string: "myapp://") { //myapp://open
DispatchQueue.main.async {
openURL(url)
}
}
return .result()
}
}

0 comments on commit 3df2c8f

Please sign in to comment.