mirror of
https://github.com/divkit/divkit.git
synced 2026-06-06 20:07:59 +00:00
using DivView in Sample App.
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
43E33AE32A83DD7F00CEFAD5 /* DivKit in Frameworks */ = {isa = PBXBuildFile; productRef = 43E33AE22A83DD7F00CEFAD5 /* DivKit */; };
|
||||
43E33AE52A83DD7F00CEFAD5 /* DivKitExtensions in Frameworks */ = {isa = PBXBuildFile; productRef = 43E33AE42A83DD7F00CEFAD5 /* DivKitExtensions */; };
|
||||
895BED8428BD0FF400F572B6 /* DivHostView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 895BED8328BD0FF400F572B6 /* DivHostView.swift */; };
|
||||
8C705EF8258A3FDE00468EB6 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C705EF7258A3FDE00468EB6 /* AppDelegate.swift */; };
|
||||
8C705EFC258A3FDE00468EB6 /* DivHostViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C705EFB258A3FDE00468EB6 /* DivHostViewController.swift */; };
|
||||
@@ -15,7 +17,6 @@
|
||||
8C705F04258A3FE000468EB6 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8C705F02258A3FE000468EB6 /* LaunchScreen.storyboard */; };
|
||||
8C705F1E258A48CC00468EB6 /* div_json.json in Resources */ = {isa = PBXBuildFile; fileRef = 8C705F1D258A48CC00468EB6 /* div_json.json */; };
|
||||
8C705F23258A4F1000468EB6 /* DivJson.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C705F22258A4F1000468EB6 /* DivJson.swift */; };
|
||||
8C896C8D28E1C6C500D864DB /* DivKit in Frameworks */ = {isa = PBXBuildFile; productRef = 8C896C8C28E1C6C500D864DB /* DivKit */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
@@ -36,7 +37,8 @@
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
8C896C8D28E1C6C500D864DB /* DivKit in Frameworks */,
|
||||
43E33AE32A83DD7F00CEFAD5 /* DivKit in Frameworks */,
|
||||
43E33AE52A83DD7F00CEFAD5 /* DivKitExtensions in Frameworks */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
@@ -92,7 +94,8 @@
|
||||
);
|
||||
name = DivKitSample;
|
||||
packageProductDependencies = (
|
||||
8C896C8C28E1C6C500D864DB /* DivKit */,
|
||||
43E33AE22A83DD7F00CEFAD5 /* DivKit */,
|
||||
43E33AE42A83DD7F00CEFAD5 /* DivKitExtensions */,
|
||||
);
|
||||
productName = DivKitSample;
|
||||
productReference = 8C705EF4258A3FDE00468EB6 /* DivKitSample.app */;
|
||||
@@ -122,7 +125,7 @@
|
||||
);
|
||||
mainGroup = 8C705EEB258A3FDE00468EB6;
|
||||
packageReferences = (
|
||||
8C896C8B28E1C6C500D864DB /* XCRemoteSwiftPackageReference "divkit-ios" */,
|
||||
43E33AE12A83DD7F00CEFAD5 /* XCRemoteSwiftPackageReference "divkit-ios" */,
|
||||
);
|
||||
productRefGroup = 8C705EF5258A3FDE00468EB6 /* Products */;
|
||||
projectDirPath = "";
|
||||
@@ -368,22 +371,27 @@
|
||||
/* End XCConfigurationList section */
|
||||
|
||||
/* Begin XCRemoteSwiftPackageReference section */
|
||||
8C896C8B28E1C6C500D864DB /* XCRemoteSwiftPackageReference "divkit-ios" */ = {
|
||||
43E33AE12A83DD7F00CEFAD5 /* XCRemoteSwiftPackageReference "divkit-ios" */ = {
|
||||
isa = XCRemoteSwiftPackageReference;
|
||||
repositoryURL = "https://github.com/divkit/divkit-ios";
|
||||
requirement = {
|
||||
branch = main;
|
||||
kind = branch;
|
||||
kind = upToNextMajorVersion;
|
||||
minimumVersion = 27.0.0;
|
||||
};
|
||||
};
|
||||
/* End XCRemoteSwiftPackageReference section */
|
||||
|
||||
/* Begin XCSwiftPackageProductDependency section */
|
||||
8C896C8C28E1C6C500D864DB /* DivKit */ = {
|
||||
43E33AE22A83DD7F00CEFAD5 /* DivKit */ = {
|
||||
isa = XCSwiftPackageProductDependency;
|
||||
package = 8C896C8B28E1C6C500D864DB /* XCRemoteSwiftPackageReference "divkit-ios" */;
|
||||
package = 43E33AE12A83DD7F00CEFAD5 /* XCRemoteSwiftPackageReference "divkit-ios" */;
|
||||
productName = DivKit;
|
||||
};
|
||||
43E33AE42A83DD7F00CEFAD5 /* DivKitExtensions */ = {
|
||||
isa = XCSwiftPackageProductDependency;
|
||||
package = 43E33AE12A83DD7F00CEFAD5 /* XCRemoteSwiftPackageReference "divkit-ios" */;
|
||||
productName = DivKitExtensions;
|
||||
};
|
||||
/* End XCSwiftPackageProductDependency section */
|
||||
};
|
||||
rootObject = 8C705EEC258A3FDE00468EB6 /* Project object */;
|
||||
|
||||
+2
-2
@@ -5,8 +5,8 @@
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/divkit/divkit-ios",
|
||||
"state" : {
|
||||
"branch" : "main",
|
||||
"revision" : "fbc52037c5a5be8b3640e36c10cf6771e0aea158"
|
||||
"revision" : "9c6581956b4798b7a3f828d63529883f37286082",
|
||||
"version" : "27.1.0"
|
||||
}
|
||||
}
|
||||
],
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import UIKit
|
||||
|
||||
import CommonCore
|
||||
import BasePublic
|
||||
import CommonCorePublic
|
||||
import DivKit
|
||||
import LayoutKit
|
||||
|
||||
final class DivHostView: UICollectionView {
|
||||
private let components: DivKitComponents
|
||||
private var items: [Item] = []
|
||||
var items: [DivData] = []
|
||||
|
||||
init(components: DivKitComponents) {
|
||||
self.components = components
|
||||
@@ -25,68 +26,29 @@ final class DivHostView: UICollectionView {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
func setData(_ items: [DivData]) {
|
||||
self.items = items.compactMap { makeItem(data: $0) }
|
||||
}
|
||||
|
||||
func reloadItem(cardId: String) {
|
||||
guard let index = items.firstIndex(where: { $0.data.logId == cardId }) else {
|
||||
return
|
||||
}
|
||||
|
||||
let item = items[index]
|
||||
items[index] = makeItem(
|
||||
data: item.data,
|
||||
cachedImageHolders: item.block.getImageHolders()
|
||||
)
|
||||
|
||||
reloadItems(at: [IndexPath(item: index, section: 0)])
|
||||
}
|
||||
|
||||
private func makeItem(
|
||||
data: DivData,
|
||||
cachedImageHolders: [ImageHolder] = []
|
||||
) -> Item {
|
||||
let context = components.makeContext(
|
||||
cardId: DivCardID(rawValue: data.logId),
|
||||
cachedImageHolders: cachedImageHolders
|
||||
)
|
||||
return Item(data: data, block: try! data.makeBlock(context: context))
|
||||
}
|
||||
|
||||
private struct Item {
|
||||
let data: DivData
|
||||
let block: Block
|
||||
}
|
||||
|
||||
private class Cell: UICollectionViewCell {
|
||||
static let reuseIdentifier = "cell"
|
||||
|
||||
struct State {
|
||||
let block: Block
|
||||
let view: BlockView
|
||||
var divView: DivView? {
|
||||
didSet {
|
||||
guard let divView else { return }
|
||||
addSubview(divView)
|
||||
}
|
||||
}
|
||||
|
||||
var state: State?
|
||||
|
||||
func setBlock(_ block: Block) {
|
||||
let view = block.reuse(
|
||||
state?.view,
|
||||
observer: nil,
|
||||
overscrollDelegate: nil,
|
||||
renderingDelegate: nil,
|
||||
superview: self
|
||||
)
|
||||
state = State(block: block, view: view)
|
||||
func configureCell(divKitComponents: DivKitComponents, divData: DivData) {
|
||||
if divView == nil {
|
||||
self.divView = DivView(divKitComponents: divKitComponents)
|
||||
}
|
||||
divView?.setSource(.init(kind: .divData(divData), cardId: DivCardID(rawValue: divData.logId)))
|
||||
}
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
|
||||
if let state = state {
|
||||
let blockSize = state.block.size(forResizableBlockSize: bounds.size)
|
||||
state.view.frame = CGRect(origin: .zero, size: blockSize)
|
||||
}
|
||||
divView?.frame = CGRect(
|
||||
origin: .zero,
|
||||
size: divView?.intrinsicContentSize(for: bounds.size) ?? .zero
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,8 +69,7 @@ extension DivHostView: UICollectionViewDataSource {
|
||||
withReuseIdentifier: Cell.reuseIdentifier,
|
||||
for: indexPath
|
||||
) as! Cell
|
||||
let block = items[indexPath.row].block
|
||||
cell.setBlock(block)
|
||||
cell.configureCell(divKitComponents: components, divData: items[indexPath.row])
|
||||
return cell
|
||||
}
|
||||
}
|
||||
@@ -119,8 +80,11 @@ extension DivHostView: UICollectionViewDelegateFlowLayout {
|
||||
layout _: UICollectionViewLayout,
|
||||
sizeForItemAt indexPath: IndexPath
|
||||
) -> CGSize {
|
||||
let block = items[indexPath.row].block
|
||||
let height = block.heightOfVerticallyNonResizableBlock(forWidth: bounds.width)
|
||||
return CGSize(width: bounds.width, height: height)
|
||||
let view = DivView(divKitComponents: components)
|
||||
view.setSource(.init(
|
||||
kind: .divData(items[indexPath.row]),
|
||||
cardId: DivCardID(rawValue: items[indexPath.row].logId)
|
||||
))
|
||||
return view.intrinsicContentSize(for: bounds.size)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,14 +10,13 @@ final class DivHostViewController: UIViewController {
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
components = DivKitComponents(
|
||||
updateCardAction: nil,
|
||||
urlOpener: { UIApplication.shared.open($0) }
|
||||
urlHandler: DivUrlHandlerDelegate { UIApplication.shared.open($0) }
|
||||
)
|
||||
divHostView = DivHostView(components: components)
|
||||
|
||||
if let cards = try? DivJson.loadCards() {
|
||||
view.addSubview(divHostView)
|
||||
divHostView.setData(cards)
|
||||
divHostView.items = cards
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,19 +25,3 @@ final class DivHostViewController: UIViewController {
|
||||
divHostView.frame = view.bounds.inset(by: view.safeAreaInsets)
|
||||
}
|
||||
}
|
||||
|
||||
extension DivHostViewController: UIActionEventPerforming {
|
||||
func perform(uiActionEvent event: UIActionEvent, from _: AnyObject) {
|
||||
switch event.payload {
|
||||
case let .divAction(params):
|
||||
components.handleActions(params: params)
|
||||
divHostView.reloadItem(cardId: params.cardId)
|
||||
case .empty,
|
||||
.url,
|
||||
.menu,
|
||||
.json,
|
||||
.composite:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ import Foundation
|
||||
|
||||
import DivKit
|
||||
import Serialization
|
||||
import TemplatesSupport
|
||||
|
||||
public struct DivJson: Deserializable {
|
||||
public let templates: [String: Any]
|
||||
|
||||
Reference in New Issue
Block a user