diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt index d7a7d1bfbf..bd5dcb7823 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt @@ -83,8 +83,7 @@ class LocalProfileFragment : DaggerFragment() { // onDestroyView. private val binding get() = _binding!! - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View { + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { _binding = LocalprofileFragmentBinding.inflate(inflater, container, false) return binding.root } @@ -140,12 +139,11 @@ class LocalProfileFragment : DaggerFragment() { // Spinner spinner = SpinnerHelper(binding.spinner) - val profileList: ArrayList = localProfilePlugin.profile?.getProfileList() - ?: ArrayList() context?.let { context -> - val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList) - spinner?.adapter = adapter - spinner?.setSelection(localProfilePlugin.currentProfileIndex) + val profileList: ArrayList = localProfilePlugin.profile?.getProfileList() ?: ArrayList() + spinner?.adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList) + val selection = localProfilePlugin.currentProfileIndex + if (selection in 0 until profileList.size) spinner?.setSelection(selection) } ?: return spinner?.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener { override fun onNothingSelected(parent: AdapterView<*>?) { @@ -159,8 +157,10 @@ class LocalProfileFragment : DaggerFragment() { localProfilePlugin.isEdited = false build() }, { - spinner?.setSelection(localProfilePlugin.currentProfileIndex) - }) + val selection = localProfilePlugin.currentProfileIndex + if (selection in 0 until (spinner?.adapter?.count ?: -1)) spinner?.setSelection(selection) + } + ) } } else { localProfilePlugin.currentProfileIndex = position diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt index 830eff3740..642bc9587d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfilePlugin.kt @@ -99,7 +99,7 @@ class LocalProfilePlugin @Inject constructor( var isEdited: Boolean = false var profiles: ArrayList = ArrayList() - var numOfProfiles = 0 + val numOfProfiles get() = profiles.size internal var currentProfileIndex = 0 fun currentProfile(): SingleProfile? = if (numOfProfiles > 0 && currentProfileIndex < numOfProfiles) profiles[currentProfileIndex] else null @@ -222,7 +222,7 @@ class LocalProfilePlugin @Inject constructor( @Synchronized fun loadSettings() { - numOfProfiles = sp.getInt(Constants.LOCAL_PROFILE + "_profiles", 0) + val numOfProfiles = sp.getInt(Constants.LOCAL_PROFILE + "_profiles", 0) profiles.clear() // numOfProfiles = max(numOfProfiles, 1) // create at least one default profile if none exists @@ -246,7 +246,6 @@ class LocalProfilePlugin @Inject constructor( } } isEdited = false - numOfProfiles = profiles.size createAndStoreConvertedProfile() } @@ -278,7 +277,6 @@ class LocalProfilePlugin @Inject constructor( } if (newProfiles.size > 0) { profiles = newProfiles - numOfProfiles = profiles.size currentProfileIndex = 0 isEdited = false createAndStoreConvertedProfile() @@ -378,7 +376,6 @@ class LocalProfilePlugin @Inject constructor( p.targetHigh = JSONArray(defaultArray) profiles.add(p) currentProfileIndex = profiles.size - 1 - numOfProfiles++ createAndStoreConvertedProfile() storeSettings() } @@ -388,7 +385,6 @@ class LocalProfilePlugin @Inject constructor( p.name = p.name + " copy" profiles.add(p) currentProfileIndex = profiles.size - 1 - numOfProfiles++ createAndStoreConvertedProfile() storeSettings() isEdited = false @@ -397,7 +393,6 @@ class LocalProfilePlugin @Inject constructor( fun addProfile(p: SingleProfile) { profiles.add(p) currentProfileIndex = profiles.size - 1 - numOfProfiles++ createAndStoreConvertedProfile() storeSettings() isEdited = false @@ -405,7 +400,6 @@ class LocalProfilePlugin @Inject constructor( fun removeCurrentProfile() { profiles.removeAt(currentProfileIndex) - numOfProfiles-- if (profiles.size == 0) addNewProfile() currentProfileIndex = 0 createAndStoreConvertedProfile()