2024-05-21 19:36:15 +05:30
|
|
|
//
|
|
|
|
|
// HomeVC.swift
|
|
|
|
|
// WOKA
|
|
|
|
|
//
|
2024-05-24 19:26:54 +05:30
|
|
|
// Created by Bilal on 24/05/2024.
|
2024-05-21 19:36:15 +05:30
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
2024-05-24 19:26:54 +05:30
|
|
|
protocol ChildViewControllerDelegate: AnyObject {
|
|
|
|
|
func didPressSwitchButton(from viewController: UIViewController)
|
|
|
|
|
}
|
2024-05-21 19:36:15 +05:30
|
|
|
|
2024-05-24 19:26:54 +05:30
|
|
|
class HomeVC: UIViewController ,ChildViewControllerDelegate{
|
|
|
|
|
|
|
|
|
|
@IBOutlet weak var containerView: UIView!
|
|
|
|
|
var currentChildViewController: UIViewController?
|
2024-05-22 18:50:16 +05:30
|
|
|
|
2024-05-21 19:36:15 +05:30
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
2024-09-24 20:10:45 +05:30
|
|
|
|
2024-09-23 19:45:24 +05:30
|
|
|
|
2024-05-27 19:42:56 +05:30
|
|
|
switch AuthFunc.shareInstance.selectedTheme {
|
|
|
|
|
case .theme1:
|
|
|
|
|
let sb = UIStoryboard(name: K.StoryBoard.theme, bundle: nil)
|
|
|
|
|
if let firstVC = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Theme.themeOneVC) as? ThemeOneVC {
|
|
|
|
|
add(asChildViewController: firstVC)
|
|
|
|
|
firstVC.delegate = self
|
|
|
|
|
}
|
|
|
|
|
case .theme2:
|
|
|
|
|
let sb = UIStoryboard(name: K.StoryBoard.theme, bundle: nil)
|
|
|
|
|
if let firstVC = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Theme.themeTwoVC) as? ThemeTwoVC {
|
|
|
|
|
add(asChildViewController: firstVC)
|
|
|
|
|
firstVC.delegate = self
|
|
|
|
|
}
|
2024-05-24 19:26:54 +05:30
|
|
|
}
|
2024-05-22 18:50:16 +05:30
|
|
|
}
|
2024-05-23 19:27:34 +05:30
|
|
|
|
2024-05-24 19:26:54 +05:30
|
|
|
func didPressSwitchButton(from viewController: UIViewController) {
|
|
|
|
|
if viewController is ThemeOneVC {
|
2024-05-27 19:42:56 +05:30
|
|
|
switchToViewController(withIdentifier: K.StoryBoardID.Theme.themeTwoVC)
|
2024-05-24 19:26:54 +05:30
|
|
|
} else if viewController is ThemeTwoVC {
|
2024-05-27 19:42:56 +05:30
|
|
|
switchToViewController(withIdentifier: K.StoryBoardID.Theme.themeOneVC)
|
2024-05-24 19:26:54 +05:30
|
|
|
}
|
2024-05-23 19:27:34 +05:30
|
|
|
}
|
|
|
|
|
|
2024-05-21 19:36:15 +05:30
|
|
|
|
2024-05-24 19:26:54 +05:30
|
|
|
private func switchToViewController(withIdentifier identifier: String) {
|
2024-05-27 19:42:56 +05:30
|
|
|
let sb = UIStoryboard(name: K.StoryBoard.theme, bundle: nil)
|
2024-05-24 19:26:54 +05:30
|
|
|
let newVC = sb.instantiateViewController(withIdentifier: identifier)
|
|
|
|
|
if let newVC = newVC as? ThemeOneVC {
|
|
|
|
|
newVC.delegate = self
|
|
|
|
|
} else if let newVC = newVC as? ThemeTwoVC {
|
|
|
|
|
newVC.delegate = self
|
2024-05-21 19:36:15 +05:30
|
|
|
}
|
2024-05-24 19:26:54 +05:30
|
|
|
|
|
|
|
|
print("Switching to view controller with identifier \(identifier)")
|
2024-08-28 19:37:21 +05:30
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
|
guard let self else{return}
|
|
|
|
|
self.replaceCurrentChildViewController(with: newVC)
|
|
|
|
|
}
|
2024-05-21 19:36:15 +05:30
|
|
|
}
|
|
|
|
|
|
2024-05-24 19:26:54 +05:30
|
|
|
private func add(asChildViewController viewController: UIViewController) {
|
|
|
|
|
print("Adding child view controller: \(viewController)")
|
|
|
|
|
addChild(viewController)
|
|
|
|
|
viewController.view.frame = containerView.bounds
|
|
|
|
|
containerView.addSubview(viewController.view)
|
|
|
|
|
viewController.didMove(toParent: self)
|
|
|
|
|
currentChildViewController = viewController
|
2024-05-21 19:36:15 +05:30
|
|
|
}
|
|
|
|
|
|
2024-08-28 19:37:21 +05:30
|
|
|
private func remove(asChildViewController viewController: UIViewController, onCompletion : @escaping () -> Void) {
|
2024-05-24 19:26:54 +05:30
|
|
|
print("Removing child view controller: \(viewController)")
|
|
|
|
|
viewController.willMove(toParent: nil)
|
|
|
|
|
viewController.view.removeFromSuperview()
|
|
|
|
|
viewController.removeFromParent()
|
2024-08-28 19:37:21 +05:30
|
|
|
onCompletion()
|
2024-05-24 19:26:54 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func replaceCurrentChildViewController(with newViewController: UIViewController) {
|
|
|
|
|
// Clear previous views
|
|
|
|
|
containerView.subviews.forEach { $0.removeFromSuperview() }
|
|
|
|
|
|
|
|
|
|
// Remove current child view controller if exists
|
|
|
|
|
if let currentVC = currentChildViewController {
|
2024-08-28 19:37:21 +05:30
|
|
|
remove(asChildViewController: currentVC) { [weak self] in
|
|
|
|
|
guard let self else{return}
|
|
|
|
|
// Add new child view controller
|
|
|
|
|
add(asChildViewController: newViewController)
|
|
|
|
|
}
|
2024-05-24 19:26:54 +05:30
|
|
|
}
|
2024-05-21 19:36:15 +05:30
|
|
|
}
|
|
|
|
|
}
|