Skip to content

Commit

Permalink
[D-3] Fix widget deep link 문제 해결 (530) (#81)
Browse files Browse the repository at this point in the history
* fix: deep link 문제 해결

* fix: deep link noti 전송 시 userInfo를 통해 agencyID 전송

* feat: Widget LinkManager 구현
  • Loading branch information
Siwon-L committed Nov 26, 2024
1 parent 0125fb1 commit 890322a
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 35 deletions.
5 changes: 3 additions & 2 deletions Projects/App/Sources/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import DesignSystem

final class AppCoordinator: Coordinator {
var navigationController: UINavigationController
var diContainer: AppDIContainer = AppDIContainer()
let diContainer: AppDIContainer
weak var parentCoordinator: Coordinator?
var childCoordinators: [Coordinator] = []

init(navigationController: UINavigationController) {
init(navigationController: UINavigationController, diContainer: AppDIContainer) {
self.navigationController = navigationController
self.diContainer = diContainer
}

func start(animated: Bool) {
Expand Down
16 changes: 5 additions & 11 deletions Projects/App/Sources/DIContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,21 @@ import MyPageFeature
import Core

final class AppDIContainer {

private let localStorage: LocalStorageInterface
private let networkManager: NetworkManagerInterfacae
private let tokenIntercepter: TokenRequestIntercepter

let signDIContainer: SignDIContainer
let mainDIContainer: MainDIContainer

init() {
self.localStorage = LocalStorage()
self.networkManager = NetworkManager()
init(
localStorage: LocalStorageInterface,
networkManager: NetworkManagerInterfacae
) {

self.tokenIntercepter = TokenRequestIntercepter(
(networkManager as? NetworkManager)?.tokenIntercepter = TokenRequestIntercepter(
localStorage: localStorage,
tokenRepository: TokenRepository(
networkManager: networkManager,
localStorage: localStorage
)
)

(networkManager as? NetworkManager)?.tokenIntercepter = tokenIntercepter

self.signDIContainer = SignDIContainer(
localStorage: localStorage,
Expand Down
13 changes: 10 additions & 3 deletions Projects/App/Sources/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import Core
import DesignSystem

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
private let localStorage = LocalStorage()
private let networkManager = NetworkManager()
private lazy var diContainer = AppDIContainer(localStorage: localStorage, networkManager: networkManager)

private var appCoordinator: AppCoordinator?
var window: UIWindow?

Expand All @@ -17,17 +21,20 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
self.window?.makeKeyAndVisible()
self.window?.rootViewController = navigationController

self.appCoordinator = AppCoordinator(navigationController: navigationController)
self.appCoordinator = AppCoordinator(
navigationController: navigationController,
diContainer: diContainer
)
appCoordinator?.start(animated: false)

self.scene(scene, openURLContexts: connectionOptions.urlContexts)
}

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else { return }

if url.absoluteString.contains("widget://") {
DeepLinkManager.setDestination(url.absoluteString)
DeepLinkManager.setDestination(url.absoluteString, agencyID: localStorage.selectedAgency)
} else {
KakaoAuthManager.shared.openURL(url)
}
Expand Down
17 changes: 17 additions & 0 deletions Projects/App/WidgetExtension/Sources/Model/LinkManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Foundation

enum LinkManager {
case ocr
case ledgerDetail
case createLedger

var url: URL {
let urlString: String
switch self {
case .ocr: urlString = "widget://OCR"
case .ledgerDetail: urlString = "widget://LedgerDetail"
case .createLedger: urlString = "widget://CreateLedger"
}
return URL(string: urlString)!
}
}
6 changes: 3 additions & 3 deletions Projects/App/WidgetExtension/Sources/Widgets/MainWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct MainWidgetEntryView: View {

var body: some View {
VStack {
Link(destination: URL(string: "widget://CreateLedger")!) {
Link(destination: LinkManager.createLedger.url) {
HStack(alignment: .center) {
Text("\(entry.name)에 오늘 사용한 금액 입력")
.bold()
Expand All @@ -43,7 +43,7 @@ struct MainWidgetEntryView: View {

Spacer()
HStack {
Link(destination: URL(string: "widget://OCR")!) {
Link(destination: LinkManager.ocr.url) {
HStack {
Spacer()
Text("영수증 스캔")
Expand All @@ -56,7 +56,7 @@ struct MainWidgetEntryView: View {

Divider()
.background(Color(uiColor: Colors.Gray._5))
Link(destination: URL(string: "widget://LedgerDetail")!) {
Link(destination: LinkManager.ledgerDetail.url) {
HStack {
Spacer()
Text("회비 내역 확인")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct OCRWidgetEntryView: View {
.font(.system(size: 16, weight: .bold))
}
.padding(.vertical, 20)
.widgetURL(URL(string: "widget://OCR"))
.widgetURL(LinkManager.ocr.url)
.widgetBackground(Color(uiColor: Colors.Gray._1))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct SecondWidgetEntryView: View {
}

HStack(spacing: 10) {
Link(destination: URL(string: "widget://OCR")!) {
Link(destination: LinkManager.ocr.url) {
Text("영수증 스캔")
.font(.system(size: 12, weight: .bold))
.foregroundColor(Color(uiColor: Colors.White._1))
Expand All @@ -59,7 +59,7 @@ struct SecondWidgetEntryView: View {
.cornerRadius(10)
}

Link(destination: URL(string: "widget://LedgerDetail")!) {
Link(destination: LinkManager.ledgerDetail.url) {
Text("회비 내역 확인")
.font(.system(size: 12, weight: .bold))
.foregroundColor(Color(uiColor: Colors.Blue._4))
Expand Down
13 changes: 11 additions & 2 deletions Projects/Core/Core/Sources/DeepLinkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ public struct DeepLinkManager {
public static var destination: String?

// OCR, CreateLedger, LedgerDetail
public static func setDestination(_ urlString: String) {
public static func setDestination(_ urlString: String, agencyID: Int?) {
let query = urlString.replacingOccurrences(of: "widget://", with: "")
destination = query

NotificationCenter.default.post(name: .init("deeplink"), object: nil, userInfo: ["query": query])
guard let agencyID else { return }

NotificationCenter.default.post(
name: .init("deeplink"),
object: nil,
userInfo: [
"query": query,
"agencyID": agencyID
]
)
}

public static func clear() {
Expand Down
2 changes: 1 addition & 1 deletion Projects/Feature/Main/Sources/MainDIContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class MainDIContainer {
}

func mainTab(with coordinator: Coordinator) -> MainTapViewController {
let tabVC = MainTapViewController(agencyID: localStorage.selectedAgency)
let tabVC = MainTapViewController()
tabVC.coordinator = coordinator
tabVC.setViewControllers(
[agencyTab(with: coordinator),
Expand Down
20 changes: 10 additions & 10 deletions Projects/Feature/Main/Sources/MainTapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import RxSwift
public final class MainTapViewController: UITabBarController {
private let disposeBag = DisposeBag()
weak var coordinator: Coordinator?
private let agencyID: Int?

public init(agencyID: Int?) {
self.agencyID = agencyID
public init() {
super.init(nibName: nil, bundle: nil)
}

Expand Down Expand Up @@ -67,15 +65,17 @@ public final class MainTapViewController: UITabBarController {
private func observeNotification() {
NotificationCenter.default.rx
.notification(.init("deeplink"))
.compactMap { $0.userInfo?["query"] as? String }
.bind(with: self) { owner, query in
guard let agencyID = owner.agencyID else { return }

switch query {
.compactMap { noti -> (query: String, agencyID: Int)? in
guard let query = noti.userInfo?["query"] as? String,
let agencyID = noti.userInfo?["agencyID"] as? Int else { return nil }
return (query, agencyID)
}
.bind(with: self) { owner, userInfo in
switch userInfo.query {
case "OCR":
owner.coordinator?.move(to: .createOCRLedger(agencyID))
owner.coordinator?.move(to: .createOCRLedger(userInfo.agencyID))
case "CreateLedger":
owner.coordinator?.move(to: .createManualLedger(agencyID))
owner.coordinator?.move(to: .createManualLedger(userInfo.agencyID))
case "LedgerDetail":
owner.coordinator?.move(to: .ledger)
default: break
Expand Down

0 comments on commit 890322a

Please sign in to comment.