Remove react-native; rewrite main screen in (native) swift

This commit is contained in:
2019-03-25 20:39:44 -05:00
parent a546f6de73
commit 8544c5dc8a
148 changed files with 2007 additions and 8899 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 350 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 345 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 424 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@@ -19,16 +19,19 @@
package org.isoron.uhabits
import org.isoron.uhabits.gui.*
import org.isoron.uhabits.models.*
import org.isoron.uhabits.utils.*
class Backend(var databaseOpener: DatabaseOpener,
var fileOpener: FileOpener,
var log: Log) {
var database: Database
var habitsRepository: HabitRepository
var habits = mutableMapOf<Int, Habit>()
class Backend(databaseOpener: DatabaseOpener,
fileOpener: FileOpener,
log: Log) {
private var database: Database
private var habitsRepository: HabitRepository
private var habits = mutableMapOf<Int, Habit>()
var theme: Theme = LightTheme()
init {
val dbFile = fileOpener.openUserFile("uhabits.db")
@@ -39,11 +42,14 @@ class Backend(var databaseOpener: DatabaseOpener,
}
fun getHabitList(): List<Map<String, *>> {
return habits.values.sortedBy { h -> h.position }.map { h ->
mapOf("key" to h.id.toString(),
"name" to h.name,
"color" to h.color.paletteIndex)
}
return habits.values
.filter { h -> !h.isArchived }
.sortedBy { h -> h.position }
.map { h ->
mapOf("key" to h.id.toString(),
"name" to h.name,
"color" to h.color.index)
}
}
fun createHabit(name: String) {
@@ -52,7 +58,7 @@ class Backend(var databaseOpener: DatabaseOpener,
name = name,
description = "",
frequency = Frequency(1, 1),
color = Color(3),
color = PaletteColor(3),
isArchived = false,
position = habits.size,
unit = "",

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.gui
enum class Font {
REGULAR,
BOLD,
FONT_AWESOME
}
interface Canvas {
fun setColor(color: Color)
fun drawLine(x1: Double, y1: Double, x2: Double, y2: Double)
fun drawText(text: String, x: Double, y: Double)
fun fillRect(x: Double, y: Double, width: Double, height: Double)
fun drawRect(x: Double, y: Double, width: Double, height: Double)
fun getHeight(): Double
fun getWidth(): Double
fun setFont(font: Font)
fun setTextSize(size: Double)
fun setStrokeWidth(size: Double)
fun fillArc(centerX: Double,
centerY: Double,
radius: Double,
startAngle: Double,
swipeAngle: Double)
fun fillCircle(centerX: Double, centerY: Double, radius: Double)
}

View File

@@ -17,23 +17,13 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits
package org.isoron.uhabits.gui
import org.isoron.uhabits.models.HabitRepository
import org.isoron.uhabits.utils.*
import org.junit.Before
data class PaletteColor(val index: Int)
open class BaseTest {
val fileOpener = JavaFileOpener()
val log = StandardLog()
val databaseOpener = JavaDatabaseOpener(log)
lateinit var db: Database
@Before
open fun setUp() {
val dbFile = fileOpener.openUserFile("test.sqlite3")
if (dbFile.exists()) dbFile.delete()
db = databaseOpener.open(dbFile)
db.migrateTo(LOOP_DATABASE_VERSION, fileOpener, log)
}
}
class Color(val argb: Int) {
val alpha = 1.0
val red = ((argb shr 16) and 0xFF) / 255.0
val green = ((argb shr 8 ) and 0xFF) / 255.0
val blue = ((argb shr 0 ) and 0xFF) / 255.0
}

View File

@@ -17,6 +17,11 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.models
package org.isoron.uhabits.gui
class Timestamp(val unixTime: Long)
class FontAwesome {
companion object {
val CHECK = "\uf00c"
val TIMES = "\uf00d"
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.gui
abstract class Theme {
val toolbarColor = Color(0xffffff)
val lowContrastTextColor = Color(0xe0e0e0)
val mediumContrastTextColor = Color(0x808080)
val highContrastTextColor = Color(0x202020)
val cardBackgroundColor = Color(0xFFFFFF)
val appBackgroundColor = Color(0xf4f4f4)
val toolbarBackgroundColor = Color(0xf4f4f4)
val statusBarBackgroundColor = Color(0x333333)
val headerBackgroundColor = Color(0xeeeeee)
val headerBorderColor = Color(0xcccccc)
val headerTextColor = mediumContrastTextColor
val itemBackgroundColor = Color(0xffffff)
fun color(paletteIndex: Int): Color {
return when (paletteIndex) {
0 -> Color(0xD32F2F)
1 -> Color(0x512DA8)
2 -> Color(0xF57C00)
3 -> Color(0xFF8F00)
4 -> Color(0xF9A825)
5 -> Color(0xAFB42B)
6 -> Color(0x7CB342)
7 -> Color(0x388E3C)
8 -> Color(0x00897B)
9 -> Color(0x00ACC1)
10 -> Color(0x039BE5)
11 -> Color(0x1976D2)
12 -> Color(0x303F9F)
13 -> Color(0x5E35B1)
14 -> Color(0x8E24AA)
15 -> Color(0xD81B60)
16 -> Color(0x5D4037)
else -> Color(0x000000)
}
}
val checkmarkButtonSize = 48.0
val smallTextSize = 12.0
val regularTextSize = 17.0
}
class LightTheme : Theme()

View File

@@ -0,0 +1,41 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.gui.components.HabitList
import org.isoron.uhabits.gui.*
import org.isoron.uhabits.gui.components.*
class CheckmarkButton(private val value: Int,
private val color: Color,
private val theme: Theme) : Component {
override fun draw(canvas: Canvas) {
canvas.setFont(Font.FONT_AWESOME)
canvas.setTextSize(theme.smallTextSize * 1.5)
canvas.setColor(when (value) {
2 -> color
else -> theme.lowContrastTextColor
})
val text = when (value) {
0 -> FontAwesome.TIMES
else -> FontAwesome.CHECK
}
canvas.drawText(text, canvas.getWidth() / 2.0, canvas.getHeight() / 2.0)
}
}

View File

@@ -17,6 +17,10 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.models
package org.isoron.uhabits.gui.components
data class Color(val paletteIndex: Int)
import org.isoron.uhabits.gui.*
interface Component {
fun draw(canvas: Canvas)
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.gui.components.HabitList
import org.isoron.uhabits.gui.*
import org.isoron.uhabits.gui.components.*
import org.isoron.uhabits.utils.*
class HabitListHeader(private val today: LocalDate,
private val nButtons: Int,
private val theme: Theme,
private val fmt: LocalDateFormatter,
private val calc: LocalDateCalculator) : Component {
override fun draw(canvas: Canvas) {
val width = canvas.getWidth()
val height = canvas.getHeight()
val buttonSize = theme.checkmarkButtonSize
canvas.setColor(theme.headerBackgroundColor)
canvas.fillRect(0.0, 0.0, width, height)
canvas.setColor(theme.headerBorderColor)
canvas.setStrokeWidth(0.5)
canvas.drawLine(0.0, height - 0.5, width, height - 0.5)
canvas.setColor(theme.headerTextColor)
canvas.setFont(Font.BOLD)
canvas.setTextSize(theme.smallTextSize)
repeat(nButtons) { index ->
val date = calc.minusDays(today, nButtons - index - 1)
val name = fmt.shortWeekdayName(date).toUpperCase()
val number = date.day.toString()
val x = width - (index + 1) * buttonSize + buttonSize / 2
val y = height / 2
canvas.drawText(name, x, y - theme.smallTextSize * 0.6)
canvas.drawText(number, x, y + theme.smallTextSize * 0.6)
}
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.gui.components.HabitList
import org.isoron.uhabits.gui.*
import org.isoron.uhabits.gui.components.*
import org.isoron.uhabits.utils.*
import kotlin.math.*
fun Double.toShortString(): String = when {
this >= 1e9 -> sprintf("%.1fG", this / 1e9)
this >= 1e8 -> sprintf("%.0fM", this / 1e6)
this >= 1e7 -> sprintf("%.1fM", this / 1e6)
this >= 1e6 -> sprintf("%.1fM", this / 1e6)
this >= 1e5 -> sprintf("%.0fk", this / 1e3)
this >= 1e4 -> sprintf("%.1fk", this / 1e3)
this >= 1e3 -> sprintf("%.1fk", this / 1e3)
this >= 1e2 -> sprintf("%.0f", this)
this >= 1e1 -> when {
round(this) == this -> sprintf("%.0f", this)
else -> sprintf("%.1f", this)
}
else -> when {
round(this) == this -> sprintf("%.0f", this)
round(this * 10) == this * 10 -> sprintf("%.1f", this)
else -> sprintf("%.2f", this)
}
}
class NumberButton(val color: Color,
val value: Double,
val threshold: Double,
val units: String,
val theme: Theme) : Component {
override fun draw(canvas: Canvas) {
val width = canvas.getWidth()
val height = canvas.getHeight()
val em = theme.smallTextSize
canvas.setColor(when {
value >= threshold -> color
value >= 0.01 -> theme.mediumContrastTextColor
else -> theme.lowContrastTextColor
})
canvas.setTextSize(theme.regularTextSize)
canvas.setFont(Font.BOLD)
canvas.drawText(value.toShortString(), width / 2, height / 2 - 0.6 * em)
canvas.setTextSize(theme.smallTextSize)
canvas.setFont(Font.REGULAR)
canvas.drawText(units, width / 2, height / 2 + 0.6 * em)
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.gui.components
import org.isoron.uhabits.gui.*
import org.isoron.uhabits.utils.*
import kotlin.math.*
class Ring(val color: Color,
val percentage: Double,
val thickness: Double,
val radius: Double,
val theme: Theme,
val label: Boolean = false) : Component {
override fun draw(canvas: Canvas) {
val width = canvas.getWidth()
val height = canvas.getHeight()
val angle = 360.0 * max(0.0, min(360.0, percentage))
canvas.setColor(theme.lowContrastTextColor)
canvas.fillCircle(width/2, height/2, radius)
canvas.setColor(color)
canvas.fillArc(width/2, height/2, radius, 90.0, -angle)
canvas.setColor(theme.cardBackgroundColor)
canvas.fillCircle(width/2, height/2, radius - thickness)
if(label) {
canvas.setColor(color)
canvas.setTextSize(radius * 0.4)
canvas.drawText(sprintf("%.0f%%", percentage*100), width/2, height/2)
}
}
}

View File

@@ -19,5 +19,7 @@
package org.isoron.uhabits.models
import org.isoron.uhabits.utils.*
data class Checkmark(var timestamp: Timestamp,
var value: Int)
var value: Int)

View File

@@ -19,11 +19,13 @@
package org.isoron.uhabits.models
import org.isoron.uhabits.gui.*
data class Habit(var id: Int,
var name: String,
var description: String,
var frequency: Frequency,
var color: Color,
var color: PaletteColor,
var isArchived: Boolean,
var position: Int,
var unit: String,

View File

@@ -19,6 +19,7 @@
package org.isoron.uhabits.models
import org.isoron.uhabits.gui.*
import org.isoron.uhabits.utils.Database
import org.isoron.uhabits.utils.PreparedStatement
import org.isoron.uhabits.utils.StepResult
@@ -69,7 +70,7 @@ class HabitRepository(var db: Database) {
name = stmt.getText(1),
description = stmt.getText(2),
frequency = Frequency(stmt.getInt(3), stmt.getInt(4)),
color = Color(stmt.getInt(5)),
color = PaletteColor(stmt.getInt(5)),
isArchived = stmt.getInt(6) != 0,
position = stmt.getInt(7),
unit = stmt.getText(8),
@@ -83,7 +84,7 @@ class HabitRepository(var db: Database) {
statement.bindText(2, habit.description)
statement.bindInt(3, habit.frequency.numerator)
statement.bindInt(4, habit.frequency.denominator)
statement.bindInt(5, habit.color.paletteIndex)
statement.bindInt(5, habit.color.index)
statement.bindInt(6, if (habit.isArchived) 1 else 0)
statement.bindInt(7, habit.position)
statement.bindText(8, habit.unit)

View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.utils
data class Timestamp(val unixTime: Long)
data class LocalDate(val year: Int,
val month: Int,
val day: Int) {
init {
if ((month <= 0) or (month >= 13)) throw(IllegalArgumentException())
if ((day <= 0) or (day >= 32)) throw(IllegalArgumentException())
}
}
interface LocalDateCalculator {
fun plusDays(date: LocalDate, days: Int): LocalDate
fun minusDays(date: LocalDate, days: Int): LocalDate {
return plusDays(date, -days)
}
}
interface LocalDateFormatter {
fun shortWeekdayName(date: LocalDate): String
}

View File

@@ -1,55 +0,0 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.utils
import platform.Foundation.*
class IosResourceFile(val path: String) : ResourceFile {
private val fileManager = NSFileManager.defaultManager()
override fun readLines(): List<String> {
val contents = NSString.stringWithContentsOfFile(path) as NSString
return contents.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) as List<String>
}
}
class IosUserFile(val path: String) : UserFile {
override fun exists(): Boolean {
return NSFileManager.defaultManager().fileExistsAtPath(path)
}
override fun delete() {
NSFileManager.defaultManager().removeItemAtPath(path, null)
}
}
class IosFileOpener : FileOpener {
override fun openResourceFile(filename: String): ResourceFile {
val root = NSBundle.mainBundle.resourcePath!!
return IosResourceFile("$root/$filename")
}
override fun openUserFile(filename: String): UserFile {
val manager = NSFileManager.defaultManager()
val root = manager.URLForDirectory(NSDocumentDirectory,
NSUserDomainMask,
null, false, null)!!.path
return IosUserFile("$root/$filename")
}
}

View File

@@ -0,0 +1,144 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.gui
import org.isoron.uhabits.utils.*
import java.awt.*
import java.awt.RenderingHints.*
import java.awt.font.*
import java.lang.Math.*
import kotlin.math.*
fun createFont(path: String): java.awt.Font {
return java.awt.Font.createFont(0,
(JavaFileOpener().openResourceFile(path) as JavaResourceFile).stream())
}
private val ROBOTO_REGULAR_FONT = createFont("fonts/Roboto-Regular.ttf")
private val ROBOTO_BOLD_FONT = createFont("fonts/Roboto-Bold.ttf")
private val FONT_AWESOME_FONT = createFont("fonts/FontAwesome.ttf")
class JavaCanvas(val g2d: Graphics2D,
val widthPx: Int,
val heightPx: Int,
val pixelScale: Double = 2.0) : Canvas {
private val frc = FontRenderContext(null, true, true)
private var fontSize = 12.0
private var font = Font.REGULAR
init {
g2d.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON);
g2d.setRenderingHint(KEY_FRACTIONALMETRICS, VALUE_FRACTIONALMETRICS_ON);
updateFont()
}
private fun toPixel(x: Double): Int {
return (pixelScale * x).toInt()
}
private fun toDp(x: Int): Double {
return x / pixelScale
}
override fun setColor(color: Color) {
g2d.color = java.awt.Color(color.red.toFloat(),
color.green.toFloat(),
color.blue.toFloat())
}
override fun drawLine(x1: Double, y1: Double, x2: Double, y2: Double) {
g2d.drawLine(toPixel(x1), toPixel(y1), toPixel(x2), toPixel(y2))
}
override fun drawText(text: String, x: Double, y: Double) {
val bounds = g2d.font.getStringBounds(text, frc)
val bWidth = bounds.width.roundToInt()
val bHeight = bounds.height.roundToInt()
val bx = bounds.x.roundToInt()
val by = bounds.y.roundToInt()
g2d.drawString(text,
toPixel(x) - bx - bWidth / 2,
toPixel(y) - by - bHeight / 2)
}
override fun fillRect(x: Double, y: Double, width: Double, height: Double) {
g2d.fillRect(toPixel(x), toPixel(y), toPixel(width), toPixel(height))
}
override fun drawRect(x: Double, y: Double, width: Double, height: Double) {
g2d.drawRect(toPixel(x), toPixel(y), toPixel(width), toPixel(height))
}
override fun getHeight(): Double {
return toDp(heightPx)
}
override fun getWidth(): Double {
return toDp(widthPx)
}
override fun setFont(font: Font) {
this.font = font
updateFont()
}
override fun setTextSize(size: Double) {
fontSize = size
updateFont()
}
override fun setStrokeWidth(size: Double) {
g2d.setStroke(BasicStroke(size.toFloat()))
}
private fun updateFont() {
val size = (fontSize * pixelScale).toFloat()
g2d.font = when (font) {
Font.REGULAR -> ROBOTO_REGULAR_FONT.deriveFont(size)
Font.BOLD -> ROBOTO_BOLD_FONT.deriveFont(size)
Font.FONT_AWESOME -> FONT_AWESOME_FONT.deriveFont(size)
}
}
override fun fillCircle(centerX: Double, centerY: Double, radius: Double) {
g2d.fillOval(toPixel(centerX - radius),
toPixel(centerY - radius),
toPixel(radius * 2),
toPixel(radius * 2))
}
override fun fillArc(centerX: Double,
centerY: Double,
radius: Double,
startAngle: Double,
swipeAngle: Double) {
g2d.fillArc(toPixel(centerX - radius),
toPixel(centerY - radius),
toPixel(radius * 2),
toPixel(radius * 2),
startAngle.roundToInt(),
swipeAngle.roundToInt())
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.utils
import java.util.*
import java.util.Calendar.*
class JavaLocalDateFormatter(private val locale: Locale) : LocalDateFormatter {
override fun shortWeekdayName(date: LocalDate): String {
val d = GregorianCalendar(date.year, date.month - 1, date.day)
return d.getDisplayName(DAY_OF_WEEK, SHORT, locale);
}
}
class JavaLocalDateCalculator : LocalDateCalculator {
override fun plusDays(date: LocalDate, days: Int): LocalDate {
val d = GregorianCalendar(date.year, date.month - 1, date.day)
d.add(Calendar.DAY_OF_MONTH, days)
return LocalDate(d.get(Calendar.YEAR),
d.get(Calendar.MONTH) + 1,
d.get(Calendar.DAY_OF_MONTH))
}
}

View File

@@ -19,6 +19,7 @@
package org.isoron.uhabits.utils
import java.io.*
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
@@ -27,6 +28,10 @@ class JavaResourceFile(private val path: Path) : ResourceFile {
override fun readLines(): List<String> {
return Files.readAllLines(path)
}
fun stream(): InputStream {
return Files.newInputStream(path)
}
}
class JavaUserFile(val path: Path) : UserFile {

View File

@@ -0,0 +1,99 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits
import junit.framework.TestCase.*
import org.isoron.uhabits.gui.*
import org.isoron.uhabits.gui.components.*
import org.isoron.uhabits.utils.*
import org.junit.Before
import java.awt.image.*
import java.io.*
import java.lang.RuntimeException
import javax.imageio.*
import kotlin.math.*
open class BaseTest {
val fileOpener = JavaFileOpener()
val log = StandardLog()
val databaseOpener = JavaDatabaseOpener(log)
lateinit var db: Database
@Before
open fun setUp() {
val dbFile = fileOpener.openUserFile("test.sqlite3")
if (dbFile.exists()) dbFile.delete()
db = databaseOpener.open(dbFile)
db.migrateTo(LOOP_DATABASE_VERSION, fileOpener, log)
}
}
open class BaseViewTest {
val theme = LightTheme()
fun distance(actual: BufferedImage,
expected: BufferedImage): Double {
if (actual.width != expected.width) return Double.POSITIVE_INFINITY
if (actual.height != expected.height) return Double.POSITIVE_INFINITY
var distance = 0.0;
for (x in 0 until actual.width) {
for (y in 0 until actual.height) {
val p1 = Color(actual.getRGB(x, y))
val p2 = Color(expected.getRGB(x, y))
distance += abs(p1.red - p2.red)
distance += abs(p1.green - p2.green)
distance += abs(p1.blue - p2.blue)
}
}
return 255 * distance / (actual.width * actual.height)
}
fun assertRenders(width: Int,
height: Int,
expectedPath: String,
component: Component,
threshold: Double = 1.0) {
val actual = BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
val canvas = JavaCanvas(actual.createGraphics(), width, height)
val expectedFile: JavaResourceFile
val actualPath = "/tmp/${expectedPath}"
component.draw(canvas)
try {
expectedFile = JavaFileOpener().openResourceFile(expectedPath) as JavaResourceFile
} catch(e: RuntimeException) {
File(actualPath).parentFile.mkdirs()
ImageIO.write(actual, "png", File(actualPath))
fail("Expected file is missing. Actual render saved to $actualPath")
return
}
val expected = ImageIO.read(expectedFile.stream())
val d = distance(actual, expected)
if (d >= threshold) {
File(actualPath).parentFile.mkdirs()
ImageIO.write(actual, "png", File(actualPath))
ImageIO.write(expected, "png", File(actualPath.replace(".png", ".expected.png")))
fail("Images differ (distance=${d}). Actual rendered saved to ${actualPath}.")
}
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.gui
import org.junit.*
import java.awt.image.*
import java.io.*
import javax.imageio.*
class JavaCanvasTest {
@Test
fun testDrawing() {
val image = BufferedImage(500, 400, BufferedImage.TYPE_INT_RGB)
val canvas = JavaCanvas(image.createGraphics(), 500, 400, pixelScale=1.0)
canvas.setColor(Color(0x303030))
canvas.fillRect(0.0, 0.0, 500.0, 400.0)
canvas.setColor(Color(0x606060))
canvas.setStrokeWidth(25.0)
canvas.drawRect(100.0, 100.0, 300.0, 200.0)
canvas.setColor(Color(0xFFFF00))
canvas.setStrokeWidth(1.0)
canvas.drawRect(0.0, 0.0, 100.0, 100.0)
canvas.fillCircle(50.0, 50.0, 30.0)
canvas.drawRect(0.0, 100.0, 100.0, 100.0)
canvas.fillArc(50.0, 150.0, 30.0, 90.0, 135.0)
canvas.drawRect(0.0, 200.0, 100.0, 100.0)
canvas.fillArc(50.0, 250.0, 30.0, 90.0, -135.0)
canvas.drawRect(0.0, 300.0, 100.0, 100.0)
canvas.fillArc(50.0, 350.0, 30.0, 45.0, 90.0)
canvas.setColor(Color(0xFF0000))
canvas.setStrokeWidth(2.0)
canvas.drawLine(0.0, 0.0, 500.0, 400.0)
canvas.drawLine(500.0, 0.0, 0.0, 400.0)
canvas.setTextSize(50.0)
canvas.setColor(Color(0x00FF00))
canvas.drawText("Test", 250.0, 200.0)
canvas.setFont(Font.BOLD)
canvas.drawText("Test", 250.0, 100.0)
canvas.setFont(Font.FONT_AWESOME)
canvas.drawText(FontAwesome.CHECK, 250.0, 300.0)
ImageIO.write(image, "png", File("/tmp/JavaCanvasTest.png"))
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.gui.components
import org.isoron.uhabits.*
import org.isoron.uhabits.gui.components.HabitList.*
import org.junit.*
class CheckmarkButtonTest : BaseViewTest() {
val base = "components/CheckmarkButton"
@Test
fun testDrawExplicit() {
val component = CheckmarkButton(2, theme.color(8), theme)
assertRenders(96, 96, "$base/explicit.png", component)
}
@Test
fun testDrawImplicit() {
val component = CheckmarkButton(1, theme.color(8), theme)
assertRenders(96, 96, "$base/implicit.png", component)
}
@Test
fun testDrawUnchecked() {
val component = CheckmarkButton(0, theme.color(8), theme)
assertRenders(96, 96, "$base/unchecked.png", component)
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.gui.components
import org.isoron.uhabits.*
import org.isoron.uhabits.gui.components.HabitList.*
import org.isoron.uhabits.utils.*
import org.isoron.uhabits.utils.LocalDate
import org.junit.*
import java.util.*
class HabitListHeaderTest : BaseViewTest() {
@Test
fun testDraw() {
val header = HabitListHeader(LocalDate(2019, 3, 25),
5,
theme,
JavaLocalDateFormatter(Locale.US),
JavaLocalDateCalculator())
assertRenders(1200, 96,
"components/HabitListHeader/light.png",
header)
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.gui.components
import org.hamcrest.CoreMatchers.*
import org.isoron.uhabits.*
import org.isoron.uhabits.gui.components.HabitList.*
import org.junit.*
import org.junit.Assert.*
class NumberButtonTest : BaseViewTest() {
val base = "components/NumberButton/"
@Test
fun testFormatValue() {
assertThat(0.1235.toShortString(), equalTo("0.12"))
assertThat(0.1000.toShortString(), equalTo("0.1"))
assertThat(5.0.toShortString(), equalTo("5"))
assertThat(5.25.toShortString(), equalTo("5.25"))
assertThat(12.3456.toShortString(), equalTo("12.3"))
assertThat(123.123.toShortString(), equalTo("123"))
assertThat(321.2.toShortString(), equalTo("321"))
assertThat(4321.2.toShortString(), equalTo("4.3k"))
assertThat(54321.2.toShortString(), equalTo("54.3k"))
assertThat(654321.2.toShortString(), equalTo("654k"))
assertThat(7654321.2.toShortString(), equalTo("7.7M"))
assertThat(87654321.2.toShortString(), equalTo("87.7M"))
assertThat(987654321.2.toShortString(), equalTo("988M"))
assertThat(1987654321.2.toShortString(), equalTo("2.0G"))
}
@Test
fun testRenderAbove() {
val btn = NumberButton(theme.color(8), 500.0, 100.0, "steps", theme)
assertRenders(96, 96, "$base/render_above.png", btn)
}
@Test
fun testRenderBelow() {
val btn = NumberButton(theme.color(8), 99.0, 100.0, "steps", theme)
assertRenders(96, 96, "$base/render_below.png", btn)
}
@Test
fun testRenderZero() {
val btn = NumberButton(theme.color(8), 0.0, 100.0, "steps", theme)
assertRenders(96, 96, "$base/render_zero.png", btn)
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.gui.components
import org.isoron.uhabits.*
import org.isoron.uhabits.gui.components.HabitList.*
import org.junit.*
class RingTest : BaseViewTest() {
val base = "components/Ring"
@Test
fun testDraw() {
val component = Ring(theme.color(8),
percentage = 0.30,
thickness = 5.0,
radius = 30.0,
theme = theme,
label = true)
assertRenders(120, 120, "$base/draw1.png", component)
}
}

View File

@@ -21,6 +21,7 @@ package org.isoron.uhabits.models
import junit.framework.Assert.assertEquals
import org.isoron.uhabits.BaseTest
import org.isoron.uhabits.gui.*
import org.junit.Before
import org.junit.Test
@@ -39,7 +40,7 @@ class HabitRepositoryTest : BaseTest() {
name = "Wake up early",
description = "Did you wake up before 6am?",
frequency = Frequency(1, 1),
color = Color(3),
color = PaletteColor(3),
isArchived = false,
position = 0,
unit = "",
@@ -50,7 +51,7 @@ class HabitRepositoryTest : BaseTest() {
name = "Exercise",
description = "Did you exercise for at least 20 minutes?",
frequency = Frequency(1, 2),
color = Color(4),
color = PaletteColor(4),
isArchived = false,
position = 1,
unit = "",
@@ -61,7 +62,7 @@ class HabitRepositoryTest : BaseTest() {
name = "Learn Japanese",
description = "Did you study Japanese today?",
frequency = Frequency(1, 1),
color = Color(3),
color = PaletteColor(3),
isArchived = false,
position = 2,
unit = "",

View File

@@ -0,0 +1,50 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.utils
import junit.framework.TestCase.*
import org.junit.*
import java.util.*
class JavaDatesTest {
val calc = JavaLocalDateCalculator()
@Test
fun plusMinusDays() {
val today = LocalDate(2019, 3, 25)
assertEquals(calc.minusDays(today, 28), LocalDate(2019, 2, 25))
assertEquals(calc.plusDays(today, 7), LocalDate(2019, 4, 1))
assertEquals(calc.plusDays(today, 42), LocalDate(2019, 5, 6))
}
@Test
fun shortMonthName() {
var fmt = JavaLocalDateFormatter(Locale.US)
assertEquals(fmt.shortWeekdayName(LocalDate(2019, 3, 25)), "Mon")
assertEquals(fmt.shortWeekdayName(LocalDate(2019, 4, 4)), "Thu")
assertEquals(fmt.shortWeekdayName(LocalDate(2019, 5, 12)), "Sun")
fmt = JavaLocalDateFormatter(Locale.JAPAN)
assertEquals(fmt.shortWeekdayName(LocalDate(2019, 3, 25)), "")
assertEquals(fmt.shortWeekdayName(LocalDate(2019, 4, 4)), "")
assertEquals(fmt.shortWeekdayName(LocalDate(2019, 5, 12)), "")
}
}