Skip to content

Instantly share code, notes, and snippets.

@jarakys
Created December 13, 2022 16:47
Show Gist options
  • Select an option

  • Save jarakys/943acb41db8bf623ccaf231c3b8c4474 to your computer and use it in GitHub Desktop.

Select an option

Save jarakys/943acb41db8bf623ccaf231c3b8c4474 to your computer and use it in GitHub Desktop.
import SwiftUI
// MAIN MAIN
struct ContentView: View {
@EnvironmentObject var appState: AppState
@EnvironmentObject private var toastCenter: ToastCenter
@StateObject private var flowFabric = FlowFabric()
var body: some View {
ZStack {
if appState.isLoggedIn {
flowFabric.makeFlow()
.ignoresSafeArea(.all, edges: .bottom)
} else {
StylishLoginPageWrapper()
}
}
.toast(isPresented: $toastCenter.toastModel.isShown) {
toastCenter.simpleToast()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
// Create fabric for UI - ok
// Create base ViewModel for each flow, for different users use their own ViewModel
// BaseLoginViewModel
// UserLoginViewModel
// ResearcherViewModel
// Each UI can be different, we need separate "common" UI in a file
// MAIN FLOW
class FlowFabric: ObservableObject {
lazy var uiFabric: any FlowUI = {
let fabric: any FlowUI = 1 == 1 ? UserUI() : ResearcherUI()
return fabric
}()
@ViewBuilder public func makeFlow() -> some View {
AnyView(uiFabric.makeFlow())
}
}
protocol FlowUI {
associatedtype T: View
@ViewBuilder func makeFlow() -> T
}
protocol FlowContentMakeable {
associatedtype T: View
@ViewBuilder func makeContent() -> T
}
class UserUI: FlowUI {
func makeFlow() -> some View {
MainTabBarView(fabric: MainTabBarContentFabric())
}
class MainTabBarContentFabric: FlowContentMakeable, ObservableObject {
@Binding public var isHidden: Bool
@Binding public var selectedTabIndexIs: Int
init() {
_isHidden = .init(get: {
AppState.shared.tabBarIsHidden
}, set: { value in
AppState.shared.tabBarIsHidden = value
})
_selectedTabIndexIs = .init(get: {
AppState.shared.selectedTabIndexIs
}, set: { value in
AppState.shared.selectedTabIndexIs = value
})
}
func makeContent() -> some View {
UIKitTabView(selectedIndex: $selectedTabIndexIs, isHidden: $isHidden, tabs: [
HomePageWrapper()
.tab(image: SelectedStateTabBarState.homePage.imageName),
MainCatalogueWrapper()
.tab(image: SelectedStateTabBarState.catalog.imageName),
SuggestionsCatalogWrapper()
.tab(image: SelectedStateTabBarState.suggestion.imageName),
NewProductWrapper()
.tab(image: SelectedStateTabBarState.addProduct.imageName)
])
}
}
}
class ResearcherUI: FlowUI {
func makeFlow() -> some View {
MainTabBarView(fabric: MainTabBarContentFabric())
}
class MainTabBarContentFabric: FlowContentMakeable {
@ViewBuilder func makeContent() -> some View {
Image("")
}
}
}
// MAIN TABBAR
protocol MainTabBarProtocol {
func configureBarAppearance()
}
extension MainTabBarProtocol {
func configureBarAppearance() {
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.backgroundColor = AppColors.surfaceSecondary.uiColor
navigationBarAppearance.largeTitleTextAttributes = [.foregroundColor: AppColors.surfaceTextPrimary.uiColor]
navigationBarAppearance.titleTextAttributes = [.foregroundColor: AppColors.surfaceTextPrimary.uiColor]
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UISegmentedControl.appearance().selectedSegmentTintColor = AppColors.surfaceItemQuaternary.uiColor
UISegmentedControl.appearance().backgroundColor = AppColors.surfaceItemTertiary.uiColor
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: AppColors.surfaceTextPrimary.uiColor, .font: AppFonts.regular(size: 14).uiFont], for: .normal)
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: AppColors.surfaceTextPrimary.uiColor, .font: AppFonts.regularBold(size: 14).uiFont], for: .selected)
let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.configureWithDefaultBackground()
tabBarAppearance.backgroundColor = AppColors.surfaceSecondary.uiColor
UITabBar.appearance().standardAppearance = tabBarAppearance
if #available(iOS 15.0, *) {
UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
}
}
}
struct MainTabBarView: View, MainTabBarProtocol {
@EnvironmentObject private var appState: AppState
@EnvironmentObject private var renewSession: RenewAppSessionManager
@State private var hideBar = false
private let fabric: any FlowContentMakeable
init(fabric: any FlowContentMakeable) {
self.fabric = fabric
configureBarAppearance()
}
var body: some View {
if !renewSession.appCanMakeRequests {
SpinnerView()
} else {
AnyView(fabric.makeContent())
}
}
}
struct MainTabBarView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
// Next SuggestionsCatalogWrapper()
// contains MainCatalog(viewModel: .init(title: "Suggestions", model: [SubmittedCatalog(), DeclinedCatalog(), AcceptedCatalog()]))
// Where SubmittedCatalog is different for each user role
// Two options: Create BaseSubmittedCatalog, and recreate for each role USerSubmittedCatalog, ResearcherSubmittedCatalog etc
// Or Inject into SubmittedCatalog modules, that handles: fetch, error hadnling, delete of version and create inteface for communication between them ("event bus")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment