eliminate numOfProfiles in ProfilePlugin

This commit is contained in:
Milos Kozak 2021-12-09 14:49:10 +01:00
parent a83eb1c456
commit fb2eaad1d2
2 changed files with 11 additions and 17 deletions

View file

@ -83,8 +83,7 @@ class LocalProfileFragment : DaggerFragment() {
// onDestroyView. // onDestroyView.
private val binding get() = _binding!! private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
savedInstanceState: Bundle?): View {
_binding = LocalprofileFragmentBinding.inflate(inflater, container, false) _binding = LocalprofileFragmentBinding.inflate(inflater, container, false)
return binding.root return binding.root
} }
@ -140,12 +139,11 @@ class LocalProfileFragment : DaggerFragment() {
// Spinner // Spinner
spinner = SpinnerHelper(binding.spinner) spinner = SpinnerHelper(binding.spinner)
val profileList: ArrayList<CharSequence> = localProfilePlugin.profile?.getProfileList()
?: ArrayList()
context?.let { context -> context?.let { context ->
val adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList) val profileList: ArrayList<CharSequence> = localProfilePlugin.profile?.getProfileList() ?: ArrayList()
spinner?.adapter = adapter spinner?.adapter = ArrayAdapter(context, R.layout.spinner_centered, profileList)
spinner?.setSelection(localProfilePlugin.currentProfileIndex) val selection = localProfilePlugin.currentProfileIndex
if (selection in 0 until profileList.size) spinner?.setSelection(selection)
} ?: return } ?: return
spinner?.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener { spinner?.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) { override fun onNothingSelected(parent: AdapterView<*>?) {
@ -159,8 +157,10 @@ class LocalProfileFragment : DaggerFragment() {
localProfilePlugin.isEdited = false localProfilePlugin.isEdited = false
build() build()
}, { }, {
spinner?.setSelection(localProfilePlugin.currentProfileIndex) val selection = localProfilePlugin.currentProfileIndex
}) if (selection in 0 until (spinner?.adapter?.count ?: -1)) spinner?.setSelection(selection)
}
)
} }
} else { } else {
localProfilePlugin.currentProfileIndex = position localProfilePlugin.currentProfileIndex = position

View file

@ -99,7 +99,7 @@ class LocalProfilePlugin @Inject constructor(
var isEdited: Boolean = false var isEdited: Boolean = false
var profiles: ArrayList<SingleProfile> = ArrayList() var profiles: ArrayList<SingleProfile> = ArrayList()
var numOfProfiles = 0 val numOfProfiles get() = profiles.size
internal var currentProfileIndex = 0 internal var currentProfileIndex = 0
fun currentProfile(): SingleProfile? = if (numOfProfiles > 0 && currentProfileIndex < numOfProfiles) profiles[currentProfileIndex] else null fun currentProfile(): SingleProfile? = if (numOfProfiles > 0 && currentProfileIndex < numOfProfiles) profiles[currentProfileIndex] else null
@ -222,7 +222,7 @@ class LocalProfilePlugin @Inject constructor(
@Synchronized @Synchronized
fun loadSettings() { fun loadSettings() {
numOfProfiles = sp.getInt(Constants.LOCAL_PROFILE + "_profiles", 0) val numOfProfiles = sp.getInt(Constants.LOCAL_PROFILE + "_profiles", 0)
profiles.clear() profiles.clear()
// numOfProfiles = max(numOfProfiles, 1) // create at least one default profile if none exists // numOfProfiles = max(numOfProfiles, 1) // create at least one default profile if none exists
@ -246,7 +246,6 @@ class LocalProfilePlugin @Inject constructor(
} }
} }
isEdited = false isEdited = false
numOfProfiles = profiles.size
createAndStoreConvertedProfile() createAndStoreConvertedProfile()
} }
@ -278,7 +277,6 @@ class LocalProfilePlugin @Inject constructor(
} }
if (newProfiles.size > 0) { if (newProfiles.size > 0) {
profiles = newProfiles profiles = newProfiles
numOfProfiles = profiles.size
currentProfileIndex = 0 currentProfileIndex = 0
isEdited = false isEdited = false
createAndStoreConvertedProfile() createAndStoreConvertedProfile()
@ -378,7 +376,6 @@ class LocalProfilePlugin @Inject constructor(
p.targetHigh = JSONArray(defaultArray) p.targetHigh = JSONArray(defaultArray)
profiles.add(p) profiles.add(p)
currentProfileIndex = profiles.size - 1 currentProfileIndex = profiles.size - 1
numOfProfiles++
createAndStoreConvertedProfile() createAndStoreConvertedProfile()
storeSettings() storeSettings()
} }
@ -388,7 +385,6 @@ class LocalProfilePlugin @Inject constructor(
p.name = p.name + " copy" p.name = p.name + " copy"
profiles.add(p) profiles.add(p)
currentProfileIndex = profiles.size - 1 currentProfileIndex = profiles.size - 1
numOfProfiles++
createAndStoreConvertedProfile() createAndStoreConvertedProfile()
storeSettings() storeSettings()
isEdited = false isEdited = false
@ -397,7 +393,6 @@ class LocalProfilePlugin @Inject constructor(
fun addProfile(p: SingleProfile) { fun addProfile(p: SingleProfile) {
profiles.add(p) profiles.add(p)
currentProfileIndex = profiles.size - 1 currentProfileIndex = profiles.size - 1
numOfProfiles++
createAndStoreConvertedProfile() createAndStoreConvertedProfile()
storeSettings() storeSettings()
isEdited = false isEdited = false
@ -405,7 +400,6 @@ class LocalProfilePlugin @Inject constructor(
fun removeCurrentProfile() { fun removeCurrentProfile() {
profiles.removeAt(currentProfileIndex) profiles.removeAt(currentProfileIndex)
numOfProfiles--
if (profiles.size == 0) addNewProfile() if (profiles.size == 0) addNewProfile()
currentProfileIndex = 0 currentProfileIndex = 0
createAndStoreConvertedProfile() createAndStoreConvertedProfile()