Skip to content

Commit

Permalink
Merge pull request #346 from OMZigak/refactor/#340-seflQA
Browse files Browse the repository at this point in the history
[refactor] 자체 QA 반영
  • Loading branch information
mmaybei authored Aug 29, 2024
2 parents 4212b0b + 863de54 commit 92d3a35
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 25 deletions.
37 changes: 37 additions & 0 deletions KkuMulKum/Resource/Extension/UILabel+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extension UILabel {
attributedText = NSAttributedString(string: text ?? " ", attributes: currentAttributes)
}

/// 단일 단어를 하이라이트할 때
func setHighlightText(_ words: String..., style: UIFont.Pretendard, color: UIColor? = nil) {
guard let currentAttributedText = attributedText else { return }

Expand All @@ -51,4 +52,40 @@ extension UILabel {
}
}
}

/// 여러 단어를 하이라이트할 때, 순차적으로 하이라이트하여 동일한 단어를 처리
func setHighlightText(for words: [String], style: UIFont.Pretendard, color: UIColor? = nil) {
guard let currentAttributedText = attributedText else { return }

let mutableAttributedString = NSMutableAttributedString(attributedString: currentAttributedText)
let textColor = color ?? self.textColor ?? .black

for word in words {
var searchRange = NSRange(location: 0, length: currentAttributedText.length)

while searchRange.location < currentAttributedText.length {
let range = (currentAttributedText.string as NSString).range(
of: word,
options: [],
range: searchRange
)

guard range.location != NSNotFound else { break }

let highlightedAttributes: [NSAttributedString.Key: Any] = [
.font: UIFont.pretendard(style),
.foregroundColor: textColor
]
mutableAttributedString.addAttributes(highlightedAttributes, range: range)

/// 다음 검색 범위를 설정
searchRange = NSRange(
location: range.location + range.length,
length: currentAttributedText.length - (range.location + range.length)
)
}
}

attributedText = mutableAttributedString
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ final class UpcomingPromiseCollectionViewCell: BaseCollectionViewCell {
// MARK: - Data Bind

extension UpcomingPromiseCollectionViewCell {
func dataBind(_ contentData: UpcomingPromise, formattedTime: String, formattedDay: String) {
func dataBind(
_ contentData: UpcomingPromise,
formattedTime: String,
formattedDay: String,
placeName: String
) {
let dDayText = contentData.dDay == 0 ? "-DAY" : "\(contentData.dDay)"
dDayLabel.setText(
"D\(dDayText)",
Expand All @@ -150,7 +155,7 @@ extension UpcomingPromiseCollectionViewCell {
dateLabel.setText(formattedDay, style: .body06, color: .gray7)
timeLabel.setText(formattedTime, style: .body06, color: .gray7)
placeNameLabel.setText(
contentData.placeName,
placeName,
style: .body06,
color: .gray7,
isSingleLine: true
Expand Down
14 changes: 10 additions & 4 deletions KkuMulKum/Source/Home/ViewController/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,14 @@ extension HomeViewController: UICollectionViewDataSource {
if let data = viewModel.upcomingPromiseList.value?.data?.promises[indexPath.item] {
let formattedTime = viewModel.formattedTimes.value[indexPath.item]
let formattedDay = viewModel.formattedDays.value[indexPath.item]
cell.dataBind(data, formattedTime: formattedTime, formattedDay: formattedDay)
let placeName = viewModel.placeNames.value[indexPath.item]

cell.dataBind(
data,
formattedTime: formattedTime,
formattedDay: formattedDay,
placeName: placeName
)
}
return cell
}
Expand Down Expand Up @@ -236,8 +243,7 @@ private extension HomeViewController {
color: .white
)
self.rootView.kkumulLabel.setHighlightText(
"\(data.promiseCount)",
"\(data.tardyCount)",
for: ["\(data.promiseCount)", "\(data.tardyCount)"],
style: .title00,
color: .lightGreen
)
Expand Down Expand Up @@ -292,7 +298,7 @@ private extension HomeViewController {
color: .gray8
)
self.rootView.todayPromiseView.placeNameLabel.setText(
data?.placeName ?? "",
self.viewModel.todayPlaceName.value,
style: .body06,
color: .gray7
)
Expand Down
28 changes: 14 additions & 14 deletions KkuMulKum/Source/Home/ViewModel/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ final class HomeViewModel {

/// 오늘의 약속
var todayFormattedTime = ObservablePattern<String>("")
var todayPlaceName = ObservablePattern<String>("")

/// 다가올 나의 약속
var formattedTimes = ObservablePattern<[String]>([])
var formattedDays = ObservablePattern<[String]>([])
var placeNames = ObservablePattern<[String]>([])


// MARK: - Initializer
Expand Down Expand Up @@ -138,12 +140,9 @@ final class HomeViewModel {
func updatePrepareStatus() {
Task {
do {
guard let responseBody = try await service.updatePreparationStatus(
_ = try await service.updatePreparationStatus(
with: nearestPromise.value?.data?.promiseID ?? 1
)
else {
return
}
)
} catch {
print(">>> \(error.localizedDescription) : \(#function)")
}
Expand All @@ -153,12 +152,9 @@ final class HomeViewModel {
func updateMoveStatus() {
Task {
do {
guard let responseBody = try await service.updateDepartureStatus(
_ = try await service.updateDepartureStatus(
with: nearestPromise.value?.data?.promiseID ?? 1
)
else {
return
}
)
} catch {
print(">>> \(error.localizedDescription) : \(#function)")
}
Expand All @@ -168,12 +164,9 @@ final class HomeViewModel {
func updateArriveStatus() {
Task {
do {
guard let responseBody = try await service.updateArrivalStatus(
_ = try await service.updateArrivalStatus(
with: nearestPromise.value?.data?.promiseID ?? 1
)
else {
return
}
} catch {
print(">>> \(error.localizedDescription) : \(#function)")
}
Expand All @@ -197,6 +190,9 @@ final class HomeViewModel {
do {
nearestPromise.value = try await service.fetchNearestPromise()
todayFormattedTime.value = formatTimeToString(nearestPromise.value?.data?.time ?? "") ?? ""
let placeName = self.nearestPromise.value?.data?.placeName ?? ""
todayPlaceName.value = placeName.count > 14 ? String(placeName.prefix(14)) + "..." : placeName

requestMyReadyStatus()
} catch {
print(">>> \(error.localizedDescription) : \(#function)")
Expand All @@ -212,6 +208,10 @@ final class HomeViewModel {
let promises = upcomingPromiseList.value?.data?.promises ?? []
formattedTimes.value = promises.map { formatTimeToString($0.time) ?? "" }
formattedDays.value = promises.map { formatDateToString($0.time) ?? "" }
placeNames.value = promises.map { promise in
let name = promise.placeName
return name.count > 12 ? String(name.prefix(12)) + "..." : name
}
} catch {
print(">>> \(error.localizedDescription) : \(#function)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ class MeetingListViewController: BaseViewController {
viewModel.loginUser.bind { [weak self] _ in
DispatchQueue.main.async {
guard let self = self else { return }
let data = self.viewModel.meetingList.value
let userData = self.viewModel.loginUser.value?.data
let listData = self.viewModel.meetingList.value?.data

self.rootView.infoLabel.setText(
"\(self.viewModel.loginUser.value?.data?.name ?? "꾸물리안") 님이 가입한 모임은\n\(self.viewModel.meetingList.value?.data?.count ?? 0)개예요!",
"\(userData?.name ?? "꾸물리안") 님이 가입한 모임은\n\(listData?.count ?? 0)개예요!",
style: .head01,
color: .gray8
)
Expand All @@ -92,15 +93,16 @@ class MeetingListViewController: BaseViewController {
viewModel.meetingList.bind { [weak self] _ in
DispatchQueue.main.async {
guard let self = self else { return }
let data = self.viewModel.meetingList.value
let userData = self.viewModel.loginUser.value?.data
let listData = self.viewModel.meetingList.value?.data

self.rootView.infoLabel.setText(
"\(self.viewModel.loginUser.value?.data?.name ?? "꾸물리안") 님이 가입한 모임은\n\(self.viewModel.meetingList.value?.data?.count ?? 0)개예요!",
"\(userData?.name ?? "꾸물리안") 님이 가입한 모임은\n\(listData?.count ?? 0)개예요!",
style: .head01,
color: .gray8
)

if data?.data?.count == 0 {
if listData?.count == 0 {
self.rootView.emptyLabel.isHidden = false
self.rootView.emptyCharacter.isHidden = false
} else {
Expand Down

0 comments on commit 92d3a35

Please sign in to comment.