Updating timeline list in configurable widgets

I’m making an app to keep track of values across a list of trends. Part of it is to have clickers on the phone home screen to increase the trend value. With widgets now being configurable, I want the user to be able to choose which trend to see.

I was able to set it up so that an app intent handles the increment in the widget body. Then, I was able to get it to a state for the user to switch between there trends, but the issue arises when the user goes in the app and puts a new trend in the list or takes one out. The list in the widget isnt updating to reflect this and showing whatever list existed at build time.

Here’s my provider code:

struct TrendTimelineProvider: AppIntentTimelineProvider {
func timeline(for configuration: SelectClickerTrendsIntent, in context: Context) async -> Timeline {
Timeline(entries:
[TrendEntry(date: Date(), trend: getTrend(for: configuration.trend.id))],
policy: .atEnd)
}

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>func snapshot(for configuration: SelectClickerTrendsIntent, in context: Context) async -> TrendEntry {
return TrendEntry(date: Date(), trend: configuration.trend)
}
func placeholder(in context: Context) -> TrendEntry {
return TrendEntry(date: .now, trend: TrendData(blockType: ""))
}
func getTrend(for uuid: UUID) -> TrendData {
if let store = UserDefaults(suiteName: suiteName),
let data = store.data(forKey: storageKey),
let trends = try? JSONDecoder().decode([TrendData].self, from: data) {
// Find the trend with the specified UUID
if let selectedTrend = trends.first(where: { $0.id == uuid }) {
return selectedTrend
}
}
return TrendData(blockType: "")
}
</code>
<code>func snapshot(for configuration: SelectClickerTrendsIntent, in context: Context) async -> TrendEntry { return TrendEntry(date: Date(), trend: configuration.trend) } func placeholder(in context: Context) -> TrendEntry { return TrendEntry(date: .now, trend: TrendData(blockType: "")) } func getTrend(for uuid: UUID) -> TrendData { if let store = UserDefaults(suiteName: suiteName), let data = store.data(forKey: storageKey), let trends = try? JSONDecoder().decode([TrendData].self, from: data) { // Find the trend with the specified UUID if let selectedTrend = trends.first(where: { $0.id == uuid }) { return selectedTrend } } return TrendData(blockType: "") } </code>
func snapshot(for configuration: SelectClickerTrendsIntent, in context: Context) async -> TrendEntry {
    return TrendEntry(date: Date(), trend: configuration.trend)
}

func placeholder(in context: Context) -> TrendEntry {
    return TrendEntry(date: .now, trend: TrendData(blockType: ""))
}

func getTrend(for uuid: UUID) -> TrendData {
    if let store = UserDefaults(suiteName: suiteName),
       let data = store.data(forKey: storageKey),
       let trends = try? JSONDecoder().decode([TrendData].self, from: data) {
        
        // Find the trend with the specified UUID
        if let selectedTrend = trends.first(where: { $0.id == uuid }) {
            return selectedTrend
        }
    }
    return TrendData(blockType: "")
}

}

struct SelectClickerTrendsIntent: WidgetConfigurationIntent {
static var title: LocalizedStringResource = “Clicker Trend Selection”
static var description: IntentDescription? = IntentDescription(“Select a clicker trend to show”)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Parameter(title: "Clicker Trends")
var trend: TrendData
</code>
<code>@Parameter(title: "Clicker Trends") var trend: TrendData </code>
@Parameter(title: "Clicker Trends")
var trend: TrendData

}

struct TrendDataQuery: EntityQuery {

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public func entities(for identifiers: [TrendData.ID]) async throws -> [TrendData] {
return TrendData.allClicker.filter { identifiers.contains($0.id) }
}
public func suggestedEntities() async throws -> [TrendData] {
return TrendData.allClicker
}
public func defaultResult() async -> TrendData? {
return TrendData.allClicker.first
}
</code>
<code>public func entities(for identifiers: [TrendData.ID]) async throws -> [TrendData] { return TrendData.allClicker.filter { identifiers.contains($0.id) } } public func suggestedEntities() async throws -> [TrendData] { return TrendData.allClicker } public func defaultResult() async -> TrendData? { return TrendData.allClicker.first } </code>
public func entities(for identifiers: [TrendData.ID]) async throws -> [TrendData] {
    
    return TrendData.allClicker.filter { identifiers.contains($0.id) }
   
    
}

public func suggestedEntities() async throws -> [TrendData] {
    
    return TrendData.allClicker
    
    
}

public func defaultResult() async -> TrendData? {
    
        return TrendData.allClicker.first
    
}

}

struct TrendData: Identifiable, Codable, Equatable, Hashable, AppEntity {

public static var typeDisplayRepresentation: TypeDisplayRepresentation = “Trends”
public static var defaultQuery = TrendDataQuery()

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "(title)")
}
</code>
<code>public var displayRepresentation: DisplayRepresentation { DisplayRepresentation(title: "(title)") } </code>
public var displayRepresentation: DisplayRepresentation {
    DisplayRepresentation(title: "(title)")
}


}

** Notes:

  1. “trend: getTrend(for: configuration.trend.id))” was the only way i could get the buttons to actually update in the widget when they get clicked.
  2. I have two types of trends and ones called clicker hence “allClicker”.

New contributor

Jeremy Gavrilov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật