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

pull/498/head
Alinson S. Xavier 7 years ago
parent a546f6de73
commit 8544c5dc8a

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

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

@ -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)
}

@ -17,19 +17,13 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { package org.isoron.uhabits.gui
NativeEventEmitter,
DeviceEventEmitter,
NativeModules,
Platform,
} from 'react-native';
const { CoreModule } = NativeModules; data class PaletteColor(val index: Int)
let emitter = DeviceEventEmitter; class Color(val argb: Int) {
if (Platform.OS === 'ios') { val alpha = 1.0
emitter = new NativeEventEmitter(CoreModule); 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
} }
export const Emitter = emitter;
export const Backend = NativeModules.CoreModule;

@ -17,6 +17,11 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * 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"
}
}

@ -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()

@ -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)
}
}

@ -17,6 +17,10 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * 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)
}

@ -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)
}
}
}

@ -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)
}
}

@ -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)
}
}
}

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

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

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

@ -17,22 +17,26 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import React from 'react'; package org.isoron.uhabits.utils
import PropTypes from 'prop-types';
import Svg, { Circle } from 'react-native-svg';
import { Colors } from '../../helpers/Colors';
export default function ColorCircle(props) { data class Timestamp(val unixTime: Long)
const { size, color } = props;
return ( data class LocalDate(val year: Int,
<Svg height={size} width={size} viewBox="0 0 100 100"> val month: Int,
<Circle cx={50} cy={50} r={50} fill={color} /> val day: Int) {
<Circle cx={50} cy={50} r={30} fill={Colors.itemBackground} /> init {
</Svg> 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)
}
} }
ColorCircle.propTypes = { interface LocalDateFormatter {
size: PropTypes.number.isRequired, fun shortWeekdayName(date: LocalDate): String
color: PropTypes.string.isRequired, }
};

@ -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")
}
}

@ -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())
}
}

@ -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))
}
}

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

@ -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}.")
}
}
}

@ -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"))
}
}

@ -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)
}
}

@ -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)
}
}

@ -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)
}
}

@ -17,23 +17,23 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits package org.isoron.uhabits.gui.components
import org.isoron.uhabits.models.HabitRepository import org.isoron.uhabits.*
import org.isoron.uhabits.utils.* import org.isoron.uhabits.gui.components.HabitList.*
import org.junit.Before import org.junit.*
open class BaseTest { class RingTest : BaseViewTest() {
val fileOpener = JavaFileOpener() val base = "components/Ring"
val log = StandardLog()
val databaseOpener = JavaDatabaseOpener(log)
lateinit var db: Database
@Before @Test
open fun setUp() { fun testDraw() {
val dbFile = fileOpener.openUserFile("test.sqlite3") val component = Ring(theme.color(8),
if (dbFile.exists()) dbFile.delete() percentage = 0.30,
db = databaseOpener.open(dbFile) thickness = 5.0,
db.migrateTo(LOOP_DATABASE_VERSION, fileOpener, log) radius = 30.0,
theme = theme,
label = true)
assertRenders(120, 120, "$base/draw1.png", component)
} }
} }

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

@ -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)), "")
}
}

@ -1,41 +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/>.
*/
import UIKit
class ListHabitsController: UIViewController {
override func viewDidLoad() {
self.title = "Habits"
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add,
target: self,
action: #selector(self.onCreateHabitClicked))
// let box = UIView(frame: view.bounds.insetBy(dx: 100, dy: 100))
// box.backgroundColor = .blue
//// box.translatesAutoresizingMaskIntoConstraints = true
// box.autoresizingMask = [UIView.AutoresizingMask.flexibleLeftMargin,
// UIView.AutoresizingMask.flexibleRightMargin]
// view.addSubview(box)
}
@objc func onCreateHabitClicked() {
self.navigationController?.pushViewController(EditHabitController(), animated: true)
}
}

28
ios/.gitignore vendored

@ -1,28 +0,0 @@
!default.mode1v3
!default.mode2v3
!default.pbxuser
!default.perspectivev3
*.dSYM
*.dSYM.zip
*.hmap
*.ipa
*.mode1v3
*.mode2v3
*.moved-aside
*.pbxuser
*.perspectivev3
*.xccheckout
*.xcscmblueprint
*.xcworkspace
.build/
Carthage/Build
DerivedData/
build/
fastlane/Preview.html
fastlane/report.xml
fastlane/screenshots/**/*.png
fastlane/test_output
iOSInjectionProject/
playground.xcworkspace
timeline.xctimeline
xcuserdata/

