Skip to content

Commit

Permalink
MBX-0000 New way 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Akylbek Utekeshev committed Sep 29, 2023
1 parent 338df1d commit 23426d8
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 8 deletions.
1 change: 1 addition & 0 deletions Mindbox/CoreController/CoreController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CoreController {
self.inAppMessagesManager.start()
}

Logger.common(message: "New way 3 test commit!", level: .info, category: .general)
Logger.common(message: "[Configuration]: \(configuration)", level: .info, category: .general)
Logger.common(message: "[SDK Version]: \(self.utilitiesFetcher.sdkVersion ?? "null")", level: .info, category: .general)
Logger.common(message: "[APNS Token]: \(self.persistenceStorage.apnsToken ?? "null")", level: .info, category: .general)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ final class PresentationDisplayUseCase {
return
}

Logger.common(message: "PresentationDisplayUseCase window: \(window)")

guard let factory = self.factory else {
Logger.common(message: "Factory does not exists.", level: .error, category: .general)
return
}

Logger.common(message: "PresentationDisplayUseCase factory: \(factory)", level: .error)

guard let viewController = factory.create(model: model.content,
id: model.inAppId,
imagesDict: model.imagesDict,
Expand All @@ -45,6 +49,8 @@ final class PresentationDisplayUseCase {
return
}

Logger.common(message: "PresentationDisplayUseCase viewController: \(viewController)")

presentedVC = viewController
presentationStrategy?.present(id: model.inAppId, in: window, using: viewController)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,28 @@ import MindboxLogger

final class SnackbarPresentationStrategy: PresentationStrategyProtocol {
func getWindow() -> UIWindow? {
Logger.common(message: "SnackbarPresentationStrategy getWindow started.")
if #available(iOS 13, *) {
return UIApplication.shared.connectedScenes
let window = UIApplication.shared.connectedScenes
.filter { $0.activationState == .foregroundActive }
.compactMap { $0 as? UIWindowScene }
.first?.windows
.first(where: { $0.isKeyWindow })

Logger.common(message: "SnackbarPresentationStrategy window iOS 13+: \(window).")
return window
} else {
return UIApplication.shared.keyWindow
let window = UIApplication.shared.keyWindow
Logger.common(message: "SnackbarPresentationStrategy window iOS 12 or less: \(window).")
return window
}
}

func present(id: String, in window: UIWindow, using viewController: UIViewController) {
if var topController = window.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
Logger.common(message: "SnackbarPresentationStrategy topController equal = \(topController).")
}

topController.view.addSubview(viewController.view)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import UIKit
import MindboxLogger

final class InAppImageOnlyView: UIView {
var onClose: (() -> Void)?
Expand All @@ -29,11 +30,15 @@ final class InAppImageOnlyView: UIView {

func customInit() {
guard let image = image else {
Logger.common(message: "[Error]: \(#function) at line \(#line) of \(#file)", level: .error)
return
}

Logger.common(message: "InAppImageOnlyView custom init started.")

imageView.contentMode = .scaleAspectFill
imageView.image = image
imageView.layer.opacity = 0.5

imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(imageView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
//

import UIKit
import MindboxLogger

class ImageLayerFactory: LayerFactory {
func create(from image: UIImage, layer: ContentBackgroundLayer, in view: UIView, with controller: GestureHandler) -> UIView? {
if case .image(let imageContentBackgroundLayer) = layer {
let inAppView = InAppImageOnlyView(image: image, action: imageContentBackgroundLayer.action)
let imageTapGestureRecognizer = UITapGestureRecognizer(target: controller, action: #selector(controller.imageTapped(_:)))
inAppView.addGestureRecognizer(imageTapGestureRecognizer)

Logger.common(message: "ImageLayerFactory return uiView.")
return inAppView
}


Logger.common(message: "ImageLayerFactory return nil.")
return nil
}

Expand All @@ -39,5 +41,6 @@ class ImageLayerFactory: LayerFactory {
view.leadingAnchor.constraint(equalTo: parentView.leadingAnchor),
view.trailingAnchor.constraint(equalTo: parentView.trailingAnchor)
])
Logger.common(message: "ImageLayerFactory setupConstraintsSnackbar finished.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import UIKit
import MindboxLogger

class SnackbarView: UIView {

Expand Down Expand Up @@ -39,7 +40,7 @@ class SnackbarView: UIView {
self.onClose = onClose
self.animationTime = animationTime
super.init(frame: .zero)

Logger.common(message: "SnackbarView inited.")
setupPanGesture()
}

Expand All @@ -63,6 +64,7 @@ class SnackbarView: UIView {
private func handleSwipeGesture(translation: CGPoint) {
if (swipeDirection == .up && translation.y < 0) || (swipeDirection == .down && translation.y > 0) {
self.transform = CGAffineTransform(translationX: 0, y: translation.y)
Logger.common(message: "SnackbarView handleSwipeGesture.")
}
}

Expand All @@ -72,9 +74,12 @@ class SnackbarView: UIView {
if ((swipeDirection == .up && translation.y < 0) || (swipeDirection == .down && translation.y > 0)) &&
abs(translation.y) > threshold {
animateHide(completion: onClose, animated: true)
Logger.common(message: "SnackbarView finalizeGesture.")

} else {
UIView.animate(withDuration: animationTime) {
self.transform = .identity
Logger.common(message: "SnackbarView finalizeGesture fallback.")
}
}
}
Expand Down Expand Up @@ -103,6 +108,7 @@ class SnackbarView: UIView {
yOffset = 0
}
self.transform = CGAffineTransform(translationX: 0, y: yOffset)
Logger.common(message: "SnackbarView setHiddenTransform yOffset: \(yOffset).")
}

public func hide(animated: Bool = true, completion: (() -> Void)? = nil) {
Expand All @@ -113,6 +119,7 @@ class SnackbarView: UIView {
}

required init?(coder: NSCoder) {
Logger.common(message: "SnackbarView init(coder:) has not been implemented.")
fatalError("init(coder:) has not been implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,49 +64,62 @@ class SnackbarViewController: UIViewController, InappViewControllerProtocol {
self.onPresented = onPresented
self.onTapAction = onTapAction
super.init(nibName: nil, bundle: nil)
Logger.common(message: "SnackbarViewController inited.")
}

required init?(coder: NSCoder) {
Logger.common(message: "SnackbarViewController init(coder:) has not been implemented.")
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(snackbarView)
view.isUserInteractionEnabled = true
snackbarView.translatesAutoresizingMaskIntoConstraints = false
snackbarView.isUserInteractionEnabled = true
view.addSubview(snackbarView)
snackbarView.backgroundColor = .blue
Logger.common(message: "SnackbarViewController viewDidLoad.")
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
Logger.common(message: "SnackbarViewController flag hasSetupLayers: \(hasSetupLayers).")
if !hasSetupLayers {
hasSetupLayers = true
setupConstraints()
setupLayers()
Logger.common(message: "SnackbarViewController we're in !hasSetupLayers condition.")

if snackbarView.bounds.size != .zero {
Logger.common(message: "SnackbarViewController we're in !snackbarView.bound.size != zero condition.")
setupElements()
hasSetupElements = true
}
} else if !hasSetupElements && snackbarView.bounds.size != .zero {
Logger.common(message: "SnackbarViewController we're in !hasSetupElements && snackbarView.bounds.size != .zero condition.")
UIView.performWithoutAnimation {
setupElements()
hasSetupElements = true
view.layoutIfNeeded()
}
}

Logger.common(message: "SnackbarViewController viewDidLayoutSubviews done.")
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
animateConstraints(withDuration: Constants.animationDuration)
onPresented()
Logger.common(message: "SnackbarViewController viewDidAppear.")
}

private func setupLayers() {
Logger.common(message: "SnackbarViewController setupLayers function started.")
let layers = model.content.background.layers.elements

Logger.common(message: "SnackbarViewController layers: \(layers.count).")
for layer in layers {
if let factory = layersFactories[layer.layerType] {
if case .image(let imageContentBackgroundLayer) = layer {
Expand All @@ -116,11 +129,20 @@ class SnackbarViewController: UIViewController, InappViewControllerProtocol {
self.layers.append(layerView)
snackbarView.addSubview(layerView)
factory.setupConstraintsSnackbar(for: layerView, in: snackbarView)
} else {
Logger.common(message: "SnackbarViewController layer view does not exists.")
}
} else {
Logger.common(message: "SnackbarViewController imageContentBackgroundLayer source or value does not exists.\n[Layer]: \(imageContentBackgroundLayer)\n[Dict]: \(imagesDict)")
}
} else {
Logger.common(message: "SnackbarViewController not image type of layer.")
}
} else {
Logger.common(message: "SnackbarViewController layersFactory does not exists.")
}
}
Logger.common(message: "SnackbarViewController setupLayers function finished.")
}

private func setupElements() {
Expand All @@ -146,15 +168,19 @@ class SnackbarViewController: UIViewController, InappViewControllerProtocol {
}

private func animateConstraints(withDuration duration: TimeInterval) {
Logger.common(message: "SnackbarViewController animateConstraints function started.")
view.layoutIfNeeded()

UIView.animate(withDuration: duration) {
self.edgeConstraint?.constant = 0
Logger.common(message: "SnackbarViewController animateConstraints UIView.animate condition.")
self.view.layoutIfNeeded()
}
Logger.common(message: "SnackbarViewController animateConstraints function finished.")
}

func setupConstraints() {
Logger.common(message: "SnackbarViewController setupConstraints function started.")
guard let image = imagesDict[firstImageValue] else {
Logger.common(message: "[Error]: \(#function) at line \(#line) of \(#file)", level: .error)
return
Expand All @@ -172,26 +198,32 @@ class SnackbarViewController: UIViewController, InappViewControllerProtocol {

setupLayoutConstraints(with: finalHeight)
setupEdgeConstraint(with: finalHeight)

Logger.common(message: "SnackbarViewController setupConstraints function finished.")
}

func setViewFrame(with height: CGFloat) {
// Need override in sub-class.
}

func setupLayoutConstraints(with height: CGFloat) {
Logger.common(message: "SnackbarViewController setupLayoutConstraints function started.")
if #available(iOS 11.0, *) {
Logger.common(message: "SnackbarViewController setupLayoutConstraints iOS 11+.")
NSLayoutConstraint.activate([
snackbarView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
snackbarView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
snackbarView.heightAnchor.constraint(equalToConstant: height),
])
} else {
Logger.common(message: "SnackbarViewController setupLayoutConstraints iOS 10.")
NSLayoutConstraint.activate([
snackbarView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
snackbarView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
snackbarView.heightAnchor.constraint(equalToConstant: height),
])
}
Logger.common(message: "SnackbarViewController setupLayoutConstraints function finished.")
}

func setupEdgeConstraint(with height: CGFloat) {
Expand Down Expand Up @@ -271,16 +303,21 @@ class BottomSnackbarViewController: SnackbarViewController {
self.view.frame = CGRect(x: leftOffset, y: screenHeight - finalHeight,
width: UIScreen.main.bounds.width - leftOffset - rightOffset,
height: finalHeight)
self.view.backgroundColor = .green
self.view.backgroundColor = .yellow
Logger.common(message: "SnackbarViewController setViewFrame function finished.")
}

override func setupEdgeConstraint(with height: CGFloat) {
Logger.common(message: "SnackbarViewController setupEdgeConstraint function started.")
if #available(iOS 11.0, *) {
Logger.common(message: "SnackbarViewController setupEdgeConstraint iOS 11+.")
edgeConstraint = snackbarView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: height)
} else {
Logger.common(message: "SnackbarViewController setupEdgeConstraint iOS 10.")
edgeConstraint = snackbarView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: height)
}

edgeConstraint?.isActive = true
Logger.common(message: "SnackbarViewController setupEdgeConstraint function finished.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation
import UIKit
import MindboxLogger

class SnackbarViewFactory: ViewFactoryProtocol {

Expand All @@ -20,18 +21,23 @@ class SnackbarViewFactory: ViewFactoryProtocol {
let snackbarView = SnackbarView(onClose: onClose)
switch gravity {
case .top:
Logger.common(message: "SnackbarViewFactory TopSnackbarViewController handleSwipeGesture created.")
snackbarViewController = TopSnackbarViewController(model: snackbarFormVariant, imagesDict: imagesDict, snackbarView: snackbarView, firstImageValue: firstImageValue, onPresented: onPresented, onTapAction: onTapAction)
case .bottom:
Logger.common(message: "SnackbarViewFactory BottomSnackbarViewController handleSwipeGesture created.")
snackbarViewController = BottomSnackbarViewController(model: snackbarFormVariant, imagesDict: imagesDict, snackbarView: snackbarView, firstImageValue: firstImageValue, onPresented: onPresented, onTapAction: onTapAction)
default:
Logger.common(message: "SnackbarViewFactory controller is nil.")
return nil
}

self.viewController = snackbarViewController

return viewController
}
}


Logger.common(message: "SnackbarViewFactory create returns nil.")
return nil
}
}

0 comments on commit 23426d8

Please sign in to comment.