Skip to content

Commit

Permalink
Move localized strings to viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
Momo Ozawa committed May 14, 2024
1 parent 862259e commit 52c6b34
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

enum InAppUpdateType {
case flexible
case flexible(AppStoreInfo)
case blocking(AppStoreInfo)
}

Expand Down Expand Up @@ -44,8 +44,8 @@ final class InAppUpdateCoordinator {
}

switch updateType {
case .flexible:
presenter.showNotice()
case .flexible(let appStoreInfo):
presenter.showNotice(using: appStoreInfo)
case .blocking(let appStoreInfo):
presenter.showBlockingUpdate(using: appStoreInfo)
}
Expand All @@ -66,7 +66,7 @@ final class InAppUpdateCoordinator {
return .blocking(appStoreInfo)
}
if currentVersion.isLower(than: appStoreInfo.version) {
return .flexible
return .flexible(appStoreInfo)
}
}

Expand Down
34 changes: 16 additions & 18 deletions WordPress/Classes/Services/InAppUpdate/InAppUpdatePresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ import Foundation
import WordPressFlux

protocol InAppUpdatePresenterProtocol {
func showNotice()
func showNotice(using appStoreInfo: AppStoreInfo)
func showBlockingUpdate(using appStoreInfo: AppStoreInfo)
func openAppStore()
}

