-
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 (#2731)
<!-- Thank you for submitting a Pull Request and helping to improve Home Assistant. Please complete the following sections to help the processing and review of your changes. Please do not delete anything from this template. --> ## Summary <!-- Provide a brief summary of the changes you have made and most importantly what they aim to achieve --> ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> <img width="1411" alt="Screenshot 2024-04-22 at 10 54 56" src="https://github.com/home-assistant/iOS/assets/5808343/f4cd2b88-1c32-4fb7-95bc-bf9bf60a1d88"> ## Link to pull request in Documentation repository <!-- Pull requests that add, change or remove functionality must have a corresponding pull request in the Companion App Documentation repository (https://github.com/home-assistant/companion.home-assistant). Please add the number of this pull request after the "#" --> Documentation: home-assistant/companion.home-assistant# ## Any other notes <!-- If there is any other information of note, like if this Pull Request is part of a bigger change, please include it here. -->
- 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