Skip to content

Commit

Permalink
Merge pull request #121 from Gwamegis/feature/SongSearchAll
Browse files Browse the repository at this point in the history
Feature/song search all
  • Loading branch information
JMM00 authored Jun 19, 2023
2 parents 276b563 + 0dae6c6 commit 1d5c6ce
Show file tree
Hide file tree
Showing 23 changed files with 262 additions and 119 deletions.
8 changes: 4 additions & 4 deletions Halmap.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
A81FFC9128F1775000B0FC7C /* SongSearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A81FFC9028F1775000B0FC7C /* SongSearchView.swift */; };
A81FFC9328F1775C00B0FC7C /* StadiumListSheetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A81FFC9228F1775C00B0FC7C /* StadiumListSheetView.swift */; };
A81FFC9528F1776300B0FC7C /* StadiumMapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A81FFC9428F1776300B0FC7C /* StadiumMapView.swift */; };
A81FFC9728F1776F00B0FC7C /* SongInformationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A81FFC9628F1776F00B0FC7C /* SongInformationView.swift */; };
A81FFC9928F1777B00B0FC7C /* TeamSelectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A81FFC9828F1777B00B0FC7C /* TeamSelectionView.swift */; };
A81FFC9D28F180A000B0FC7C /* SongPlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A81FFC9C28F180A000B0FC7C /* SongPlayerView.swift */; };
A81FFC9F28F180B200B0FC7C /* SongContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A81FFC9E28F180B200B0FC7C /* SongContentView.swift */; };
Expand All @@ -37,6 +36,7 @@
B2699E1028F1CB9700267A4F /* Pretendard-Medium.otf in Resources */ = {isa = PBXBuildFile; fileRef = B2699E0F28F1CB9700267A4F /* Pretendard-Medium.otf */; };
EEBC26A028F1CAE900BD5B3D /* TabBarItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBC269E28F1CAE800BD5B3D /* TabBarItemView.swift */; };
EEBC26A128F1CAE900BD5B3D /* TabBarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBC269F28F1CAE800BD5B3D /* TabBarView.swift */; };
F9174B2A2A2B2F5F00B1CE87 /* SeasonSong.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9174B292A2B2F5F00B1CE87 /* SeasonSong.swift */; };
F91BE5FB29B18AF800F7E488 /* MainTabView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F91BE5FA29B18AF800F7E488 /* MainTabView.swift */; };
F91BE5FD29B18D1E00F7E488 /* MainSongListTabView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F91BE5FC29B18D1E00F7E488 /* MainSongListTabView.swift */; };
F939EBBA29D58FB2005ED8CA /* StorageContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F939EBB929D58FB2005ED8CA /* StorageContentView.swift */; };
Expand Down Expand Up @@ -74,7 +74,6 @@
A81FFC9028F1775000B0FC7C /* SongSearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SongSearchView.swift; sourceTree = "<group>"; };
A81FFC9228F1775C00B0FC7C /* StadiumListSheetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StadiumListSheetView.swift; sourceTree = "<group>"; };
A81FFC9428F1776300B0FC7C /* StadiumMapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StadiumMapView.swift; sourceTree = "<group>"; };
A81FFC9628F1776F00B0FC7C /* SongInformationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SongInformationView.swift; sourceTree = "<group>"; };
A81FFC9828F1777B00B0FC7C /* TeamSelectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TeamSelectionView.swift; sourceTree = "<group>"; };
A81FFC9C28F180A000B0FC7C /* SongPlayerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SongPlayerView.swift; sourceTree = "<group>"; };
A81FFC9E28F180B200B0FC7C /* SongContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SongContentView.swift; sourceTree = "<group>"; };
Expand All @@ -87,6 +86,7 @@
B2699E0F28F1CB9700267A4F /* Pretendard-Medium.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Pretendard-Medium.otf"; sourceTree = "<group>"; };
EEBC269E28F1CAE800BD5B3D /* TabBarItemView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TabBarItemView.swift; sourceTree = "<group>"; };
EEBC269F28F1CAE800BD5B3D /* TabBarView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TabBarView.swift; sourceTree = "<group>"; };
F9174B292A2B2F5F00B1CE87 /* SeasonSong.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeasonSong.swift; sourceTree = "<group>"; };
F91BE5FA29B18AF800F7E488 /* MainTabView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainTabView.swift; sourceTree = "<group>"; };
F91BE5FC29B18D1E00F7E488 /* MainSongListTabView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainSongListTabView.swift; sourceTree = "<group>"; };
F939EBB929D58FB2005ED8CA /* StorageContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -203,7 +203,6 @@
A81FFC9B28F1808700B0FC7C /* SongInformation */ = {
isa = PBXGroup;
children = (
A81FFC9628F1776F00B0FC7C /* SongInformationView.swift */,
A81FFC9C28F180A000B0FC7C /* SongPlayerView.swift */,
A81FFC9E28F180B200B0FC7C /* SongContentView.swift */,
F9B3B7B629B842DD00232BB8 /* SongDetailView.swift */,
Expand Down Expand Up @@ -268,6 +267,7 @@
F98F623029B59CE60025F50E /* TeamName.swift */,
F9B3B7B429B7397600232BB8 /* MapName.swift */,
A8F8BA9829C8CE5B00FCB229 /* AudioManager.swift */,
F9174B292A2B2F5F00B1CE87 /* SeasonSong.swift */,
);
path = Data;
sourceTree = "<group>";
Expand Down Expand Up @@ -380,6 +380,7 @@
F98F622B29B4CB450025F50E /* ThemeManager.swift in Sources */,
A81FFC9128F1775000B0FC7C /* SongSearchView.swift in Sources */,
F9B3B7B729B842DD00232BB8 /* SongDetailView.swift in Sources */,
F9174B2A2A2B2F5F00B1CE87 /* SeasonSong.swift in Sources */,
EEBC26A028F1CAE900BD5B3D /* TabBarItemView.swift in Sources */,
A81FFCA728F1B84A00B0FC7C /* Halmap+Color.swift in Sources */,
F98F622929B4C9BD0025F50E /* Themes.swift in Sources */,
Expand All @@ -392,7 +393,6 @@
F9B95BBB28F271B800728048 /* Model.swift in Sources */,
F98F623129B59CE60025F50E /* TeamName.swift in Sources */,
F9B3B7B529B7397600232BB8 /* MapName.swift in Sources */,
A81FFC9728F1776F00B0FC7C /* SongInformationView.swift in Sources */,
A81FFC9528F1776300B0FC7C /* StadiumMapView.swift in Sources */,
F9A178262A1C8B01003E8E4C /* PlayListFilter.swift in Sources */,
);
Expand Down
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions Halmap/Assets.xcassets/Image/Search/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"images" : [
{
"filename" : "NCPlayer.png",
"filename" : "searchEmpty.png",
"idiom" : "universal",
"scale" : "1x"
},
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"images" : [
{
"filename" : "NC23.png",
"filename" : "searchInfo.png",
"idiom" : "universal",
"scale" : "1x"
},
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
88 changes: 73 additions & 15 deletions Halmap/Data/DataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,45 @@
// Created by 전지민 on 2022/10/09.
//

import Foundation
import SwiftUI
import FirebaseFirestore

class DataManager: ObservableObject {

private let db = Firestore.firestore()

var selectedTeam: String = (UserDefaults.standard.string(forKey: "selectedTeam") ?? "Hanwha")
@AppStorage("selectedTeam") var selectedTeam = "Hanwha"

var teams: [Team] = []
@Published var teamSongList: [TeamSong] = []
@Published var playerList: [Player] = []
@Published var playerSongs: [Song] = []
@Published var teamSongs: [Song] = []
@Published var favoriteSongs = PersistenceController.shared.fetchFavoriteSong()

@Published var playerSongsAll = [[Song]](repeating: [], count: 10)
@Published var teamSongsAll = [[Song]](repeating: [], count: 10)

@Published var seasonSongs = [[String]](repeating: [], count: 10)

var teamLists = TeamName.allCases

init() {
loadData()
fetchSong(team: selectedTeam, type: true) { songs in
self.playerSongs = songs.sorted { lhs, rhs in
lhs.title <= rhs.title
teamLists.forEach { teamName in
fetchSong(team: teamName.rawValue, type: true) { songs in
self.playerSongsAll[teamName.fetchTeamIndex()] = songs
self.setSongList(team: self.selectedTeam)
}
}
fetchSong(team: selectedTeam, type: false) { songs in
self.teamSongs = songs.sorted { lhs, rhs in
lhs.title <= rhs.title
fetchSong(team: teamName.rawValue, type: false) { songs in
self.teamSongsAll[teamName.fetchTeamIndex()] = songs
}
}

fetchSeasonData { data in
self.seasonSongs = data
}

}

func loadData(){
Expand Down Expand Up @@ -71,12 +83,8 @@ class DataManager: ObservableObject {
}

func setSongList(team: String) {
fetchSong(team: team, type: true) { songs in
self.playerSongs = songs
}
fetchSong(team: team, type: false) { songs in
self.teamSongs = songs
}
self.playerSongs = playerSongsAll[TeamName(rawValue: selectedTeam)?.fetchTeamIndex() ?? 0]
self.teamSongs = teamSongsAll[TeamName(rawValue: selectedTeam)?.fetchTeamIndex() ?? 0]
}

//MARK: 파이어스토어에서 해당하는 팀의 응원가 정보를 가져오는 함수
Expand All @@ -87,6 +95,7 @@ class DataManager: ObservableObject {

db.collection(team)
.whereField("type", isEqualTo: type)
.order(by: "title", descending: false)
.getDocuments { (querySnapshot, error) in
if let error {
print("Error getting documents: \(error)")
Expand All @@ -108,4 +117,53 @@ class DataManager: ObservableObject {
}
}
}

func fetchSeasonData(completionHandler: @escaping ([[String]])->()) {
db.collection("SeasonSong")
.getDocuments { (querySnapshot, error) in
if let error {
print("Error getting documents: \(error)")
} else {
guard let documents = querySnapshot?.documents else { return }
let decoder = JSONDecoder()

for document in documents {
do {
let data = document.data()
let jsonData = try JSONSerialization.data(withJSONObject: data)
let seasonSong = try decoder.decode(SeasonSong.self, from: jsonData)

completionHandler(self.processingSeasonSongData(data: seasonSong))
} catch let error {
print("error: \(error)")
}
}
}
}
}

func processingSeasonSongData(data: SeasonSong) -> [[String]] {
var seasonData = [[String]](repeating: [], count: 10)

seasonData[TeamName.doosan.fetchTeamIndex()] = splitData(data: data.doosan)
seasonData[TeamName.hanwha.fetchTeamIndex()] = splitData(data: data.hanwha)
seasonData[TeamName.samsung.fetchTeamIndex()] = splitData(data: data.samsung)
seasonData[TeamName.lotte.fetchTeamIndex()] = splitData(data: data.lotte)
seasonData[TeamName.lg.fetchTeamIndex()] = splitData(data: data.lg)
seasonData[TeamName.ssg.fetchTeamIndex()] = splitData(data: data.ssg)
seasonData[TeamName.kt.fetchTeamIndex()] = splitData(data: data.kt)
seasonData[TeamName.nc.fetchTeamIndex()] = splitData(data: data.nc)
seasonData[TeamName.kiwoom.fetchTeamIndex()] = splitData(data: data.kiwoom)
seasonData[TeamName.kia.fetchTeamIndex()] = splitData(data: data.kia)

return seasonData
}

func splitData(data: String) -> [String] {
return data.split(separator: ",").map{ String($0) }
}

func checkSeasonSong(data: SongInfo) -> Bool {
self.seasonSongs[TeamName(rawValue: data.team)?.fetchTeamIndex() ?? 0].contains(data.title)
}
}
22 changes: 22 additions & 0 deletions Halmap/Data/SeasonSong.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// SeasonSong.swift
// Halmap
//
// Created by 전지민 on 2023/06/03.
//

import Foundation

struct SeasonSong: Identifiable, Codable {
var id: String
var doosan: String
var hanwha: String
var samsung: String
var lotte: String
var lg: String
var ssg: String
var kt: String
var nc: String
var kiwoom: String
var kia: String
}
9 changes: 9 additions & 0 deletions Halmap/Data/Song.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ struct Song: Identifiable, Codable {
var info: String
var url: String
}
struct SongInfo: Identifiable, Codable {
var id: String
var team: String
var type: Bool
var title: String
var lyrics: String
var info: String
var url: String
}
25 changes: 25 additions & 0 deletions Halmap/Data/TeamName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,29 @@ enum TeamName: String, CaseIterable {
return "기아 타이거즈"
}
}

func fetchTeamIndex() -> Int {
switch self {
case .doosan:
return 0
case .hanwha:
return 1
case .samsung:
return 2
case .lotte:
return 3
case .lg:
return 4
case .ssg:
return 5
case .kt:
return 6
case .nc:
return 7
case .kiwoom:
return 8
case .kia:
return 9
}
}
}
4 changes: 2 additions & 2 deletions Halmap/Persistence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct PersistenceController {
container.viewContext.automaticallyMergesChangesFromParent = true
}

func saveSongs(song: Song, playListTitle: String?) {
func saveSongs(song: SongInfo, playListTitle: String?) {
let context = container.viewContext
let collectedSong = CollectedSong(context: context)
collectedSong.id = song.id
Expand All @@ -39,7 +39,7 @@ struct PersistenceController {
collectedSong.url = song.url
collectedSong.type = song.type
collectedSong.playListTitle = playListTitle
collectedSong.team = selectedTeam
collectedSong.team = song.team
collectedSong.date = Date()

if context.hasChanges {
Expand Down
43 changes: 32 additions & 11 deletions Halmap/View/MainSongListTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import SwiftUI

struct MainSongListTabView: View {

@State var selectedTeam: String = (UserDefaults.standard.string(forKey: "selectedTeam") ?? "Hanwha")
@ObservedObject var dataManager = DataManager()
@AppStorage("selectedTeam") var selectedTeam = "Hanwha"
@EnvironmentObject var dataManager: DataManager

@State private var showingTeamChaingView: Bool = false
@State var index = 0

Expand Down Expand Up @@ -50,10 +51,17 @@ struct MainSongListTabView: View {
lyrics: song.lyrics,
info: song.info,
url: song.url)
let songInfo = SongInfo(id: song.id,
team: selectedTeam,
type: song.type,
title: song.title,
lyrics: song.lyrics,
info: song.info,
url: song.url)

NavigationLink(destination: SongDetailView(song: music)) {
NavigationLink(destination: SongDetailView(song: music, team: selectedTeam)) {
HStack(spacing: 16) {
Image("\(selectedTeam)Album")
Image(dataManager.checkSeasonSong(data: songInfo) ? "\(selectedTeam)23" : "\(selectedTeam)Album")
.resizable()
.frame(width: 40, height: 40)
.cornerRadius(8)
Expand All @@ -66,6 +74,8 @@ struct MainSongListTabView: View {
.foregroundColor(.customDarkGray)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
.lineLimit(1)
}
}
}
Expand All @@ -84,15 +94,24 @@ struct MainSongListTabView: View {
List {
ForEach(dataManager.playerSongs) { song in
let music = Song(id: song.id,
type: song.type,
title: song.title,
lyrics: song.lyrics,
info: song.info,
url: song.url)
type: song.type,
title: song.title,
lyrics: song.lyrics,
info: song.info,
url: song.url)

let songInfo = SongInfo(id: song.id,
team: selectedTeam,
type: song.type,
title: song.title,
lyrics: song.lyrics,
info: song.info,
url: song.url)


NavigationLink(destination: SongDetailView(song: music)) {
NavigationLink(destination: SongDetailView(song: music, team: selectedTeam)) {
HStack(spacing: 16) {
Image("\(selectedTeam)Player")
Image(dataManager.checkSeasonSong(data: songInfo) ? "\(selectedTeam)23" : "\(selectedTeam)Player")
.resizable()
.frame(width: 40, height: 40)
.cornerRadius(8)
Expand All @@ -105,6 +124,8 @@ struct MainSongListTabView: View {
.foregroundColor(.customDarkGray)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
.lineLimit(1)
}
}
}
Expand Down
Loading

0 comments on commit 1d5c6ce

Please sign in to comment.