From 05efcf6d9619de106065ee05458d0ed8372b536d Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Fri, 13 Dec 2024 11:36:21 +0100 Subject: [PATCH] Move widget-handling out of viewcontroller to make it reusable (#2426) --- deltachat-ios.xcodeproj/project.pbxproj | 2 -- .../Controller/FilesViewController.swift | 26 +++---------------- deltachat-ios/DC/DcContext+Extension.swift | 20 ++++++++++++++ 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/deltachat-ios.xcodeproj/project.pbxproj b/deltachat-ios.xcodeproj/project.pbxproj index 84e2b6ca7..109d88bb2 100644 --- a/deltachat-ios.xcodeproj/project.pbxproj +++ b/deltachat-ios.xcodeproj/project.pbxproj @@ -226,7 +226,6 @@ D89C456D2D0AF6A8005B1491 /* WidgetEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = D89C456C2D0AF6A8005B1491 /* WidgetEntry.swift */; }; D89C456F2D0AF7A5005B1491 /* DcContext+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D89C456E2D0AF7A5005B1491 /* DcContext+Extension.swift */; }; D89C45702D0AF7A5005B1491 /* DcContext+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D89C456E2D0AF7A5005B1491 /* DcContext+Extension.swift */; }; - D89C45712D0AF7A5005B1491 /* DcContext+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D89C456E2D0AF7A5005B1491 /* DcContext+Extension.swift */; }; D89C45722D0B0166005B1491 /* WidgetEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = D89C456C2D0AF6A8005B1491 /* WidgetEntry.swift */; }; D89C45732D0B0166005B1491 /* WidgetEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = D89C456C2D0AF6A8005B1491 /* WidgetEntry.swift */; }; D8A072A02BED0FD8001A4C7C /* InstantOnboardingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8A0729F2BED0FD8001A4C7C /* InstantOnboardingView.swift */; }; @@ -1627,7 +1626,6 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - D89C45712D0AF7A5005B1491 /* DcContext+Extension.swift in Sources */, 30C7D5F128F4808C0078D24C /* MessageCounter.swift in Sources */, 30C7D5F028F47F4F0078D24C /* InitialsBadge.swift in Sources */, 3057027F24C5B2F800D84EFC /* ChatListViewModel.swift in Sources */, diff --git a/deltachat-ios/Controller/FilesViewController.swift b/deltachat-ios/Controller/FilesViewController.swift index d9626b4a9..eb1aaa7d4 100644 --- a/deltachat-ios/Controller/FilesViewController.swift +++ b/deltachat-ios/Controller/FilesViewController.swift @@ -245,8 +245,8 @@ extension FilesViewController: UITableViewDelegate, UITableViewDataSource { systemImageName: "rectangle.on.rectangle.slash", indexPath: indexPath, action: { _ in - self.removeFromHomescreen(messageId: messageId) - }) + self.dcContext.removeWebxdcFromHomescreen(messageId: messageId) + }) ) } else { children.append( @@ -255,7 +255,7 @@ extension FilesViewController: UITableViewDelegate, UITableViewDataSource { systemImageName: "plus.rectangle.on.rectangle", indexPath: indexPath, action: { _ in - self.addToHomescreen(messageId: messageId) + self.dcContext.addWebxdcToHomescreenWidget(messageId: messageId) }) ) } @@ -324,26 +324,6 @@ extension FilesViewController { Utils.share(message: dcContext.getMessage(id: msgId), parentViewController: self, sourceView: cell.contentView) } } - - @available(iOS 15, *) - func addToHomescreen(messageId: Int) { - let entry = WidgetEntry(accountId: dcContext.id, messageId: messageId) - var entries = dcContext.shownWidgets() - entries.insert(entry, at: entries.startIndex) - - dcContext.storeShownWidgets(entries) - WidgetCenter.shared.reloadTimelines(ofKind: "DcWebxdcWidget") - } - - @available(iOS 15, *) - func removeFromHomescreen(messageId: Int) { - let entry = WidgetEntry(accountId: dcContext.id, messageId: messageId) - var entries = dcContext.shownWidgets() - entries.removeAll { $0 == entry } - - dcContext.storeShownWidgets(entries) - WidgetCenter.shared.reloadTimelines(ofKind: "DcWebxdcWidget") - } } class WebxdcItemSource: NSObject, UIActivityItemSource { diff --git a/deltachat-ios/DC/DcContext+Extension.swift b/deltachat-ios/DC/DcContext+Extension.swift index 66e7ffaea..952109659 100644 --- a/deltachat-ios/DC/DcContext+Extension.swift +++ b/deltachat-ios/DC/DcContext+Extension.swift @@ -1,6 +1,8 @@ import Foundation import DcCore +import WidgetKit +@available(iOS 15, *) extension DcContext { private static let key = "ui.ios.selected_apps_for_widget" func shownWidgets() -> [WidgetEntry] { @@ -21,4 +23,22 @@ extension DcContext { setConfig(Self.key, jsonString) } + + func addWebxdcToHomescreenWidget(messageId: Int) { + let entry = WidgetEntry(accountId: self.id, messageId: messageId) + var entries = shownWidgets() + entries.insert(entry, at: entries.startIndex) + + storeShownWidgets(entries) + WidgetCenter.shared.reloadTimelines(ofKind: "DcWebxdcWidget") + } + + func removeWebxdcFromHomescreen(messageId: Int) { + let entry = WidgetEntry(accountId: self.id, messageId: messageId) + var entries = shownWidgets() + entries.removeAll { $0 == entry } + + storeShownWidgets(entries) + WidgetCenter.shared.reloadTimelines(ofKind: "DcWebxdcWidget") + } }