use env object for ProfileContainerView

This commit is contained in:
Satindar Dhillon 2022-02-23 22:49:58 -08:00
parent 732941a620
commit bb63806f08
4 changed files with 30 additions and 78 deletions

View file

@ -4,7 +4,7 @@ import Views
// TODO: maybe this can be removed??
enum PrimaryContentCategory: Identifiable, Hashable, Equatable {
case feed(viewModel: HomeFeedViewModel)
case profile(viewModel: ProfileContainerViewModel)
case profile
static func == (lhs: PrimaryContentCategory, rhs: PrimaryContentCategory) -> Bool {
lhs.id == rhs.id
@ -40,8 +40,8 @@ enum PrimaryContentCategory: Identifiable, Hashable, Equatable {
switch self {
case let .feed(viewModel: viewModel):
HomeFeedView(viewModel: viewModel)
case let .profile(viewModel: viewModel):
ProfileContainerView(viewModel: viewModel)
case .profile:
ProfileContainerView()
}
}

View file

@ -9,13 +9,6 @@ import Views
extension HomeFeedViewModel {
static func make(services: Services) -> HomeFeedViewModel {
let viewModel = HomeFeedViewModel()
#if os(iOS)
if UIDevice.isIPhone {
viewModel.profileContainerViewModel = ProfileContainerViewModel.make(services: services)
}
#endif
viewModel.bind(services: services)
viewModel.loadItems(dataService: services.dataService, searchQuery: nil, isRefresh: false)
return viewModel
@ -185,22 +178,9 @@ extension HomeFeedViewModel {
}
}
private func startNetworkActivityIndicator() {
#if os(iOS)
UIApplication.shared.isNetworkActivityIndicatorVisible = true
#endif
}
private func stopNetworkActivityIndicator() {
#if os(iOS)
UIApplication.shared.isNetworkActivityIndicatorVisible = false
#endif
}
// TODO: remove this view model
final class HomeFeedViewModel: ObservableObject {
var currentDetailViewModel: LinkItemDetailViewModel?
var profileContainerViewModel: ProfileContainerViewModel?
@Published var items = [FeedItem]()
@Published var isLoading = false
@ -452,15 +432,13 @@ struct HomeFeedView: View {
var body: some View {
#if os(iOS)
if UIDevice.isIPhone, let profileContainerViewModel = viewModel.profileContainerViewModel {
if UIDevice.isIPhone {
NavigationView {
conditionalInnerBody
.toolbar {
ToolbarItem {
NavigationLink(
destination: {
ProfileContainerView(viewModel: profileContainerViewModel)
},
destination: { ProfileContainerView() },
label: {
Image.profile
.resizable()
@ -484,3 +462,15 @@ struct HomeFeedView: View {
viewModel.performActionSubject.send(.refreshItems(query: searchQuery))
}
}
private func startNetworkActivityIndicator() {
#if os(iOS)
UIApplication.shared.isNetworkActivityIndicatorVisible = true
#endif
}
private func stopNetworkActivityIndicator() {
#if os(iOS)
UIApplication.shared.isNetworkActivityIndicatorVisible = false
#endif
}

View file

@ -5,11 +5,9 @@ import Views
public struct PrimaryContentView: View {
let homeFeedViewModel: HomeFeedViewModel
let profileContainerViewModel: ProfileContainerViewModel
public init(services: Services) {
self.homeFeedViewModel = HomeFeedViewModel.make(services: services)
self.profileContainerViewModel = ProfileContainerViewModel.make(services: services)
}
public var body: some View {
@ -33,7 +31,7 @@ public struct PrimaryContentView: View {
private var regularView: some View {
let categories = [
PrimaryContentCategory.feed(viewModel: homeFeedViewModel),
PrimaryContentCategory.profile(viewModel: profileContainerViewModel)
PrimaryContentCategory.profile
]
return NavigationView {

View file

@ -5,31 +5,13 @@ import SwiftUI
import Utils
import Views
// TODO: remove this view model
extension ProfileContainerViewModel {
static func make(services: Services) -> ProfileContainerViewModel {
let viewModel = ProfileContainerViewModel()
viewModel.bind(services: services)
return viewModel
}
final class ProfileContainerViewModel: ObservableObject {
@Published var isLoading = false
@Published var profileCardData = ProfileCardData()
func bind(services: Services) {
performActionSubject.sink { [weak self] action in
switch action {
case .logout:
services.authenticator.logout()
case .loadProfileData:
self?.loadProfileData(dataService: services.dataService)
case .showIntercomMessenger:
DataService.showIntercomMessenger?()
case .deleteAccount:
print("delete account")
}
}
.store(in: &subscriptions)
}
var subscriptions = Set<AnyCancellable>()
private func loadProfileData(dataService: DataService) {
func loadProfileData(dataService: DataService) {
dataService.viewerPublisher().sink(
receiveCompletion: { _ in },
receiveValue: { [weak self] viewer in
@ -44,30 +26,12 @@ extension ProfileContainerViewModel {
}
}
final class ProfileContainerViewModel: ObservableObject {
@Published var isLoading = false
@Published var profileCardData = ProfileCardData()
enum Action {
case logout
case loadProfileData
case showIntercomMessenger
case deleteAccount
}
var subscriptions = Set<AnyCancellable>()
let performActionSubject = PassthroughSubject<Action, Never>()
init() {}
}
struct ProfileContainerView: View {
@ObservedObject private var viewModel: ProfileContainerViewModel
@State private var showLogoutConfirmation = false
@EnvironmentObject var authenticator: Authenticator
@EnvironmentObject var dataService: DataService
init(viewModel: ProfileContainerViewModel) {
self.viewModel = viewModel
}
@ObservedObject private var viewModel = ProfileContainerViewModel()
@State private var showLogoutConfirmation = false
var body: some View {
#if os(iOS)
@ -87,7 +51,7 @@ struct ProfileContainerView: View {
Section {
ProfileCard(data: viewModel.profileCardData)
.onAppear {
viewModel.performActionSubject.send(.loadProfileData)
viewModel.loadProfileData(dataService: dataService)
}
}
@ -114,7 +78,7 @@ struct ProfileContainerView: View {
if FeatureFlag.showAccountDeletion {
NavigationLink(
destination: ManageAccountView(handleAccountDeletion: {
viewModel.performActionSubject.send(.deleteAccount)
print("delete account")
})
) {
Text("Manage Account")
@ -129,7 +93,7 @@ struct ProfileContainerView: View {
Alert(
title: Text("Are you sure you want to logout?"),
primaryButton: .destructive(Text("Confirm")) {
viewModel.performActionSubject.send(.logout)
authenticator.logout()
},
secondaryButton: .cancel()
)