final class InAppUpdatePresenter: InAppUpdatePresenterProtocol {
func showNotice() {
func showNotice(using appStoreInfo: AppStoreInfo) {
let viewModel = AppStoreInfoViewModel(appStoreInfo) {
self.openAppStore()
}
let notice = Notice(
title: Strings.Notice.title,
message: Strings.Notice.message,
title: viewModel.title,
message: viewModel.message,
feedbackType: .warning,
style: InAppUpdateNoticeStyle(),
actionTitle: Strings.Notice.update
) { [weak self] _ in
self?.openAppStore()
actionTitle: viewModel.updateButtonTitle,
cancelTitle: viewModel.cancelButtonTitle
) { onAccepted in
if onAccepted {
viewModel.onUpdateTapped()
}
}
ActionDispatcher.dispatch(NoticeAction.post(notice))
// Todo: if the notice is dismissed, show notice again after a defined interval
Expand All @@ -29,23 +35,15 @@ final class InAppUpdatePresenter: InAppUpdatePresenterProtocol {
wpAssertionFailure("Failed to show blocking update view")
return
}
let viewModel = AppStoreInfoViewModel(appStoreInfo) { [weak self] in
self?.openAppStore()
let viewModel = AppStoreInfoViewModel(appStoreInfo) {
self.openAppStore()
}
let controller = BlockingUpdateViewController(viewModel: viewModel)
let navigation = UINavigationController(rootViewController: controller)
topViewController.present(navigation, animated: true)
}

func openAppStore() {
// Todo
}
}

private enum Strings {
enum Notice {
static let title = NSLocalizedString("inAppUpdate.notice.title", value: "App Update Available", comment: "Title for notice displayed when there's a newer version of the app available")
static let message = NSLocalizedString("inAppUpdate.notice.message", value: "To use this app, download the latest version.", comment: "Message for notice displayed when there's a newer version of the app available")
static let update = NSLocalizedString("inAppUpdate.notice.update", value: "Update", comment: "Button title for notice displayed when there's a newer version of the app available")
// TODO
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ struct AppStoreInfoViewModel {
let releaseNotes: [String.SubSequence]
let onUpdateTapped: () -> Void

let title = Strings.title
let message = Strings.message
let whatsNewTitle = Strings.whatsNew
let updateButtonTitle = Strings.Actions.update
let cancelButtonTitle = Strings.Actions.cancel
let moreInfoButtonTitle = Strings.Actions.moreInfo

init(_ appStoreInfo: AppStoreInfo, onUpdateTapped: @escaping () -> Void) {
self.appName = appStoreInfo.trackName
self.version = String(format: Strings.versionFormat, appStoreInfo.version)
Expand All @@ -15,5 +22,14 @@ struct AppStoreInfoViewModel {
}

private enum Strings {
static let versionFormat = NSLocalizedString("inAppUpdate.appStoreInfo.version", value: "Version %@", comment: "Format for latest version available")
static let versionFormat = NSLocalizedString("inAppUpdate.versionFormat", value: "Version %@", comment: "Format for latest version available")
static let title = NSLocalizedString("inAppUpdate.title", value: "App Update Available", comment: "Title for view displayed when there's a newer version of the app available")
static let message = NSLocalizedString("inAppUpdate.message", value: "To use this app, download the latest version.", comment: "Message for view displayed when there's a newer version of the app available")
static let whatsNew = NSLocalizedString("blockingUpdate.whatsNew", value: "What's New", comment: "Section title for what's new in the latest update available")

enum Actions {
static let update = NSLocalizedString("inAppUpdate.action.update", value: "Update", comment: "Update button title")
static let cancel = NSLocalizedString("inAppUpdate.action.cancel", value: "Cancel", comment: "Cancel button title")
static let moreInfo = NSLocalizedString("blockiaction.action.moreInfo", value: "More info", comment: "More info button title")
}
}
21 changes: 5 additions & 16 deletions WordPress/Classes/ViewRelated/InAppUpdate/BlockingUpdateView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ struct BlockingUpdateView: View {

var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(Strings.title)
Text(viewModel.title)
.font(.system(.title))
.fontWeight(.heavy)
.foregroundStyle(.primary)
.padding(.top, 24)

Text(Strings.description)
Text(viewModel.message)
.font(.system(.body))
.foregroundStyle(.secondary)

Expand Down Expand Up @@ -67,7 +67,7 @@ struct BlockingUpdateView: View {

private var whatsNewView: some View {
VStack(alignment: .leading, spacing: 8) {
Text(Strings.whatsNew)
Text(viewModel.whatsNewTitle)
.font(.system(.callout))
.fontWeight(.bold)
.foregroundStyle(.primary)
Expand All @@ -82,23 +82,12 @@ struct BlockingUpdateView: View {

private var buttonsView: some View {
VStack {
DSButton(title: Strings.Button.update, style: .init(emphasis: .primary, size: .large)) {
DSButton(title: viewModel.updateButtonTitle, style: .init(emphasis: .primary, size: .large)) {
viewModel.onUpdateTapped()
}
DSButton(title: Strings.Button.moreInfo, style: .init(emphasis: .tertiary, size: .large)) {
DSButton(title: viewModel.moreInfoButtonTitle, style: .init(emphasis: .tertiary, size: .large)) {
// Todo
}
}
}
}

private enum Strings {
static let title = NSLocalizedString("blockingUpdate.title", value: "App Update Available", comment: "Title for the blocking version update screen")
static let description = NSLocalizedString("blockingUpdate.description", value: "Your app version is out of date. To use this app, download the latest version.", comment: "Description for the blocking version update screen")
static let whatsNew = NSLocalizedString("blockingUpdate.whatsNew", value: "What's New", comment: "Section title for what's new in hte blocking version update screen")

enum Button {
static let update = NSLocalizedString("blockingUpdate.button.update", value: "Update", comment: "Title for button that shows the app store listing when tapped")
static let moreInfo = NSLocalizedString("blockingUpdate.button.moreInfo", value: "More info", comment: "Title for button that shows more information about the update when tapped")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private final class MockInAppUpdatePresenter: InAppUpdatePresenterProtocol {
var didShowBlockingUpdate = false
var didOpenAppStore = false

func showNotice() {
func showNotice(using appStoreInfo: AppStoreInfo) {
didShowNotice = true
}

Expand Down

0 comments on commit 52c6b34

Please sign in to comment.