@ -22,14 +22,17 @@ import UIKit
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? var window: UIWindow?
var backend = Backend(databaseOpener: IosDatabaseOpener(withLog: StandardLog()),
fileOpener: IosFileOpener(),
log: StandardLog())
func application(_ application: UIApplication, func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds) window = UIWindow(frame: UIScreen.main.bounds)
if let window = window { if let window = window {
let nav = UINavigationController() let nav = UINavigationController()
nav.viewControllers = [ListHabitsController()] nav.viewControllers = [ListHabitsController(withBackend: backend)]
window.backgroundColor = UIColor.white window.backgroundColor = UIColor.white
window.rootViewController = nav window.rootViewController = nav
window.makeKeyAndVisible() window.makeKeyAndVisible()

@ -17,8 +17,4 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#import <core.h> #import <core.h>

@ -2,6 +2,10 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>UIAppFonts</key>
<array>
<string>fonts/FontAwesome.ttf</string>
</array>
<key>CFBundleDevelopmentRegion</key> <key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string> <string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key> <key>CFBundleExecutable</key>

@ -0,0 +1,151 @@
/*
* 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/>.
*/
import UIKit
class ListHabitsCell : UITableViewCell {
var ring: ComponentView
var label = UILabel()
var buttons: [ComponentView] = []
var theme = LightTheme()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
ring = ComponentView(frame: CGRect(), component: nil)
super.init(style: .default, reuseIdentifier: reuseIdentifier)
let size = CGFloat(theme.checkmarkButtonSize)
let stack = UIStackView(frame: contentView.frame)
stack.autoresizingMask = [.flexibleWidth, .flexibleHeight]
stack.backgroundColor = .red
stack.axis = .horizontal
stack.distribution = .fill
stack.alignment = .center
contentView.addSubview(stack)
ring.backgroundColor = .white
ring.widthAnchor.constraint(equalToConstant: size * 0.75).isActive = true
ring.heightAnchor.constraint(equalToConstant: size).isActive = true
stack.addArrangedSubview(ring)
label.backgroundColor = .white
label.heightAnchor.constraint(equalToConstant: size).isActive = true
stack.addArrangedSubview(label)
for _ in 1...4 {
let btn = ComponentView(frame: frame, component: nil)
btn.backgroundColor = .white
btn.widthAnchor.constraint(equalToConstant: size).isActive = true
btn.heightAnchor.constraint(equalToConstant: size).isActive = true
buttons.append(btn)
stack.addArrangedSubview(btn)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
func setColor(_ color: Color) {
label.textColor = color.uicolor
ring.component = Ring(color: color,
percentage: Double.random(in: 0...1),
thickness: 2.5,
radius: 7,
theme: theme,
label: false)
ring.setNeedsDisplay()
let isNumerical = Int.random(in: 1...4) == 1
for btn in buttons {
if isNumerical {
btn.component = NumberButton(color: color,
value: Double.random(in: 0...5000),
threshold: 2000,
units: "steps",
theme: theme)
} else {
btn.component = CheckmarkButton(value: Int32.random(in: 0...2),
color: color,
theme: theme)
}
btn.setNeedsDisplay()
}
}
}
class ListHabitsController: UITableViewController {
var backend: Backend
var habits: [[String: Any]]
var theme: Theme
required init?(coder aDecoder: NSCoder) {
fatalError()
}
init(withBackend backend:Backend) {
self.backend = backend
self.habits = backend.getHabitList()
self.theme = backend.theme
super.init(nibName: nil, bundle: nil)
}
override func viewDidLoad() {
self.title = "Habits"
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add,
target: self,
action: #selector(self.onCreateHabitClicked))
tableView.register(ListHabitsCell.self, forCellReuseIdentifier: "cell")
tableView.backgroundColor = theme.headerBackgroundColor.uicolor
}
@objc func onCreateHabitClicked() {
self.navigationController?.pushViewController(EditHabitController(), animated: true)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return habits.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = indexPath.row
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ListHabitsCell
let color = theme.color(paletteIndex: habits[row]["color"] as! Int32)
cell.label.text = habits[row]["name"] as? String
cell.setColor(color)
return cell
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let component = HabitListHeader(today: LocalDate(year: 2019, month: 3, day: 24),
nButtons: 4,
theme: theme,
fmt: IosLocalDateFormatter(),
calc: IosLocalDateCalculator())
return ComponentView(frame: CGRect(x: 0, y: 0, width: 100, height: CGFloat(theme.checkmarkButtonSize)),
component: component)
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat(theme.checkmarkButtonSize)
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return CGFloat(theme.checkmarkButtonSize) + 1
}
}

@ -0,0 +1,141 @@
/*
* 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/>.
*/
import UIKit
class ComponentView : UIView {
var component: Component?
init(frame: CGRect, component: Component?) {
self.component = component
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
override func draw(_ rect: CGRect) {
let canvas = IosCanvas(withBounds: bounds)
component?.draw(canvas: canvas)
}
}
class IosCanvas : NSObject, Canvas {
func fillArc(centerX: Double, centerY: Double, radius: Double, startAngle: Double, swipeAngle: Double) {
let center = CGPoint(x: CGFloat(centerX), y: CGFloat(centerY))
let a1 = startAngle / 180 * .pi * (-1)
let a2 = a1 - swipeAngle / 180 * .pi
self.ctx.beginPath()
self.ctx.move(to: center)
self.ctx.addArc(center: center,
radius: CGFloat(radius),
startAngle: CGFloat(a1),
endAngle: CGFloat(a2),
clockwise: swipeAngle >= 0)
self.ctx.closePath()
self.ctx.fillPath()
}
func fillCircle(centerX: Double, centerY: Double, radius: Double) {
self.ctx.fillEllipse(in: CGRect(x: CGFloat(centerX - radius),
y: CGFloat(centerY - radius),
width: CGFloat(radius * 2),
height: CGFloat(radius * 2)))
}
var bounds: CGRect
var ctx: CGContext
var font = Font.regular
var textSize = CGFloat(12)
var textColor = UIColor.black
init(withBounds bounds: CGRect) {
self.bounds = bounds
self.ctx = UIGraphicsGetCurrentContext()!
}
func setColor(color: Color) {
self.ctx.setStrokeColor(color.cgcolor)
self.ctx.setFillColor(color.cgcolor)
textColor = color.uicolor
}
func drawLine(x1: Double, y1: Double, x2: Double, y2: Double) {
self.ctx.move(to: CGPoint(x: CGFloat(x1), y: CGFloat(y1)))
self.ctx.addLine(to: CGPoint(x: CGFloat(x2), y: CGFloat(y2)))
self.ctx.strokePath()
}
func drawText(text: String, x: Double, y: Double) {
let nsText = text as NSString
var uifont = UIFont.systemFont(ofSize: textSize)
if font == Font.bold {
uifont = UIFont.boldSystemFont(ofSize: textSize)
}
if font == Font.fontAwesome {
uifont = UIFont(name: "FontAwesome", size: textSize)!
}
let attrs = [NSAttributedString.Key.font: uifont,
NSAttributedString.Key.foregroundColor: textColor]
let size = nsText.size(withAttributes: attrs)
nsText.draw(at: CGPoint(x: CGFloat(x) - size.width / 2,
y : CGFloat(y) - size.height / 2),
withAttributes: attrs)
}
func drawRect(x: Double, y: Double, width: Double, height: Double) {
self.ctx.stroke(CGRect(x: CGFloat(x),
y: CGFloat(y),
width: CGFloat(width),
height: CGFloat(height)))
}
func fillRect(x: Double, y: Double, width: Double, height: Double) {
self.ctx.fill(CGRect(x: CGFloat(x),
y: CGFloat(y),
width: CGFloat(width),
height: CGFloat(height)))
}
func getHeight() -> Double {
return Double(bounds.height)
}
func getWidth() -> Double {
return Double(bounds.width)
}
func setTextSize(size: Double) {
self.textSize = CGFloat(size)
}
func setFont(font: Font) {
self.font = font
}
func setStrokeWidth(size: Double) {
self.ctx.setLineWidth(CGFloat(size))
}
}

@ -0,0 +1,57 @@
/*
* 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/>.
*/
import Foundation
class IosLocalDateFormatter : NSObject, LocalDateFormatter {
func shortWeekdayName(date: LocalDate) -> String {
let calendar = Calendar(identifier: .gregorian)
var dc = DateComponents()
dc.year = Int(date.year)
dc.month = Int(date.month)
dc.day = Int(date.day)
dc.hour = 13
dc.minute = 0
let d = calendar.date(from: dc)!
let fmt = DateFormatter()
fmt.dateFormat = "EEE"
return fmt.string(from: d)
}
}
class IosLocalDateCalculator : NSObject, LocalDateCalculator {
func plusDays(date: LocalDate, days: Int32) -> LocalDate {
let calendar = Calendar(identifier: .gregorian)
var dc = DateComponents()
dc.year = Int(date.year)
dc.month = Int(date.month)
dc.day = Int(date.day)
dc.hour = 13
dc.minute = 0
let d1 = calendar.date(from: dc)!
let d2 = d1.addingTimeInterval(24.0 * 60 * 60 * Double(days))
return LocalDate(year: Int32(calendar.component(.year, from: d2)),
month: Int32(calendar.component(.month, from: d2)),
day: Int32(calendar.component(.day, from: d2)))
}
func minusDays(date: LocalDate, days: Int32) -> LocalDate {
return plusDays(date: date, days: -days)
}
}

@ -17,13 +17,17 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#import <Foundation/Foundation.h> import UIKit
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
@interface RCT_EXTERN_MODULE(CoreModule, RCTEventEmitter) extension Color {
RCT_EXTERN_METHOD(createHabit:(NSString)name) var uicolor: UIColor {
//RCT_EXTERN_METHOD(updateHabit:(int)id (NSString)name) return UIColor(red: CGFloat(self.red),
//RCT_EXTERN_METHOD(deleteHabit:(int)id) green: CGFloat(self.green),
RCT_EXTERN_METHOD(requestHabitList) blue: CGFloat(self.blue),
@end alpha: 1.0)
}
var cgcolor : CGColor {
return uicolor.cgColor
}
}

@ -0,0 +1,79 @@
/*
* 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/>.
*/
import Foundation
class IosResourceFile : NSObject, ResourceFile {
var path: String
var fileManager = FileManager.default
init(forPath path: String) {
self.path = path
}
func readLines() -> [String] {
do {
let contents = try String(contentsOfFile: self.path, encoding: .utf8)
return contents.components(separatedBy: CharacterSet.newlines)
} catch {
return ["ERROR"]
}
}
}
class IosUserFile : NSObject, UserFile {
var path: String
init(forPath path: String) {
self.path = path
}
func delete() {
do {
try FileManager.default.removeItem(atPath: path)
} catch {
}
}
func exists() -> Bool {
return FileManager.default.fileExists(atPath: path)
}
}
class IosFileOpener : NSObject, FileOpener {
func openResourceFile(filename: String) -> ResourceFile {
let path = "\(Bundle.main.resourcePath!)/\(filename)"
return IosResourceFile(forPath: path)
}
func openUserFile(filename: String) -> UserFile {
do {
let root = try FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false).path
return IosUserFile(forPath: "\(root)/\(filename)")
} catch {
return IosUserFile(forPath: "invalid")
}
}
}

Binary file not shown.

@ -0,0 +1,5 @@
create table Habits ( id integer primary key autoincrement, archived integer, color integer, description text, freq_den integer, freq_num integer, highlight integer, name text, position integer, reminder_hour integer, reminder_min integer )
create table Checkmarks ( id integer primary key autoincrement, habit integer references habits(id), timestamp integer, value integer )
create table Repetitions ( id integer primary key autoincrement, habit integer references habits(id), timestamp integer )
create table Streak ( id integer primary key autoincrement, end integer, habit integer references habits(id), length integer, start integer )
create table Score ( id integer primary key autoincrement, habit integer references habits(id), score integer, timestamp integer )

@ -0,0 +1,3 @@
delete from Score
delete from Streak
delete from Checkmarks

@ -0,0 +1 @@
alter table Habits add column reminder_days integer not null default 127

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save