Skip to content

can't listen callback on flutter app from IOS #344

Closed
@ysavr

Description

@ysavr

What happened?

i tried to execute callback on flutter app when tap button from IOS widget

import WidgetKit
import SwiftUI

struct Provider: TimelineProvider {
    
    private func getDataFromFlutter() -> SimpleEntry {
        let userDefault = UserDefaults(suiteName: "group.attendance")
        let checkInTimeString = userDefault?.string(forKey: "checkInTime") ?? ""
        
        let timerText: String
        if !checkInTimeString.isEmpty {
            timerText = getElapsedTimeString(from: checkInTimeString)
        } else {
            timerText = "00:00:00"
        }
        
        let checkInStatus = userDefault?.bool(forKey: "status") ?? false
        let checkInLoading = userDefault?.bool(forKey: "onLoading") ?? false
        return SimpleEntry(date: Date(), timer: timerText, status: checkInStatus, checkInLoading: checkInLoading)
    }
    
    func getElapsedTimeString(from dateString: String) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        formatter.timeZone = TimeZone.current

        guard let pastDate = formatter.date(from: dateString) else {
            return "00:00:00"
        }

        let now = Date()
        let elapsed = now.timeIntervalSince(pastDate)

        if elapsed < 0 {
            return "00:00:00" // Date is in the future
        }

        let hours = Int(elapsed) / 3600
        let minutes = (Int(elapsed) % 3600) / 60
        let seconds = Int(elapsed) % 60

        return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
    }
    
    // preview in widget gallery
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date(), timer: "00:00:00", status: false, checkInLoading: false)
    }
    
    // widget gallery/selection preview
    func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        let entry = SimpleEntry(date: Date(), timer: "00:00:00", status: false, checkInLoading: false)
        completion(entry)
    }
    
    // actual widget
    func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        let entry = getDataFromFlutter()
        
        let nextUpdate = Calendar.current.date(byAdding: .second, value: 10, to: Date())!
        let timeline = Timeline(entries: [entry], policy: .after(nextUpdate))
        completion(timeline)
    }
    
    //    func relevances() async -> WidgetRelevances<Void> {
    //        // Generate a list containing the contexts this widget is relevant in.
    //    }
}

struct SimpleEntry: TimelineEntry {
    let date: Date
    let timer: String
    let status: Bool
    var checkInStatusText: String {
        status ? "Check Out" : "Check In"
    }
    let checkInLoading: Bool
}

struct AttendanceWidgetEntryView : View {
    var entry: Provider.Entry
    
    var body: some View {
        VStack(spacing: 16) {
            Text("Work Time")
                .font(.headline)
                .foregroundColor(.black)
            
            HStack(spacing: 8) {
                Image(systemName: "clock")
                    .foregroundColor(.black)
                Text(entry.timer)
                    .font(.system(size: 36, weight: .bold, design: .monospaced))
                    .foregroundColor(.black)
            }
            
            Link(destination: URL(string: "homeWidget://checkout")!) {
                RoundedRectangle(cornerRadius: 12)
                    .fill(Color.white.opacity(0.2))
                    .overlay(
                        HStack {
                            Image(systemName: "arrow.right.circle.fill")
                                .foregroundColor(.white)
                            
                            Text(entry.checkInStatusText)
                                .font(.system(size: 16, weight: .semibold))
                                .foregroundColor(.white)
                        }
                            .frame(maxWidth: .infinity)
                            .padding(.vertical, 12)
                            .background(Color(red: 0/255, green: 222/255, blue: 156/255))
                            .cornerRadius(12)
                            .padding(.horizontal, 16)
                    )
                    .frame(height: 44)
            }
        }
    
        .background(Color.clear)
        .cornerRadius(20)
        .containerBackground(.fill.tertiary, for: .widget)
    }
}

struct AttendanceWidget: Widget {
    let kind: String = "AttendanceWidget"
    
    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Provider()) { entry in
            if #available(iOS 17.0, *) {
                AttendanceWidgetEntryView(entry: entry)
                    .containerBackground(.fill.tertiary, for: .widget)
            } else {
                AttendanceWidgetEntryView(entry: entry)
                    .padding()
                    .background()
            }
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
    }
}

#Preview(as: .systemSmall) {
    AttendanceWidget()
} timeline: {
    SimpleEntry(date: .now, timer: "00:00:00", status: false, checkInLoading: false)
    SimpleEntry(date: .now, timer: "00:00:00", status: false, checkInLoading: false)
}
@pragma('vm:entry-point')
Future<void> interactiveCallback(Uri? uri) async {
  log.d('interactiveCallback uri: $uri');
  await HomeWidget.setAppGroupId(StaticConstant.appGroupId);
  if (uri?.host == 'checkout') {
    log.d('interactiveCallback callback checkout');
  }
}

What do you expect?

the interactiveCallback can listen form android but when running on IOS the method not being called, i want to executing some api from this

Relevant log output

Execute in a terminal and put output into the code block below

Output of: flutter doctor -v

On which Platform do you experience this issue?

iOS

Other information

No response

Are you interested in working on a PR for this?

  • I want to work on this

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions