Invoke PhotoPicker Programmatically? - swiftui

The SwiftUI PhotoPicker is great for creating a button/label to press & then show a Photo Picker when the label is pressed. However, I'd like to invoke a photo picker not after the Picker's label is pressed, but after a conditional test has passed.
For example, if the user clicks on a button that would invoke a Photo Picker, I'd like to first check to see if the record the image will be attached to has been saved. If the record has been saved, I want to launch the picker. If it hasn't been saved, I'll show an alert asking if they want to save or cancel. If they select save, I'll save the record, THEN I'd like to invoke the photo picker automatically.
So can I invoke the Picker programmatically rather than have the user click it? Thanks for advice!

From iOS 16 you can do this by using the photosPicker(isPresented:
struct DemoView: View {
#ObservedObject var viewModel: DemoViewModel
var body: some View {
VStack {
Text("Demo Project")
}
.photosPicker(isPresented: $viewModel.shouldPresentPhotoPicker, selection: $viewModel.selectedPickerItem)
}
}
class DemoViewModel: ObservableObject {
#Published var shouldPresentPhotoPicker = false
#Published var selectedPickerItem: PhotosPickerItem?
func saveTheRecord() {
/// Make an async call, and wait
shouldPresentPhotoPicker = true // Shows the Picker
}
}

Related

Swiftui: How to Close one Tab in a TabView?

I'm learning SwiftUI and having trouble with closing Each Tab View Element.
My App shows photos from user's album by TabView with pageViewStyle one by one.
And What I want to make is user can click save button in each view, and when button is clicked, save that photo and close only that view while other photos are still displayed. So unless all photos are saved or discarded, if user clicks save button, TabView should automatically move to another one.
However, I don't know how to close only one Tab Element. I've tried to use dismiss() and dynamically changing vm.images element. Latter one actually works, but it displays awkward movement and it also requires quite messy code. How could I solve this issue?
Here is my code.
TabView {
ForEach(vm.images, id: \.self) { image in
TestView(image: image)
}
}
.tabViewStyle(.page(indexDisplayMode: .never))
struct TestView: View {
#ObservedObject var vm: TestviewModel
...
var body: some View {
VStack(spacing: 10) {
Image(...)
Spacer()
Button {
...
} label: {
Text("Save")
}
}
You need actually to remove saved image from the viewModel container, and UI will be updated automatically
literally
Button {
vm.images.removeAll { $0.id == image.id } // << here !!
} label: {
Text("Save")
}
You need to use the selection initializer of TabView in order to control what it displays. So replace TabView with:
TabView(selection: $selection)
Than add a new property: #State var selection: YourIdType = someDefaultValue, and in the Button action you set selection to whatever you want to display.
Also add .tag(TheIdTheViewWillUse) remember that whatever Id you use must be the same as your selection variable. I recommend you use Int for the simple use.

How can I push a detailpage and remove all previous navigationlinks in swiftUI?

I have an app where there are multiple screens with textfields to create some new object. When the user selects "Create" on the last screen, an API call is performed which creates the new object.
From there I want to push the detail page of the newly created object, and (when the view is no longer visible) remove all the screens with textfields (as that is no longer relevant, and would only cause confusion. Luckily there is only one screen that should remain before the detailpage.
In UIKit, this would be performed by doing a push on the navigationController, and then editing the viewControllers array of the navigationController in the viewDidLoad of the new screen.
If I am correct, there is no way to edit the views in a SwiftUI NavigationView, so how can I perform this action in SwiftUI?
I solved it by adding an id to the NavigationView, and then setting this to another id in the viewmodel when it should reset.
Like this:
struct MyView: View {
#StateObject var viewModel = ViewModel()
var body: some View {
NavigationView {
// ...
}
.id(viewModel.id)
}
}
In the viewmodel:
class ViewModel: ObservableObject {
#Published var id = UUID().uuidString
func reset() {
id = UUID().uuidString
}
}
This resets the NavigationView as Swift thinks it's a different view because the id changed.

NavigationLink doesn't fire after FullScreenCover is dismissed

I have a button in a view (inside a NavigationView) that opens a full screen cover - a loading screen while some data is processing. When the cover is dismissed, I want to automatically route to the next view programmatically. I'm using a NavigationLink with a tag and selection binding, and the binding value updates when the cover is dismissed, but the routing doesn't happen unless I tap that same "open modal" button again.
import SwiftUI
struct OpenerView: View {
#EnvironmentObject var viewModel: OpenerViewModel
#State private var selection: Int? = nil
#State private var presentLoadingScreen = false
var body: some View {
VStack {
NavigationLink(destination: SecondScreen(), tag: 1, selection: $selection) { EmptyView() }
Button(action: {
viewModel.frequency = 0
self.presentLoadingScreen.toggle()
}, label: {
Text("Open cover")
}).buttonStyle(PlainButtonStyle())
}
.navigationBarTitle("Nav title", displayMode: .inline)
.fullScreenCover(isPresented: $presentLoadingScreen, onDismiss: {
self.selection = 1
}, content: ModalView.init)
}
}
struct ModalView: View {
#Environment(\.presentationMode) var presentationMode
var body: some View {
Text("Howdy")
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
presentationMode.wrappedValue.dismiss()
}
}
}
}
The first time I hit the Button, the cover opens. Inside the cover is just a DispatchQueue.main.asyncAfter which dismisses it after 2 seconds. When it's dismissed, the onDismiss fires, but then I have to hit the button again to route to SecondScreen.
Any ideas?
Edit: Added the modal's View
I got it working with some changes to the code, and I'm sharing here along with what I think is happening.
I believe the problem is a race condition using a #State boolean to toggle the cover and the navigation. When the cover is being dismissed, my main OpenerView is being recreated - to be expected with state changes. Because of this, I try to set the #State var selection to trigger the navigation change, but before it can do so, the view is recreated with selection = nil.
There seem to be two ways to solve it in my case:
Move the cover boolean to my view model - this worked, but I didn't want it there because it only applied to this view and it's a shared view model for this user flow. Plus, when the modal is dismissed, you see the current OpenerView for a brief flash and then get routed to the SecondScreen.
Keep the cover boolean in #State, but trigger the navigation change in the button immediately after setting the boolean to open the modal. This worked better for my use case because the modal opens, and when it closes, the user is already on the next screen.
I had a similar problem where I was trying to draw a view after dismissing a fullScreenCover. I kept getting an error that said that the view had been deallocated since it was trying to draw to the fullScreenCover.
I used Joe's hints above to make this work. Specifically:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
viewToShow()
}
I had previously tried onChange, onDisappear, onAppear - but none of those fit the use case I needed.

SwiftUI Is it possible to use Toggle to update value of ObservableObject

I have a date selector in a view and, once the user enters a date and saves I display a new view with a toggle. Ideally, once the users flips the toggle I'd like to be able to set a reminder using the date field already entered.
I have created an ObservableObject
import SwiftUI
import Combine
class UpdateVM: ObservableObject{
#Published var reminderDate = Date() {didSet {
print("set")
}
which I declare in the View as:
#ObservedObject var updateVM = UpdateVM()
if(self.isToggle){
updateVM.reminderDate = flossTheCat.reminderDate!
}
I get an error "Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols"
This works fine in the action area of a button press but I don't see if it's possible to react to a toggle flip - is the toggle just designed to reflect UI changes and should I implement a button instead ? Definitely having a hard time adjusting to the SwiftUI paradigm even if it makes sense overall
Thanks !
per the request - here is where it does work as I hoped it would (via a button)
trailing: Button(action: {
do {
let flossingReminders = FlossingPets.init(context: self.context)
self.flossingVM.reminderDate = self.flossingDate
if self.context.hasChanges {
try self.context.save()
}
}catch {
print(error)
}

UIPicker in Swift UI

When I add picker in form it shows me correctly to navigate on another page.But after selecting on one row it come back to previous screen and I am not able to click on it again.
I had same problem with button. It's not perfect solution, but it worked for me.
You can try add UUID as #State to your picker, and chang it when action is triggered
Like this:
#State private var pickerID = UUID()
...
Picker {...}.id(self.pickerID)
...
youAction {
.....
self.pickerID = UUID()
}