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

๐Ÿ“ฎ pet ์ •๋ณด ๋ฆฌ์ŠคํŠธ api ์—ฐ๋™ #71

Merged
merged 8 commits into from
Jan 20, 2024

Conversation

heejinnn
Copy link
Collaborator

@heejinnn heejinnn commented Jan 19, 2024

์ž‘์—… ์ด์œ 

  • pet info ๋ฆฌ์ŠคํŠธ api ์—ฐ๋™
  • pet care ๋ฆฌ์ŠคํŠธ api ์—ฐ๋™
  • api๋กœ ๋ฐ›์€ ๋ฐ์ดํ„ฐ ๊ฐ’ ์ ์šฉ
  • ui ์ˆ˜์ •

์ž‘์—… ์‚ฌํ•ญ

  • ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ
    => PetDataManager.swift
struct PetDataManager {
    static var pets: [Pet] = []
    static var careCategories: [CareCategory] = []
    static var cares: [Care] = []

    static func updatePets(with data: Data) {
        do {
            let decoder = JSONDecoder()
            let petInfoResponse = try decoder.decode(PetInfoResponse.self, from: data)

            if let newPets = petInfoResponse.data?.pets {
                pets = newPets
            }
        } catch {
            print("Error updating pet data: \(error)")
        }
    }
    
    
    static func updateCareInfo(with data: Data) {
        do {
            let decoder = JSONDecoder()
            let careInfoResponse = try decoder.decode(CareInfoResponse.self, from: data)
            
            if let newCareCategories = careInfoResponse.data?.careCategories {
                careCategories = newCareCategories
                cares = newCareCategories.flatMap { $0.cares }
            }
        } catch {
            print("Error updating care info: \(error)")
        }
    }

}

1๏ธโƒฃ pet info ๋ฆฌ์ŠคํŠธ api ์—ฐ๋™

  • GET /api/v2/users/{user_id}/pets
  • AuthorizationAlamofire.userPetInfoList

2๏ธโƒฃ pet care ๋ฆฌ์ŠคํŠธ api ์—ฐ๋™

  • GET /api/v2/users/{user_id}/pets/{pet_id}/cares
    -AuthorizationAlamofire.userPetCareInfoList({pet_id})

๐Ÿ“ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ

AuthorizationAlamofire.shared.userPetInfoList { result in
    switch result {
    case .success(let data):
        if let responseData = data {
            PetDataManager.updatePets(with: responseData)
            
            let dispatchGroup = DispatchGroup() //๋น„๋™๊ธฐ์  ์ฒ˜๋ฆฌ
            self.petListCollectionView.reloadData()

            for (index, pet) in PetDataManager.pets.enumerated() {
                dispatchGroup.enter()
                
                AuthorizationAlamofire.shared.userPetCareInfoList(pet.id) { careInfoResult in
                    defer {
                        dispatchGroup.leave()
                    }
                    
                    switch careInfoResult {
                    case .success(let careInfoData):
                        if let responseData = careInfoData {
                            PetDataManager.updateCareInfo(with: responseData)
                            if let cell = self.petListCollectionView.cellForItem(at: IndexPath(item: index, section: 0)) as? PetCollectionViewCell {
                                cell.petCareSubview.updateCareCategories(PetDataManager.careCategories)
                            }
                        }
                        
                    case .failure(let careInfoError):
                        print("Error fetching pet care info for pet \(pet.id): \(careInfoError)")
                    }
                }
            }
            dispatchGroup.notify(queue: .main) {
                print("All pet care info requests completed.")
            }
        }
        
    case .failure(let profileError):
        print("Error fetching user profile info: \(profileError)")
    }
}

โœ๏ธ ์ค‘์ฒฉ๋œ view๋‚ด์—์„œ ์ฒ˜๋ฆฌ

  1. PetVC
  2. petListCollectionView
    (3. 1) PetProfileView
    (3. 2) PetCareListView -> careCategoryListTableView -> careCategoryListTableView

์œ„์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ui๊ฐ€ ์—„์ฒญ ๋ณต์žกํ–ˆ๋Š”๋ฐ PetVC ์•ˆ์— ์žˆ๋Š”, petListCollectionView ์•ˆ์— ์žˆ๋Š”, PetCareListView ์•ˆ์— ์žˆ๋Š”, careCategoryListTableView์˜ cell ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ ํ–ˆ๋‹ค.


๋‹ค์Œ๊ณผ ๊ฐ™์ด petListCollectionView์˜ cell์˜ index ๊ฐ’์„ ์ฐพ์•„ PetCareListView์˜ updateCareCategories()๋กœ category๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

if let cell = self.petListCollectionView.cellForItem(at: IndexPath(item: index, section: 0)) as? PetCollectionViewCell {
    cell.petCareSubview.updateCareCategories(PetDataManager.careCategories)
}

์ด์Šˆ ์—ฐ๊ฒฐ

close #70

@heejinnn heejinnn self-assigned this Jan 19, 2024
Copy link
Member

@psychology50 psychology50 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ตฟ, ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.

@psychology50 psychology50 merged commit 4f342d6 into develop Jan 20, 2024
1 check failed
@psychology50 psychology50 deleted the feat/70 branch January 20, 2024 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants