Skip to content
Merged
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
4 changes: 1 addition & 3 deletions PickaView/Views/Home/HomeViewController+SearchBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ extension HomeViewController: UISearchBarDelegate {

//검색바 클릭 됐을때 태그와 맞는 비디오 가져옴
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print("🔍 검색 버튼 클릭됨")

searchBar.resignFirstResponder()

guard let keyword = searchBar.text, !keyword.isEmpty else {
Expand All @@ -84,8 +84,6 @@ extension HomeViewController: UISearchBarDelegate {

//태그가 필터링 되었고 필터된 비디오목록이 있을때만 동작
func textFieldShouldClear(_ textField: UITextField) -> Bool {
print("❌ 클리어 버튼 눌림")
print("isTagSearchActive:", isTagSearchActive)

if isTagSearchActive {

Expand Down
5 changes: 2 additions & 3 deletions PickaView/Views/Home/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,11 @@ class HomeViewController: UIViewController, ScrollToTopCapable {
self.tags = viewModel?.allTags ?? []
self.filteredTags = self.tags
self.tableView.reloadData()
print("로드된 태그 개수: \(self.tags.count)")
}
}

Task {
guard let viewModel = viewModel else {
print("viewModel이 아직 초기화되지 않았습니다.")
return
}

Expand Down Expand Up @@ -142,7 +140,8 @@ class HomeViewController: UIViewController, ScrollToTopCapable {
self.collectionView.collectionViewLayout.invalidateLayout()//화면 회전시 셀 크기와 배치 다시 계산
}, completion: nil)
}


//새로고침 기능을 설정하는 함수
private func setupPullToRefresh() {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
Expand Down
19 changes: 10 additions & 9 deletions PickaView/Views/Home/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,30 @@ final class HomeViewModel {
let videos = try await pixabayVideoService.fetchVideos(query: query)
await MainActor.run {
// Core Data에 비디오 데이터 저장
print("Saving videos to Core Data")
self.coreDataManager.saveVideos(videos)
print("Saved videos successfully")
}
} catch {
// 네트워크 요청 실패 시 에러 출력
print("Failed to fetch videos: \(error.localizedDescription)")
} catch {
fatalError("비디오를 가져오거나 저장하는 데 실패했습니다: \(error.localizedDescription)")
}

}

// 모든 비디오를 Core Data에서 불러와 추천 점수 기준으로 정렬하고,
// 현재 페이지를 첫 페이지(1)로 초기화하는 함수
func refreshVideos() {
let allVideos = coreDataManager.fetch()
self.allRecommendedVideos = VideoRecommender.sortVideosByRecommendationScore(from: allVideos)
self.currentPage = 1
}

// 현재 페이지에 해당하는 비디오 배열을 반환하는 함수
func getCurrentPageVideos() -> [Video] {
let offset = (currentPage - 1) * limit
let end = min(offset + limit, allRecommendedVideos.count)
guard offset < end else { return [] }
return Array(allRecommendedVideos[offset..<end])
}

// 다음 페이지로 이동 후, 해당 페이지에 맞는 비디오 배열을 반환하는 함수
func loadNextPage() -> [Video] {
currentPage += 1
return getCurrentPageVideos()
Expand All @@ -64,8 +64,8 @@ final class HomeViewModel {
self.allTags = tags
}
}
//실시간 태그목록 갱신용

//실시간 태그목록 갱신용
func filterTags(keyword: String) -> [Tag] {
let trimmed = keyword.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else {
Expand All @@ -80,10 +80,11 @@ final class HomeViewModel {
}
}

// 특정 태그 이름에 해당하는 비디오 배열을 Core Data에서 불러온다.
func fetchVideosForTag(_ tagName: String) -> [Video] {
return coreDataManager.fetch(tag: tagName)
}

func getCoreDataManager() -> CoreDataManager {
return coreDataManager
}
Expand Down