Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues in final update #3

Open
devhopes-ca opened this issue Nov 24, 2022 · 5 comments
Open

Issues in final update #3

devhopes-ca opened this issue Nov 24, 2022 · 5 comments

Comments

@devhopes-ca
Copy link

After add Google plist for Firebase, the following errors are coming up all in one file: EditProfileViewModel.swift:

  1. Invalid redeclaration of 'ProfilePicture' on line 11
  2. 'ProfilePicture' is ambiguous for type lookup in this context on line 22
  3. Type of expression is ambiguous without more context on line 30
  4. 'ProfilePicture' is ambiguous for type lookup in this context on line 40
  5. 'ProfilePicture' is ambiguous for type lookup in this context on line 77
  6. Type of expression is ambiguous without more context on line 79
  7. Command SwiftCompile failed with a nonzero exit code

Please assist

Attached is the code:

//
// EditProfileViewModel.swift
// tinder-clone
//
// Created by Alejandro Piguave on 27/10/22.
//

import Foundation
import UIKit

struct ProfilePicture: Hashable{
let filename: String?
let picture: UIImage
}

class EditProfileViewModel: NSObject, ObservableObject {

private let firestoreRepository: FirestoreRepository = FirestoreRepository.shared
private let storageRepository: StorageRepository = StorageRepository.shared

@Published var userProfile: FirestoreUser? = nil
@Published var userPictures: [ProfilePicture] = []
@Published var isProfileUpdated: Bool = false

@Published private (set) var isLoading: Bool = true
@Published private (set) var error: String = ""

func fetchProfile() {
    self.isLoading = true
    Task{
        do{
            //Fetch profile information
            let user = try await firestoreRepository.getUserProfile()
            DispatchQueue.main.async {
                self.userProfile = user
            }

            //Fetch pictures
            let pictures = try await storageRepository.getPicturesFromUser(fileNames: user.pictures)
            var profilePictures: [ProfilePicture] = []
            for i in user.pictures.indices {
                profilePictures.append(ProfilePicture(filename: user.pictures[i], picture: pictures[i]))
            }

            let finalProfilePictures = profilePictures
            DispatchQueue.main.async {
                self.userPictures = finalProfilePictures
                self.isLoading = false
            }
        }catch{
            publishError(message: error.localizedDescription)
        }
    }
}

//Update only profile fields
func updateProfile(modified profileFields: [String: Any]) {
    self.isLoading = true
    Task{
        do{
            try await firestoreRepository.updateUserProfile(modified: profileFields)
            
            self.isLoading = false
            self.isProfileUpdated = true
        }catch{
            publishError(message: error.localizedDescription)
        }
    }
}

/**
 Updates the profile pictures

 - Parameter oldPictures: Array of the file names of the pictures that are already uploaded, in their respective order.
 - Parameter newPictures: Array of either: the file name of a picture that is already uploaded or a new picture.
 */
func updateProfile(newPictures: [ProfilePicture], modified profileFields: [String: Any]? = nil){
    self.isLoading = true
    Task{
        let oldPictures = userProfile!.pictures
        //Gets the name of the pictures that are not among the new pictures.
        let filesToDelete = oldPictures.filter({ fileName in
            !newPictures.contains(where: { newPicture in
                if let newPictureFilename = newPicture.filename {
                    return fileName == newPictureFilename
                } else {
                    return false
                    
                }
            })
        })
        
        let picturesToUpload: [UIImage] = newPictures.compactMap({ pictureUpload in
            if pictureUpload.filename == nil{
                return pictureUpload.picture
            } else {
                return nil
            }
        })
        
        do{
            async let deletePictures: () = storageRepository.deleteUserPictures(fileNames: filesToDelete)
            async let uploadPictures = storageRepository.uploadUserPictures(picturesToUpload)
            
            let (_, newFileNames): ((), [String]) = try await (deletePictures, uploadPictures)
            
            var updatedFileNames: [String] = []
            var count: Int = 0
            
            newPictures.forEach({
                if let oldFilename = $0.filename {
                    updatedFileNames.append(oldFilename)
                } else {
                    updatedFileNames.append(newFileNames[count])
                    count += 1
                }
            })
            
            //If more values need to be updated then create a copy, otherwise create an empty dictionary
            var allProfileFields: [String: Any] = profileFields ?? [:]
            allProfileFields["pictures"] = updatedFileNames
            
            try await firestoreRepository.updateUserProfile(modified: allProfileFields)
            self.isLoading = false
            self.isProfileUpdated = true
        }catch{
            publishError(message: error.localizedDescription)
        }
    }
}


private func publishError(message: String) {
    DispatchQueue.main.async {
        self.error = message
        self.isLoading = false
    }
}

}

@devhopes-ca
Copy link
Author

There are more issues coming up, if i can send you the project to see if you can fix it?

@havasu78
Copy link

havasu78 commented Dec 7, 2022

There are more issues coming up, if i can send you the project to see if you can fix it?

devhopes, i cloned this project from github and four swift files were missing from my download: Persistence.swift, UserProfile.swift, CreateProfileView.swift, and EditProfileView.swift. devhopes, did you create those files yourself?

@devhopes-ca
Copy link
Author

devhopes-ca commented Dec 7, 2022 via email

@maceip
Copy link

maceip commented Dec 24, 2022

fixed in #4

@Lxdro
Copy link

Lxdro commented Jul 23, 2024

@devhopes-ca, did you figured out how to run the project? I have some packages issues that I struggle to fix. I added my firebase file but build keep failing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants