ProfileStore -> kt
This commit is contained in:
parent
bf6449f201
commit
363d6d48be
7 changed files with 77 additions and 115 deletions
|
@ -1,108 +0,0 @@
|
|||
package info.nightscout.androidaps.data;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.collection.ArrayMap;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Created by mike on 01.06.2017.
|
||||
*/
|
||||
|
||||
public class ProfileStore {
|
||||
private static Logger log = LoggerFactory.getLogger(ProfileStore.class);
|
||||
private JSONObject json;
|
||||
|
||||
private ArrayMap<String, Profile> cachedObjects = new ArrayMap<>();
|
||||
|
||||
public ProfileStore(JSONObject json) {
|
||||
this.json = json;
|
||||
getDefaultProfile();
|
||||
}
|
||||
|
||||
public JSONObject getData() {
|
||||
return json;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Profile getDefaultProfile() {
|
||||
Profile profile = null;
|
||||
try {
|
||||
String defaultProfileName = json.getString("defaultProfile");
|
||||
JSONObject store = json.getJSONObject("store");
|
||||
if (store.has(defaultProfileName)) {
|
||||
profile = cachedObjects.get(defaultProfileName);
|
||||
if (profile == null) {
|
||||
if (store.has("units")) {
|
||||
String units = store.getString("units");
|
||||
profile = new Profile(store.getJSONObject(defaultProfileName), units);
|
||||
cachedObjects.put(defaultProfileName, profile);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
return profile;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getDefaultProfileName() {
|
||||
String defaultProfileName = null;
|
||||
try {
|
||||
defaultProfileName = json.getString("defaultProfile");
|
||||
JSONObject store = json.getJSONObject("store");
|
||||
if (store.has(defaultProfileName)) {
|
||||
return defaultProfileName;
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
return defaultProfileName;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Profile getSpecificProfile(String profileName) {
|
||||
Profile profile = null;
|
||||
try {
|
||||
JSONObject store = json.getJSONObject("store");
|
||||
if (store.has(profileName)) {
|
||||
profile = cachedObjects.get(profileName);
|
||||
if (profile == null) {
|
||||
JSONObject profileObject = store.getJSONObject(profileName);
|
||||
if (profileObject!= null && profileObject.has("units")) {
|
||||
profile = new Profile(profileObject, profileObject.getString("units"));
|
||||
cachedObjects.put(profileName, profile);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
return profile;
|
||||
}
|
||||
|
||||
public ArrayList<CharSequence> getProfileList() {
|
||||
ArrayList<CharSequence> ret = new ArrayList<>();
|
||||
|
||||
JSONObject store;
|
||||
try {
|
||||
store = json.getJSONObject("store");
|
||||
Iterator<?> keys = store.keys();
|
||||
|
||||
while (keys.hasNext()) {
|
||||
String profileName = (String) keys.next();
|
||||
ret.add(profileName);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
log.error("Unhandled exception", e);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package info.nightscout.androidaps.data
|
||||
|
||||
import androidx.collection.ArrayMap
|
||||
import org.json.JSONException
|
||||
import org.json.JSONObject
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.util.*
|
||||
|
||||
class ProfileStore(val data: JSONObject) {
|
||||
private val log = LoggerFactory.getLogger(ProfileStore::class.java)
|
||||
|
||||
private val cachedObjects = ArrayMap<String, Profile>()
|
||||
|
||||
private fun getStore(): JSONObject? {
|
||||
try {
|
||||
if (data.has("store")) return data.getJSONObject("store")
|
||||
} catch (e: JSONException) {
|
||||
log.error("Unhandled exception", e)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun getDefaultProfile(): Profile? = getDefaultProfileName()?.let { getSpecificProfile(it) }
|
||||
|
||||
fun getDefaultProfileName(): String? {
|
||||
val defaultProfileName = data.getString("defaultProfile")
|
||||
return getStore()?.has(defaultProfileName)?.let { defaultProfileName }
|
||||
}
|
||||
|
||||
fun getProfileList(): ArrayList<CharSequence> {
|
||||
val ret = ArrayList<CharSequence>()
|
||||
getStore()?.keys()?.let { keys ->
|
||||
while (keys.hasNext()) {
|
||||
val profileName = keys.next() as String
|
||||
ret.add(profileName)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
fun getSpecificProfile(profileName: String): Profile? {
|
||||
var profile: Profile? = null
|
||||
try {
|
||||
getStore()?.let { store ->
|
||||
if (store.has(profileName)) {
|
||||
profile = cachedObjects[profileName]
|
||||
if (profile == null) {
|
||||
val profileObject = store.getJSONObject(profileName)
|
||||
if (profileObject != null && profileObject.has("units")) {
|
||||
profile = Profile(profileObject, profileObject.getString("units"))
|
||||
cachedObjects[profileName] = profile
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: JSONException) {
|
||||
log.error("Unhandled exception", e)
|
||||
}
|
||||
return profile
|
||||
}
|
||||
}
|
|
@ -214,7 +214,7 @@ class WizardDialog : DialogFragment() {
|
|||
}
|
||||
|
||||
val profileList: ArrayList<CharSequence>
|
||||
profileList = profileStore.profileList
|
||||
profileList = profileStore.getProfileList()
|
||||
profileList.add(0, MainApp.gs(R.string.active))
|
||||
context?.let { context ->
|
||||
val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList)
|
||||
|
|
|
@ -47,7 +47,7 @@ class LocalProfileFragment : Fragment() {
|
|||
}
|
||||
|
||||
private fun sumLabel(): String {
|
||||
val profile = LocalProfilePlugin.createProfileStore().defaultProfile
|
||||
val profile = LocalProfilePlugin.createProfileStore().getDefaultProfile()
|
||||
val sum = profile?.baseBasalSum() ?: 0.0
|
||||
return " ∑" + DecimalFormatter.to2Decimal(sum) + MainApp.gs(R.string.insulin_unit_shortname)
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ class LocalProfileFragment : Fragment() {
|
|||
|
||||
// Spinner
|
||||
spinner = SpinnerHelper(view?.findViewById(R.id.localprofile_spinner))
|
||||
val profileList: ArrayList<CharSequence> = LocalProfilePlugin.profile?.profileList
|
||||
val profileList: ArrayList<CharSequence> = LocalProfilePlugin.profile?.getProfileList()
|
||||
?: ArrayList()
|
||||
context?.let { context ->
|
||||
val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList)
|
||||
|
|
|
@ -63,7 +63,7 @@ object LocalProfilePlugin : PluginBase(PluginDescription()
|
|||
|
||||
@Synchronized
|
||||
fun isValidEditState(): Boolean {
|
||||
return createProfileStore().defaultProfile?.isValid(MainApp.gs(R.string.localprofile), false)
|
||||
return createProfileStore().getDefaultProfile()?.isValid(MainApp.gs(R.string.localprofile), false)
|
||||
?: false
|
||||
}
|
||||
|
||||
|
@ -107,6 +107,7 @@ object LocalProfilePlugin : PluginBase(PluginDescription()
|
|||
val LOCAL_PROFILE_NUMBERED = LOCAL_PROFILE + "_" + i + "_"
|
||||
|
||||
p.name = SP.getString(LOCAL_PROFILE_NUMBERED + "name", LOCAL_PROFILE + i)
|
||||
if (isExistingName(p.name)) continue
|
||||
p.mgdl = SP.getBoolean(LOCAL_PROFILE_NUMBERED + "mgdl", false)
|
||||
p.dia = SP.getDouble(LOCAL_PROFILE_NUMBERED + "dia", Constants.defaultDIA)
|
||||
try {
|
||||
|
@ -162,9 +163,17 @@ object LocalProfilePlugin : PluginBase(PluginDescription()
|
|||
profiles.add(p)
|
||||
}
|
||||
isEdited = false
|
||||
numOfProfiles = profiles.size
|
||||
createAndStoreConvertedProfile()
|
||||
}
|
||||
|
||||
private fun isExistingName(name: String?) : Boolean {
|
||||
for (p in profiles) {
|
||||
if (p.name == name) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun doConversion() { // conversion from 2.3 to 2.4 format
|
||||
if (L.isEnabled(L.PROFILE))
|
||||
|
@ -347,7 +356,7 @@ object LocalProfilePlugin : PluginBase(PluginDescription()
|
|||
}
|
||||
|
||||
override fun getProfileName(): String {
|
||||
return DecimalFormatter.to2Decimal(rawProfile?.defaultProfile?.percentageBasalSum()
|
||||
return DecimalFormatter.to2Decimal(rawProfile?.getDefaultProfile()?.percentageBasalSum()
|
||||
?: 0.0) + "U "
|
||||
}
|
||||
|
||||
|
|
|
@ -119,7 +119,7 @@ class NSProfileFragment : Fragment() {
|
|||
profileview_noprofile.visibility = View.VISIBLE
|
||||
|
||||
NSProfilePlugin.getPlugin().profile?.let { profileStore ->
|
||||
val profileList = profileStore.profileList
|
||||
val profileList = profileStore.getProfileList()
|
||||
val adapter = ArrayAdapter(context!!, R.layout.spinner_centered, profileList)
|
||||
nsprofile_spinner.adapter = adapter
|
||||
// set selected to actual profile
|
||||
|
|
|
@ -58,7 +58,7 @@ class ProfileViewerDialog : DialogFragment() {
|
|||
profileview_datelayout.visibility = View.VISIBLE
|
||||
}
|
||||
Mode.PUMP_PROFILE -> {
|
||||
profile = (ConfigBuilderPlugin.getPlugin().activePump as ProfileInterface?)?.profile?.defaultProfile
|
||||
profile = (ConfigBuilderPlugin.getPlugin().activePump as ProfileInterface?)?.profile?.getDefaultProfile()
|
||||
profileName = (ConfigBuilderPlugin.getPlugin().activePump as ProfileInterface?)?.profileName
|
||||
date = ""
|
||||
profileview_reload.visibility = View.VISIBLE
|
||||
|
|
Loading…
Reference in a new issue