Skip to content

Commit

Permalink
[refactor] #157 PokitLinkPopup 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
ShapeKim98 committed Nov 30, 2024
1 parent 1ff6183 commit c4878b6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 72 deletions.
10 changes: 7 additions & 3 deletions Projects/App/Sources/MainTab/MainTabFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import FeaturePokit
import FeatureRemind
import FeatureContentDetail
import Domain
import DSKit
import Util
import CoreKit

Expand All @@ -28,7 +29,7 @@ public struct MainTabFeature {
public struct State: Equatable {
var selectedTab: MainTab = .pokit
var isBottomSheetPresented: Bool = false
var isLinkSheetPresented: Bool = false
var linkPopup: PokitLinkPopup.PopupType?
var isErrorSheetPresented: Bool = false
var link: String?

Expand Down Expand Up @@ -157,7 +158,7 @@ private extension MainTabFeature {
}

case .linkCopyButtonTapped:
state.isLinkSheetPresented = false
state.linkPopup = nil
return .run { send in await send(.delegate(.링크추가하기)) }

case .onAppear:
Expand Down Expand Up @@ -198,7 +199,10 @@ private extension MainTabFeature {
switch action {
case let .linkCopySuccess(url):
guard let url else { return .none }
state.isLinkSheetPresented = true
state.linkPopup = .link(
title: "복사한 링크 저장하기",
url: url.absoluteString
)
state.link = url.absoluteString
return .none

Expand Down
12 changes: 4 additions & 8 deletions Projects/App/Sources/MainTab/MainTabFeatureView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,9 @@ public extension MainTabView {
}
}

if self.store.isLinkSheetPresented {
if self.store.linkPopup != nil {
PokitLinkPopup(
"복사한 링크 저장하기",
isPresented: $store.isLinkSheetPresented,
type: .link(url: self.store.link ?? ""),
type: $store.linkPopup,
action: { send(.linkCopyButtonTapped) }
)
}
Expand All @@ -94,11 +92,9 @@ private extension MainTabView {
tabView
.overlay(alignment: .bottom) {
VStack(spacing: 0) {
if store.isLinkSheetPresented {
if store.linkPopup != nil {
PokitLinkPopup(
"복사한 링크 저장하기",
isPresented: $store.isLinkSheetPresented,
type: .link(url: store.link ?? ""),
type: $store.linkPopup,
action: { send(.linkCopyButtonTapped) }
)
.padding(.bottom, 20)
Expand Down
100 changes: 56 additions & 44 deletions Projects/DSKit/Sources/Components/PokitLinkPopup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import SwiftUI
import Combine

public struct PokitLinkPopup: View {
@Binding private var isPresented: Bool
@Binding
private var type: PokitLinkPopup.PopupType?

@State private var second: Int = 0
private let titleKey: String
private let type: PokitLinkPopup.PopupType
@State
private var second: Int = 0
private let action: (() -> Void)?
private let timer = Timer.publish(
every: 1,
Expand All @@ -22,14 +22,10 @@ public struct PokitLinkPopup: View {
).autoconnect()

public init(
_ titleKey: String,
isPresented: Binding<Bool>,
type: PokitLinkPopup.PopupType,
type: Binding<PokitLinkPopup.PopupType?>,
action: (() -> Void)? = nil
) {
self.titleKey = titleKey
self._isPresented = isPresented
self.type = type
self._type = type
self.action = action
}

Expand All @@ -44,7 +40,7 @@ public struct PokitLinkPopup: View {
.frame(width: 335, height: 60)
.transition(.move(edge: .bottom).combined(with: .opacity))
.onReceive(timer) { _ in
guard second < 2 && isPresented else {
guard second < 2 && type != nil else {
closedPopup()
return
}
Expand All @@ -58,17 +54,13 @@ public struct PokitLinkPopup: View {
action?()
} label: {
VStack(alignment: .leading, spacing: 0) {
Text(titleKey)
Text(title)
.lineLimit(2)
.pokitFont(.b2(.b))
.multilineTextAlignment(.leading)
.foregroundStyle(
type == .warning
? .pokit(.text(.primary))
: .pokit(.text(.inverseWh))
)
.foregroundStyle(textColor)

if case .link(let url) = type {
if case let .link(_, url) = type {
Text(url)
.lineLimit(1)
.pokitFont(.detail2)
Expand Down Expand Up @@ -100,18 +92,14 @@ public struct PokitLinkPopup: View {
Image(.icon(.x))
.resizable()
.frame(width: 24, height: 24)
.foregroundStyle(
type == .warning
? .pokit(.icon(.primary))
: .pokit(.icon(.inverseWh))
)
.foregroundStyle(iconColor)
}
}

private func closedPopup() {
withAnimation(.pokitSpring) {
second = 0
isPresented = false
type = nil
}
}

Expand All @@ -125,50 +113,74 @@ public struct PokitLinkPopup: View {
return .pokit(.bg(.error))
case .warning:
return .pokit(.bg(.warning))
case .none: return .clear
}
}

private var iconColor: Color {
switch type {
case .warning:
return .pokit(.icon(.primary))
default:
return .pokit(.icon(.inverseWh))
}
}

private var textColor: Color {
switch type {
case .warning:
return .pokit(.text(.primary))
default:
return .pokit(.text(.inverseWh))
}
}

private var title: String {
switch type {
case let .link(title, _),
let .text(title),
let .success(title),
let .error(title),
let .warning(title):
return title
default: return ""
}
}
}

public extension PokitLinkPopup {
enum PopupType: Equatable {
case link(url: String)
case text
case success
case error
case warning
case link(title: String, url: String)
case text(title: String)
case success(title: String)
case error(title: String)
case warning(title: String)
}
}

#Preview {
VStack {
PokitLinkPopup(
"복사한 링크 저장하기",
isPresented: .constant(true),
type: .link(url: "https://www.youtube.com/watch?v=xSTwqKUyM8k")
type: .constant(.link(
title: "복사한 링크 저장하기",
url: "https://www.youtube.com/watch?v=xSTwqKUyM8k"
))
)

PokitLinkPopup(
"최대 30개의 포킷을 생성할 수 있습니다.\n포킷을 삭제한 뒤에 추가해주세요.",
isPresented: .constant(true),
type: .text
type: .constant(.text(title: "최대 30개의 포킷을 생성할 수 있습니다.\n포킷을 삭제한 뒤에 추가해주세요."))
)

PokitLinkPopup(
"링크저장 완료",
isPresented: .constant(true),
type: .success
type: .constant(.success(title: "링크저장 완료"))
)

PokitLinkPopup(
"링크저장 실패",
isPresented: .constant(true),
type: .error
type: .constant(.error(title: "링크저장 실패"))
)

PokitLinkPopup(
"저장공간 부족",
isPresented: .constant(true),
type: .warning
type: .constant(.warning(title: "저장공간 부족"))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public struct ContentSettingFeature {
var selectedPokit: BaseCategoryItem?
var linkTitle: String? = nil
var linkImageURL: String? = nil
var showMaxCategoryPopup: Bool = false
var showDetectedURLPopup: Bool = false
var linkPopup: PokitLinkPopup.PopupType?
var contentLoading: Bool = false
var saveIsLoading: Bool = false
var link: String?
Expand Down Expand Up @@ -208,7 +207,7 @@ private extension ContentSettingFeature {
case .포킷추가_버튼_눌렀을때:
guard state.domain.categoryTotalCount < 30 else {
/// 🚨 Error Case [1]: 포킷 갯수가 30개 이상일 경우
state.showMaxCategoryPopup = true
state.linkPopup = .text(title: "최대 30개의 포킷을 생성할 수 있습니다.\n포킷을 삭제한 뒤에 추가해주세요.")
return .none
}

Expand All @@ -234,7 +233,10 @@ private extension ContentSettingFeature {
case let .linkPopup(url):
guard let url else { return .none }
state.link = url.absoluteString
state.showDetectedURLPopup = true
state.linkPopup = .link(
title: "복사한 링크 저장하기",
url: url.absoluteString
)
return .none
case .linkPreview:
state.showLinkPreview = true
Expand All @@ -261,7 +263,7 @@ private extension ContentSettingFeature {
guard let url = URL(string: state.domain.data),
!state.domain.data.isEmpty else {
/// 🚨 Error Case [1]: 올바른 링크가 아닐 때
state.showDetectedURLPopup = false
state.linkPopup = nil
state.linkTitle = nil
state.domain.title = ""
state.linkImageURL = nil
Expand All @@ -270,7 +272,7 @@ private extension ContentSettingFeature {
}
return .send(.inner(.메타데이터_조회_수행(url: url)), animation: .pokitDissolve)
case .링크복사_반영(let urlText):
state.showDetectedURLPopup = false
state.linkPopup = nil
state.link = nil
guard let urlText else { return .none }
state.domain.data = urlText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,12 @@ public extension ContentSettingView {
.padding(.top, 16)
}
.overlay(alignment: .bottom) {
if store.state.showMaxCategoryPopup {
if store.linkPopup != nil {
PokitLinkPopup(
"최대 30개의 포킷을 생성할 수 있습니다. \n포킷을 삭제한 뒤에 추가해주세요.",
isPresented: $store.showMaxCategoryPopup,
type: .text
)
.animation(.pokitSpring, value: store.showMaxCategoryPopup)
} else if store.state.showDetectedURLPopup {
PokitLinkPopup(
"복사한 링크 저장하기",
isPresented: $store.showDetectedURLPopup,
type: .link(url: store.link ?? ""),
type: $store.linkPopup,
action: { send(.링크복사_버튼_눌렀을때, animation: .pokitSpring) }
)
.animation(.pokitSpring, value: store.linkPopup)
}
}
.pokitMaxWidth()
Expand Down

0 comments on commit c4878b6

Please sign in to comment.