🛠 Refactor: ContextMenu Logic WIP

This commit is contained in:
Dominic Go 2020-10-24 12:06:42 +08:00
parent 611ee4823f
commit 5edebd06e4
5 changed files with 34 additions and 32 deletions

View File

@ -8,12 +8,13 @@
import Foundation
import UIKit
@available(iOS 13, *)
class RCTContextMenuView: UIView {
@objc var onPressMenuItem: RCTDirectEventBlock?;
private var _menuConfig: RCTMenuItem<RCTMenuElementItem>?;
private var _menuConfig: RCTMenuItem?;
@objc var menuConfig: NSDictionary? {
didSet {
guard
@ -60,7 +61,7 @@ extension RCTContextMenuView: UIContextMenuInteractionDelegate {
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { suggestedActions in
return menuConfig.createMenu({ (key, action) in
self.onPressMenuItem?(["key": key]);
})
});
});
};
};

View File

@ -9,7 +9,8 @@
import Foundation
struct RCTMenuActionItem: Hashable, Encodable, RCTMenuElement {
@available(iOS 13, *)
class RCTMenuActionItem: RCTMenuElement {
var actionKey : String;
var actionTitle: String;
@ -20,14 +21,10 @@ struct RCTMenuActionItem: Hashable, Encodable, RCTMenuElement {
var menuState : String?;
var menuAttributes: [String]?;
};
// ------------------------------
// MARK: RCTMenuActionItem - Init
// ------------------------------
@available(iOS 13, *)
extension RCTMenuActionItem {
init?(dictionary: NSDictionary){
guard
let actionKey = dictionary["actionKey" ] as? NSString,
@ -67,7 +64,7 @@ extension RCTMenuActionItem {
#endif
};
init?(dictionary: NSDictionary?){
convenience init?(dictionary: NSDictionary?){
guard let dictionary = dictionary else { return nil };
self.init(dictionary: dictionary);
};

View File

@ -0,0 +1,20 @@
//
// RCTMenuProtocols.swift
// IosContextMenuExample
//
// Created by Dominic Go on 10/24/20.
// Copyright © 2020 Facebook. All rights reserved.
//
import Foundation
class RCTMenuElement: Hashable, Encodable {
static func == (lhs: RCTMenuElement, rhs: RCTMenuElement) -> Bool {
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs);
};
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self).hashValue)
};
};

View File

@ -9,9 +9,8 @@
import UIKit;
struct RCTMenuElementItem: RCTMenuElement {};
struct RCTMenuItem<T>: Hashable, Encodable where T: RCTMenuElement {
@available(iOS 13.0, *)
class RCTMenuItem: RCTMenuElement {
var menuTitle: String;
@ -19,18 +18,13 @@ struct RCTMenuItem<T>: Hashable, Encodable where T: RCTMenuElement {
var imageType : ImageType = .NONE;
var imageValue : String?;
var menuItems: Array<T>?;
var menuItems: [RCTMenuElement]?;
};
// ------------------------
// MARK: RCTMenuItem - Init
// ------------------------
@available(iOS 13, *)
extension RCTMenuItem {
init?(dictionary: NSDictionary){
guard let menuTitle = dictionary["menuTitle"] as? NSString else {
#if DEBUG
print("RCTMenuItem, init failed - menuTitle: nil");
@ -64,13 +58,13 @@ extension RCTMenuItem {
#if DEBUG
print("RCTMenuItem, init - compactMap: Creating RCTMenuItem...");
#endif
return menuItem as? T;
return menuItem;
} else if let menuAction = RCTMenuActionItem(dictionary: $0 as? NSDictionary) {
#if DEBUG
print("RCTMenuItem, init - compactMap: Creating RCTMenuActionItem...");
#endif
return menuAction as? T;
return menuAction;
} else {
#if DEBUG
@ -88,7 +82,7 @@ extension RCTMenuItem {
#endif
};
init?(dictionary: NSDictionary?){
convenience init?(dictionary: NSDictionary?){
guard let dictionary = dictionary else { return nil };
self.init(dictionary: dictionary);
};
@ -166,3 +160,5 @@ extension RCTMenuItem {
};

View File

@ -1,12 +0,0 @@
//
// RCTMenuProtocols.swift
// IosContextMenuExample
//
// Created by Dominic Go on 10/24/20.
// Copyright © 2020 Facebook. All rights reserved.
//
import Foundation
protocol RCTMenuElement: Hashable, Encodable {};