From 0687642095a5a82084dd4bc2fb4b6564e83c9ee9 Mon Sep 17 00:00:00 2001 From: Kiryl Kaveryn Date: Tue, 11 Feb 2025 22:05:58 +0400 Subject: [PATCH] [ios] fix memory leak in the PlacePageInfoViewController Signed-off-by: Kiryl Kaveryn --- .../PlacePageInfoViewController.swift | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/iphone/Maps/UI/PlacePage/Components/PlacePageInfoViewController.swift b/iphone/Maps/UI/PlacePage/Components/PlacePageInfoViewController.swift index 746659484a..c22551590a 100644 --- a/iphone/Maps/UI/PlacePage/Components/PlacePageInfoViewController.swift +++ b/iphone/Maps/UI/PlacePage/Components/PlacePageInfoViewController.swift @@ -131,7 +131,7 @@ class PlacePageInfoViewController: UIViewController { private var driveThroughView: InfoItemViewController? private var networkView: InfoItemViewController? - var placePageInfoData: PlacePageInfoData! + weak var placePageInfoData: PlacePageInfoData! weak var delegate: PlacePageInfoViewControllerDelegate? var coordinatesFormatId: Int { get { UserDefaults.standard.integer(forKey: Constants.coordFormatIdKey) } @@ -366,33 +366,22 @@ class PlacePageInfoViewController: UIViewController { if formatId >= coordFormats.count { formatId = 0 } - - func setCoordinatesSelected(formatId: Int) { - coordinatesFormatId = formatId - let coordinates: String = coordFormats[formatId] - coordinatesView?.infoLabel.text = coordinates - } - - func copyCoordinatesToPasteboard() { - let coordinates: String = coordFormats[coordinatesFormatId] - self.delegate?.didCopy(coordinates) - } - coordinatesView = createInfoItem(coordFormats[formatId], icon: UIImage(named: "ic_placepage_coordinate"), accessoryImage: UIImage(named: "ic_placepage_change"), - tapHandler: { [unowned self] in + tapHandler: { [weak self] in + guard let self else { return } let formatId = (self.coordinatesFormatId + 1) % coordFormats.count - setCoordinatesSelected(formatId: formatId) + self.setCoordinatesSelected(formatId: formatId) }, - longPressHandler: { - copyCoordinatesToPasteboard() + longPressHandler: { [weak self] in + self?.copyCoordinatesToPasteboard() }) if #available(iOS 14.0, *) { let menu = UIMenu(children: coordFormats.enumerated().map { (index, format) in - UIAction(title: format, handler: { _ in - setCoordinatesSelected(formatId: index) - copyCoordinatesToPasteboard() + UIAction(title: format, handler: { [weak self] _ in + self?.setCoordinatesSelected(formatId: index) + self?.copyCoordinatesToPasteboard() }) }) coordinatesView?.accessoryButton.menu = menu @@ -400,6 +389,19 @@ class PlacePageInfoViewController: UIViewController { } } + private func setCoordinatesSelected(formatId: Int) { + guard let coordFormats = placePageInfoData.coordFormats as? Array else { return } + coordinatesFormatId = formatId + let coordinates: String = coordFormats[formatId] + coordinatesView?.infoLabel.text = coordinates + } + + private func copyCoordinatesToPasteboard() { + guard let coordFormats = placePageInfoData.coordFormats as? Array else { return } + let coordinates: String = coordFormats[coordinatesFormatId] + delegate?.didCopy(coordinates) + } + private func setupOpenWithAppView() { guard let delegate, delegate.shouldShowOpenInApp else { return } openWithAppView = createInfoItem(L("open_in_app"),