Skip to content

Commit

Permalink
Merge pull request #46 from iamport/fix/card-app-infinite-popup
Browse files Browse the repository at this point in the history
�카드사 앱 팝업 무한 호출 현상 수정
  • Loading branch information
youjmen authored Oct 7, 2024
2 parents e937472 + 9e5951e commit 202f7d2
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let package = Package(
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/ReactiveX/RxSwift.git", .upToNextMajor(from: "6.0.0")),
.package(name: "RxBusForPort", url: "https://github.com/iamport/RxBus-Swift", .upToNextMinor(from: "1.3.0")),
.package(name: "RxBusForPort", url: "https://github.com/iamport/RxBus-Swift", .upToNextMinor(from: "1.3.6-alpha.1")),
.package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.4.0")),
.package(url: "https://github.com/devxoul/Then.git", .upToNextMajor(from: "2.7.0")),
.package(url: "https://github.com/nicklockwood/SwiftFormat", from: "0.50.4")
Expand Down
22 changes: 16 additions & 6 deletions Sources/iamport-ios/Classes/Domain/StrategyRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@ class StrategyRepository {
enum PaymentKind {
case CHAI, NICE, WEB, INICIS
}
init(eventBus: EventBus) {
self.eventBus = eventBus
judgeStrategy = JudgeStrategy(eventBus: self.eventBus)
chaiStrategy = ChaiStrategy(eventBus: self.eventBus)
webViewStrategy = WebViewStrategy(eventBus: self.eventBus)
niceTransWebViewStrategy = NiceTransWebViewStrategy(eventBus: self.eventBus)
inicisTransWebViewStrategy = InicisTransWebViewStrategy(eventBus: self.eventBus)
certificationWebViewStrategy = CertificationWebViewStrategy(eventBus: self.eventBus)
}
let eventBus: EventBus

let judgeStrategy = JudgeStrategy() // 결제 판별
let chaiStrategy = ChaiStrategy() // 결제 중 BG 폴링하는 차이 전략
let judgeStrategy: JudgeStrategy // 결제 판별
let chaiStrategy: ChaiStrategy // 결제 중 BG 폴링하는 차이 전략

private let webViewStrategy = WebViewStrategy() // webview 사용하는 pg
private let niceTransWebViewStrategy = NiceTransWebViewStrategy()
private let inicisTransWebViewStrategy = InicisTransWebViewStrategy()
private let webViewStrategy: WebViewStrategy // webview 사용하는 pg
private let niceTransWebViewStrategy: NiceTransWebViewStrategy
private let inicisTransWebViewStrategy: InicisTransWebViewStrategy

private let certificationWebViewStrategy = CertificationWebViewStrategy()
private let certificationWebViewStrategy: CertificationWebViewStrategy

/**
* PG 와 PayMethod 로 결제 타입하여 가져옴
Expand Down
10 changes: 7 additions & 3 deletions Sources/iamport-ios/Classes/Domain/strategy/BaseStrategy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import RxSwift
public class BaseStrategy: IStrategy {
var disposeBag = DisposeBag()
var request: IamportRequest?

let eventBus: EventBus

init(eventBus: EventBus) {
self.eventBus = eventBus
}
func clear() {
request = nil
disposeBag = DisposeBag()
Expand Down Expand Up @@ -38,14 +42,14 @@ public class BaseStrategy: IStrategy {

func finish(_ response: IamportResponse?) {
clear()
EventBus.shared.impResponseRelay.accept(response)
eventBus.impResponseRelay.accept(response)
}

func doWork(_ request: IamportRequest) {
clear()
self.request = request

EventBus.shared.clearBus.subscribe { [weak self] _ in
eventBus.clearBus.subscribe { [weak self] _ in
self?.clear()
}.disposed(by: disposeBag)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import WebKit
public class BaseWebViewStrategy: IStrategy {
var disposeBag = DisposeBag()
var request: IamportRequest?
let eventBus: EventBus

init(eventBus: EventBus) {
self.eventBus = eventBus
}

func clear() {
request = nil
Expand Down Expand Up @@ -39,7 +44,7 @@ public class BaseWebViewStrategy: IStrategy {
clear()
self.request = request

EventBus.shared.clearBus.subscribe { [weak self] _ in
eventBus.clearBus.subscribe { [weak self] _ in
self?.clear() // 종료 없이 only clear
}.disposed(by: disposeBag)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ public class JudgeStrategy: BaseStrategy {
enum JudgeKind {
case CHAI, WEB, CERT, ERROR
}

var ignoreNative = false

override init(eventBus: EventBus) {
super.init(eventBus: eventBus)
}
func doWork(_ request: IamportRequest, ignoreNative: Bool) {
self.ignoreNative = ignoreNative
doWork(request)
Expand Down
18 changes: 9 additions & 9 deletions Sources/iamport-ios/Classes/Presentation/IamportSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class IamportSdk: Then {
*/
public init(webViewMode: WKWebView) {
webview = webViewMode
iamportWebViewMode = IamportWebViewMode()
iamportWebViewMode = IamportWebViewMode(eventBus: viewModel.eventBus)
}

/**
Expand All @@ -44,7 +44,7 @@ public class IamportSdk: Then {
*/
public init(mobileWebMode: WKWebView) {
webview = mobileWebMode
iamportMobileWebMode = IamportMobileWebMode().then { mode in
iamportMobileWebMode = IamportMobileWebMode(eventBus: viewModel.eventBus).then { mode in
mode.start(webview: mobileWebMode)
}
}
Expand All @@ -61,7 +61,7 @@ public class IamportSdk: Then {
iamportMobileWebMode?.close()

viewModel.clear()
EventBus.shared.clearRelay.accept(())
viewModel.eventBus.clearRelay.accept(())

disposeBag = DisposeBag()
}
Expand Down Expand Up @@ -96,14 +96,14 @@ public class IamportSdk: Then {

private func subscribe(_ request: IamportRequest) {
// 결제결과 옵저빙
EventBus.shared.impResponseBus.subscribe { [weak self] iamportResponse in
viewModel.eventBus.impResponseBus.subscribe { [weak self] iamportResponse in
self?.finish(iamportResponse)
}.disposed(by: disposeBag)

// TODO: subscribe 결제결과

// subscribe 웹뷰열기
EventBus.shared.paymentBus.subscribe { [weak self] event in
viewModel.eventBus.paymentBus.subscribe { [weak self] event in
guard let el = event.element, let pay = el else {
print("Error paymentBus is nil")
return
Expand Down Expand Up @@ -140,12 +140,12 @@ public class IamportSdk: Then {

private func subscribeCertification(_ request: IamportRequest) {
// 본인인증 옵저빙
EventBus.shared.impResponseBus.subscribe { [weak self] iamportResponse in
viewModel.eventBus.impResponseBus.subscribe { [weak self] iamportResponse in
self?.finish(iamportResponse)
}.disposed(by: disposeBag)

// subscribe 웹뷰열기
EventBus.shared.paymentBus.subscribe { [weak self] event in
viewModel.eventBus.paymentBus.subscribe { [weak self] event in
guard let el = event.element, let pay = el else {
print("Error paymentBus is nil")
return
Expand Down Expand Up @@ -229,14 +229,14 @@ public class IamportSdk: Then {
private func openWebViewController(_ request: IamportRequest) {
DispatchQueue.main.async { [weak self] in

EventBus.shared.webViewPaymentRelay.accept(request) // 여기서 먼저 결제 데이터를 넘김
self!.viewModel.eventBus.webViewPaymentRelay.accept(request) // 여기서 먼저 결제 데이터를 넘김

if let wv = self?.webview {
self?.iamportWebViewMode?.start(webview: wv)
return
}

let wvc = WebViewController()
let wvc = WebViewController(eventBus: self!.viewModel.eventBus)

self?.navController?.pushViewController(wvc, animated: self?.animate ?? true)

Expand Down
14 changes: 10 additions & 4 deletions Sources/iamport-ios/Classes/Presentation/IamportWebViewMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ import WebKit

class IamportWebViewMode: UIView, WKUIDelegate {
var disposeBag = DisposeBag()
let viewModel = WebViewModel()
let viewModel: WebViewModel

var webview: WKWebView?
var request: IamportRequest?
init(eventBus: EventBus) {
self.viewModel = WebViewModel(eventBus: eventBus)
super.init(frame: .zero)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func start(webview: WKWebView) {
debug_log("IamportWebViewMode :: start")
Expand Down Expand Up @@ -68,10 +75,9 @@ class IamportWebViewMode: UIView, WKUIDelegate {

internal func subscribePayment() {
debug_log("IamportWebViewMode :: subscribePayment")
let eventBus = EventBus.shared

// 결제 데이터
eventBus.webViewPaymentBus.subscribe { [weak self] event in
viewModel.eventBus.webViewPaymentBus.subscribe { [weak self] event in
guard let elem = event.element, let payment = elem else {
print("IamportWebViewMode :: Error not found PaymentEvent")
return
Expand Down Expand Up @@ -233,7 +239,7 @@ class IamportWebViewMode: UIView, WKUIDelegate {
debug_dump(iamportResponse)

close()
EventBus.shared.impResponseRelay.accept(iamportResponse)
viewModel.eventBus.impResponseRelay.accept(iamportResponse)
}

/**
Expand Down
12 changes: 8 additions & 4 deletions Sources/iamport-ios/Classes/Presentation/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import RxSwift

class MainViewModel {
private var disposeBag = DisposeBag()
private let repository = StrategyRepository() // TODO: dependency inject

let eventBus: EventBus
private let repository: StrategyRepository // TODO: dependency inject
func clear() {
disposeBag = DisposeBag()
}
init() {
eventBus = EventBus()
repository = StrategyRepository(eventBus: eventBus)
}

func judgePayment(_ request: IamportRequest, ignoreNative: Bool = false) {
subscribe()
Expand All @@ -23,7 +27,7 @@ class MainViewModel {
if !valid {
IamportResponse.makeFail(request: request, msg: desc).do { it in
self?.clear()
EventBus.shared.impResponseRelay.accept(it)
self!.eventBus.impResponseRelay.accept(it)
}
}
}
Expand Down Expand Up @@ -56,7 +60,7 @@ class MainViewModel {
}
}
case .WEB, .CERT:
EventBus.shared.paymentRelay.accept(judge.2) // 웹뷰 컨트롤러 열기
eventBus.paymentRelay.accept(judge.2) // 웹뷰 컨트롤러 열기
case .ERROR:
print("판단불가 \(judge)")
}
Expand Down
17 changes: 13 additions & 4 deletions Sources/iamport-ios/Classes/Presentation/WebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import UIKit
import WebKit

class WebViewController: UIViewController, WKUIDelegate, UINavigationBarDelegate {
let eventBus: EventBus
// for communicate WebView
enum JsInterface: String, CaseIterable {
case RECEIVED = "received"
Expand All @@ -21,9 +22,18 @@ class WebViewController: UIViewController, WKUIDelegate, UINavigationBarDelegate
return nil
}
}
init(eventBus: EventBus){
self.eventBus = eventBus
viewModel = WebViewModel(eventBus: self.eventBus)
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

var disposeBag = DisposeBag()
let viewModel = WebViewModel()
let viewModel: WebViewModel

var webView: WKWebView?
var popupWebView: WKWebView? // window.open()으로 열리는 새창
Expand All @@ -42,6 +52,7 @@ class WebViewController: UIViewController, WKUIDelegate, UINavigationBarDelegate
super.viewDidDisappear(animated)
debug_log("viewDidDisappear")
clearAll()
RxBus.shared.removeAllSubscription()
}

override func viewDidLoad() {
Expand Down Expand Up @@ -140,8 +151,6 @@ class WebViewController: UIViewController, WKUIDelegate, UINavigationBarDelegate
}

private func subscribePayment() {
let eventBus = EventBus.shared

// 결제 데이터
eventBus.webViewPaymentBus.subscribe { [weak self] event in
guard let el = event.element, let request = el else {
Expand Down Expand Up @@ -277,7 +286,7 @@ class WebViewController: UIViewController, WKUIDelegate, UINavigationBarDelegate

navigationController?.popViewController(animated: false)
dismiss(animated: true) {
EventBus.shared.impResponseRelay.accept(iamportResponse)
self.eventBus.impResponseRelay.accept(iamportResponse)
}
}

Expand Down
7 changes: 6 additions & 1 deletion Sources/iamport-ios/Classes/Presentation/WebViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import RxBusForPort
import RxSwift

internal class WebViewModel {
let repository = StrategyRepository()
let eventBus: EventBus
let repository: StrategyRepository
let delegate = IamportWKWebViewDelegate()

init(eventBus: EventBus) {
self.eventBus = eventBus
repository = StrategyRepository(eventBus: self.eventBus)
}
/**
* 뱅크페이 결과 처리
*/
Expand Down

0 comments on commit 202f7d2

Please sign in to comment.