Skip to content

Commit

Permalink
refactor: address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedbashir committed Dec 15, 2023
1 parent bf42b25 commit 83d62bd
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ struct SignInView_Previews: PreviewProvider {
router: AuthorizationRouterMock(),
config: ConfigMock(),
analytics: AuthorizationAnalyticsMock(),
validator: Validator()
validator: Validator(),
sourceScreen: .default
)

SignInView(viewModel: vm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SignInViewModel: ObservableObject {
@Published private(set) var isShowProgress = false
@Published private(set) var showError: Bool = false
@Published private(set) var showAlert: Bool = false
public var sourceScreen: LogistrationSourceScreen = .default
public var sourceScreen: LogistrationSourceScreen

var errorMessage: String? {
didSet {
Expand Down Expand Up @@ -47,13 +47,15 @@ public class SignInViewModel: ObservableObject {
router: AuthorizationRouter,
config: ConfigProtocol,
analytics: AuthorizationAnalytics,
validator: Validator
validator: Validator,
sourceScreen: LogistrationSourceScreen
) {
self.interactor = interactor
self.router = router
self.config = config
self.analytics = analytics
self.validator = validator
self.sourceScreen = sourceScreen
}

var socialAuthEnabled: Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ struct SignUpView_Previews: PreviewProvider {
analytics: AuthorizationAnalyticsMock(),
config: ConfigMock(),
cssInjector: CSSInjectorMock(),
validator: Validator()
validator: Validator(),
sourceScreen: .default
)

SignUpView(viewModel: vm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SignUpViewModel: ObservableObject {
@Published var scrollTo: Int?
@Published var showError: Bool = false
@Published var thirdPartyAuthSuccess: Bool = false
public var sourceScreen: LogistrationSourceScreen = .default
public var sourceScreen: LogistrationSourceScreen

var errorMessage: String? {
didSet {
Expand All @@ -45,14 +45,16 @@ public class SignUpViewModel: ObservableObject {
analytics: AuthorizationAnalytics,
config: ConfigProtocol,
cssInjector: CSSInjector,
validator: Validator
validator: Validator,
sourceScreen: LogistrationSourceScreen
) {
self.interactor = interactor
self.router = router
self.analytics = analytics
self.config = config
self.cssInjector = cssInjector
self.validator = validator
self.sourceScreen = sourceScreen
}

var socialAuthEnabled: Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public struct StartupView: View {
if searchQuery.isEmpty { return }
viewModel.router.showDiscoveryScreen(
searchQuery: searchQuery,
sourceScreen: LogistrationSourceScreen.startup
sourceScreen: .startup
)
})
.autocapitalization(.none)
Expand All @@ -76,7 +76,7 @@ public struct StartupView: View {
Button {
viewModel.router.showDiscoveryScreen (
searchQuery: searchQuery,
sourceScreen: LogistrationSourceScreen.startup
sourceScreen: .startup
)
} label: {
Text(AuthLocalization.Startup.exploreAllCourses)
Expand Down
26 changes: 15 additions & 11 deletions Course/Course/Presentation/Details/CourseDetailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,21 @@ private struct CourseStateView: View {
.padding(.vertical, 24)
case .alreadyEnrolled:
StyledButton(CourseLocalization.Details.viewCourse, action: {
viewModel.viewCourseClicked(courseId: courseDetails.courseID,
courseName: courseDetails.courseTitle)
viewModel.router.showCourseScreens(
courseID: courseDetails.courseID,
isActive: nil,
courseStart: courseDetails.courseStart,
courseEnd: courseDetails.courseEnd,
enrollmentStart: courseDetails.enrollmentStart,
enrollmentEnd: courseDetails.enrollmentEnd,
title: title
)
if !viewModel.userloggedIn {
viewModel.router.showRegisterScreen(sourceScreen: .courseDetail(courseDetails.courseID, courseDetails.courseTitle))
} else {
viewModel.viewCourseClicked(courseId: courseDetails.courseID,
courseName: courseDetails.courseTitle)
viewModel.router.showCourseScreens(
courseID: courseDetails.courseID,
isActive: nil,
courseStart: courseDetails.courseStart,
courseEnd: courseDetails.courseEnd,
enrollmentStart: courseDetails.enrollmentStart,
enrollmentEnd: courseDetails.enrollmentEnd,
title: title
)
}
})
.padding(16)
}
Expand Down
3 changes: 2 additions & 1 deletion Discovery/Discovery/Presentation/DiscoveryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ struct DiscoveryView_Previews: PreviewProvider {
config: ConfigMock(),
interactor: DiscoveryInteractor.mock,
connectivity: Connectivity(),
analytics: DiscoveryAnalyticsMock())
analytics: DiscoveryAnalyticsMock(),
storage: CoreStorageMock())
let router = DiscoveryRouterMock()

DiscoveryView(viewModel: vm, router: router)
Expand Down
8 changes: 5 additions & 3 deletions Discovery/Discovery/Presentation/DiscoveryViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public class DiscoveryViewModel: ObservableObject {
@Published var showError: Bool = false

var userloggedIn: Bool {
guard let container = Container.shared.resolve(CoreStorage.self) else { return false }
return !(container.user?.username?.isEmpty ?? true)
return !(storage.user?.username?.isEmpty ?? true)
}

var errorMessage: String? {
Expand All @@ -40,19 +39,22 @@ public class DiscoveryViewModel: ObservableObject {
let connectivity: ConnectivityProtocol
private let interactor: DiscoveryInteractorProtocol
private let analytics: DiscoveryAnalytics
private let storage: CoreStorage

public init(
router: DiscoveryRouter,
config: ConfigProtocol,
interactor: DiscoveryInteractorProtocol,
connectivity: ConnectivityProtocol,
analytics: DiscoveryAnalytics
analytics: DiscoveryAnalytics,
storage: CoreStorage
) {
self.router = router
self.config = config
self.interactor = interactor
self.connectivity = connectivity
self.analytics = analytics
self.storage = storage
}

@MainActor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ final class DiscoveryViewModelTests: XCTestCase {
let interactor = DiscoveryInteractorProtocolMock()
let connectivity = Connectivity()
let analytics = DiscoveryAnalyticsMock()
let viewModel = DiscoveryViewModel(router: DiscoveryRouterMock(),
let viewModel = DiscoveryViewModel(router: DiscoveryRouterMock(),
config: ConfigMock(),
interactor: interactor,
connectivity: connectivity,
analytics: analytics)
analytics: analytics,
storage: CoreStorageMock())

let items = [
CourseItem(name: "Test",
Expand Down Expand Up @@ -79,7 +80,8 @@ final class DiscoveryViewModelTests: XCTestCase {
config: ConfigMock(),
interactor: interactor,
connectivity: connectivity,
analytics: analytics)
analytics: analytics,
storage: CoreStorageMock())
let items = [
CourseItem(name: "Test",
org: "org",
Expand Down Expand Up @@ -126,7 +128,8 @@ final class DiscoveryViewModelTests: XCTestCase {
config: ConfigMock(),
interactor: interactor,
connectivity: connectivity,
analytics: analytics)
analytics: analytics,
storage: CoreStorageMock())
let items = [
CourseItem(name: "Test",
org: "org",
Expand Down Expand Up @@ -175,7 +178,8 @@ final class DiscoveryViewModelTests: XCTestCase {
config: ConfigMock(),
interactor: interactor,
connectivity: connectivity,
analytics: analytics)
analytics: analytics,
storage: CoreStorageMock())

let noInternetError = AFError.sessionInvalidated(error: URLError(.notConnectedToInternet))

Expand All @@ -198,7 +202,8 @@ final class DiscoveryViewModelTests: XCTestCase {
config: ConfigMock(),
interactor: interactor,
connectivity: connectivity,
analytics: analytics)
analytics: analytics,
storage: CoreStorageMock())

let noInternetError = AFError.sessionInvalidated(error: NSError())

Expand Down
18 changes: 11 additions & 7 deletions OpenEdX/DI/ScreenAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ class ScreenAssembly: Assembly {
}

// MARK: MainScreenView
container.register(MainScreenViewModel.self) { r in
container.register(MainScreenViewModel.self) { r, sourceScreen in
MainScreenViewModel(
analytics: r.resolve(MainScreenAnalytics.self)!,
config: r.resolve(ConfigProtocol.self)!,
profileInteractor: r.resolve(ProfileInteractorProtocol.self)!
profileInteractor: r.resolve(ProfileInteractorProtocol.self)!,
sourceScreen: sourceScreen
)
}
// MARK: Startup screen
Expand All @@ -51,23 +52,25 @@ class ScreenAssembly: Assembly {
}

// MARK: SignIn
container.register(SignInViewModel.self) { r in
container.register(SignInViewModel.self) { r, sourceScreen in
SignInViewModel(
interactor: r.resolve(AuthInteractorProtocol.self)!,
router: r.resolve(AuthorizationRouter.self)!,
config: r.resolve(ConfigProtocol.self)!,
analytics: r.resolve(AuthorizationAnalytics.self)!,
validator: r.resolve(Validator.self)!
validator: r.resolve(Validator.self)!,
sourceScreen: sourceScreen
)
}
container.register(SignUpViewModel.self) { r in
container.register(SignUpViewModel.self) { r, sourceScreen in
SignUpViewModel(
interactor: r.resolve(AuthInteractorProtocol.self)!,
router: r.resolve(AuthorizationRouter.self)!,
analytics: r.resolve(AuthorizationAnalytics.self)!,
config: r.resolve(ConfigProtocol.self)!,
cssInjector: r.resolve(CSSInjector.self)!,
validator: r.resolve(Validator.self)!
validator: r.resolve(Validator.self)!,
sourceScreen: sourceScreen
)
}
container.register(ResetPasswordViewModel.self) { r in
Expand Down Expand Up @@ -103,7 +106,8 @@ class ScreenAssembly: Assembly {
config: r.resolve(ConfigProtocol.self)!,
interactor: r.resolve(DiscoveryInteractorProtocol.self)!,
connectivity: r.resolve(ConnectivityProtocol.self)!,
analytics: r.resolve(DiscoveryAnalytics.self)!
analytics: r.resolve(DiscoveryAnalytics.self)!,
storage: r.resolve(CoreStorage.self)!
)
}

Expand Down
5 changes: 4 additions & 1 deletion OpenEdX/RouteController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ class RouteController: UIViewController {
let controller = UIHostingController(rootView: whatsNewView)
navigation.viewControllers = [controller]
} else {
let viewModel = Container.shared.resolve(MainScreenViewModel.self)!
let viewModel = Container.shared.resolve(
MainScreenViewModel.self,
argument: LogistrationSourceScreen.default
)!
let controller = UIHostingController(rootView: MainScreenView(viewModel: viewModel))
navigation.viewControllers = [controller]
}
Expand Down
25 changes: 16 additions & 9 deletions OpenEdX/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public class Router: AuthorizationRouter,
var storage = Container.shared.resolve(WhatsNewStorage.self)!
let config = Container.shared.resolve(ConfigProtocol.self)!

let viewModel = WhatsNewViewModel(storage: storage)
viewModel.sourceScreen = sourceScreen
let viewModel = WhatsNewViewModel(storage: storage, sourceScreen: sourceScreen)
let whatsNew = WhatsNewView(router: Container.shared.resolve(WhatsNewRouter.self)!, viewModel: viewModel)
let shouldShowWhatsNew = viewModel.shouldShowWhatsNew()

Expand All @@ -76,18 +75,24 @@ public class Router: AuthorizationRouter,
navigationController.viewControllers = [controller]
navigationController.setViewControllers([controller], animated: true)
} else {
let viewModel = Container.shared.resolve(MainScreenViewModel.self)!
viewModel.sourceScreen = sourceScreen
let viewModel = Container.shared.resolve(
MainScreenViewModel.self,
argument: sourceScreen
)!


let controller = UIHostingController(rootView: MainScreenView(viewModel: viewModel))
navigationController.viewControllers = [controller]
navigationController.setViewControllers([controller], animated: true)
}
}

public func showLoginScreen(sourceScreen: LogistrationSourceScreen) {
guard let viewModel = Container.shared.resolve(SignInViewModel.self) else { return }

viewModel.sourceScreen = sourceScreen
guard let viewModel = Container.shared.resolve(
SignInViewModel.self,
argument: sourceScreen
) else { return }

let view = SignInView(viewModel: viewModel)
let controller = UIHostingController(rootView: view)
navigationController.pushViewController(controller, animated: true)
Expand Down Expand Up @@ -171,9 +176,11 @@ public class Router: AuthorizationRouter,
}

public func showRegisterScreen(sourceScreen: LogistrationSourceScreen) {
guard let viewModel = Container.shared.resolve(SignUpViewModel.self) else { return }
guard let viewModel = Container.shared.resolve(
SignUpViewModel.self,
argument: sourceScreen
) else { return }

viewModel.sourceScreen = sourceScreen
let view = SignUpView(viewModel: viewModel)
let controller = UIHostingController(rootView: view)
navigationController.pushViewController(controller, animated: true)
Expand Down
9 changes: 7 additions & 2 deletions OpenEdX/View/MainScreenViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ class MainScreenViewModel: ObservableObject {
private let analytics: MainScreenAnalytics
let config: ConfigProtocol
let profileInteractor: ProfileInteractorProtocol
var sourceScreen: LogistrationSourceScreen = .default
var sourceScreen: LogistrationSourceScreen

init(analytics: MainScreenAnalytics, config: ConfigProtocol, profileInteractor: ProfileInteractorProtocol) {
init(analytics: MainScreenAnalytics,
config: ConfigProtocol,
profileInteractor: ProfileInteractorProtocol,
sourceScreen: LogistrationSourceScreen = .default
) {
self.analytics = analytics
self.config = config
self.profileInteractor = profileInteractor
self.sourceScreen = sourceScreen
}

func trackMainDiscoveryTabClicked() {
Expand Down
5 changes: 3 additions & 2 deletions WhatsNew/WhatsNew/Presentation/WhatsNewViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ public class WhatsNewViewModel: ObservableObject {
@Published var index: Int = 0
@Published var newItems: [WhatsNewPage] = []
private let storage: WhatsNewStorage
public var sourceScreen: LogistrationSourceScreen = .default
var sourceScreen: LogistrationSourceScreen

public init(storage: WhatsNewStorage) {
public init(storage: WhatsNewStorage, sourceScreen: LogistrationSourceScreen = .default) {
self.storage = storage
self.sourceScreen = sourceScreen
newItems = loadWhatsNew()
}

Expand Down

0 comments on commit 83d62bd

Please sign in to comment.