Skip to content

Commit

Permalink
Clean up setting of Activity reference - modern projects
Browse files Browse the repository at this point in the history
will set in Main.kt
  • Loading branch information
aabewhite committed Aug 23, 2024
1 parent c89e7f3 commit b1abb0a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
8 changes: 5 additions & 3 deletions Sources/SkipUI/SkipUI/Containers/PresentationRoot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ import androidx.compose.ui.platform.LocalLayoutDirection
}
}

// This would better be done in Main.kt, but we have no way of retroactively changing
// existing users' Main.kt file to add it.
@Composable func launchUIApplicationActivity() {
// Modern Skip projects will set the launch activity in Main.kt. This function exists for older projects
guard UIApplication.shared.launchActivity == nil else {
return
}
var context: Context? = LocalContext.current
var activity: Activity? = nil
while context != nil {
Expand All @@ -111,7 +113,7 @@ import androidx.compose.ui.platform.LocalLayoutDirection
}
}
if let activity {
UIApplication.shared.launch(activity)
UIApplication.launch(activity)
}
}

Expand Down
21 changes: 12 additions & 9 deletions Sources/SkipUI/SkipUI/System/UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,26 @@ let logger: Logger = Logger(subsystem: "skip.ui", category: "SkipUI") // adb log
}

#if SKIP
private var _androidActivity: Activity?
var launchActivity: Activity? {
didSet {
if isIdleTimerDisabled {
setWindowFlagsForIsIdleTimerDisabled()
}
}
}

/// The Android main activity.
///
/// This API mirrors `ProcessInfo.androidContext` for the application context.
public var androidActivity: Activity {
return _androidActivity!
return launchActivity!
}

/// Setup the Android main activity.
///
/// This API mirrors `ProcessInfo.launch` for the application context.
public func launch(_ activity: Activity) {
_androidActivity = activity
if isIdleTimerDisabled {
setWindowFlagsForIsIdleTimerDisabled()
}
public static func launch(_ activity: Activity) {
UIApplication.shared.launchActivity = activity
}
#endif

Expand All @@ -75,9 +78,9 @@ let logger: Logger = Logger(subsystem: "skip.ui", category: "SkipUI") // adb log
#if SKIP
let flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
if isIdleTimerDisabled {
_androidActivity?.window?.addFlags(flags)
launchActivity?.window?.addFlags(flags)
} else {
_androidActivity?.window?.clearFlags(flags)
launchActivity?.window?.clearFlags(flags)
}
#endif
}
Expand Down

0 comments on commit b1abb0a

Please sign in to comment.