Skip to content

Commit

Permalink
[Examples] [SwiftUI] sync changes from 901ecdc & 7788a3e
Browse files Browse the repository at this point in the history
  • Loading branch information
Luki120 committed Mar 17, 2024
1 parent 6e61e05 commit d085816
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 45 deletions.
1 change: 1 addition & 0 deletions Examples/SwiftUI/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Rhus_FILES = $(wildcard */**/**.m) $(wildcard */**/**/**.swift)
Rhus_CFLAGS = -fobjc-arc -ISources/RhusC/include
Rhus_SWIFTFLAGS = -ISources/RhusC/include
Rhus_EXTRA_FRAMEWORKS = Chrissa
Rhus_PRIVATE_FRAMEWORKS = SpringBoard

include $(THEOS)/makefiles/common.mk
include $(THEOS_MAKE_PATH)/tweak.mk
25 changes: 6 additions & 19 deletions Examples/SwiftUI/Sources/Rhus/Hooks/Tweak.x.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ import protocol SwiftUI.View
import class SwiftUI.UIHostingController


private var lsDateVC: SBFLockScreenDateVCHook!
private var weatherViewViewModel: WeatherViewViewModel!

class SBFLockScreenDateVCHook: ClassHook<UIViewController> {

static let targetName = "SBFLockScreenDateViewController"

@Property(.nonatomic) private(set) var weatherViewViewModel = WeatherViewViewModel()
@Property(.nonatomic, .retain) private var _weatherViewViewModel = WeatherViewViewModel()

func viewDidLoad() {
orig.viewDidLoad()

lsDateVC = self

let vc = HostingController(rootView: WeatherView(viewModel: weatherViewViewModel))
let vc = HostingController(rootView: WeatherView(viewModel: _weatherViewViewModel))
vc.view.backgroundColor = .clear
vc.view.translatesAutoresizingMaskIntoConstraints = false

Expand All @@ -30,6 +28,8 @@ class SBFLockScreenDateVCHook: ClassHook<UIViewController> {
vc.view.leadingAnchor.constraint(equalTo: target.view.leadingAnchor),
vc.view.trailingAnchor.constraint(equalTo: target.view.trailingAnchor)
])

weatherViewViewModel = _weatherViewViewModel
}

}
Expand All @@ -40,18 +40,7 @@ class CSCoverSheetVCHook: ClassHook<UIViewController> {

func viewWillAppear(_ animated: Bool) {
orig.viewWillAppear(animated)
lsDateVC.weatherViewViewModel.updateWeather()
}

}

class SBLockScreenPluginManagerHook: ClassHook<NSObject> {

static let targetName = "SBLockScreenPluginManager"

func setEnabled(_ enabled: Bool) {
orig.setEnabled(enabled)
ScreenListener.sharedInstance.isScreenOff = !enabled
weatherViewViewModel.updateWeather()
}

}
Expand All @@ -63,8 +52,6 @@ class SBBacklightControllerHook: ClassHook<NSObject> {
func turnOnScreenFullyWithBacklightSource(_ source: Int) {
orig.turnOnScreenFullyWithBacklightSource(source)

guard let SBLockScreenManager = NSClassFromString("SBLockScreenManager") else { return }

if !SBLockScreenManager.sharedInstance().isLockScreenVisible() { return }
NotificationCenter.default.post(name: .didRefreshWeatherDataNotification, object: nil)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Chrissa
import Combine
import Foundation


final class WeatherViewViewModel: ObservableObject {
Expand All @@ -25,19 +24,6 @@ final class WeatherViewViewModel: ObservableObject {
return formatter
}()

init() {
updateLocation()
updateWeather()
}

func updateLocation() {
ScreenListener.sharedInstance.$isScreenOff
.sink { state in
if state == false { WeatherService.shared.updateLocation(false) }
}
.store(in: &subscriptions)
}

func updateWeather() {
guard shouldRefresh() else { return }

Expand Down Expand Up @@ -75,17 +61,6 @@ final class WeatherViewViewModel: ObservableObject {

}

final class ScreenListener: ObservableObject {

static let sharedInstance = ScreenListener()
@Published var isScreenOff = false {
didSet {
NotificationCenter.default.post(name: .didRefreshWeatherDataNotification, object: nil)
}
}

}

extension Notification.Name {
static let didRefreshWeatherDataNotification = Notification.Name("didRefreshWeatherDataNotification")
}
Expand Down
2 changes: 1 addition & 1 deletion Examples/SwiftUI/Sources/Rhus/Views/WeatherView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct WeatherView: View {
private(set) var viewModel: WeatherViewViewModel

var body: some View {
VStack(spacing: 2) {
VStack(spacing: 5) {
Text(viewModel.weatherText)
.frame(maxWidth: .infinity, alignment: .center)
.foregroundColor(.primary)
Expand Down

0 comments on commit d085816

Please sign in to comment.