-
public final class PermissionsViewModel extends ViewModel
ViewModel that handles the business logic for permission requests. This separates the permission handling logic from the Activity lifecycle. Uses AndroidX ViewModel with StateFlow for lifecycle-aware state management.
Responsibilities:
Store permission request state (survives configuration changes)
Handle permission result business logic
Manage callbacks and preferences
Does NOT hold Activity references or call Activity APIs directly
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classPermissionsViewModel.Companion
-
Field Summary
Fields Modifier and Type Field Description private final StateFlow<Boolean>shouldFinishprivate final StateFlow<Boolean>waitingprivate StringpermissionRequestType
-
Constructor Summary
Constructors Constructor Description PermissionsViewModel()
-
Method Summary
Modifier and Type Method Description final StateFlow<Boolean>getShouldFinish()final StateFlow<Boolean>getWaiting()final StringgetPermissionRequestType()final UnitsetPermissionRequestType(String permissionRequestType)final Booleaninitialize(Activity activity, String permissionType, String androidPermission)Initialize OneSignal and the ViewModel with intent data. final BooleanshouldRequestPermission()Check if we should request permission (prevents duplicate requests). final UnitresetWaitingState()Reset the waiting flag. final UnitrecordRationaleState(Boolean shouldShowRationale)Record the rationale state before the permission request. final UnitonRequestPermissionsResult(Array<String> permissions, IntArray grantResults, Boolean shouldShowRationaleAfter)Handle the permission request result. -
-
Method Detail
-
getShouldFinish
final StateFlow<Boolean> getShouldFinish()
-
getWaiting
final StateFlow<Boolean> getWaiting()
-
getPermissionRequestType
final String getPermissionRequestType()
-
setPermissionRequestType
final Unit setPermissionRequestType(String permissionRequestType)
-
initialize
final Boolean initialize(Activity activity, String permissionType, String androidPermission)
Initialize OneSignal and the ViewModel with intent data. Returns false if initialization fails.
- Parameters:
activity- Activity context (not stored, used only for initialization)
-
shouldRequestPermission
final Boolean shouldRequestPermission()
Check if we should request permission (prevents duplicate requests). Activity should call this before requesting permission.
-
resetWaitingState
final Unit resetWaitingState()
Reset the waiting flag. This should be called when the activity is interrupted or destroyed without completing the permission request flow. This ensures the permission dialog can be shown again.
-
recordRationaleState
final Unit recordRationaleState(Boolean shouldShowRationale)
Record the rationale state before the permission request. Activity calls this with the result of shouldShowRequestPermissionRationale().
-
onRequestPermissionsResult
final Unit onRequestPermissionsResult(Array<String> permissions, IntArray grantResults, Boolean shouldShowRationaleAfter)
Handle the permission request result. Activity should call this with the result from onRequestPermissionsResult.
- Parameters:
shouldShowRationaleAfter- The result of shouldShowRequestPermissionRationale AFTER the user responded
-
-
-
-