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

RMET-3069 H&F Plugin - Migrate ListBackgroundJobs feature #99

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ The changes documented here do not include those from the original repository.

## [Unreleased]

## 2024-02-09
- Re-implemented ListBackgroundJobs feature (https://outsystemsrd.atlassian.net/browse/RMET-3069).

## 2024-02-08
- Re-implement `GetLastRecord` feature:
- GetFitnessData (https://outsystemsrd.atlassian.net/browse/RMET-3048)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ class OSHealthFitness : CordovaImplementation() {
lateinit var healthConnectRepository: HealthConnectRepository
lateinit var healthConnectDataManager: HealthConnectDataManager
lateinit var healthConnectHelper: HealthConnectHelper
lateinit var backgroundParameters: BackgroundJobParameters

override fun initialize(cordova: CordovaInterface, webView: CordovaWebView) {
super.initialize(cordova, webView)
val manager = HealthFitnessManager(cordova.context, cordova.activity)
val database = DatabaseManager(cordova.context)
healthStore = HealthStore(cordova.context.applicationContext.packageName, manager, database)

healthConnectDataManager = HealthConnectDataManager()
healthConnectDataManager = HealthConnectDataManager(database)
healthConnectRepository = HealthConnectRepository(healthConnectDataManager)
healthConnectHelper = HealthConnectHelper()
healthConnectViewModel = HealthConnectViewModel(healthConnectRepository, healthConnectHelper)
Expand Down Expand Up @@ -217,20 +218,26 @@ class OSHealthFitness : CordovaImplementation() {
}

private fun setBackgroundJob(args: JSONArray) {
notificationPermissions.requestNotificationPermission(
this,
ACTIVITY_NOTIFICATION_PERMISSIONS_REQUEST_CODE
)
// save arguments for later use in case we need to request permissions
backgroundParameters = gson.fromJson(args.getString(0), BackgroundJobParameters::class.java)
if (!notificationPermissions.hasNotificationPermission(this)) {
notificationPermissions.requestNotificationPermission(
this,
ACTIVITY_NOTIFICATION_PERMISSIONS_REQUEST_CODE
)
} else {
setBackgroundJobWithParameters(backgroundParameters)
}
}

//process parameters
val parameters = gson.fromJson(args.getString(0), BackgroundJobParameters::class.java)
healthStore?.setBackgroundJob(
private fun setBackgroundJobWithParameters(parameters: BackgroundJobParameters) {
healthConnectViewModel.setBackgroundJob(
parameters,
{ response ->
sendPluginResult(response)
{
sendPluginResult("success", null)
},
{ error ->
sendPluginResult(null, Pair(error.code.toString(), error.message))
{
sendPluginResult(null, Pair(it.code.toString(), it.message))
}
)
}
Expand All @@ -249,13 +256,13 @@ class OSHealthFitness : CordovaImplementation() {
}

private fun listBackgroundJobs() {
healthStore?.listBackgroundJobs(
{ response ->
val pluginResponseJson = gson.toJson(response)
healthConnectViewModel.listBackgroundJobs(
{
val pluginResponseJson = gson.toJson(it)
sendPluginResult(pluginResponseJson)
},
{ error ->
sendPluginResult(null, Pair(error.code.toString(), error.message))
{
sendPluginResult(null, Pair(it.code.toString(), it.message))
}
)
}
Expand Down Expand Up @@ -336,6 +343,21 @@ class OSHealthFitness : CordovaImplementation() {
}
return
}
ACTIVITY_NOTIFICATION_PERMISSIONS_REQUEST_CODE -> {
if (grantResults.isNotEmpty() &&
grantResults[0] == PackageManager.PERMISSION_GRANTED
) {
setBackgroundJobWithParameters(backgroundParameters)
} else {
sendPluginResult(
null,
Pair(
HealthFitnessError.NOTIFICATION_PERMISSION_DENIED_ERROR.code.toString(),
HealthFitnessError.NOTIFICATION_PERMISSION_DENIED_ERROR.message
)
)
}
}
}
}

Expand Down
Loading