Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
make unowned references weak (#1443)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasaraujo authored and uziasferreirazup committed Mar 24, 2021
1 parent 34b00de commit 1fb51b4
Show file tree
Hide file tree
Showing 27 changed files with 146 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class NativeViewController: UIViewController {
)
})

private lazy var serverDrivenBeagleView = BeagleView(.init(url: .textLazyComponentEndpoint)) { state in
private lazy var serverDrivenBeagleView = BeagleView(.init(url: .textLazyComponentEndpoint)) {
[weak self] state in
guard let self = self else { return }
switch state {
case .started:
self.loadingLabel.isHidden = false
Expand Down
4 changes: 4 additions & 0 deletions iOS/Sources/Beagle/Beagle.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
A05C74DD2498162B009DBB66 /* FormValidationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A05C74DC2498162B009DBB66 /* FormValidationTests.swift */; };
A068234C238430C500CEE57A /* ValidatorProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = A068234B238430C500CEE57A /* ValidatorProvider.swift */; };
A068234E2384320000CEE57A /* Validator.swift in Sources */ = {isa = PBXBuildFile; fileRef = A068234D2384320000CEE57A /* Validator.swift */; };
A0C4BF152600EEEC004B0422 /* BindingConfiguratorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0C4BF142600EEEB004B0422 /* BindingConfiguratorTests.swift */; };
A0D608D82448DF8700F7D851 /* ScreenType.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0D608D52448DF8600F7D851 /* ScreenType.swift */; };
A0D608DA2448DF8700F7D851 /* BeagleNavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0D608D72448DF8700F7D851 /* BeagleNavigationController.swift */; };
A0EB6BD725910FC8001A3E65 /* AnalyticsRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0EB6BD425910FC8001A3E65 /* AnalyticsRecord.swift */; };
Expand Down Expand Up @@ -573,6 +574,7 @@
A05C74DC2498162B009DBB66 /* FormValidationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FormValidationTests.swift; sourceTree = "<group>"; };
A068234B238430C500CEE57A /* ValidatorProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValidatorProvider.swift; sourceTree = "<group>"; };
A068234D2384320000CEE57A /* Validator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Validator.swift; sourceTree = "<group>"; };
A0C4BF142600EEEB004B0422 /* BindingConfiguratorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BindingConfiguratorTests.swift; sourceTree = "<group>"; };
A0D608D52448DF8600F7D851 /* ScreenType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScreenType.swift; sourceTree = "<group>"; };
A0D608D72448DF8700F7D851 /* BeagleNavigationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BeagleNavigationController.swift; sourceTree = "<group>"; };
A0EB6BD425910FC8001A3E65 /* AnalyticsRecord.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnalyticsRecord.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1876,6 +1878,7 @@
2532D5DB25099ABB001D1469 /* BeagleViewTests.swift */,
93D24A56251402B8005A5CBD /* AutoLayoutWrapperTests.swift */,
93EE0CA3252D0EE50032BE77 /* ViewConfiguratorTests.swift */,
A0C4BF142600EEEB004B0422 /* BindingConfiguratorTests.swift */,
);
path = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -2568,6 +2571,7 @@
C08D4E3624857CBA00AAAC0A /* URLOpenerTests.swift in Sources */,
9398CA5A255325730003A010 /* PathTests.swift in Sources */,
A05C74DD2498162B009DBB66 /* FormValidationTests.swift in Sources */,
A0C4BF152600EEEC004B0422 /* BindingConfiguratorTests.swift in Sources */,
E5A7763F237DA1F3002F5515 /* FormTests.swift in Sources */,
C08D4E472485804D00AAAC0A /* LazyComponentTests.swift in Sources */,
93ADB87923FAD31D005B7CD2 /* ComposeComponentTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class EventsGestureRecognizer: UITapGestureRecognizer {
let events: [Event]
weak var controller: BeagleController?

init(events: [Event], controller: BeagleController) {
init(events: [Event], controller: BeagleController?) {
self.events = events
self.controller = controller
super.init(target: nil, action: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ extension Deprecated.Form {

registerFormSubmit(view: childView)
if !hasFormSubmit {
renderer.controller.dependencies.logger.log(Log.form(.submitNotFound(form: self)))
renderer.dependencies.logger.log(Log.form(.submitNotFound(form: self)))
}
return childView
}

private func register(formView: UIView, submitView: UIView, controller: BeagleController) {
private func register(formView: UIView, submitView: UIView, controller: BeagleController?) {
let gestureRecognizer = SubmitFormGestureRecognizer(
form: self,
formView: formView,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class SubmitFormGestureRecognizer: UITapGestureRecognizer {
weak var formSubmitView: UIView?
weak var controller: BeagleController?

init(form: Deprecated.Form, formView: UIView, formSubmitView: UIView, controller: BeagleController) {
init(form: Deprecated.Form, formView: UIView, formSubmitView: UIView, controller: BeagleController?) {
self.form = form
self.formView = formView
self.formSubmitView = formSubmitView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ extension LazyComponent {
}

private func lazyLoad(initialState view: UIView, renderer: BeagleRenderer) {
renderer.controller.dependencies.repository.fetchComponent(url: path, additionalData: nil, useCache: false) {
renderer.dependencies.repository.fetchComponent(url: path, additionalData: nil, useCache: false) {
[weak view] result in
guard let view = view else { return }
switch result {
case .success(let component):
view.update(lazyLoaded: component, renderer: renderer)
case .failure(let error):
renderer.controller.serverDrivenState = .error(
renderer.controller?.serverDrivenState = .error(
.lazyLoad(error),
self.retryClosure(initialState: view, renderer: renderer)
)
Expand All @@ -55,7 +55,7 @@ extension UIView {
) {
if let updatable = self as? OnStateUpdatable,
updatable.onUpdateState(component: lazyLoaded) {
renderer.controller.setNeedsLayout(component: self)
renderer.controller?.setNeedsLayout(component: self)
} else {
DispatchQueue.main.async {
self.replace(with: lazyLoaded, renderer: renderer)
Expand All @@ -74,9 +74,9 @@ extension UIView {
superview.insertSubview(newView, belowSubview: self)
removeFromSuperview()

if renderer.controller.dependencies.style(self).isFlexEnabled {
renderer.controller.dependencies.style(newView).isFlexEnabled = true
if renderer.dependencies.style(self).isFlexEnabled {
renderer.dependencies.style(newView).isFlexEnabled = true
}
renderer.controller.setNeedsLayout(component: newView)
renderer.controller?.setNeedsLayout(component: newView)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ final class LazyComponentTests: XCTestCase {
initialState: ComponentDummy(resultView: initialView)
)
let repository = LazyRepositoryStub()
let controller = BeagleControllerStub()
let controller = BeagleControllerStub(dependencies: BeagleScreenDependencies(repository: repository))
let renderer = BeagleRenderer(controller: controller)
controller.dependencies = BeagleScreenDependencies(repository: repository)

let view = sut.toView(renderer: renderer)
repository.componentCompletion?(.success(ComponentDummy()))
Expand All @@ -97,9 +96,8 @@ final class LazyComponentTests: XCTestCase {
initialState: ComponentDummy(resultView: initialView)
)
let repository = LazyRepositoryStub()
let controller = BeagleControllerStub()
let controller = BeagleControllerStub(dependencies: BeagleScreenDependencies(repository: repository))
let renderer = BeagleRenderer(controller: controller)
controller.dependencies = BeagleScreenDependencies(repository: repository)

// When
let view = sut.toView(renderer: renderer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ final class ListViewController: UIViewController {
extension ListViewController: BeagleControllerProtocol {

var dependencies: BeagleDependenciesProtocol {
return renderer.controller.dependencies
return renderer.dependencies
}

var serverDrivenState: ServerDrivenState {
get { return renderer.controller.serverDrivenState }
set { renderer.controller.serverDrivenState = newValue }
get { return renderer.controller?.serverDrivenState ?? .finished }
set { renderer.controller?.serverDrivenState = newValue }
}

var screenType: ScreenType {
return renderer.controller.screenType
return renderer.controller?.screenType ?? .declarativeText("")
}

var screen: Screen? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ final class ListViewUIComponent: UIView {
collection.showsVerticalScrollIndicator = model.isScrollIndicatorVisible

let parentController = listController.renderer.controller
parentController.addChild(listController)
parentController?.addChild(listController)
addSubview(listController.view)
listController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
listController.view.frame = bounds
Expand Down Expand Up @@ -194,7 +194,7 @@ final class ListViewUIComponent: UIView {

if items?.count == 0 || didReachScrollThreshol {
onScrollEndExecuted = true
renderer.controller.execute(actions: model.onScrollEnd, event: "onScrollEnd", origin: self)
renderer.controller?.execute(actions: model.onScrollEnd, event: "onScrollEnd", origin: self)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ class ComponentHostController: BeagleController {
let bindings = Bindings()

var dependencies: BeagleDependenciesProtocol {
return renderer.controller.dependencies
return renderer.dependencies
}
var serverDrivenState: ServerDrivenState {
get { renderer.controller.serverDrivenState }
set { renderer.controller.serverDrivenState = newValue }
get { renderer.controller?.serverDrivenState ?? .finished }
set { renderer.controller?.serverDrivenState = newValue }
}
var screenType: ScreenType {
return renderer.controller.screenType
return renderer.controller?.screenType ?? .declarativeText("")
}
var screen: Screen? {
return renderer.controller.screen
return renderer.controller?.screen
}

func addOnInit(_ onInit: [Action], in view: UIView) {
renderer.controller.addOnInit(onInit, in: view)
renderer.controller?.addOnInit(onInit, in: view)
}

func execute(actions: [Action]?, event: String?, origin: UIView) {
renderer.controller.execute(actions: actions, event: event, origin: origin)
renderer.controller?.execute(actions: actions, event: event, origin: origin)
}

func execute(actions: [Action]?, with contextId: String, and contextValue: DynamicObject, origin: UIView) {
renderer.controller.execute(actions: actions, with: contextId, and: contextValue, origin: origin)
renderer.controller?.execute(actions: actions, with: contextId, and: contextValue, origin: origin)
}

public func addBinding<T: Decodable>(expression: ContextExpression, in view: UIView, update: @escaping (T?) -> Void) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension PageView {
if let actions = onPageChange {
view.onPageChange = { [weak view] page in
guard let view = view else { return }
renderer.controller.execute(actions: actions, with: "onPageChange", and: .int(page), origin: view)
renderer.controller?.execute(actions: actions, with: "onPageChange", and: .int(page), origin: view)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PageViewUIComponent: UIView {
init(
model: Model,
indicatorView: PageIndicatorUIView?,
controller: BeagleController
controller: BeagleController?
) {
self.model = model
self.indicatorView = indicatorView
Expand All @@ -76,8 +76,8 @@ class PageViewUIComponent: UIView {
navigationOrientation: .horizontal
)

private func setupLayout(controller: BeagleController) {
controller.addChild(pageViewController)
private func setupLayout(controller: BeagleController?) {
controller?.addChild(pageViewController)
addSubview(pageViewController.view)
pageViewController.didMove(toParent: controller)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension ScreenComponent {

public func toView(renderer: BeagleRenderer) -> UIView {

prefetch(helper: renderer.controller.dependencies.preFetchHelper)
prefetch(helper: renderer.dependencies.preFetchHelper)

return buildChildView(renderer: renderer)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ final class ScreenComponentTests: XCTestCase {

func test_shouldPrefetchNavigateAction() {
let prefetch = BeaglePrefetchHelpingSpy()
let controller = BeagleControllerStub()
let dependencies = BeagleScreenDependencies(preFetchHelper: prefetch)
let controller = BeagleControllerStub(dependencies: dependencies)
let renderer = BeagleRenderer(controller: controller)
controller.dependencies = BeagleScreenDependencies(preFetchHelper: prefetch)

let navigatePath = "button-item-prefetch"
let navigate = Navigate.pushView(.remote(.init(url: navigatePath, shouldPrefetch: true)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ extension TabBar {
}
}

tabBarScroll.onTabSelection = { tab in
renderer.controller.execute(actions: self.onTabSelection, with: "onTabSelection", and: .int(tab), origin: tabBarScroll)
tabBarScroll.onTabSelection = { [weak tabBarScroll] tab in
guard let view = tabBarScroll else { return }
renderer.controller?.execute(actions: self.onTabSelection, with: "onTabSelection", and: .int(tab), origin: view)
}

return tabBarScroll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@ final class TabBarItemUIComponent: UIView {

private func handleContextOnImage(iconName: String) {
let expression: Expression<String> = "\(iconName)"

renderer?.observe(expression, andUpdateManyIn: self) { icon in

renderer?.observe(expression, andUpdateManyIn: self) { [weak self] icon in
guard let self = self else { return }
if let icon = icon {
self.icon.image = self.theme?.selectedIconColor == nil ?
UIImage(named: icon, in: self.renderer?.controller.dependencies.appBundle, compatibleWith: nil) :
UIImage(named: icon, in: self.renderer?.controller.dependencies.appBundle, compatibleWith: nil)?.withRenderingMode(.alwaysTemplate)
UIImage(named: icon, in: self.renderer?.dependencies.appBundle, compatibleWith: nil) :
UIImage(named: icon, in: self.renderer?.dependencies.appBundle, compatibleWith: nil)?.withRenderingMode(.alwaysTemplate)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ final class TabBarUIComponent: UIScrollView {
}
guard let selectedTabItem = tabItemViews[model.tabIndex ?? 0] else { return }
setupIndicatorViewStyle(for: selectedTabItem)
model.renderer.controller.setNeedsLayout(component: self)
model.renderer.controller?.setNeedsLayout(component: self)
}

private func setupScrollView() {
Expand Down Expand Up @@ -234,10 +234,8 @@ private extension TabBarUIComponent {
options: .curveLinear,
animations: {
self.setupIndicatorViewStyle(for: tabItem)

// TODO: setNeedLayout should call layoutIfNeeded
self.model.renderer.controller.setNeedsLayout(component: self)
self.model.renderer.controller.view.layoutIfNeeded()
self.model.renderer.controller?.setNeedsLayout(component: self)
self.model.renderer.controller?.view.layoutIfNeeded()
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ extension Touchable {
}

register(events: events, inView: childView, controller: renderer.controller)
prefetchComponent(helper: renderer.controller.dependencies.preFetchHelper)
prefetchComponent(helper: renderer.dependencies.preFetchHelper)
return childView
}

private func register(events: [Event], inView view: UIView, controller: BeagleController) {
private func register(events: [Event], inView view: UIView, controller: BeagleController?) {
let eventsGestureRecognizer = EventsGestureRecognizer(
events: events,
controller: controller
Expand All @@ -49,10 +49,10 @@ extension Touchable {
view.isUserInteractionEnabled = true
}

private func prefetchComponent(helper: BeaglePrefetchHelping) {
private func prefetchComponent(helper: BeaglePrefetchHelping?) {
onPress.forEach { action in
guard let newPath = (action as? Navigate)?.newPath else { return }
helper.prefetchComponent(newPath: newPath)
helper?.prefetchComponent(newPath: newPath)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class WebViewUIComponent: UIView {
didSet { updateView() }
}

init(url: String? = nil, controller: BeagleController) {
init(url: String? = nil, controller: BeagleController?) {
self.url = url
self.controller = controller
super.init(frame: .zero)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extension Button {
}
}

let preFetchHelper = renderer.controller.dependencies.preFetchHelper
let preFetchHelper = renderer.dependencies.preFetchHelper
onPress?
.compactMap { ($0 as? Navigate)?.newPath }
.forEach { preFetchHelper.prefetchComponent(newPath: $0) }
Expand All @@ -61,7 +61,7 @@ extension Button {
required init(
onPress: [Action]?,
clickAnalyticsEvent: AnalyticsClick? = nil,
controller: BeagleController
controller: BeagleController?
) {
super.init(frame: .zero)
self.onPress = onPress
Expand Down
Loading

0 comments on commit 1fb51b4

Please sign in to comment.