-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add widgets reload button to settings
- Loading branch information
Showing
10 changed files
with
143 additions
and
13 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Foundation | ||
|
||
extension WidgetsSettingsView { | ||
static func build() -> WidgetsSettingsView { | ||
let viewModel = WidgetsSettingsViewModel() | ||
return .init(viewModel: viewModel) | ||
} | ||
} |
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,53 @@ | ||
import Shared | ||
import SwiftUI | ||
|
||
struct WidgetsSettingsView: View { | ||
@StateObject private var viewModel: WidgetsSettingsViewModel | ||
|
||
init(viewModel: WidgetsSettingsViewModel) { | ||
self._viewModel = .init(wrappedValue: viewModel) | ||
} | ||
|
||
var body: some View { | ||
content | ||
.navigationTitle(L10n.Settings.Widgets.title) | ||
.navigationBarTitleDisplayMode(.inline) | ||
} | ||
|
||
private var content: some View { | ||
List { | ||
Button(action: { | ||
viewModel.reloadWidgets() | ||
}, label: { | ||
HStack { | ||
Label(L10n.SettingsDetails.Widgets.ReloadAll.title, systemImage: "square.text.square.fill") | ||
.frame(maxWidth: .infinity, alignment: .leading) | ||
if viewModel.isLoading { | ||
ProgressView() | ||
.progressViewStyle(.circular) | ||
} | ||
} | ||
}) | ||
.listRowSeparator(.hidden) | ||
|
||
// New section to avoid list to not round previous item corners | ||
Section { | ||
Text(L10n.SettingsDetails.Widgets.ReloadAll.description) | ||
.font(.footnote) | ||
.foregroundStyle(Color(uiColor: .secondaryLabel)) | ||
.listRowBackground(Color.clear) | ||
} | ||
} | ||
.modify { | ||
if #available(iOS 17.0, *) { | ||
$0.listSectionSpacing(.leastNonzeroMagnitude) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
NavigationView { | ||
WidgetsSettingsView(viewModel: .init()) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Sources/App/Settings/Widgets/WidgetsSettingsViewModel.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,16 @@ | ||
import Foundation | ||
import WidgetKit | ||
|
||
final class WidgetsSettingsViewModel: ObservableObject { | ||
@Published var isLoading = false | ||
|
||
func reloadWidgets() { | ||
isLoading = true | ||
WidgetCenter.shared.reloadAllTimelines() | ||
|
||
// Delay to give some sense of execution | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in | ||
self?.isLoading = false | ||
} | ||
} | ||
} |
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,13 @@ | ||
import Foundation | ||
import SwiftUI | ||
|
||
public extension View { | ||
@ViewBuilder | ||
func modify(@ViewBuilder _ transform: (Self) -> (some View)?) -> some View { | ||
if let view = transform(self), !(view is EmptyView) { | ||
view | ||
} else { | ||
self | ||
} | ||
} | ||
} |
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