UserDefaults to store a button selection from an array - ios

Struggling to figure out how to store the selection of a button here and hoping somebody could help. I am first making a db query to return a list of results, each item has a button that when clicked, it will always show that they clicked it. The way it is currently working, while I'm on the page, I can click to add, which works and writes to the db, and when clicked it changes to the Undo button, which also works the way I want it to, but as soon as I leave this view and come back, it reverts back to the original with the "Add" button. If a user presses Add, i would like it to persist and always show the Undo button when the user comes back to this screen. Any help would be amazing as I'm quite new to this.
Code below:
Main View
struct AdditemView: View {
let movie: Movie
#State var movieitems = [MovieitemsModel]()
#State var movieID = ""
#State var selection = ""
var body: some View {
VStack {
ForEach(movieitems.indices, id: \.self) { i in
HStack {
Text(movieitems[i].name)
Spacer()
if movieitems[i].selected == false {
Button(action: {
self.selection = movieitems[i].name
self.movieitems[i].selected = true
self.yesitem()
}) {
Text("Add")
}
} else {
HStack {
Text("Added!")
Button(action: {
self.selection = movieitems[i].name
self.movieitems[i].selected = false
self.undoitem()
}) {
Text("Undo")
}
}
}
}
Divider()
}
}
.onAppear {
self.movieID = "\(movie.id)"
fetchitems()
}
}
}
func fetchitems() {
let db = Firestore.firestore()
db.collection("movies").document("\(self.movieID)").collection("items").getDocuments { NewQuerySnapshot, error in
guard let documents = NewQuerySnapshot?.documents else {
print("No Documents")
return
}
movieitems = documents.map { (NewQueryDocumentSnapshot) -> MovieitemsModel in
let data = NewQueryDocumentSnapshot.data()
let name = data["name"] as? String ?? ""
let yes = data["yes"] as? Int?
let no = data["no"] as? Int?
return MovieitemsModel(name: name, yes: yes! ?? 0, no: no! ?? 0)
}
}
}
func yesitem() {
let db = Firestore.firestore()
db.collection("movies").document(movieID).collection("items").document("\(self.selection)").updateData([AnyHashable("yes"): FieldValue.increment(Int64(1))])
}
func undoitem() {
let db = Firestore.firestore()
db.collection("movies").document(movieID).collection("items").document("\(self.selection)").updateData([AnyHashable("yes"): FieldValue.increment(Int64(-1))])
}
Items Model
struct MovieitemsModel: Identifiable {
var id: String = UUID().uuidString
var name: String
var yes: Int
var no: Int
var selected:Bool = false
}

One way of doing this is writing another function that saves the movies data back to the database and call it in each button’s action handler.
// Example button
Button(action: {
self.selection = movieitems[i].name
self.movieitems[i].selected = true
self.undoitem()
self.saveItems()
}) {
Text("Undo")
}
// The save method
func saveItems():
// Your Firebase saving code here

Related

Realm Sync Not Updating SwiftUI View After Observe is Fired

I am having issues with Realm, which is not causing my View to update. I have a checkbox in a List and when I click on the checkbox, I want to update the isCompleted to true. This part is working and it updates the isCompleted property in Realm Sync (MongoDB Atlas). The problem is that the view never re-renders again.
Here is code for my TaskCellView which updates the isCompleted.
struct TaskCellView: View {
var task: Task
let realmManager = RealmManager.shared
var body: some View {
let _ = print(Self._printChanges())
HStack {
Image(systemName: task.isCompleted ? "checkmark.square": "square")
.onTapGesture {
try! realmManager.realm.write {
task.isCompleted.toggle()
}
}
Text(task.title)
}
}
}
In my RealmManager I have setupObservers which fires and assigns the new tasks to the tasks property.
private func setupObservers() {
let observableTasks = realm.objects(Task.self)
notificationToken = observableTasks.observe { [weak self] _ in
DispatchQueue.main.async {
print(observableTasks)
self?.tasks = observableTasks
}
}
}
Then in my ContentView I use the following code to send the tasks to the TodoListView.
if let tasks = realmManager.tasksArray {
let _ = print("INSIDE IF")
let _ = print(tasks.count)
TodoListView(tasks: tasks)
}
I have checked and the print lines are getting printed but the body of the TodoListView is never executed.
TodoListView
struct TodoListView: View {
let tasks: [Task]
var pendingTasks: [Task] {
// tasks.where { $0.isCompleted == false }
tasks.filter { !$0.isCompleted }
}
var completedTasks: [Task] {
tasks.filter { $0.isCompleted }
}
var body: some View {
let _ = print(Self._printChanges())
List {
ForEach(Sections.allCases, id: \.self) { section in
Section {
ForEach(section == .pending ? pendingTasks: completedTasks, id: \._id) { task in
TaskCellView(task: task)
}
} header: {
Text(section.rawValue)
}
}
}.listStyle(.plain)
}
}
I know it is a lot of code and but maybe someone can see the problem. So even though the checkbox is checked and the Realm database is updated and observe is fired and the tasks are updated to reflect the isCompleted new states. The TodoListView is never re-rendered.

