Skip to content

Commit

Permalink
Refactor and improve news and event details flow
Browse files Browse the repository at this point in the history
  • Loading branch information
matax87 committed Dec 5, 2024
1 parent 107347f commit 691fd84
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 248 deletions.
4 changes: 4 additions & 0 deletions NOICommunity.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
31F0DBD3280F0D3D00E782D5 /* AuthCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31F0DBD2280F02DC00E782D5 /* AuthCoordinator.swift */; };
31FAEA7D28197D9700CDBC1B /* AppCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31FAEA7C28197D9700CDBC1B /* AppCoordinator.swift */; };
31FAEA7F28197EC200CDBC1B /* NavigationCoordinatorType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31FAEA7E28197EC200CDBC1B /* NavigationCoordinatorType.swift */; };
31FFE9502D01E068007C699B /* NewsPageViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31FFE94F2D01E065007C699B /* NewsPageViewController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -458,6 +459,7 @@
31F0DBD2280F02DC00E782D5 /* AuthCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthCoordinator.swift; sourceTree = "<group>"; };
31FAEA7C28197D9700CDBC1B /* AppCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppCoordinator.swift; sourceTree = "<group>"; };
31FAEA7E28197EC200CDBC1B /* NavigationCoordinatorType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationCoordinatorType.swift; sourceTree = "<group>"; };
31FFE94F2D01E065007C699B /* NewsPageViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewsPageViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -523,6 +525,7 @@
3102F45C282C069500687CF7 /* View Controllers */ = {
isa = PBXGroup;
children = (
31FFE94F2D01E065007C699B /* NewsPageViewController.swift */,
3102F45E282C06B700687CF7 /* NewsViewController.swift */,
3182A1BC282D5159005439B4 /* NewsDetailsViewController.swift */,
3182A1BD282D5159005439B4 /* NewsDetailsViewController.xib */,
Expand Down Expand Up @@ -1305,6 +1308,7 @@
31FAEA7F28197EC200CDBC1B /* NavigationCoordinatorType.swift in Sources */,
31157D1926FCA1EB00A43B33 /* WebViewController.swift in Sources */,
31AD1DB22B27947B006F71A4 /* ComeOnBoardOnboardingViewModel.swift in Sources */,
31FFE9502D01E068007C699B /* NewsPageViewController.swift in Sources */,
312F5D272808250000C84598 /* AuthWelcomeViewController.swift in Sources */,
31260A652CFF68CF00ADBDEF /* EventDetailsViewModel.swift in Sources */,
3185AA172820005700767E31 /* KeychainAuthStateStorageClient.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,24 @@ private extension AppCoordinator {
showAuthCoordinator(animated: animated)
}

func showNewsExternalLink(of news: Article, sender: Any?) {
func showNewsExternalLink(
of news: Article,
from viewController: UIViewController
) {
let author = localizedValue(from: news.languageToAuthor)
let safariVC = SFSafariViewController(url: author!.externalURL!)
navigationController.presentedViewController?.present(
navigationController.presentedViewController?.present(
safariVC,
animated: true
)
}

func showNewsAskAQuestion(for news: Article, sender: Any?) {
func showNewsAskAQuestion(
for news: Article,
from viewController: UIViewController
) {
let author = localizedValue(from: news.languageToAuthor)
navigationController.presentedViewController?.mailTo(
navigationController.presentedViewController?.mailTo(
author!.email!,
delegate: self,
completion: nil
Expand All @@ -198,25 +204,13 @@ private extension AppCoordinator {
func showNewsDetails(newsId: String, sender: Any?) {
func configureBindings(
viewModel: NewsDetailsViewModel,
detailsViewController: NewsDetailsViewController
pageViewController: NewsPageViewController
) {
viewModel.showExternalLinkPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] (news, sender) in
self?.showNewsExternalLink(of: news, sender: sender)
}
.store(in: &subscriptions)
viewModel.showAskAQuestionPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] (news, sender) in
self?.showNewsAskAQuestion(for: news, sender: sender)
}
.store(in: &subscriptions)
viewModel.$result
.compactMap { $0 }
.receive(on: DispatchQueue.main)
.sink { [weak detailsViewController] news in
detailsViewController?.navigationItem.title = localizedValue(
.sink { [weak pageViewController] news in
pageViewController?.navigationItem.title = localizedValue(
from: news.languageToDetails
)?
.title
Expand All @@ -225,31 +219,46 @@ private extension AppCoordinator {
}

let viewModel = dependencyContainer.makeNewsDetailsViewModel(
availableNews: nil
)

let detailsVC = dependencyContainer.makeNewsDetailsViewController(
newsId: newsId,
viewModel: viewModel
newsId: newsId
)

let pageVC = {
let pageVC = dependencyContainer.makeNewsPageViewController(
viewModel: viewModel
)

pageVC.externalLinkActionHandler = { [weak self, weak pageVC] in
guard let pageVC
else { return }

self?.showNewsExternalLink(of: $0, from: pageVC)
}
pageVC.askQuestionActionHandler = { [weak self, weak pageVC] in
guard let pageVC
else { return }

self?.showNewsAskAQuestion(for: $0, from: pageVC)
}

pageVC.navigationItem.title = nil
pageVC.navigationItem.largeTitleDisplayMode = .never
pageVC.navigationItem.leftBarButtonItem = UIBarButtonItem(
image: UIImage(systemName: "xmark.circle.fill"),
style: .plain,
target: self,
action: #selector(closeModal(sender:))
)
pageVC.modalPresentationStyle = .fullScreen

return pageVC
}()

configureBindings(
viewModel: viewModel,
detailsViewController: detailsVC
pageViewController: pageVC
)

detailsVC.navigationItem.title = nil
detailsVC.navigationItem.largeTitleDisplayMode = .never
detailsVC.navigationItem.leftBarButtonItem = UIBarButtonItem(
image: UIImage(systemName: "xmark.circle.fill"),
style: .plain,
target: self,
action: #selector(closeModal(sender:))
)
detailsVC.modalPresentationStyle = .fullScreen

navigationController.present(
NavigationController(rootViewController: detailsVC),
NavigationController(rootViewController: pageVC),
animated: true
)
}
Expand Down Expand Up @@ -320,6 +329,7 @@ private extension AppCoordinator {
let pageVC = dependencyContainer.makeEventPageViewController(
viewModel: viewModel
)

pageVC.addToCalendarActionHandler = { [weak self, weak pageVC] in
guard let pageVC
else { return }
Expand All @@ -339,6 +349,7 @@ private extension AppCoordinator {

self?.signupEvent($0, from: pageVC)
}

pageVC.navigationItem.title = nil
pageVC.navigationItem.largeTitleDisplayMode = .never
pageVC.navigationItem.leftBarButtonItem = UIBarButtonItem(
Expand All @@ -348,6 +359,7 @@ private extension AppCoordinator {
action: #selector(closeModal(sender:))
)
pageVC.modalPresentationStyle = .fullScreen

return pageVC
}()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private extension EventsCoordinator {
let pageVC = dependencyContainer.makeEventPageViewController(
viewModel: viewModel
)

pageVC.addToCalendarActionHandler = { [weak self] in
self?.addEventToCalendar($0)
}
Expand All @@ -119,6 +120,7 @@ private extension EventsCoordinator {
pageVC.signupActionHandler = { [weak self] in
self?.signupEvent($0)
}

pageVC.navigationItem.title = event.title
pageVC.navigationItem.largeTitleDisplayMode = .never

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,30 @@ final class EventPageViewController: BasePageViewController<EventDetailsViewMode

private lazy var containerViewController = ContainerViewController()

var locateActionHandler: ((Event) -> Void)?
private var eventDetailsViewController: EventDetailsViewController? {
children
.lazy
.compactMap { $0 as? EventDetailsViewController }
.first
}

var addToCalendarActionHandler: ((Event) -> Void)?
var locateActionHandler: ((Event) -> Void)? {
didSet {
eventDetailsViewController?.locateActionHandler = locateActionHandler
}
}

var signupActionHandler: ((Event) -> Void)?
var addToCalendarActionHandler: ((Event) -> Void)? {
didSet {
eventDetailsViewController?.addToCalendarActionHandler = addToCalendarActionHandler
}
}

var signupActionHandler: ((Event) -> Void)? {
didSet {
eventDetailsViewController?.signupActionHandler = signupActionHandler
}
}

override func configureBindings() {
super.configureBindings()
Expand Down Expand Up @@ -72,7 +91,7 @@ private extension EventPageViewController {
containerViewController.content = content
}

func makeResultContent(for event: Event) -> UIViewController {
func makeResultContent(for event: Event) -> EventDetailsViewController {
let result = EventDetailsViewController(for: event)
result.locateActionHandler = locateActionHandler
result.addToCalendarActionHandler = addToCalendarActionHandler
Expand Down
27 changes: 15 additions & 12 deletions NOICommunity/Factories/DependencyContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,17 @@ extension DependencyContainer: ViewModelFactory {
func makeNewsListViewModel() -> NewsListViewModel {
.init(articlesClient: makeArticlesClient())
}

func makeNewsDetailsViewModel(availableNews: Article?) -> NewsDetailsViewModel {
.init(
articlesClient: makeArticlesClient(),
availableNews: availableNews,
language: nil
)

func makeNewsDetailsViewModel(
newsId: String
) -> NewsDetailsViewModel {
.init(articlesClient: makeArticlesClient(), newsId: newsId)
}

func makeNewsDetailsViewModel(
news: Article
) -> NewsDetailsViewModel {
.init(articlesClient: makeArticlesClient(), news: news)
}

func makePeopleViewModel() -> PeopleViewModel {
Expand Down Expand Up @@ -248,11 +252,10 @@ extension DependencyContainer: ViewControllerFactory {
.init(viewModel: viewModel)
}

func makeNewsDetailsViewController(
newsId: String,
viewModel: NewsDetailsViewModel
) -> NewsDetailsViewController {
.init(newsId: newsId, viewModel: viewModel)
func makeNewsPageViewController(
viewModel: NewsDetailsViewModel
) -> NewsPageViewController {
.init(viewModel: viewModel)
}

func makeMeetMainViewController(
Expand Down
5 changes: 2 additions & 3 deletions NOICommunity/Factories/ViewControllerFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ protocol ViewControllerFactory {
viewModel: NewsListViewModel
) -> NewsViewController

func makeNewsDetailsViewController(
newsId: String,
func makeNewsPageViewController(
viewModel: NewsDetailsViewModel
) -> NewsDetailsViewController
) -> NewsPageViewController

func makeMeetMainViewController(
viewModel: PeopleViewModel
Expand Down
12 changes: 8 additions & 4 deletions NOICommunity/Factories/ViewModelFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ protocol ViewModelFactory {
func makeMyAccountViewModel() -> MyAccountViewModel

func makeNewsListViewModel() -> NewsListViewModel

func makeNewsDetailsViewModel(
availableNews: Article?
) -> NewsDetailsViewModel

func makeNewsDetailsViewModel(
newsId: String
) -> NewsDetailsViewModel

func makeNewsDetailsViewModel(
news: Article
) -> NewsDetailsViewModel

func makeLoadUserInfoViewModel() -> LoadUserInfoViewModel

Expand Down
54 changes: 27 additions & 27 deletions NOICommunity/NewsFeature/Coordinators/NewsCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ final class NewsCoordinator: BaseNavigationCoordinator {

override func start(animated: Bool) {
newsListViewModel = dependencyContainer.makeNewsListViewModel()
newsListViewModel.showDetailsHandler = { [weak self] in
self?.goToDetails(of: $0, sender: $1)
newsListViewModel.showDetailsHandler = { [weak self] news, _ in
self?.goToDetails(of: news)
}
mainVC = dependencyContainer.makeNewsViewController(
viewModel: newsListViewModel
Expand All @@ -45,40 +45,40 @@ final class NewsCoordinator: BaseNavigationCoordinator {

private extension NewsCoordinator {

func goToDetails(of news: Article, sender: Any?) {
func goToDetails(of news: Article) {
let viewModel = dependencyContainer.makeNewsDetailsViewModel(
availableNews: news
news: news
)
viewModel.showExternalLinkPublisher
.sink { [weak self] (news, sender) in
self?.showExternalLink(of: news, sender: sender)
}
.store(in: &subscriptions)
viewModel.showAskAQuestionPublisher
.sink { [weak self] (news, sender) in
self?.showAskAQuestion(for: news, sender: sender)
}
.store(in: &subscriptions)
let detailVC = dependencyContainer.makeNewsDetailsViewController(
newsId: news.id,
viewModel: viewModel
)
detailVC.navigationItem.title = localizedValue(
from: news.languageToDetails
)?
.title
detailVC.navigationItem.largeTitleDisplayMode = .never
navigationController.pushViewController(detailVC, animated: true)
let pageVC = {
let pageVC = dependencyContainer.makeNewsPageViewController(
viewModel: viewModel
)

pageVC.externalLinkActionHandler = { [weak self] in
self?.showExternalLink(of: $0)
}
pageVC.askQuestionActionHandler = { [weak self] in
self?.showAskAQuestion(for: $0)
}
pageVC.navigationItem.title = localizedValue(
from: news.languageToDetails
)?
.title
pageVC.navigationItem.largeTitleDisplayMode = .never

return pageVC
}()
navigationController.pushViewController(pageVC, animated: true)
}

func showExternalLink(of news: Article, sender: Any?) {
func showExternalLink(of news: Article) {
let author = localizedValue(from: news.languageToAuthor)
let safariVC = SFSafariViewController(url: author!.externalURL!)
navigationController.present(safariVC, animated: true)
}

func showAskAQuestion(for news: Article, sender: Any?) {
func showAskAQuestion(for news: Article) {
let author = localizedValue(from: news.languageToAuthor)
navigationController.mailTo(
author!.email!,
Expand Down
Loading

0 comments on commit 691fd84

Please sign in to comment.