Skip to content

お気に入りされたアニメを保存するRealmのクラスを作成 #31

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Colombia/Sources/Model/AnnictAPIModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ struct Work: Decodable {
self.title = try container.decode(String.self, forKey: .title)
self.image = try container.decode(Image.self, forKey: .image)
}

init(id: Int, title: String, image: Image) {
self.id = id
self.title = title
self.image = image
}
}

struct Image: Decodable {
Expand All @@ -41,4 +47,8 @@ struct Image: Decodable {
let container = try decoder.container(keyedBy: Key.self)
self.recommendedUrl = try container.decode(String.self, forKey: .recommendedUrl)
}

init(url: String) {
self.recommendedUrl = url
}
}
32 changes: 30 additions & 2 deletions Colombia/Sources/View/Home/WorksIndexViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import UIKit
import RxSwift
import RxCocoa
import RealmSwift


final class WorksIndexViewController: UIViewController {

var realm: Realm!

private let activityIndicator = UIActivityIndicatorView()

private let repository: AnnictDataRepository
Expand Down Expand Up @@ -58,6 +62,9 @@ final class WorksIndexViewController: UIViewController {
super.viewDidLoad()
setComponent()

realm = try! Realm()


//お気に入りの状態に変更があった時
worksIndexModel.favoriteValueChanged
.subscribe(
Expand All @@ -77,12 +84,30 @@ final class WorksIndexViewController: UIViewController {
let value = favoriteWorks.value + [work]
favoriteWorks.accept(value)
// ② work をRealmに新しく追加する
let newRealmWork = RealmWork()
newRealmWork.id = work.id
newRealmWork.title = work.title
newRealmWork.image = work.image.recommendedUrl ?? "undefined"

try! self.realm.write {
self.realm.add(newRealmWork)
}
}
else {
//お気に入り解除された時
let value = favoriteWorks.value.filter({ $0.id != work.id })
favoriteWorks.accept(value)
// ③work をRealmから削除する
//全部取得
let testRealm = self.realm.objects(RealmWork.self)
//lazyを解消するためにcompactMapしてる
guard let selectedItem = testRealm.filter({ $0.id != work.id }).compactMap({$0}).first else {
return
}

try! self.realm.write {
self.realm.delete(selectedItem)
}
}

if callingVC == .favorite {
Expand All @@ -106,10 +131,13 @@ final class WorksIndexViewController: UIViewController {

// ① Realmからデータを取り出す。(API取得の前に行う⇨そのデータを用いてtrueかfalseか判断できるようにするため)
// Realm(DB)からお気に入りデータを取り出す。
let realmData = self.realm.objects(RealmWork.self)
// Result<AnnictData> -> works [Work]
let favoritesArray: [WorkForDisplay] = realmData.compactMap({
WorkForDisplay.init(id: $0.id, title: $0.title, image: Image(url: $0.image), isFavorited: $0.isFavorite)
})

// favoriteWorksの中にそのデータを入れる。
// worksIndexModel.favoriteWorks.accept(works)
self.worksIndexModel.favoriteWorks.accept(favoritesArray)

//21個のアニメのデータを一覧画面用に取得
fetchAPI()
Expand Down