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

Moving the FhirEngine creation in the Application #90

Merged
merged 1 commit into from
Jul 15, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.fhirengine.example

import android.app.Application
import android.content.Context
import androidx.work.Constraints
import ca.uhn.fhir.context.FhirContext
import com.google.fhirengine.FhirEngine
import com.google.fhirengine.FhirEngineBuilder
import com.google.fhirengine.example.api.HapiFhirService.Companion.create
import com.google.fhirengine.example.data.HapiFhirResourceDataSource
import com.google.fhirengine.sync.FhirDataSource
import com.google.fhirengine.sync.SyncConfiguration
import com.google.fhirengine.sync.SyncData
import org.hl7.fhir.r4.model.ResourceType
import java.util.ArrayList

class FhirApplication : Application() {

// only initiate the FhirEngine when used for the first time, not when the app is created
private val fhirEngine: FhirEngine by lazy { constructFhirEngine() }

private fun constructFhirEngine(): FhirEngine {
val parser = FhirContext.forR4().newJsonParser()
val service = create(parser)
val params = mutableMapOf("address-country" to "United States")
val syncData: MutableList<SyncData> = ArrayList()
syncData.add(SyncData(ResourceType.Patient, params))
val configuration = SyncConfiguration(syncData, Constraints.Builder().build(), false)
val dataSource: FhirDataSource = HapiFhirResourceDataSource(service)
return FhirEngineBuilder(configuration, dataSource, this).build()
}

companion object {
@JvmStatic
fun fhirEngine(context: Context) =
(context.applicationContext as FhirApplication).fhirEngine
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,7 @@ protected void onCreate(Bundle savedInstanceState) {
expressionInput = findViewById(R.id.expression_input);
evaluationResultTextView = findViewById(R.id.evaluate_result);

IParser parser = FhirContext.forR4().newJsonParser();
HapiFhirService service = HapiFhirService.Companion.create(parser);
Map<String, String> params = new HashMap();
params.put("address-country", "United States");
List<SyncData> syncData = new ArrayList<>();
syncData.add(new SyncData(ResourceType.Patient, params));
SyncConfiguration configuration =
new SyncConfiguration(syncData, new Constraints.Builder().build(), false);
FhirDataSource dataSource = new HapiFhirResourceDataSource(service);
fhirEngine = new FhirEngineBuilder(configuration, dataSource, this).build();
fhirEngine = FhirApplication.fhirEngine(this);

MainActivityViewModel viewModel =
new ViewModelProvider(this, new MainActivityViewModelFactory(fhirEngine))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.google.fhirengine.sync.SyncConfiguration
/**
* The builder for [FhirEngine] instance
*/
class FhirEngineBuilder internal constructor(
class FhirEngineBuilder constructor(
syncConfiguration: SyncConfiguration,
dataSource: FhirDataSource,
context: Context
Expand Down