Skip to content

Commit

Permalink
MBX-0000 Snackbar Custom Window
Browse files Browse the repository at this point in the history
  • Loading branch information
Akylbek Utekeshev committed Oct 5, 2023
1 parent 3843e35 commit 74acb1a
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class PresentationDisplayUseCase {
changeType(model: model.content)

guard let window = presentationStrategy?.getWindow() else {
Logger.common(message: "In-app modal window creating failed")
Logger.common(message: "In-app window creating failed")
return
}

Expand All @@ -37,8 +37,6 @@ final class PresentationDisplayUseCase {
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 @@ -49,8 +47,6 @@ final class PresentationDisplayUseCase {
return
}

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

presentedVC = viewController

if let image = model.imagesDict[model.firstImageValue] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ final class SnackbarPresentationStrategy: PresentationStrategyProtocol {

window.frame = windowFrame
window.backgroundColor = .clear
window.windowLevel = .alert
window.windowLevel = .normal + 3
let viewController = UIViewController()
window.rootViewController = viewController
viewController.view.frame = windowFrame
viewController.view.layer.borderWidth = 2.0
viewController.view.layer.borderColor = UIColor.green.cgColor

window.isHidden = false
self.window = window
return window
Expand All @@ -50,7 +47,6 @@ final class SnackbarPresentationStrategy: PresentationStrategyProtocol {
if var topController = window.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
Logger.common(message: "SnackbarPresentationStrategy topController equal = \(topController).")
}

viewController.view.frame = topController.view.frame
Expand Down Expand Up @@ -88,18 +84,9 @@ final class SnackbarPresentationStrategy: PresentationStrategyProtocol {
let heightMultiplier = width / imageSize.width
let imageHeight = imageSize.height * heightMultiplier
let finalHeight = (imageHeight < Constants.oneThirdScreenHeight) ? imageHeight : Constants.oneThirdScreenHeight
// setWindowFrame(x: leftOffset, gravity: gravity, finalHeight: finalHeight, width: width)


let safeAreaInset = getSafeAreaInset(gravity: gravity)
let y = getYPosition(gravity: gravity, finalHeight: finalHeight, safeAreaInset: safeAreaInset)

let superFinalWidth = width - leftOffset - rightOffset
let asd = superFinalWidth / imageSize.width
let superImageHeight = imageSize.height * asd
let superFinalHeight = (superImageHeight < Constants.oneThirdScreenHeight) ? superImageHeight : Constants.oneThirdScreenHeight

self.window?.frame = CGRect(x: leftOffset, y: y, width: width, height: superFinalHeight)
self.window?.frame = CGRect(x: leftOffset, y: y, width: width, height: finalHeight)
}
default:
break
Expand All @@ -126,27 +113,9 @@ private extension SnackbarPresentationStrategy {
if gravity == .bottom {
y = UIScreen.main.bounds.height - finalHeight - safeAreaInset
} else if gravity == .top {
y = 0
y = safeAreaInset
}

return y
}

// func setWindowFrame(x: CGFloat, gravity: ContentPositionGravity.VerticalType, imageSize: CGSize, width: CGFloat) {
// guard let window = window else {
// Logger.common(message: "Unable to get window in getWindowFrame func. Abort.", level: .error, category: .inAppMessages)
// return
// }
//
// let newWidth = width - rightOffset -
// let heightMultiplier = newWidth / imageSize.width
// let imageHeight = imageSize.height * heightMultiplier
// let finalHeight = (imageHeight < Constants.oneThirdScreenHeight) ? imageHeight : Constants.oneThirdScreenHeight
//
//
// let safeAreaInset = getSafeAreaInset(gravity: gravity)
// let y = getYPosition(gravity: gravity, finalHeight: finalHeight, safeAreaInset: safeAreaInset)
//
// window.frame = CGRect(x: x, y: y, width: width, height: finalHeight)
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ final class InAppImageOnlyView: UIView {

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 @@ -71,6 +71,10 @@ final class ModalViewController: UIViewController, InappViewControllerProtocol,

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
elements.forEach({
$0.removeFromSuperview()
})

setupElements()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ 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 @@ -74,12 +73,10 @@ 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 @@ -108,7 +105,6 @@ 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import UIKit
import MindboxLogger

class SnackbarViewController: UIViewController, InappViewControllerProtocol {


var edgeConstraint: NSLayoutConstraint?
let model: SnackbarFormVariant
Expand Down Expand Up @@ -78,48 +77,37 @@ class SnackbarViewController: UIViewController, InappViewControllerProtocol {
view.isUserInteractionEnabled = true
snackbarView.translatesAutoresizingMaskIntoConstraints = false
snackbarView.isUserInteractionEnabled = true
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 @@ -129,20 +117,11 @@ 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 @@ -168,25 +147,21 @@ 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
}

let width = view.layer.frame.width - leftOffset - rightOffset
let width = view.layer.frame.width
let heightMultiplier = width / image.size.width
let imageHeight = image.size.height * heightMultiplier
let finalHeight = (imageHeight < Constants.oneThirdScreenHeight) ? imageHeight : Constants.oneThirdScreenHeight
Expand All @@ -198,16 +173,13 @@ 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([
Expand All @@ -223,7 +195,6 @@ class SnackbarViewController: UIViewController, InappViewControllerProtocol {
snackbarView.heightAnchor.constraint(equalToConstant: height),
])
}
Logger.common(message: "SnackbarViewController setupLayoutConstraints function finished.")
}

func setupEdgeConstraint(with height: CGFloat) {
Expand Down Expand Up @@ -303,12 +274,10 @@ class BottomSnackbarViewController: SnackbarViewController {
self.view.frame = CGRect(x: leftOffset, y: screenHeight - finalHeight,
width: UIScreen.main.bounds.width - leftOffset - rightOffset,
height: finalHeight)
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)
Expand All @@ -318,6 +287,5 @@ class BottomSnackbarViewController: SnackbarViewController {
}

edgeConstraint?.isActive = true
Logger.common(message: "SnackbarViewController setupEdgeConstraint function finished.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ 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.")
Expand Down

0 comments on commit 74acb1a

Please sign in to comment.