What is wrong with this simple SwiftUI example? [duplicate]

I am using .sheet view in SwiftUI and I am observing a strange behavior in the execution of the code.
I am having a view SignInView2:
struct SignInView2: View {
#Environment(\.presentationMode) var presentationMode
#State var invitationUrl = URL(string: "www")
#State private var showingSheet = false
var body: some View {
VStack {
Text("Share Screen")
Button(action: {
print("link: \(invitationUrl)") // Here I see the new value assigned from createLink()
self.showingSheet = true
}) {
Text("Share")
}
.sheet(isPresented: $showingSheet) {
let invitationLink = invitationUrl?.absoluteString // Paasing the old value (www)
ActivityView(activityItems: [NSURL(string: invitationLink!)] as [Any], applicationActivities: nil)
}
}
.onAppear() {
createLink()
}
}
}
which calls create a link method when it appears:
extension SignInView2 {
func createLink() {
guard let uid = Auth.auth().currentUser?.uid else {
print("tuk0")
return }
let link = URL(string: "https://www.example.com/?invitedby=\(uid)")
print("tuk1:\(String(describing: link))")
let referralLink = DynamicLinkComponents(link: link!, domainURIPrefix: "https://makeitso.page.link")
print("tuk2:\(String(describing: referralLink))")
referralLink?.iOSParameters = DynamicLinkIOSParameters(bundleID: "com.IVANDOS.ToDoFirebase")
referralLink?.iOSParameters?.minimumAppVersion = "1.0"
referralLink?.iOSParameters?.appStoreID = "13129650"
referralLink?.shorten { (shortURL, warnings, error) in
if let error = error {
print(error.localizedDescription)
return
}
print("tuk4: \(shortURL)")
self.invitationUrl = shortURL!
}
}
}
That method assigns a value to the invitationUrl variable, which is passed to the sheet. Unfortunatelly, when the sheet appears, I don't see the newly assigned variable but I see only "www". Can you explain me how to pass the new value generated from createLink()?
This is known behaviour of sheet since SwiftUI 2.0. Content is created in time of sheet created not in time of showing. So the solution can be either to use .sheet(item:... modifier or passing binding in sheet content view (which is kind of reference to state storage and don't need to be updated).
Here is a demo of possible approach. Prepared with Xcode 12.4.
struct SignInView2: View {
#Environment(\.presentationMode) var presentationMode
#State private var invitationUrl: URL? // by default is absent
var body: some View {
VStack {
Text("Share Screen")
Button(action: {
print("link: \(invitationUrl)")
self.invitationUrl = createLink() // assignment activates sheet
}) {
Text("Share")
}
.sheet(item: $invitationUrl) {
ActivityView(activityItems: [$0] as [Any], applicationActivities: nil)
}
}
}
}
// Needed to be used as sheet item
extension URL: Identifiable {
public var id: String { self.absoluteString }
}

State not updating correctly in SwiftUI

Am trying to implement like and unlike button, to allow a user to like a card and unlike. When a user likes a button the id is saved and the icon changes from line heart to filled heart. I can save the id correctly but the issue is that at times the icon does not switch to filled one to show the user the changes especially after selecting the first one. The subsequent card won't change state but remain the same, while it will add save the id correctly. To be able to see the other card I have to, unlike the first card it cand display both like at the same time. I have tried both Observable and Environmental.
My Class to handle like and unlike
import Foundation
import Disk
class FavouriteRest: ObservableObject {
#Published private var fav = [Favourite]()
init() {
getFav()
}
func getFav(){
if let retrievedFav = try? Disk.retrieve("MyApp/favourite.json", from: .documents, as: [Favourite].self) {
fav = retrievedFav
} else {
print("")
}
}
//Get single data
func singleFave(id: String) -> Bool {
for x in fav {
if id == x.id {
return true
}
return false
}
return false
}
func addFav(favourite: Favourite){
if singleFave(id: favourite.id) == false {
self.fav.append(favourite)
self.saveFave()
}
}
//Remove Fav
func removeFav(_ favourite: Favourite) {
if let index = fav.firstIndex(where: { $0.id == favourite.id }) {
fav.remove(at: index)
saveFave()
}
}
//Save Fav
func saveFave(){
do {
try Disk.save(self.fav, to: .documents, as: "SmartParking/favourite.json")
}
catch let error as NSError {
fatalError("""
Domain: \(error.domain)
Code: \(error.code)
Description: \(error.localizedDescription)
Failure Reason: \(error.localizedFailureReason ?? "")
Suggestions: \(error.localizedRecoverySuggestion ?? "")
""")
}
}
}
Single Card
#EnvironmentObject var favourite:FavouriteRest
HStack(alignment: .top){
VStack(alignment: .leading, spacing: 4){
Text(self.myViewModel.myModel.title)
.font(.title)
.fontWeight(.bold)
Text("Some text")
.foregroundColor(Color("Gray"))
.font(.subheadline)
.fontWeight(.bold)
}
Spacer()
VStack{
self.favourite.singleFave(id: self.myViewModel.myModel.id) ? Heart(image: "suit.heart.fill").foregroundColor(Color.red) : Heart(image: "suit.heart").foregroundColor(Color("Gray"))
}
.onTapGesture {
if self.favourite.singleFave(id: self.myViewModel.myModel.id) {
self.favourite.removeFav(Favourite(id: self.myViewModel.myModel.id))
} else {
self.favourite.addFav(favourite: Favourite(id: self.myViewModel.myModel.id))
}
}
}
I was able to solve the question by moving the code inside the card view and using #States as shown below.
#State private var fav = [Favourite]()
#State var liked = false
VStack{
// Heading
HStack(alignment: .top){
VStack{
self.liked ? Heart(image:"suit.heart.fill").foregroundColor(Color.red) : Heart(image: "suit.heart").foregroundColor(Color("Gray"))
}
.onTapGesture {
if self.liked {
self.removeFav(self.singleFav!)
} else {
let faveID = self.myViewModel.myModel.id
let myFav = Favourite(id:faveID)
self.fav.append(myFav)
self.saveFave()
}
}
}
In the method for fetch and remove, I updated the #State var liked. Everything working as expected now.

Using .environmentObject with a View presented as a sheet causes onReceive not to fire/conflicts with #EnvironmentObject

In my ContentView, I have a button that does a simple sheet present of my SettingsView. There seems to be some conflict with my #EnvironmentObject var iconSettings: IconNames in SettingsView when the view is presented modally where my onReceive function only fires when the view loads the first time and never when the Picker is used.
Looking around for answers related to this, I was only able to find something related to CoreData which wasn't really helpful but I'm sure others have experienced this, so would be great to have something canonical and more general for others to reference.
Thanks!
Button(action: { self.modalDisplayed = true }) {
Assets.gear
}.sheet(isPresented: $modalDisplayed) {
SettingsView(state: self.state, loadCards: self.load)
.environmentObject(IconNames())
}
Then in my SettingsView, I have the following:
struct SettingsView: View {
#ObservedObject var state: AppState
#EnvironmentObject var iconSettings: IconNames
let loadCards: () -> Void
var body: some View {
VStack {
Picker("", selection: $iconSettings.currentIndex) {
ForEach(Publication.allCases, id: \.pubId) {
Text($0.pubName).tag($0.pubId)
}
}
.pickerStyle(SegmentedPickerStyle())
.padding(20)
Spacer()
}
.onReceive([self.iconSettings.currentIndex].publisher.first()) { value in
print(value) // only hits on first load, never on tap
print("")
let index = self.iconSettings.iconNames.firstIndex(of: UIApplication.shared.alternateIconName) ?? 0
if index != value { UIApplication.shared.setAlternateIconName(self.iconSettings.iconNames[value]) { error in
if let error = error {
print(error.localizedDescription)
} else {
print("Success!")
}
}
}
}
}
}
Finally, my IconNames class:
class IconNames: ObservableObject {
var iconNames: [String?] = [nil]
#Published var currentIndex = 0
init() {
getAlternateIconNames()
if let currentIcon = UIApplication.shared.alternateIconName {
self.currentIndex = iconNames.firstIndex(of: currentIcon) ?? 0
}
}
func getAlternateIconNames() {
if let icons = Bundle.main.object(forInfoDictionaryKey: "CFBundleIcons") as? [String: Any],
let alternateIcons = icons["CFBundleAlternateIcons"] as? [String: Any]
{
for (_, value) in alternateIcons{
guard let iconList = value as? Dictionary<String,Any> else{return}
guard let iconFiles = iconList["CFBundleIconFiles"] as? [String]
else{return}
guard let icon = iconFiles.first else{return}
iconNames.append(icon)
}
}
}
}
Well, as provided code snapshot is not testable by copy-paste, so only by code reading, try instead of
.onReceive([self.iconSettings.currentIndex].publisher.first()) { value in
use this one
.onReceive(self.iconSettings.$currentIndex) { value in

SwiftUI pagination for List object

I've implemented a List with a search bar in SwiftUI. Now I want to implement paging for this list. When the user scrolls to the bottom of the list, new elements should be loaded. My problem is, how can I detect that the user scrolled to the end? When this happens I want to load new elements, append them and show them to the user.
My code looks like this:
import Foundation
import SwiftUI
struct MyList: View {
#EnvironmentObject var webService: GetRequestsWebService
#ObservedObject var viewModelMyList: MyListViewModel
#State private var query = ""
var body: some View {
let binding = Binding<String>(
get: { self.query },
set: { self.query = $0; self.textFieldChanged($0) }
)
return NavigationView {
// how to detect here when end of the list is reached by scrolling?
List {
// searchbar here inside the list element
TextField("Search...", text: binding) {
self.fetchResults()
}
ForEach(viewModelMyList.items, id: \.id) { item in
MyRow(itemToProcess: item)
}
}
.navigationBarTitle("Title")
}.onAppear(perform: fetchResults)
}
private func textFieldChanged(_ text: String) {
text.isEmpty ? viewModelMyList.fetchResultsThrottelt(for: nil) : viewModelMyList.fetchResultsThrottelt(for: text)
}
private func fetchResults() {
query.isEmpty ? viewModelMyList.fetchResults(for: nil) : viewModelMyList.fetchResults(for: query)
}
}
Also a little bit special this case, because the list contains the search bar. I would be thankful for any advice because with this :).
As you have already a List with an artificial row for the search bar, you can simply add another view to the list which will trigger another fetch when it appears on screen (using onAppear() as suggested by Josh). By doing this you do not have to do any "complicated" calculations to know whether a row is the last row... the artificial row is always the last one!
I already used this in one of my projects and I've never seen this element on the screen, as the loading was triggered so quickly before it appeared on the screen. (You surely can use a transparent/invisible element, or perhaps even use a spinner ;-))
List {
TextField("Search...", text: binding) {
/* ... */
}
ForEach(viewModelMyList.items, id: \.id) { item in
// ...
}
if self.viewModelMyList.hasMoreRows {
Text("Fetching more...")
.onAppear(perform: {
self.viewModelMyList.fetchMore()
})
}
}
Add a .onAppear() to the MyRow and have it call the viewModel with the item that just appears. You can then check if its equal to the last item in the list or if its n items away from the end of the list and trigger your pagination.
This one worked for me:
You can add pagination with two different approaches to your List: Last item approach and Threshold item approach.
That's way this package adds two functions to RandomAccessCollection:
isLastItem
Use this function to check if the item in the current List item iteration is the last item of your collection.
isThresholdItem
With this function you can find out if the item of the current List item iteration is the item at your defined threshold. Pass an offset (distance to the last item) to the function so the threshold item can be determined.
import SwiftUI
extension RandomAccessCollection where Self.Element: Identifiable {
public func isLastItem<Item: Identifiable>(_ item: Item) -> Bool {
guard !isEmpty else {
return false
}
guard let itemIndex = lastIndex(where: { AnyHashable($0.id) == AnyHashable(item.id) }) else {
return false
}
let distance = self.distance(from: itemIndex, to: endIndex)
return distance == 1
}
public func isThresholdItem<Item: Identifiable>(
offset: Int,
item: Item
) -> Bool {
guard !isEmpty else {
return false
}
guard let itemIndex = lastIndex(where: { AnyHashable($0.id) == AnyHashable(item.id) }) else {
return false
}
let distance = self.distance(from: itemIndex, to: endIndex)
let offset = offset < count ? offset : count - 1
return offset == (distance - 1)
}
}
Examples
Last item approach:
struct ListPaginationExampleView: View {
#State private var items: [String] = Array(0...24).map { "Item \($0)" }
#State private var isLoading: Bool = false
#State private var page: Int = 0
private let pageSize: Int = 25
var body: some View {
NavigationView {
List(items) { item in
VStack(alignment: .leading) {
Text(item)
if self.isLoading && self.items.isLastItem(item) {
Divider()
Text("Loading ...")
.padding(.vertical)
}
}.onAppear {
self.listItemAppears(item)
}
}
.navigationBarTitle("List of items")
.navigationBarItems(trailing: Text("Page index: \(page)"))
}
}
}
extension ListPaginationExampleView {
private func listItemAppears<Item: Identifiable>(_ item: Item) {
if items.isLastItem(item) {
isLoading = true
/*
Simulated async behaviour:
Creates items for the next page and
appends them to the list after a short delay
*/
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
self.page += 1
let moreItems = self.getMoreItems(forPage: self.page, pageSize: self.pageSize)
self.items.append(contentsOf: moreItems)
self.isLoading = false
}
}
}
}
Threshold item approach:
struct ListPaginationThresholdExampleView: View {
#State private var items: [String] = Array(0...24).map { "Item \($0)" }
#State private var isLoading: Bool = false
#State private var page: Int = 0
private let pageSize: Int = 25
private let offset: Int = 10
var body: some View {
NavigationView {
List(items) { item in
VStack(alignment: .leading) {
Text(item)
if self.isLoading && self.items.isLastItem(item) {
Divider()
Text("Loading ...")
.padding(.vertical)
}
}.onAppear {
self.listItemAppears(item)
}
}
.navigationBarTitle("List of items")
.navigationBarItems(trailing: Text("Page index: \(page)"))
}
}
}
extension ListPaginationThresholdExampleView {
private func listItemAppears<Item: Identifiable>(_ item: Item) {
if items.isThresholdItem(offset: offset,
item: item) {
isLoading = true
/*
Simulated async behaviour:
Creates items for the next page and
appends them to the list after a short delay
*/
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
self.page += 1
let moreItems = self.getMoreItems(forPage: self.page, pageSize: self.pageSize)
self.items.append(contentsOf: moreItems)
self.isLoading = false
}
}
}
}
String Extension:
/*
If you want to display an array of strings
in the List view you have to specify a key path,
so each string can be uniquely identified.
With this extension you don't have to do that anymore.
*/
extension String: Identifiable {
public var id: String {
return self
}
}
Christian Elies, code reference

Resources