์๋ฆฌ์ผ |
---|
- Tab Bar๋ฅผ ์ด์ฉํด ๋ฉ์ธ ํ์ด์ง์ ์ ์ฅ ํ์ด์ง๋ฅผ ์ด๋ํ ์ ์์ต๋๋ค.
- ์๋ฒ API๋ฅผ ์ด์ฉํ์ฌ ์ด๋ฏธ์ง๋ฅผ ๋ฐ์์, ๊ฐ๋ณ ์ธ๋ก ๊ธธ์ด์ ๋ ์ด์์์ผ๋ก ๋ํ๋ ๋๋ค.
- ์ํ๋ ์ฌ์ง์ ์ ํํด ๋ฉ๋ชจ์ ํจ๊ป ์ ์ฅํ๋ ๊ธฐ๋ฅ์ ๊ฐ์ต๋๋ค.
- ๋ณ ๋ฒํผ์ ๋๋ฅด๋ฉด ์ ์ฅ์ด ๋๊ณ , ๋ค์ ๋ฉ์ธ์์ ์ฑ์์ ธ ์๋ ๋ณ์ ๋๋ฅด๋ฉด ์ญ์ ๋ฉ๋๋ค.
- ์ ์ฅ๋ ์ฌ์ง์ Long Press๋ฅผ ์ด์ฉํด ์ฌ์ง์ ์ญ์ ํ ์ ์์ต๋๋ค.
- ๊ฐ๋ฐ ๊ธฐ๊ฐ: 2022.07.25 ~ 2022.07.29 (1์ฃผ)
- ์ฌ์ฉ ๊ธฐ์ :
UIKit
,URLSession
,TabBarController
,NSCache
,CoreData
,FileManager
,UICollectionViewFlowLayout
,MVVM
-
์ฌ์ง ์ ์ฅ ์ image data๋ฅผ File Path์ FileManager๋ฅผ ์ด์ฉํด ์ ์ฅ
-
์ฌ์ง ์ญ์ ์ ๊ด๋ จ ๋ฐ์ดํฐ ์ญ์
class PhotoFileManager {
// ...
func createPhotoFile(_ image: UIImage, _ fileName: String) -> URL {
let path = directoryPath.appendingPathComponent(fileName)
if let data = image.jpegData(compressionQuality: 1) ?? image.pngData() {
do {
try data.write(to: path)
} catch let error {
print(error.localizedDescription)
}
}
return path
}
// ...
}
class PhotoFileManager {
// ...
func deletePhotoFile(_ filePath: String) {
guard let url = URL(string: filePath) else { return }
do {
try fileManager.removeItem(at: url)
} catch let error {
delegate?.fileManager(fileManager, error: FileError.canNotCreateDic, desc: error)
}
}
// ...
}
CoreData๋ฅผ ์ด์ฉํ ์ฌ์ง ์ ๋ณด ์ ์ฅ ๋ฐ ์ญ์ ๋ก์ง
class CoreDataManager {
// ...
func createSavePhoto(_ id: String, _ memo: String, _ originUrl: String, _ location: String, _ ratio: Double) {
let entity = NSEntityDescription.entity(forEntityName: "SavePhoto", in: context)
if let entity = entity {
let savePhoto = NSManagedObject(entity: entity, insertInto: context)
savePhoto.setValue(id, forKey: "id")
savePhoto.setValue(memo, forKey: "memo")
savePhoto.setValue(originUrl, forKey: "originUrl")
savePhoto.setValue(location, forKey: "location")
savePhoto.setValue(ratio, forKey: "ratio")
saveContext()
}
}
// ...
}
class CoreDataManager {
// ...
func deleteSavePhoto(_ entity: SavePhoto) {
context.delete(entity)
saveContext()
}
// ...
}
- ๋ฉ์ธ์์์ ์ฌ์ง ์ ์ฅ, ์ญ์
func savePhoto(_ photoData: Photo, _ memo: String) {
let id = photoData.id
let originUrl = photoData.urls.small
let location = PhotoFileManager.shared.createPhotoFile(loadImage(originUrl), id).absoluteString
let ratio = photoData.height / photoData.width
CoreDataManager.shared.createSavePhoto(id, memo, originUrl, location, ratio)
}
func deletePhoto(_ photoData: Photo) {
PhotoFileManager.shared.deletePhotoFile(photoData.urls.small)
guard let entity = CoreDataManager.shared.searchSavePhoto(photoData.id) else { return }
CoreDataManager.shared.deleteSavePhoto(entity)
}
- ์ ์ฅ ํ์ด์ง์์์ ์ฌ์ง ์ญ์
func deleteSavePhoto(_ savePhotoData: SavePhoto) {
guard let location = savePhotoData.location else { return }
PhotoFileManager.shared.deletePhotoFile(location)
CoreDataManager.shared.deleteSavePhoto(savePhotoData)
}
-
searchSavePhoto ์ด์ฉ
-
Key Value๋ฅผ ์ด์ฉํ Dictionary ํ์์ผ๋ก photoSet ๊ตฌ์ฑ
-
์ฌ์ง ์ ์ฅ ์ ๋ฌด์ ๋ฐ๋ฅธ ๋ณ ๋ฒํผ isSelected ์ ๋ฌด ์ฒ๋ฆฌ
-
์ ์ฅ๋ ์ฌ์ง์ผ ๊ฒฝ์ฐ, ์ฐพ์์ delete ์ฒ๋ฆฌ
-
func searchSavePhoto(_ id: String) -> SavePhoto? {
var photoSet: [String: SavePhoto] = [String: SavePhoto]()
let request: NSFetchRequest<SavePhoto> = SavePhoto.fetchRequest()
do {
let photos = try context.fetch(request)
photos.forEach { savePhoto in
if let id = savePhoto.id {
photoSet[id] = savePhoto
}
}
} catch let error {
print(error.localizedDescription)
}
return photoSet[id]
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = imageCollectionView.dequeueReusableCell(cellType: ImageCollectionViewCell.self, indexPath: indexPath)
var isStarButtonSelected: Bool = false
if CoreDataManager.shared.searchSavePhoto(photoList[indexPath.row].id) != nil {
isStarButtonSelected = true
}
// ...
}
func deletePhoto(_ photoData: Photo) {
PhotoFileManager.shared.deletePhotoFile(photoData.urls.small)
guard let entity = CoreDataManager.shared.searchSavePhoto(photoData.id) else { return }
CoreDataManager.shared.deleteSavePhoto(entity)
}
์ฌ์ง ๋ฆฌ์คํธ | ์ฌ์ง ์ ์ฅ ๋ฐ ๋ฉ๋ชจ ์ ๋ ฅ | ์ฌ์ง ์ญ์ | ๋ฉ์ธ์์์ ์ฌ์ง ์ญ์ |
---|---|---|---|