Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lockscreen Widget onTap does not call HomeWidget.widgetClicked.listen(_launchedFromWidget) #232

Open
danieljiwonkang98 opened this issue Mar 8, 2024 · 11 comments
Labels
p:iOS iOS Home Screen Widgets

Comments

@danieljiwonkang98
Copy link

I have followed the lockscreen guide from this blog. When tapping lockscreen widget the HomeWidget.widgetClicked.listen is not being called. How can I integrate this?

Dart RootScreen

  void _launchedFromWidget(Uri? uri) {
    AppLogger.logInfo('CameraScreen: Launched from widget');
    // Get.to(() => const CameraScreen());
    if (uri != null) {}
  }

  @override
  void initState() {
    HomeWidget.widgetClicked.listen(_launchedFromWidget);
    _renderLockscreenWidget();
    HiveService.init();
    super.initState();
    _navigateBasedOnAuthStatus();
  }


Xcode

//
//  LockScreen.swift
//  LockScreen
//
//  Created by Anton Borries on 02.09.23.
//

import SwiftUI
import WidgetKit

struct Provider: TimelineProvider {
  func placeholder(in context: Context) -> CounterEntry {
    CounterEntry(date: Date(), count: 0)
  }

  func getSnapshot(in context: Context, completion: @escaping (CounterEntry) -> Void) {
    // Get the UserDefaults for the AppGroup
    let prefs = UserDefaults(suiteName: "group.imhome.lockscreen")
    // Load the current Count
    let entry = CounterEntry(date: Date(), count: prefs?.integer(forKey: "lock_screen") ?? 0)
    completion(entry)
  }

  func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
    getSnapshot(in: context) { (entry) in
      let timeline = Timeline(entries: [entry], policy: .atEnd)
      completion(timeline)
    }
  }
}

struct CounterEntry: TimelineEntry {
  let date: Date
  let count: Int
}

struct LockScreenEntryView: View {
  var entry: Provider.Entry

  @Environment(\.widgetFamily) var family

  var body: some View {
    if family == .accessoryCircular {
      Image(
        uiImage: UIImage(
          contentsOfFile: UserDefaults(suiteName: "group.imhome.lockscreen")?.string(
            forKey: "lock_screen") ?? "")!
      ).resizable()
        .frame(width: 76, height: 76)
        .scaledToFill().widgetURL(URL(string: "lock_screen://message?message=na&homeWidget"))
    } else {
      VStack {
        Text("You have pushed the button this many times:").font(.caption2).frame(
          maxWidth: .infinity, alignment: .center)
        Spacer()
        Text(entry.count.description).font(.title).frame(maxWidth: .infinity, alignment: .center)
        Spacer()
        HStack {
          // This button is for clearing
          Button(intent: BackgroundIntent(method: "clear")) {
            Image(systemName: "xmark").font(.system(size: 16)).foregroundColor(.red).frame(
              width: 24, height: 24)
          }.buttonStyle(.plain).frame(alignment: .leading)
          Spacer()
          // This button is for incrementing
          Button(intent: BackgroundIntent(method: "increment")) {
            Image(systemName: "plus").font(.system(size: 16)).foregroundColor(.white)

          }.frame(width: 24, height: 24)
            .background(.blue)
            .cornerRadius(12).frame(alignment: .trailing)
        }
      }
    }
  }
}

struct LockScreen: Widget {
  let kind: String = "LockScreen"

  var body: some WidgetConfiguration {
    StaticConfiguration(kind: kind, provider: Provider()) { entry in
      if #available(iOS 17.0, *) {
        LockScreenEntryView(entry: entry)
              .containerBackground(.fill.tertiary, for: .widget)
      } else {
        LockScreenEntryView(entry: entry)
          .padding()
          .background()
      }
    }
    .configurationDisplayName("Counter Widget")
    .description("Count the Number Up")
    .supportedFamilies([
   .accessoryCircular,
    ])
  }
}

#Preview(as: .systemSmall){
  LockScreen()
} timeline: {
  CounterEntry(date: .now, count: 0)
}

@danieljiwonkang98
Copy link
Author

@ABausG can I get some help with this? Thank you so much and loved your lockscreen blog.

@IsseiKanda
Copy link

@danieljiwonkang98
Stuck in the same issue. Did you find a way to solve it?

@danieljiwonkang98
Copy link
Author

Still have no idea? @ABausG Help Please

@ABausG
Copy link
Owner

ABausG commented Mar 27, 2024

I'll look into it in the next couple of days.

In the meantime it would be great if you could try to see if the link shows up as launching the app by using a package like uni_link to see if the link gets gobbled up by iOS or if it is a bug in home_widget

@ABausG ABausG added the p:iOS iOS Home Screen Widgets label Mar 27, 2024
@danieljiwonkang98
Copy link
Author

@ABausG Thank you!

@danieljiwonkang98
Copy link
Author

@ABausG Hello! Can we get updates for this! Thank you so much.

@danieljiwonkang98
Copy link
Author

@ABausG Any updates?!

@inceptiondawn
Copy link

@ABausG Hello is this issue fixed?

@ABausG
Copy link
Owner

ABausG commented Apr 30, 2024

Hi. Sorry for not responding. I'm quite busy at the moment. Hope to be able to take a look at this soon

@danieljiwonkang98
Copy link
Author

@ABausG Thank you please leave an update here in the comment when you do get a chance to do so! Thank you once again!

@danieljiwonkang98
Copy link
Author

@ABausG Can you please have a look in this? Thank you. Really want this feature fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p:iOS iOS Home Screen Widgets
Projects
None yet
Development

No branches or pull requests

4 participants