theMomax / BatteryView

A SwiftUI library featuring a variety of battery visualisations.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Battery shows lightning bolt and nothing else.

curiousurick opened this issue · comments

Hey! I have a project where I'd like to inject a battery into a widget I'm making for personal use. However, when add the battery, all that is rendered is a lightning bolt.

I created a copy of BatteryDemo because it's not public and I am directly adding that to my view. If I set the battery state to .full or .charging, I see a lightning bolt and when I set to .unplugged, the bolt disappears. When I adjust the width, it adjusts as if it's displaying a whole battery, meaning I see a lot of room around the bolt. It seems the battery is invisible, matching the background color, or something.

Oh, I think some system assets may not be available in widget extensions

Yeah, I also found some things not working in widgets in the past. I didn’t test this library on widgets, though. There are three reasons I could imagine it not working:

  • State does not really work in widgets, so the battery is always at default 0%.
  • Some SFSymbols not available (I doubt that)
  • Some of the image manipulations used by SFSymbolStyle (e.g. masking, color inversion) are disabled in widgets to save power

Could you send a screenshot? That might help me to figure out what part doesn’t work. Maybe I can work around it then.

It seems to be caused by missing SFSymbols. You can see the lightning bolt but the battery that's supposed to surround it is missing. I've done multiple colors to test it but I think it's simply caused by the symbols missing. I'll see if I can find a way to inject them.
Screen Shot 2021-09-05 at 5 31 40 PM

I suspect that the part not working in WidgetKit is the View.mask(_:) modifier. The bolt is the only item in SFSymbolStyle that is not masked in some way. I put together another style called SFSymbolWideStyle, which does not use of .mask at all and thus should work even in widgets. You can try it out by setting the Package version to this commit: a800142.

Of course it looks a little different:
image

Unfortunately I couldn't test it myself because Widgets didn't show up when I installed a test app on my device. Probably a Xcode beta issue.

This issue seems to be resolved in iOS 17:

image