Skip to content

Commit

Permalink
Move widget-handling out of viewcontroller to make it reusable (#2426)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitschlag committed Dec 13, 2024
1 parent 087a89e commit 05efcf6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
2 changes: 0 additions & 2 deletions deltachat-ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 */,
Expand Down
26 changes: 3 additions & 23 deletions deltachat-ios/Controller/FilesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
})
)
}
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions deltachat-ios/DC/DcContext+Extension.swift
Original file line number Diff line number Diff line change
@@ -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] {
Expand All @@ -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")
}
}

0 comments on commit 05efcf6

Please sign in to comment.