mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-08 10:08:51 -06:00
Clean up kotlin code
* static imports * less nullable types * format
This commit is contained in:
@@ -23,6 +23,7 @@ import kotlinx.coroutines.runBlocking
|
||||
import org.isoron.platform.io.JavaFileOpener
|
||||
import org.isoron.platform.io.JavaResourceFile
|
||||
import java.awt.BasicStroke
|
||||
import java.awt.Graphics2D
|
||||
import java.awt.RenderingHints.KEY_ANTIALIASING
|
||||
import java.awt.RenderingHints.KEY_FRACTIONALMETRICS
|
||||
import java.awt.RenderingHints.KEY_TEXT_ANTIALIASING
|
||||
@@ -54,7 +55,7 @@ class JavaCanvas(
|
||||
private var textAlign = TextAlign.CENTER
|
||||
val widthPx = image.width
|
||||
val heightPx = image.height
|
||||
val g2d = image.createGraphics()
|
||||
val g2d: Graphics2D = image.createGraphics()
|
||||
|
||||
private val NOTO_REGULAR_FONT = createFont("fonts/NotoSans-Regular.ttf")
|
||||
private val NOTO_BOLD_FONT = createFont("fonts/NotoSans-Bold.ttf")
|
||||
@@ -96,24 +97,28 @@ class JavaCanvas(
|
||||
val bx = bounds.x.roundToInt()
|
||||
val by = bounds.y.roundToInt()
|
||||
|
||||
if (textAlign == TextAlign.CENTER) {
|
||||
g2d.drawString(
|
||||
text,
|
||||
toPixel(x) - bx - bWidth / 2,
|
||||
toPixel(y) - by - bHeight / 2
|
||||
)
|
||||
} else if (textAlign == TextAlign.LEFT) {
|
||||
g2d.drawString(
|
||||
text,
|
||||
toPixel(x) - bx,
|
||||
toPixel(y) - by - bHeight / 2
|
||||
)
|
||||
} else {
|
||||
g2d.drawString(
|
||||
text,
|
||||
toPixel(x) - bx - bWidth,
|
||||
toPixel(y) - by - bHeight / 2
|
||||
)
|
||||
when (textAlign) {
|
||||
TextAlign.CENTER -> {
|
||||
g2d.drawString(
|
||||
text,
|
||||
toPixel(x) - bx - bWidth / 2,
|
||||
toPixel(y) - by - bHeight / 2
|
||||
)
|
||||
}
|
||||
TextAlign.LEFT -> {
|
||||
g2d.drawString(
|
||||
text,
|
||||
toPixel(x) - bx,
|
||||
toPixel(y) - by - bHeight / 2
|
||||
)
|
||||
}
|
||||
else -> {
|
||||
g2d.drawString(
|
||||
text,
|
||||
toPixel(x) - bx - bWidth,
|
||||
toPixel(y) - by - bHeight / 2
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,8 +33,8 @@ class JavaResourceFile(val path: String) : ResourceFile {
|
||||
get() {
|
||||
val mainPath = Paths.get("assets/main/$path")
|
||||
val testPath = Paths.get("assets/test/$path")
|
||||
if (Files.exists(mainPath)) return mainPath
|
||||
else return testPath
|
||||
return if (Files.exists(mainPath)) mainPath
|
||||
else testPath
|
||||
}
|
||||
|
||||
override suspend fun exists(): Boolean {
|
||||
|
||||
@@ -71,7 +71,7 @@ object SQLParser {
|
||||
val buffer = BufferedInputStream(stream)
|
||||
val commands: MutableList<String> = ArrayList()
|
||||
val sb = StringBuffer()
|
||||
try {
|
||||
buffer.use { buffer ->
|
||||
val tokenizer = Tokenizer(buffer)
|
||||
var state = STATE_NONE
|
||||
while (tokenizer.hasNext()) {
|
||||
@@ -104,7 +104,7 @@ object SQLParser {
|
||||
}
|
||||
if (state == STATE_NONE || state == STATE_STRING) {
|
||||
if (state == STATE_NONE && isWhitespace(c)) {
|
||||
if (sb.length > 0 && sb[sb.length - 1] != ' ') {
|
||||
if (sb.isNotEmpty() && sb[sb.length - 1] != ' ') {
|
||||
sb.append(' ')
|
||||
}
|
||||
} else {
|
||||
@@ -112,8 +112,6 @@ object SQLParser {
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
buffer.close()
|
||||
}
|
||||
if (sb.isNotEmpty()) {
|
||||
commands.add(sb.toString().trim { it <= ' ' })
|
||||
|
||||
@@ -37,6 +37,7 @@ import java.util.LinkedList
|
||||
import java.util.Locale
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipOutputStream
|
||||
import kotlin.math.min
|
||||
|
||||
/**
|
||||
* Class that exports the application data to CSV files.
|
||||
@@ -77,7 +78,7 @@ class HabitsCSVExporter(
|
||||
|
||||
private fun sanitizeFilename(name: String): String {
|
||||
val s = name.replace("[^ a-zA-Z0-9\\._-]+".toRegex(), "")
|
||||
return s.substring(0, Math.min(s.length, 100))
|
||||
return s.substring(0, min(s.length, 100))
|
||||
}
|
||||
|
||||
private fun writeHabits() {
|
||||
|
||||
@@ -53,7 +53,7 @@ class LoopDBImporter
|
||||
|
||||
override fun canHandle(file: File): Boolean {
|
||||
if (!file.isSQLite3File()) return false
|
||||
val db = opener.open(file)!!
|
||||
val db = opener.open(file)
|
||||
var canHandle = true
|
||||
val c = db.query("select count(*) from SQLITE_MASTER where name='Habits' or name='Repetitions'")
|
||||
if (!c.moveToNext() || c.getInt(0) != 2) {
|
||||
@@ -70,7 +70,7 @@ class LoopDBImporter
|
||||
}
|
||||
|
||||
override fun importHabitsFromFile(file: File) {
|
||||
val db = opener.open(file)!!
|
||||
val db = opener.open(file)
|
||||
val helper = MigrationHelper(db)
|
||||
helper.migrateTo(DATABASE_VERSION)
|
||||
|
||||
|
||||
@@ -156,15 +156,15 @@ open class EntryList {
|
||||
get() = begin.daysUntil(end) + 1
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a list of intervals into a list of entries. Entries that fall outside of any
|
||||
* interval receive value UNKNOWN. Entries that fall within an interval but do not appear
|
||||
* in [original] receive value YES_AUTO. Entries provided in [original] are copied over.
|
||||
*
|
||||
* The intervals should be sorted by timestamp. The first element in the list should
|
||||
* correspond to the newest interval.
|
||||
*/
|
||||
companion object {
|
||||
/**
|
||||
* Converts a list of intervals into a list of entries. Entries that fall outside of any
|
||||
* interval receive value UNKNOWN. Entries that fall within an interval but do not appear
|
||||
* in [original] receive value YES_AUTO. Entries provided in [original] are copied over.
|
||||
*
|
||||
* The intervals should be sorted by timestamp. The first element in the list should
|
||||
* correspond to the newest interval.
|
||||
*/
|
||||
fun buildEntriesFromInterval(
|
||||
original: List<Entry>,
|
||||
intervals: List<Interval>,
|
||||
|
||||
@@ -31,13 +31,12 @@ interface ModelFactory {
|
||||
fun buildHabit(): Habit {
|
||||
val scores = buildScoreList()
|
||||
val streaks = buildStreakList()
|
||||
val habit = Habit(
|
||||
return Habit(
|
||||
scores = scores,
|
||||
streaks = streaks,
|
||||
originalEntries = buildOriginalEntries(),
|
||||
computedEntries = buildComputedEntries(),
|
||||
)
|
||||
return habit
|
||||
}
|
||||
fun buildComputedEntries(): EntryList
|
||||
fun buildOriginalEntries(): EntryList
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
package org.isoron.uhabits.core.models
|
||||
|
||||
import kotlin.math.pow
|
||||
import kotlin.math.sqrt
|
||||
|
||||
data class Score(
|
||||
@@ -40,7 +41,7 @@ data class Score(
|
||||
previousScore: Double,
|
||||
checkmarkValue: Double,
|
||||
): Double {
|
||||
val multiplier = Math.pow(0.5, sqrt(frequency) / 13.0)
|
||||
val multiplier = 0.5.pow(sqrt(frequency) / 13.0)
|
||||
var score = previousScore * multiplier
|
||||
score += checkmarkValue * (1 - multiplier)
|
||||
return score
|
||||
|
||||
@@ -109,7 +109,7 @@ class ScoreList {
|
||||
}
|
||||
}
|
||||
if (values[offset] != Entry.SKIP) {
|
||||
val percentageCompleted = Math.min(1.0, rollingSum / numerator)
|
||||
val percentageCompleted = min(1.0, rollingSum / numerator)
|
||||
previousValue = compute(freq, previousValue, percentageCompleted)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ class WeekdayList {
|
||||
}
|
||||
|
||||
fun toArray(): BooleanArray {
|
||||
return Arrays.copyOf(weekdays, 7)
|
||||
return weekdays.copyOf(7)
|
||||
}
|
||||
|
||||
fun toInteger(): Int {
|
||||
|
||||
@@ -29,6 +29,8 @@ import org.isoron.uhabits.core.ui.callbacks.OnConfirmedCallback
|
||||
import org.isoron.uhabits.core.utils.DateUtils
|
||||
import java.io.File
|
||||
import java.util.Random
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
class ShowHabitMenuPresenter(
|
||||
private val commandRunner: CommandRunner,
|
||||
@@ -67,7 +69,7 @@ class ShowHabitMenuPresenter(
|
||||
habit.originalEntries.clear()
|
||||
var strength = 50.0
|
||||
for (i in 0 until 365 * 5) {
|
||||
if (i % 7 == 0) strength = Math.max(0.0, Math.min(100.0, strength + 10 * random.nextGaussian()))
|
||||
if (i % 7 == 0) strength = max(0.0, min(100.0, strength + 10 * random.nextGaussian()))
|
||||
if (random.nextInt(100) > strength) continue
|
||||
var value = Entry.YES_MANUAL
|
||||
if (habit.isNumerical) value = (1000 + 250 * random.nextGaussian() * strength / 100).toInt() * 1000
|
||||
|
||||
@@ -39,9 +39,9 @@ class OverviewCardPresenter {
|
||||
val lastMonth = today.minus(30)
|
||||
val lastYear = today.minus(365)
|
||||
val scores = habit.scores
|
||||
val scoreToday = scores.get(today).value.toFloat()
|
||||
val scoreLastMonth = scores.get(lastMonth).value.toFloat()
|
||||
val scoreLastYear = scores.get(lastYear).value.toFloat()
|
||||
val scoreToday = scores[today].value.toFloat()
|
||||
val scoreLastMonth = scores[lastMonth].value.toFloat()
|
||||
val scoreLastYear = scores[lastYear].value.toFloat()
|
||||
val totalCount = habit.originalEntries.getKnown()
|
||||
.filter { it.value == Entry.YES_MANUAL }
|
||||
.count()
|
||||
|
||||
@@ -39,13 +39,13 @@ class ScoreCardPresenter(
|
||||
companion object {
|
||||
val BUCKET_SIZES = intArrayOf(1, 7, 31, 92, 365)
|
||||
fun getTruncateField(bucketSize: Int): DateUtils.TruncateField {
|
||||
when (bucketSize) {
|
||||
1 -> return DateUtils.TruncateField.DAY
|
||||
7 -> return DateUtils.TruncateField.WEEK_NUMBER
|
||||
31 -> return DateUtils.TruncateField.MONTH
|
||||
92 -> return DateUtils.TruncateField.QUARTER
|
||||
365 -> return DateUtils.TruncateField.YEAR
|
||||
else -> return DateUtils.TruncateField.MONTH
|
||||
return when (bucketSize) {
|
||||
1 -> DateUtils.TruncateField.DAY
|
||||
7 -> DateUtils.TruncateField.WEEK_NUMBER
|
||||
31 -> DateUtils.TruncateField.MONTH
|
||||
92 -> DateUtils.TruncateField.QUARTER
|
||||
365 -> DateUtils.TruncateField.YEAR
|
||||
else -> DateUtils.TruncateField.MONTH
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ class BarChart(
|
||||
val nColumns = floor((safeWidth) / barGroupWidth).toInt()
|
||||
val marginLeft = (safeWidth - nColumns * barGroupWidth) / 2
|
||||
val maxBarHeight = height - footerHeight - paddingTop
|
||||
var maxValue = series.map { it.max()!! }.max()!!
|
||||
var maxValue = series.map { it.maxOrNull()!! }.maxOrNull()!!
|
||||
maxValue = max(maxValue, 1.0)
|
||||
|
||||
canvas.setColor(theme.cardBackgroundColor)
|
||||
|
||||
@@ -192,15 +192,15 @@ class HistoryChart(
|
||||
val value = if (offset >= series.size) Square.OFF else series[offset]
|
||||
val squareColor: Color
|
||||
val color = theme.color(paletteColor.paletteIndex)
|
||||
when (value) {
|
||||
squareColor = when (value) {
|
||||
Square.ON -> {
|
||||
squareColor = color
|
||||
color
|
||||
}
|
||||
Square.OFF -> {
|
||||
squareColor = theme.lowContrastTextColor
|
||||
theme.lowContrastTextColor
|
||||
}
|
||||
Square.DIMMED, Square.HATCHED -> {
|
||||
squareColor = color.blendWith(theme.cardBackgroundColor, 0.5)
|
||||
color.blendWith(theme.cardBackgroundColor, 0.5)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ abstract class DateUtils {
|
||||
if (fixedLocalTime != null) return fixedLocalTime as Long
|
||||
|
||||
val tz = getTimeZone()
|
||||
val now = Date().getTime()
|
||||
val now = Date().time
|
||||
return now + tz.getOffset(now)
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ open class MidnightTimer @Inject constructor() {
|
||||
this.listeners.add(listener)
|
||||
}
|
||||
|
||||
@Synchronized fun onPause() = executor.shutdownNow()
|
||||
@Synchronized fun onPause(): MutableList<Runnable>? = executor.shutdownNow()
|
||||
|
||||
@Synchronized fun onResume() {
|
||||
executor = Executors.newSingleThreadScheduledExecutor()
|
||||
|
||||
@@ -59,11 +59,10 @@ class StringUtils {
|
||||
@JvmStatic
|
||||
fun splitLongs(str: String): LongArray {
|
||||
val parts: Array<String> = org.apache.commons.lang3.StringUtils.split(str, ',')
|
||||
val numbers = LongArray(parts.size) {
|
||||
return LongArray(parts.size) {
|
||||
i ->
|
||||
parts[i].toLong()
|
||||
}
|
||||
return numbers
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user