[Widgets]: (complete) Fixed previous pagination issues for ActiveAppsWidget

This commit is contained in:
Magesh K
2025-01-09 18:50:44 +05:30
parent 46871f63ed
commit 4e10527f03
7 changed files with 154 additions and 174 deletions

View File

@@ -1,5 +1,5 @@
//
// HomeScreenWidget.swift
// ActiveAppsWidget.swift
// AltWidgetExtension
//
// Created by Riley Testut on 8/16/23.
@@ -8,10 +8,6 @@
import SwiftUI
import WidgetKit
import CoreData
import AltStoreCore
import AltSign
private extension Color
{
@@ -24,24 +20,40 @@ private extension Color
//@available(iOS 17, *)
struct ActiveAppsWidget: Widget
{
private var viewModel = PaginationViewModel.getNewInstance(
"ActiveApps" + UUID().uuidString
)
// only constants/singleton what needs to be for the life of all widgets of ActiveAppsWidget type
// should be declared as instance or class fields for ActiveAppWidgets
// NOTE: The computed property (widget)body is recomputed/re-run for every
// 'type' refers to struct/class types and 'kind' refers to the tag which the widget is marked with
// multiple instances of same type or multiple types can be tagged with same 'kind'
// or each instance of same kind too, can be tagged as different(unique) 'kind'
// 1. widget-resizing(of same type)
// 2. new widget addition(of same type)
public var body: some WidgetConfiguration {
if #available(iOS 17, *)
{
let kind = "ActiveApps"
let widgetID = kind + "-" + UUID().uuidString
let holder = PaginationDataHolder.instance(widgetID)
let timelineProvider = AppsTimelineProvider(holder) // pass the holder
// each instance of this widget type is identified by unique 'kind' tag
// so that a reloadTimelineFor(kind:) will trigger reload only for that instance
let staticConfig = StaticConfiguration(
kind: viewModel.widgetID,
provider: AppsTimelineProvider(viewModel)
kind: widgetID,
provider: timelineProvider
) { entry in
ActiveAppsWidgetView(entry: entry, viewModel: viewModel)
// actual view of the widget
// this gets recreated for each trigger from the scheduled timeline entries provided by the timeline provider
// NOTE: widget views do not adhere to statefulness
// so, Combine constructs such as @State, @StateObject, @ObservedObject etc are simply ignored
ActiveAppsWidgetView(entry: entry, widgetID: widgetID)
}
.supportedFamilies([.systemMedium])
.configurationDisplayName("Active Apps")
.description("View remaining days until your active apps expire. Tap the countdown timers to refresh them in the background.")
// this widgetConfiguration is requested/drawn once per widget per process lifecycle
return staticConfig
}
else
@@ -57,17 +69,11 @@ struct ActiveAppsWidget: Widget
private struct ActiveAppsWidgetView: View
{
var entry: AppsEntry
@ObservedObject private var viewModel: PaginationViewModel
var widgetID: String
@Environment(\.colorScheme)
private var colorScheme
init(entry: AppsEntry, viewModel: PaginationViewModel){
self.entry = entry
self.viewModel = viewModel
}
var body: some View {
Group {
if entry.apps.isEmpty
@@ -95,17 +101,14 @@ private struct ActiveAppsWidgetView: View
private var content: some View {
GeometryReader { (geometry) in
let MAX_ROWS_PER_PAGE = PaginationViewModel.MAX_ROWS_PER_PAGE
let MAX_ROWS_PER_PAGE = PaginationDataHolder.MAX_ROWS_PER_PAGE
let preferredRowHeight = (geometry.size.height / Double(MAX_ROWS_PER_PAGE)) - 8
let rowHeight = min(preferredRowHeight, geometry.size.height / 2)
HStack(alignment: .center) {
// VStack(spacing: 12) {
LazyVStack(spacing: 12) {
ForEach($viewModel.sliding_window, id: \.bundleIdentifier) { app in
let app = app.wrappedValue // remove the binding
ForEach(Array(entry.apps.enumerated()), id: \.offset) { index, app in
let icon: UIImage = app.icon ?? UIImage(named: "SideStore")!
@@ -160,6 +163,8 @@ private struct ActiveAppsWidgetView: View
.padding(.all, -5)
}
.font(.system(size: 16, weight: .semibold, design: .rounded))
// this modifier invalidates the view (disables userinteraction and shows a blinking effect)
// until new timeline events occur, unless a observable boolean state is presented as parameter
.invalidatableContent()
.activatesRefreshAllAppsIntent()
}
@@ -174,9 +179,9 @@ private struct ActiveAppsWidgetView: View
Image(systemName: "arrow.up")
.resizable()
.frame(width: buttonWidth, height: buttonWidth)
.mask(Capsule())
.opacity(0.3)
.pageUpButton(widgetID: viewModel.widgetID)
// .mask(Capsule())
.pageUpButton(widgetID)
Spacer()
@@ -184,8 +189,8 @@ private struct ActiveAppsWidgetView: View
.resizable()
.frame(width: buttonWidth, height: buttonWidth)
.opacity(0.3)
.mask(Capsule())
.pageDownButton(widgetID: viewModel.widgetID)
// .mask(Capsule())
.pageDownButton(widgetID)
}
.padding(.vertical)
}

View File

@@ -0,0 +1,72 @@
//
// PaginationViewModel.swift
// AltStore
//
// Created by Magesh K on 09/01/25.
// Copyright © 2025 SideStore. All rights reserved.
//
import Foundation
class PaginationDataHolder: ObservableObject {
public static let MAX_ROWS_PER_PAGE: UInt = 3
private static var instances: [String:PaginationDataHolder] = [:]
public static func instance(_ widgetID: String) -> PaginationDataHolder {
let instance = PaginationDataHolder(widgetID)
Self.instances[widgetID] = instance
return instance
}
public static func holder(for widgetID: String) -> PaginationDataHolder? {
return Self.instances[widgetID]
}
private lazy var serializationQ = {
DispatchQueue(label: widgetID)
}()
public let widgetID: String
private var currentPageindex: UInt = 0
private init(_ widgetID: String){
self.widgetID = widgetID
}
deinit {
Self.instances.removeValue(forKey: widgetID)
}
public func updateDirection(_ direction: Direction) {
switch(direction){
case .up:
let pageIndex = Int(currentPageindex)
currentPageindex = UInt(max(0, pageIndex-1))
case .down:
// upper-bounds is checked when computing targetPageIndex in getUpdatedData
currentPageindex+=1
}
}
public func getUpdatedData(entries: [AppSnapshot]) -> [AppSnapshot] {
let count = UInt(entries.count)
if(count == 0) { return entries }
let availablePages = UInt(ceil(Double(entries.count) / Double(Self.MAX_ROWS_PER_PAGE)))
let targetPageIndex: UInt = currentPageindex < availablePages ? currentPageindex : availablePages-1
// do blocking update
serializationQ.sync {
self.currentPageindex = targetPageIndex // preserve it
}
let startIndex = targetPageIndex * Self.MAX_ROWS_PER_PAGE
let estimatedEndIndex = startIndex + (Self.MAX_ROWS_PER_PAGE-1)
let endIndex: UInt = min(count-1, estimatedEndIndex)
let currentPageEntries = entries[Int(startIndex) ... Int(endIndex)]
return Array(currentPageEntries)
}
}

View File

@@ -1,105 +0,0 @@
//
// PaginationViewModel.swift
// AltStore
//
// Created by Magesh K on 09/01/25.
// Copyright © 2025 SideStore. All rights reserved.
//
import Foundation
import Combine
class PaginationViewModel: ObservableObject {
private static var instances: [String:PaginationViewModel] = [:]
public static let MAX_ROWS_PER_PAGE = 3
@Published public var sliding_window: [AppSnapshot] = []
@Published public var refreshed: Bool = false
private init(){}
private var _widgetID: String?
public lazy var widgetID: String = {
return _widgetID!
}()
public static func getNewInstance(_ widgetID: String) -> PaginationViewModel {
let instance = PaginationViewModel()
PaginationViewModel.instances[widgetID] = instance
instance._widgetID = widgetID
return instance
}
public static func instance(_ widgetID: String) -> PaginationViewModel? {
return PaginationViewModel.instances[widgetID]
}
public private(set) var backup_entries: [AppSnapshot] = []
private var r_queue: [AppSnapshot] = []
private var l_queue: [AppSnapshot] = []
private var lastIndex: Int { r_queue.count - 1 }
public func setEntries(_ entries: [AppSnapshot]) {
r_queue = entries
backup_entries = entries
}
public func handlePagination(_ direction: Direction) {
var sliding_window = Array(sliding_window)
var l_queue = Array(l_queue)
var r_queue = Array(r_queue)
// If entries is empty, do nothing
guard !backup_entries.isEmpty else {
sliding_window.removeAll()
return
}
switch direction {
case .up:
// move window contents to left-q since we are moving right side
if !sliding_window.isEmpty {
// take the window as-is and put it to right of l_queue
l_queue.append(contentsOf: sliding_window)
}
// clear the window
sliding_window.removeAll()
let size = min(r_queue.count, Self.MAX_ROWS_PER_PAGE)
for _ in 0..<size {
if !r_queue.isEmpty {
sliding_window.append(r_queue.removeFirst())
}
}
case .down:
// move window contents to right-q since we are moving left side
if !sliding_window.isEmpty {
// take the window as-is and put it to left of r_queue
r_queue.insert(contentsOf: sliding_window, at: 0)
}
// clear the window
sliding_window.removeAll()
let size = min(l_queue.count, Self.MAX_ROWS_PER_PAGE)
for _ in 0..<size {
sliding_window.insert(l_queue.removeLast(), at: 0)
}
}
if !sliding_window.isEmpty {
// commit
self.sliding_window = sliding_window
self.l_queue = l_queue
self.r_queue = r_queue
self.refreshed = true
}
}
}