Skip to content

Commit

Permalink
#297: Share Extension의 '앱에서 보기' 버튼 클릭 시, 앱으로 이동해 Share Extension에서 드…
Browse files Browse the repository at this point in the history
…랍한 아이템에 대한 커뮤니티 Scene에서 렌더링

- Share Extension에서 드랍한 아이템 ID, UserDefaults에 저장 및 메인 Scene ViewDidAppear 또는 Bakcground -> Foreground 전환 시 커뮤니티 Scene 렌더링
  • Loading branch information
joseph704 committed Aug 22, 2024
1 parent bbfe4db commit 98efeaa
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 24 deletions.
8 changes: 8 additions & 0 deletions StreetDrop/ShareExtension/View/DropDone/DropDoneView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ final class DropDoneView: UIView {
var exitButtonEvent: Observable<Void> {
exitButtonEventRelay.asObservable()
}
private let viewOnAppButtonEventRelay: PublishRelay<Void> = .init()
var viewOnAppButtonEvent: Observable<Void> {
viewOnAppButtonEventRelay.asObservable()
}

private let droppedMusic: Music
private let droppedAddress: String
Expand Down Expand Up @@ -150,6 +154,10 @@ private extension DropDoneView {
exitButton.rx.tap
.bind(to: exitButtonEventRelay)
.disposed(by: disposeBag)

viewOnAppButton.rx.tap
.bind(to: viewOnAppButtonEventRelay)
.disposed(by: disposeBag)
}

func configureUI() {
Expand Down
58 changes: 54 additions & 4 deletions StreetDrop/ShareExtension/View/ShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@ final class ShareViewController: UIViewController, Alertable {
return reSearchingMusicForSharingView
}()

private var dropDoneView: DropDoneView?

private let failedLoadingMusicView: FailedLoadingMusicView = {
let failedLoadingMusicView: FailedLoadingMusicView = .init()
failedLoadingMusicView.isHidden = true
Expand Down Expand Up @@ -424,13 +422,13 @@ private extension ShareViewController {
containerView.isHidden = true
reSearchingMusicForSharingView.isHidden = true

dropDoneView = .init(
let dropDoneView: DropDoneView = .init(
droppedMusic: droppedMusic,
droppedAddress: droppedAddress,
droppedComment: droppedComment
)
guard let dropDoneView = dropDoneView else { return }
view.addSubview(dropDoneView)

dropDoneView.snp.makeConstraints {
$0.horizontalEdges.bottom.equalToSuperview()
}
Expand All @@ -444,6 +442,58 @@ private extension ShareViewController {
}
.disposed(by: disposeBag)

dropDoneView.viewOnAppButtonEvent
.bind(with: self) { owner, _ in
guard let droppedItemID = owner.viewModel.itemID else {
owner.showAlert(
type: .alert(
onConfirm: { [weak self] in
self?.extensionContext?.completeRequest(
returningItems: nil,
completionHandler: nil
)
}
),
state: .primary,
title: "에러 발생",
subText: "드랍된 ID를 찾을 수 없습니다.",
buttonTitle: "확인"
)
return
}

guard let url = URL(string: "streetdrop://") else { return }
var responder = self as UIResponder?
while responder != nil {
if let application = responder as? UIApplication {
guard let sharedDefaults = UserDefaults(suiteName: "group.com.depromeet.StreetDrop") else {
owner.showAlert(
type: .alert(
onConfirm: { [weak self] in
self?.extensionContext?.completeRequest(
returningItems: nil,
completionHandler: nil
)
}
),
state: .primary,
title: "에러 발생",
subText: "드랍 아이템 ID 내부 데이터 저장 실패",
buttonTitle: "확인"
)
return
}
sharedDefaults.set(droppedItemID, forKey: "ShareExtensionDroppedItemID")
sharedDefaults.synchronize()

application.open(url, options: [:], completionHandler: nil)
break
}
responder = responder?.next
}
}
.disposed(by: disposeBag)

view.layoutIfNeeded()
})
})
Expand Down
6 changes: 4 additions & 2 deletions StreetDrop/ShareExtension/ViewModel/ShareViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class ShareViewModel: NSObject, ShareViewModelType {
var sharedSongName: String = ""
var selectedMusic: Music?
var comment: String?
var itemID: Int?

init(
searchMusicUsecase: SearchMusicUsecase = DefaultSearchingMusicUsecase(),
Expand Down Expand Up @@ -131,14 +132,15 @@ final class ShareViewModel: NSObject, ShareViewModelType {
return
}

owner.dropMusicUseCase.drop(
owner.dropMusicUseCase.dropMusicResponsingOnlyItemID(
droppingInfo: .init(
location: currentLocation,
music: selectedMusic
),
content: comment
)
.subscribe(with: self) { owner, statusCode in
.subscribe(with: self) { owner, itemID in
owner.itemID = itemID
owner.output.goDropDoneViewRelay.accept(
(selectedMusic, currentLocation.address, comment)
)
Expand Down
36 changes: 18 additions & 18 deletions StreetDrop/StreetDrop/Application/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
guard let url = URLContexts.first?.url else { return }
handleDeepLink(isLaunched: false, url: url)
}

func navigateToCommunity(with itemID: Int) {
let sceneDelegate = UIApplication.shared.connectedScenes
.first!.delegate as? SceneDelegate

if let navigationView = topViewController(base: sceneDelegate?.window!.rootViewController)?
.navigationController as? UINavigationController{

let communityViewModel = CommunityViewModel(
communityInfos: [],
index: 0
)
communityViewModel.itemID = itemID

let communityView = CommunityViewController(viewModel: communityViewModel)
navigationView.pushViewController(communityView, animated: true)
}
}
}

// MARK: - Private Methods
Expand Down Expand Up @@ -89,24 +107,6 @@ private extension SceneDelegate {
}
}

func navigateToCommunity(with itemID: Int) {
let sceneDelegate = UIApplication.shared.connectedScenes
.first!.delegate as? SceneDelegate

if let navigationView = topViewController(base: sceneDelegate?.window!.rootViewController)?
.navigationController as? UINavigationController{

let communityViewModel = CommunityViewModel(
communityInfos: [],
index: 0
)
communityViewModel.itemID = itemID

let communityView = CommunityViewController(viewModel: communityViewModel)
navigationView.pushViewController(communityView, animated: true)
}
}

func topViewController(base: UIViewController?) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ final class MainViewController: UIViewController, Toastable, Alertable {
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

deinit {
NotificationCenter.default.removeObserver(
self,
name: UIApplication.willEnterForegroundNotification,
object: nil
)
}

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -64,6 +72,13 @@ final class MainViewController: UIViewController, Toastable, Alertable {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
viewDidAppearEvent.accept(Void())
NotificationCenter.default.addObserver(
self,
selector: #selector(configureShareExtensionViewOnAppButton),
name: UIApplication.willEnterForegroundNotification,
object: nil
)
configureShareExtensionViewOnAppButton()
}

// MARK: - UI
Expand Down Expand Up @@ -977,3 +992,22 @@ extension MainViewController: NMFMapViewCameraDelegate {
self.locationOverlay.circleRadius = viewModel.userCircleRadius / naverMapView.projection.metersPerPixel()
}
}

// MARK: - Share Extension

private extension MainViewController {
@objc func configureShareExtensionViewOnAppButton() {
guard let sharedDefaults = UserDefaults(suiteName: "group.com.depromeet.StreetDrop") else {
print("Failed to access shared UserDefaults.")
return
}

guard let shareExtensionDroppedItemID = sharedDefaults.object(forKey: "ShareExtensionDroppedItemID") as? Int,
let scene = view.window?.windowScene,
let sceneDelegate = scene.delegate as? SceneDelegate else { return }

sceneDelegate.navigateToCommunity(with: shareExtensionDroppedItemID)
sharedDefaults.removeObject(forKey: "ShareExtensionDroppedItemID")
sharedDefaults.synchronize()
}
}

0 comments on commit 98efeaa

Please sign in to comment.