SwiftUI - Edit Button causing a strange (and annoying) animation - animation

I recently implemented an Edit Button into my app. However, it created a strange and annoying animation in the View both when it loads (everything comes in from the left), and when I sort the elements. I noticed that if I remove the .animation that is after the .environment it solves this issue, but then everything appears and moves instantly without the .easeInOut look that I wanted to give. How can I apply this animation only to the appearing and disappearing of the sort and delete buttons of the cells of the Form?
If you want to take a look at my problem (since I don't think I was able to explain it correctly) you can look at this video.
The code is this one, ContentView:
import SwiftUI
struct ContentView: View {
#ObservedObject var dm : DataManager
#ObservedObject var vm : ValueModel
#State var showAlertDeleteContact = false
#State var isEditing = false
#State private var editMode = EditMode.inactive
var body: some View {
NavigationView {
Color(UIColor.systemGray6).ignoresSafeArea(.all).overlay(
VStack {
scrollViewFolders
Form {
ForEach(dm.storageValues) { contacts in
NavigationLink(
destination:
//Contact View,
label: {
IconView(dm: dm, vm: contacts)
})
}.onDelete(perform: { indexSet in
self.showAlertDeleteContact = true
self.indexDeleteContact = indexSet
})
.onMove(perform: onMove)
Section {
buttonNewFolder
buttonSort
}
}
}
.navigationBarTitle("Contacts")
.navigationBarItems(trailing: editButton)
.environment(\.editMode, .constant(self.isEditing ? EditMode.active : EditMode.inactive))
.animation(.easeInOut)
//I also tried with this -> .animation(.some(Animation.default))
)
}.alert(isPresented: $showAlertDeleteContact, content: {
alertDeleteContact
})
}
If you want to recreate the project, the DataManager is:
import SwiftUI
import Combine
class DataManager : Equatable, Identifiable, ObservableObject {
static let shared = DataManager()
#Published var storageValues : [ValueModel] = []
typealias StorageValues = [ValueModel]
//The rest of the code
}
And the ValueModel is:
import SwiftUI
import Combine
class ValueModel : Codable, Identifiable, Equatable, ObservableObject, Comparable {
var id = UUID()
var valueName : String
var notes : String?
var expires : Date?
init(valueName: String, notes: String?, expires: Date?) {
self.valueName = valueName
self.notes = notes
self.expires = expires
}
}
Thanks to everyone who will help me!

Related

FileImporter missing Select option

This is driving me crazy for many days now. I am trying to use the .fileImport() modifier in SwiftUI and apparently I am missing something VERY obvious but for the life of me I cannot find a way to have the "Select" option (see screenshots.
This is like the simplest it can get:
import SwiftUI
import UniformTypeIdentifiers
struct DocImporterView: View {
// #Binding var document: ProofOfBugDocument
#State var isPicking: Bool = false
var body: some View {
Button("Pick") {
isPicking.toggle()
}
.fileImporter(
isPresented: $isPicking,
allowedContentTypes: [.item, .folder, .directory],
allowsMultipleSelection: true, //this btw does not enable the select. Instead just starts the Document Picker in selection mode (but folders cannot be selected)
onCompletion: { result in
print("Picked: \(result)")
})
}
}
struct DocImporterView_Previews: PreviewProvider {
static var previews: some View {
DocImporterView()//(document: .constant(ProofOfBugDocument()))
}
}
Any ideas/help appreciated

SwiftUI - ForEach within a Form doesn't update content, even though it gets stored correctly

I looked everywhere for a solution to my problem, but I couldn't find any. There's this question that
is similar, but I think I'm having a different problem here. So, my code (Xcode 12.1, developing for iOS 14.0) looks like this:
import SwiftUI
struct ContentView: View {
#ObservedObject var cm : FolderModel //Which is conformed to Codable, Identifiable, Equatable, ObservableObject
#ObservedObject var dm : DataManager //Which is conformed to Equatable, Identifiable, ObservableObject
#State var pressedFolder = false
#State var valview : ValuesView
NavigationView {
VStack{
ScrollView(.horizontal) {
HStack { ForEach(dm.storageFolders) { foldersForEach in
Button(action: {
valview = ValuesView(cm: foldersForEach, dm: dm)
pressedFolder = true
}, label: {
Text(foldersForEach.folderName)})
}
if pressedFolder == false {
Form {
ForEach(dm.values) { passwordDelForEach in
NavigationLink(//This works correctly)
}
}
} else if pressedFolder == true {
valview //This is the thing that it's not updating when values are added to the folders
}
}
struct ValuesView : View {
#ObservedObject var cm : FolderModel //Which is conformed to Codable, Identifiable, Equatable, ObservableObject
#ObservedObject var dm : DataManager //Which is conformed to Equatable, Identifiable, ObservableObject
var body : some View {
Form {
ForEach (cm.folderValues) { folderValuesForEach in
NavigationLink(//This works correctly)
}
}
}
}
The arrays into the DataManager are all declared like this:
#Published var storage : [StorageModel] = [] {
didSet {
objectWillChange.send()
}
}
typealias Storage = [StorageModel]
If I add anything into the arrays (from another View), data is stored correctly because by opening the .plist file (that the DataManager creates) I can see it gets correctly updated. Plus, every Button that I use has either the func of the DataManager save() (which has objectWillChange.send() within it) or I manually add dm.objectWillChange.send() to the action of the Button.
Despite all this, the things into the ForEach don't update. I only see the things that were there the first time I open the app, and to see the changes I have to close the app and reopen it.
What am I doing wrong?
Thanks to everyone who will answer!
I am seeing the same thing, only as of using the 14.2 simulator. I'm still trying to figure it out, but it seems like views inside of a ForEach are not properly re-rendered on data change.

How to access a variable of an instance of a view in ContentView SwiftUI?

So in ContentView, I've created a view with the following:
ViewName()
I'd like to change a variable in ContentView to the value of a variable in ViewName. I was hoping I could do something like:
ViewName() {
contentViewVariable = self.variableNameInViewNameInstance
}
but that was just kind of a guess as to how to access the value; it didn't work. Any suggestions would be greatly appreciated!
You can use #State and #Binding to achieve that. You should watch these WWDC videos in 2019 to learn more about this.
wwdc 2019 204 - introduction to swiftui
wwdc 2019 216 - swiftui essentials
wwdc 2019 226 - data flow through swiftui
struct ContentView: View {
#State private var variable: String
var body: some View {
ViewName($variable)
}
}
struct ViewName: View {
#Binding var variableInViewName: String
init(variable: Binding<String>) {
_variableInViewName = variable
}
doSomething() {
// updates variableInViewName and also variable in ContentView
self.variableInViewName = newValue
}
}
For whatever reason it is needed technically it is possible to do via callback closure.
Caution: the action in such callback should not lead to refresh sender view, otherwise it would be just either cycle or value lost
Here is a demo of usage & solution. Tested with Xcode 11.4 / iOS 13.4
ViewName { sender in
print("Use value: \(sender.vm.text)")
}
and
struct ViewName: View {
#ObservedObject var vm = ViewNameViewModel()
var callback: ((ViewName) -> Void)? = nil // << declare
var body: some View {
HStack {
TextField("Enter:", text: $vm.text)
}.onReceive(vm.$text) { _ in
if let callback = self.callback {
callback(self) // << demo of usage
}
}
}
}
class ViewNameViewModel: ObservableObject {
#Published var text: String = ""
}

Swifui Realmdatabase Listview crashes

Good evening,
I'm implementing a Realm Database with SwiftUI.
The Database is made of a table containing "Projects" and a table containing "Measures" (relation one-to-many).
The main view displays the project list and "Measureview" displays the measures related to the project selected.
When I select a project, the measures list is displayed and then impossible to go back, the app crashes (simulator and real device).
Xcode points AppDelegate file : Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffedfd5cfd8)
I know that 4/5 months ago, some developers experienced this issue but I suppose that currently Apple fix the problem.
Please find below the relevant code :
Realm part :
import Foundation
import RealmSwift
import Combine
class Project : Object, Identifiable {
#objc dynamic var ProjectName = "" // primary key
#objc dynamic var ProjectCategorie = ""
#objc dynamic var ProjectCommentaire = ""
let Measures = List<Measure>() // one to many
override static func primaryKey() -> String? {
return "ProjectName"
}
}
class Measure : Object, Identifiable {
// #objc dynamic var id_Measure = UUID().uuidString // primary key
#objc dynamic var MeasureName = ""
#objc dynamic var MeasureDetail = ""
#objc dynamic var MeasureResult = ""
override static func primaryKey() -> String? {
return "MeasureName"
}
}
func createProject (_ title:String,_ categorie:String, _ commentaire:String) {
let realm = try! Realm()
let proj = Project()
proj.ProjectName = title
proj.ProjectCategorie = categorie
proj.ProjectCommentaire = commentaire
try! realm.write {
realm.add(proj)
}
}
//****************************************************************
class BindableResults<Element>: ObservableObject where Element: RealmSwift.RealmCollectionValue {
let didChange = PassthroughSubject<Void, Never>()
let results: Results<Element>
private var token: NotificationToken!
init(results: Results<Element>) {
self.results = results
lateInit()
}
func lateInit() {
token = results.observe { _ in
self.didChange.send(())
}
}
deinit {
token.invalidate()
}
}
Contentview :
struct ContentView : View {
#ObservedObject var Proj = BindableResults(results: try! Realm().objects(Project.self))
var body: some View {
NavigationView{
List(Proj.results) { item in
NavigationLink(destination: MeasureView(Pro: item) ){
ContenRowUI(Proj :item)
}
}
}
.navigationBarTitle(Text("Project List"))
}
}
Measure view :
struct MeasureView: View {
var Pro = Project() //= Project()
var body: some View {
NavigationView {
List(Pro.Measures) { item in
Text("Detail: \(item.MeasureDetail)")
}
.navigationBarTitle(Text("Measure"))
}
}
}
Additional information, if I replace Measureview by a simple textview, the behaviour is very weird :
I select a Project, the application shows the textview and goes back automatically to the main list (project list)
If someone could help me, I would be grateful.
Thanks a lot for your support.
Jeff

Published works for single object but not for array of objects

I am trying to make individually moveable objects. I am able to successfully do it for one object but once I place it into an array, the objects are not able to move anymore.
Model:
class SocialStore: ObservableObject {
#Published var socials : [Social]
init(socials: [Social]){
self.socials = socials
}
}
class Social : ObservableObject{
var id: Int
var imageName: String
var companyName: String
#Published var pos: CGPoint
init(id: Int, imageName: String, companyName: String, pos: CGPoint) {
self.id = id
self.imageName = imageName
self.companyName = companyName
self.pos = pos
}
var dragGesture : some Gesture {
DragGesture()
.onChanged { value in
self.pos = value.location
print(self.pos)
}
}
}
Multiple image (images not following drag):
struct ContentView : View {
#ObservedObject var socialObject: SocialStore = SocialStore(socials: testData)
#ObservedObject var images: Social = testData[2]
var body: some View {
VStack {
ForEach(socialObject.socials, id: \.id) { social in
Image(social.imageName)
.position(social.pos)
.gesture(social.dragGesture)
}
}
}
}
Single image (image follow gesture):
struct ContentView : View {
#ObservedObject var socialObject: SocialStore = SocialStore(socials: testData)
#ObservedObject var images: Social = testData[2]
var body: some View {
VStack {
Image(images.imageName)
.position(images.pos)
.gesture(images.dragGesture)
}
}
}
I expect the individual items to be able to move freely . I see that the coordinates are updating but the position of each image is not.
First, a disclaimer: The code below is not meant as a copy-and-paste solution. Its only goal is to help you understand the challenge. There may be more efficient ways of resolving it, so take your time to think of your implementation once you understand the problem.
Why the view does not update?: The #Publisher in SocialStore will only emit an update when the array changes. Since nothing is being added or removed from the array, nothing will happen. Additionally, because the array elements are objects (and not values), when they do change their position, the array remains unaltered, because the reference to the objects remains the same. Remember: Classes create objects, Structs create values.
We need a way of making the store, to emit a change when something in its element changes. In the example below, your store will subscribe to each of its elements bindings. Now, all published updates from your items, will be relayed to your store publisher, and you will obtain the desired result.
import SwiftUI
import Combine
class SocialStore: ObservableObject {
#Published var socials : [Social]
var cancellables = [AnyCancellable]()
init(socials: [Social]){
self.socials = socials
self.socials.forEach({
let c = $0.objectWillChange.sink(receiveValue: { self.objectWillChange.send() })
// Important: You have to keep the returned value allocated,
// otherwise the sink subscription gets cancelled
self.cancellables.append(c)
})
}
}
class Social : ObservableObject{
var id: Int
var imageName: String
var companyName: String
#Published var pos: CGPoint
init(id: Int, imageName: String, companyName: String, pos: CGPoint) {
self.id = id
self.imageName = imageName
self.companyName = companyName
self.pos = pos
}
var dragGesture : some Gesture {
DragGesture()
.onChanged { value in
self.pos = value.location
print(self.pos)
}
}
}
struct ContentView : View {
#ObservedObject var socialObject: SocialStore = SocialStore(socials: testData)
var body: some View {
VStack {
ForEach(socialObject.socials, id: \.id) { social in
Image(social.imageName)
.position(social.pos)
.gesture(social.dragGesture)
}
}
}
}
For those who might find it helpful. This is a more generic approach to #kontiki 's answer.
This way you will not have to be repeating yourself for different model class types.
import Foundation
import Combine
import SwiftUI
class ObservableArray<T>: ObservableObject {
#Published var array:[T] = []
var cancellables = [AnyCancellable]()
init(array: [T]) {
self.array = array
}
func observeChildrenChanges<K>(_ type:K.Type) throws ->ObservableArray<T> where K : ObservableObject{
let array2 = array as! [K]
array2.forEach({
let c = $0.objectWillChange.sink(receiveValue: { _ in self.objectWillChange.send() })
// Important: You have to keep the returned value allocated,
// otherwise the sink subscription gets cancelled
self.cancellables.append(c)
})
return self
}
}
class Social : ObservableObject{
var id: Int
var imageName: String
var companyName: String
#Published var pos: CGPoint
init(id: Int, imageName: String, companyName: String, pos: CGPoint) {
self.id = id
self.imageName = imageName
self.companyName = companyName
self.pos = pos
}
var dragGesture : some Gesture {
DragGesture()
.onChanged { value in
self.pos = value.location
print(self.pos)
}
}
}
struct ContentView : View {
//For observing changes to the array only.
//No need for model class(in this case Social) to conform to ObservabeObject protocol
#ObservedObject var socialObject: ObservableArray<Social> = ObservableArray(array: testData)
//For observing changes to the array and changes inside its children
//Note: The model class(in this case Social) must conform to ObservableObject protocol
#ObservedObject var socialObject: ObservableArray<Social> = try! ObservableArray(array: testData).observeChildrenChanges(Social.self)
var body: some View {
VStack {
ForEach(socialObject.array, id: \.id) { social in
Image(social.imageName)
.position(social.pos)
.gesture(social.dragGesture)
}
}
}
}
There are two ObservableObject types and the one that you are interested in is Combine.ObservableObject. It requires an objectWillChange variable of type ObservableObjectPublisher and it is this that SwiftUI uses to trigger a new rendering. I am not sure what Foundation.ObservableObject is used for but it is confusing.
#Published creates a PassthroughSubject publisher that can be connected to a sink somewhere else but which isn't useful to SwiftUI, except for .onReceive() of course.
You need to implement
let objectWillChange = ObservableObjectPublisher()
in your ObservableObject class

Resources