From c679118504fad656f6f25ca73971a46a72c56085 Mon Sep 17 00:00:00 2001 From: Yoonseo Kim Date: Fri, 24 Sep 2021 14:35:38 +0900 Subject: [PATCH] =?UTF-8?q?[#4]=20Person=20Model=20=E1=84=8C=E1=85=A5?= =?UTF-8?q?=E1=86=A8=E1=84=8B=E1=85=AD=E1=86=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/Managers/StringManager.swift | 65 +++++++++++++++++++ .../Sources/ViewModels/MainViewModel.swift | 34 ++++++++-- 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/CheoMooRac/CheoMooRac/Sources/Managers/StringManager.swift b/CheoMooRac/CheoMooRac/Sources/Managers/StringManager.swift index 7019314..839cc95 100644 --- a/CheoMooRac/CheoMooRac/Sources/Managers/StringManager.swift +++ b/CheoMooRac/CheoMooRac/Sources/Managers/StringManager.swift @@ -18,3 +18,68 @@ struct StringManager { return hangul[Int(index)] } } + +extension CharacterSet{ + static var modernHangul: CharacterSet{ + return CharacterSet(charactersIn: ("가".unicodeScalars.first!)...("힣".unicodeScalars.first!)) + } +} + +public class Jamo { + + // UTF-8 기준 + static let INDEX_HANGUL_START:UInt32 = 44032 // "가" + static let INDEX_HANGUL_END:UInt32 = 55199 // "힣" + + static let CYCLE_CHO :UInt32 = 588 + static let CYCLE_JUNG :UInt32 = 28 + + static let CHO = [ + "ㄱ","ㄲ","ㄴ","ㄷ","ㄸ","ㄹ","ㅁ","ㅂ","ㅃ","ㅅ", + "ㅆ","ㅇ","ㅈ","ㅉ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ" + ] + + static let JUNG = [ + "ㅏ", "ㅐ", "ㅑ", "ㅒ", "ㅓ", "ㅔ","ㅕ", "ㅖ", "ㅗ", "ㅘ", + "ㅙ", "ㅚ","ㅛ", "ㅜ", "ㅝ", "ㅞ", "ㅟ", "ㅠ", "ㅡ", "ㅢ", + "ㅣ" + ] + + static let JONG = [ + "","ㄱ","ㄲ","ㄳ","ㄴ","ㄵ","ㄶ","ㄷ","ㄹ","ㄺ", + "ㄻ","ㄼ","ㄽ","ㄾ","ㄿ","ㅀ","ㅁ","ㅂ","ㅄ","ㅅ", + "ㅆ","ㅇ","ㅈ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ" + ] + + static let JONG_DOUBLE = [ + "ㄳ":"ㄱㅅ","ㄵ":"ㄴㅈ","ㄶ":"ㄴㅎ","ㄺ":"ㄹㄱ","ㄻ":"ㄹㅁ", + "ㄼ":"ㄹㅂ","ㄽ":"ㄹㅅ","ㄾ":"ㄹㅌ","ㄿ":"ㄹㅍ","ㅀ":"ㄹㅎ", + "ㅄ":"ㅂㅅ" + ] + + // 주어진 "단어"를 자모음으로 분해해서 리턴하는 함수 + class func getJamo(_ input: String) -> String { + var jamo = "" + //let word = input.trimmingCharacters(in: .whitespacesAndNewlines).trimmingCharacters(in: .punctuationCharacters) + for scalar in input.unicodeScalars{ + jamo += getJamoFromOneSyllable(scalar) ?? "" + } + return jamo + } + + // 주어진 "코드의 음절"을 자모음으로 분해해서 리턴하는 함수 + private class func getJamoFromOneSyllable(_ n: UnicodeScalar) -> String?{ + if CharacterSet.modernHangul.contains(n){ + let index = n.value - INDEX_HANGUL_START + let cho = CHO[Int(index / CYCLE_CHO)] + let jung = JUNG[Int((index % CYCLE_CHO) / CYCLE_JUNG)] + var jong = JONG[Int(index % CYCLE_JUNG)] + if let disassembledJong = JONG_DOUBLE[jong] { + jong = disassembledJong + } + return cho + jung + jong + }else{ + return String(UnicodeScalar(n)) + } + } +} diff --git a/CheoMooRac/CheoMooRac/Sources/ViewModels/MainViewModel.swift b/CheoMooRac/CheoMooRac/Sources/ViewModels/MainViewModel.swift index 21bb66d..96a72f1 100644 --- a/CheoMooRac/CheoMooRac/Sources/ViewModels/MainViewModel.swift +++ b/CheoMooRac/CheoMooRac/Sources/ViewModels/MainViewModel.swift @@ -12,7 +12,7 @@ protocol MainViewModelInput { } protocol MainViewModelOutput { - var list: Dynamic<[String]> {get} + var list: Dynamic<[Person]> {get} var sectionHeaderList: Dynamic<[String]> {get} var nowRefreshing: Dynamic {get} var sectionArray: Dynamic<[String]> {get} @@ -33,7 +33,7 @@ class MainViewModel: MainViewModelProtocol { // MARK: - OUTPUT - let list: Dynamic<[String]> = Dynamic([]) + let list: Dynamic<[Person]> = Dynamic([]) let sectionHeaderList: Dynamic<[String]> = Dynamic([]) let nowRefreshing: Dynamic = Dynamic(false) let sectionArray: Dynamic<[String]> = Dynamic([]) @@ -41,18 +41,40 @@ class MainViewModel: MainViewModelProtocol { func getSectionArray(at section: Int) -> Dynamic<[String]> { let list$ = self.list.value.filter { - return StringManager.shared.chosungCheck(word: $0) == sectionHeaderList.value[section-1] + return StringManager.shared.chosungCheck(word: $0.familyName + $0.firstName) == sectionHeaderList.value[section-1] + }.map { person in + return person.familyName + person.firstName } + return Dynamic(list$) } init() { - self.list.value = ["김윤서", "김루희", "윤예지", "김혜수", "코코", "민재", "잼권이", "리헤이", "노제", "몬익화", "립제이", "잘린이", "엠마", "모아나", "케이데이", "가비", "시미즈zz", "강호동", "이수근", "유재석", "리정" ] + self.list.value = [Person(firstName: "윤서", familyName: "김", phoneNumber: "010-6515-6030"), + Person(firstName: "루희", familyName: "김", phoneNumber: "010-6515-6030"), + Person(firstName: "예지", familyName: "윤", phoneNumber: "010-6515-6030"), + Person(firstName: "혜수", familyName: "김", phoneNumber: "010-6515-6030"), + Person(firstName: "리헤이", familyName: "", phoneNumber: "010-6515-6030"), + Person(firstName: "제", familyName: "노", phoneNumber: "010-6515-6030"), + Person(firstName: "엠마", familyName: "", phoneNumber: "010-6515-6030"), + Person(firstName: "모아나", familyName: "", phoneNumber: "010-6515-6030"), + Person(firstName: "민재", familyName: "곽", phoneNumber: "010-6515-6030"), + Person(firstName: "케이데이", familyName: "", phoneNumber: "010-6515-6030"), + Person(firstName: "가비", familyName: "", phoneNumber: "010-6515-6030"), + Person(firstName: "시미즈", familyName: "", phoneNumber: "010-6515-6030"), + Person(firstName: "호동", familyName: "강", phoneNumber: "010-6515-6030"), + Person(firstName: "재석", familyName: "유", phoneNumber: "010-6515-6030"), + Person(firstName: "리정", familyName: "", phoneNumber: "010-6515-6030"), + Person(firstName: "몬익화", familyName: "", phoneNumber: "010-6515-6030"), + Person(firstName: "코코", familyName: "", phoneNumber: "010-6515-6030"), + Person(firstName: "잼권", familyName: "", phoneNumber: "010-6515-6030") + + ] var sectionHeaderList$: [String] = [] - self.list.value.forEach { name in - sectionHeaderList$.append(StringManager.shared.chosungCheck(word: name)) + self.list.value.forEach { person in + sectionHeaderList$.append(StringManager.shared.chosungCheck(word: person.familyName + person.firstName)) } self.sectionHeaderList.value = Array(Set(sectionHeaderList$)).sorted()