Skip to content

Commit

Permalink
Add widget edit mode apply fix (#4162)
Browse files Browse the repository at this point in the history
* add context menu for widgets

* createNewWidgets

* add widget below | add widget above

* fix navigation for modal add widgets

* grooming

* Widget size doesn't save if it was added in Edit mode
#4137

* grooming
  • Loading branch information
tigrim authored Dec 2, 2024
1 parent c63a9f9 commit 0225c5a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
23 changes: 17 additions & 6 deletions Sources/Controllers/Map/Widgets/Helpers/WidgetUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class WidgetUtils {
static func reorderWidgets(orderedWidgetPages: [[String]],
panel: WidgetsPanel,
selectedAppMode: OAApplicationMode,
widgetParams: [String: Any]? = nil) {
widgetParamsArray: [[String: Any]]? = nil) {
let widgetRegistry = OARootViewController.instance().mapPanel.mapWidgetRegistry

let enabledWidgets: [String] = orderedWidgetPages.flatMap { $0 }
Expand All @@ -22,7 +22,7 @@ final class WidgetUtils {
panel: panel,
appMode: selectedAppMode,
widgetRegistry: widgetRegistry,
widgetParams: widgetParams)
widgetParamsArray: widgetParamsArray)

panel.setWidgetsOrder(pagedOrder: newOrders, appMode: selectedAppMode)
widgetRegistry.reorderWidgets()
Expand Down Expand Up @@ -55,8 +55,8 @@ final class WidgetUtils {
panel: WidgetsPanel,
appMode: OAApplicationMode,
widgetRegistry: OAMapWidgetRegistry,
widgetParams: [String: Any]? = nil) -> [[String]] {
let newWidgetsList: NSMutableArray = buildNewWidgetsList(enabledWidgets: enabledWidgets, panel: panel, appMode: appMode, widgetRegistry: widgetRegistry, widgetParams: widgetParams)
widgetParamsArray: [[String: Any]]? = nil) -> [[String]] {
let newWidgetsList: NSMutableArray = buildNewWidgetsList(enabledWidgets: enabledWidgets, panel: panel, appMode: appMode, widgetRegistry: widgetRegistry, widgetParamsArray: widgetParamsArray)
var newOrders = [[String]]()
for page in orderedWidgetPages {
var newOrder: [String] = []
Expand All @@ -79,18 +79,29 @@ final class WidgetUtils {
panel: WidgetsPanel,
appMode: OAApplicationMode,
widgetRegistry: OAMapWidgetRegistry,
widgetParams: [String: Any]? = nil) -> NSMutableArray {
widgetParamsArray: [[String: Any]]? = nil) -> NSMutableArray {
let newWidgetsList = NSMutableArray()
let widgetsFactory = MapWidgetsFactory()
if !enabledWidgets.isEmpty {
let currentWidgetIds = NSMutableArray(array: widgetRegistry.getWidgetsFor(panel).compactMap { ($0 as! MapWidgetInfo).key })
var widgetParamsArrayLocal = widgetParamsArray
for widgetInfoId in enabledWidgets {
if !currentWidgetIds.contains(widgetInfoId) {
var params: [String: Any]?
if widgetParamsArrayLocal != nil {
if let index = widgetParamsArrayLocal?.firstIndex(where: {
guard let id = $0["id"] as? String else { return false }
return id == widgetInfoId
}) {
params = widgetParamsArrayLocal?.remove(at: index)
}
}

if let newMapWidgetInfo = createWidget(widgetId: widgetInfoId,
panel: panel,
widgetsFactory: widgetsFactory,
selectedAppMode: appMode,
widgetParams: widgetParams) {
widgetParams: params) {
newWidgetsList.add(newMapWidgetInfo)
currentWidgetIds.remove(widgetInfoId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SafariServices

@objc(OAWidgetsListViewController)
@objcMembers
class WidgetsListViewController: OABaseNavbarSubviewViewController {
final class WidgetsListViewController: OABaseNavbarSubviewViewController {

private static let enabledWidgetsFilter = Int(KWidgetModeAvailable | kWidgetModeEnabled | kWidgetModeMatchingPanels)

Expand All @@ -20,10 +20,11 @@ class WidgetsListViewController: OABaseNavbarSubviewViewController {
private let kNoWidgetsKey = "noWidgets"
private let kWidgetsInfoKey = "widget_info"
private let kIsLastWidgetInSection = "isLastWidgetInSection"
private let kWidgetAddParamsKey = "widget_add_params"

private var editingComplexWidget: MapWidgetInfo?
private let panels = WidgetsPanel.values

let panels = WidgetsPanel.values
private var editingComplexWidget: MapWidgetInfo?

private var widgetPanel: WidgetsPanel! {
didSet {
Expand Down Expand Up @@ -178,25 +179,33 @@ class WidgetsListViewController: OABaseNavbarSubviewViewController {
createNewSection = WidgetType.isComplexWidget(lastWidget?.key ?? "")
}
}

var widgetParametersForAddition: [String: Any]?
var widgetStyleForRow: EOAWidgetSizeStyle?

if createNewSection {
createWidgetItems(NSOrderedSet(object: newWidget), Int(tableData.sectionCount()))
createWidgetItems(NSOrderedSet(object: newWidget), Int(tableData.sectionCount()), params: params)
} else {
widgetStyleForRow = updateWidgetStyleForRowsInLastPage(newWidget, lastSectionData)
createWidgetItem(newWidget, lastSectionData)
if var params {
params["id"] = newWidget.key
if let widgetStyleForRow, !editMode {
params["widgetSizeStyle"] = widgetStyleForRow.rawValue
}
widgetParametersForAddition = params
}
createWidgetItem(newWidget, lastSectionData, params: widgetParametersForAddition)
configureWidgetsSeparator()
}

if editMode {
DispatchQueue.main.async { [weak self] in
self?.tableView.reloadData()
self?.updateBottomButtons()
}
} else {
if var userInfo = params {
if let widgetStyleForRow {
userInfo["widgetSizeStyle"] = widgetStyleForRow.rawValue
}
reorderWidgets(with: userInfo)
if let widgetParametersForAddition {
reorderWidgets(with: [widgetParametersForAddition])
} else {
reorderWidgets()
}
Expand All @@ -220,9 +229,10 @@ class WidgetsListViewController: OABaseNavbarSubviewViewController {

// MARK: - Additions

private func reorderWidgets(with widgetParams: [String: Any]? = nil) {
private func reorderWidgets(with widgetParamsArray: [[String: Any]]? = nil) {
var orders = [[String]]()
var currPage = [String]()
var addWidgetsParamsArray = widgetParamsArray
for i in 0..<tableData.sectionData(for: 0).rowCount() {
let rowData = tableData.sectionData(for: 0).getRow(i)

Expand All @@ -234,13 +244,20 @@ class WidgetsListViewController: OABaseNavbarSubviewViewController {
if let row = rowData.obj(forKey: kWidgetsInfoKey) as? MapWidgetInfo {
currPage.append(row.key)
}
if editMode, let params = rowData.obj(forKey: kWidgetAddParamsKey) as? [String: Any] {
if addWidgetsParamsArray == nil {
addWidgetsParamsArray = [[String: Any]]()
}
addWidgetsParamsArray?.append(params)
rowData.removeObject(forKey: kWidgetAddParamsKey)
}
}
orders.append(currPage)

WidgetUtils.reorderWidgets(orderedWidgetPages: orders,
panel: widgetPanel,
selectedAppMode: selectedAppMode,
widgetParams: widgetParams)
widgetParamsArray: widgetParamsArray ?? addWidgetsParamsArray)
}
}

Expand Down Expand Up @@ -487,13 +504,13 @@ extension WidgetsListViewController {
tableData.clearAllData()
tableData.createNewSection()
for i in 0..<pagedWidgets.count {
createWidgetItems(pagedWidgets[i], i)
createWidgetItems(pagedWidgets[i], i, params: nil)
}
configureWidgetsSeparator()
}
}

private func createWidgetItems(_ widgets: NSOrderedSet, _ pageIndex: Int) {
private func createWidgetItems(_ widgets: NSOrderedSet, _ pageIndex: Int, params: [String: Any]?) {
let section = tableData.sectionData(for: 0)
let row = section.createNewRow()
row.key = kPageKey
Expand All @@ -502,11 +519,11 @@ extension WidgetsListViewController {

let sortedWidgets = (widgets.array as! [MapWidgetInfo]).sorted { $0.priority < $1.priority }
for widget in sortedWidgets {
createWidgetItem(widget, section)
createWidgetItem(widget, section, params: params)
}
}

private func createWidgetItem(_ widget: MapWidgetInfo, _ section: OATableSectionData) {
private func createWidgetItem(_ widget: MapWidgetInfo, _ section: OATableSectionData, params: [String: Any]?) {
if section.rowCount() > 0 && section.getRow(0).key != kPageKey {
section.addRow(OATableRowData(), position: 0)
let row = section.getRow(0)
Expand All @@ -516,6 +533,9 @@ extension WidgetsListViewController {

let row = section.createNewRow()
row.setObj(widget, forKey: kWidgetsInfoKey)
if let params {
row.setObj(params, forKey: kWidgetAddParamsKey)
}
if widget.widget.widgetType == .sunPosition,
let sunPositionWidgetState = widget.getWidgetState() as? OASunriseSunsetWidgetState {
row.iconName = sunPositionWidgetState.getWidgetIconName()
Expand Down
1 change: 1 addition & 0 deletions Sources/Models/OATableRowData.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ typedef NS_ENUM(NSInteger, EOATableRowType) {

- (void) setObj:(id)data forKey:(NSString *)key;
- (nullable id) objForKey:(NSString *)key;
- (void)removeObjectForKey:(nonnull NSString *)key;

- (nullable NSString *) stringForKey:(NSString *)key;
- (NSInteger) integerForKey:(NSString *)key;
Expand Down
5 changes: 5 additions & 0 deletions Sources/Models/OATableRowData.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ - (id) objForKey:(nonnull NSString *)key
return _data[key];
}

- (void)removeObjectForKey:(nonnull NSString *)key
{
[_data removeObjectForKey:key];
}

- (NSString *) stringForKey:(nonnull NSString *)key
{
return [_data[key] stringValue];
Expand Down

0 comments on commit 0225c5a

Please sign in to comment.