This repository has been archived by the owner on Nov 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: widget refreshable in iOS 17+ (#142)
- Loading branch information
1 parent
bea7204
commit 15511a3
Showing
11 changed files
with
185 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// ContainerBackground.swift | ||
// HSRPizzaHelper | ||
// | ||
// Created by 戴藏龙 on 2023/9/18. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
extension View { | ||
func myWidgetContainerBackground<V: View>( | ||
withPadding padding: CGFloat, | ||
@ViewBuilder _ content: @escaping () -> V | ||
) | ||
-> some View { | ||
modifier(ContainerBackgroundModifier(padding: padding, background: content)) | ||
} | ||
} | ||
|
||
// MARK: - ContainerBackgroundModifier | ||
|
||
private struct ContainerBackgroundModifier<V: View>: ViewModifier { | ||
let padding: CGFloat | ||
let background: () -> V | ||
|
||
func body(content: Content) -> some View { | ||
if #available(iOS 17, *) { | ||
content.containerBackground(for: .widget) { | ||
background() | ||
} | ||
} else { | ||
content | ||
.padding(padding) | ||
.background { | ||
background() | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Features/Daily Note Widget/App Intent/GeneralWidgetRefreshIntent.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,21 @@ | ||
// | ||
// GeneralWidgetRefreshIntent.swift | ||
// HSRPizzaHelper | ||
// | ||
// Created by 戴藏龙 on 2023/9/18. | ||
// | ||
|
||
import AppIntents | ||
import Foundation | ||
|
||
/// General intent for refresh widget timeline. | ||
/// | ||
/// System will automatically update widget timeline after act an app intent. So this intent need do nothing. | ||
@available(iOSApplicationExtension 16, iOS 16, *) | ||
struct GeneralWidgetRefreshIntent: AppIntent { | ||
static var title: LocalizedStringResource = "Refresh" | ||
|
||
func perform() async throws -> some IntentResult { | ||
.result() | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
Features/Daily Note Widget/Common View/AsWidgetRefreshButton.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,30 @@ | ||
// | ||
// AsWidgetRefreshButton.swift | ||
// HSRPizzaHelper | ||
// | ||
// Created by 戴藏龙 on 2023/9/18. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
extension View { | ||
func asWidgetRefreshButton() -> some View { | ||
modifier(AsWidgetRefreshButton()) | ||
} | ||
} | ||
|
||
// MARK: - AsWidgetRefreshButton | ||
|
||
private struct AsWidgetRefreshButton: ViewModifier { | ||
func body(content: Content) -> some View { | ||
if #available(iOS 17.0, iOSApplicationExtension 17.0, *) { | ||
Button(intent: GeneralWidgetRefreshIntent()) { | ||
content | ||
} | ||
.buttonStyle(.plain) | ||
} else { | ||
content | ||
} | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.