add proper lap sorting

This commit is contained in:
tibbi
2018-03-08 13:11:16 +01:00
parent 79a4f9494f
commit 916e7db548
4 changed files with 51 additions and 5 deletions

View File

@ -41,7 +41,7 @@ android {
} }
dependencies { dependencies {
implementation 'com.simplemobiletools:commons:3.15.7' implementation 'com.simplemobiletools:commons:3.15.8'
implementation 'com.facebook.stetho:stetho:1.5.0' implementation 'com.facebook.stetho:stetho:1.5.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support.constraint:constraint-layout:1.0.2'
} }

View File

@ -12,7 +12,6 @@ import com.simplemobiletools.clock.adapters.ViewPagerAdapter
import com.simplemobiletools.clock.extensions.config import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.clock.helpers.TABS_COUNT import com.simplemobiletools.clock.helpers.TABS_COUNT
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN
import com.simplemobiletools.commons.helpers.LICENSE_STETHO import com.simplemobiletools.commons.helpers.LICENSE_STETHO
import com.simplemobiletools.commons.models.FAQItem import com.simplemobiletools.commons.models.FAQItem
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
@ -148,6 +147,6 @@ class MainActivity : SimpleActivity() {
FAQItem(R.string.faq_4_title_commons, R.string.faq_4_text_commons) FAQItem(R.string.faq_4_title_commons, R.string.faq_4_text_commons)
) )
startAboutActivity(R.string.app_name, LICENSE_KOTLIN or LICENSE_STETHO, BuildConfig.VERSION_NAME, faqItems) startAboutActivity(R.string.app_name, LICENSE_STETHO, BuildConfig.VERSION_NAME, faqItems)
} }
} }

View File

@ -54,7 +54,7 @@ class StopwatchFragment : Fragment() {
val lap = Lap(currentLap++, lapTicks * UPDATE_INTERVAL, totalTicks * UPDATE_INTERVAL) val lap = Lap(currentLap++, lapTicks * UPDATE_INTERVAL, totalTicks * UPDATE_INTERVAL)
laps.add(0, lap) laps.add(0, lap)
lapTicks = 0 lapTicks = 0
(stopwatch_list.adapter as StopwatchAdapter).updateItems(laps) updateLaps()
} }
val stopwatchAdapter = StopwatchAdapter(activity as SimpleActivity, ArrayList(), stopwatch_list) { val stopwatchAdapter = StopwatchAdapter(activity as SimpleActivity, ArrayList(), stopwatch_list) {
@ -62,6 +62,7 @@ class StopwatchFragment : Fragment() {
changeSorting(it) changeSorting(it)
} }
} }
Lap.sorting = sorting
stopwatch_list.adapter = stopwatchAdapter stopwatch_list.adapter = stopwatchAdapter
} }
@ -140,7 +141,19 @@ class StopwatchFragment : Fragment() {
} }
private fun changeSorting(clickedValue: Int) { private fun changeSorting(clickedValue: Int) {
sorting = if (sorting and clickedValue != 0) {
sorting.flipBit(SORT_DESCENDING)
} else {
clickedValue or SORT_DESCENDING
}
Lap.sorting = sorting
updateLaps()
}
private fun updateLaps() {
laps.sort()
(view.stopwatch_list.adapter as StopwatchAdapter).updateItems(laps)
} }
private val updateRunnable = object : Runnable { private val updateRunnable = object : Runnable {

View File

@ -1,3 +1,37 @@
package com.simplemobiletools.clock.models package com.simplemobiletools.clock.models
data class Lap(val id: Int, var lapTime: Long, var totalTime: Long) import com.simplemobiletools.clock.helpers.SORT_BY_LAP
import com.simplemobiletools.clock.helpers.SORT_BY_LAP_TIME
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
data class Lap(val id: Int, var lapTime: Long, var totalTime: Long) : Comparable<Lap> {
companion object {
var sorting = 0
}
override fun compareTo(other: Lap): Int {
var result = when {
sorting and SORT_BY_LAP != 0 -> when {
id == other.id -> 0
id > other.id -> 1
else -> -1
}
sorting and SORT_BY_LAP_TIME != 0 -> when {
lapTime == other.lapTime -> 0
lapTime > other.lapTime -> 1
else -> -1
}
else -> when {
totalTime == other.totalTime -> 0
totalTime > other.totalTime -> 1
else -> -1
}
}
if (sorting and SORT_DESCENDING != 0) {
result *= -1
}
return result
}
}