Skip to content

Commit

Permalink
Reorganize packages; implement checkmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
iSoron committed Mar 31, 2019
1 parent 6a30bb9 commit 70a7985
Show file tree
Hide file tree
Showing 100 changed files with 1,753 additions and 509 deletions.
Binary file modified core/assets/main/databases/template.db
Binary file not shown.
Binary file added core/assets/main/fonts/NotoSans-Bold.ttf
Binary file not shown.
Binary file added core/assets/main/fonts/NotoSans-Regular.ttf
Binary file not shown.
Binary file removed core/assets/main/fonts/Roboto-Bold.ttf
Binary file not shown.
Binary file removed core/assets/main/fonts/Roboto-Regular.ttf
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ buildscript {

dependencies {
classpath "com.android.tools.build:gradle:3.2.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.11"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <[email protected]>
*
* 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.platform.concurrency

/**
* A TaskRunner provides the ability of running tasks in different queues. The
* class is also observable, and notifies listeners when new tasks are started
* or finished.
*
* Two queues are available: a foreground queue and a background queue. These
* two queues may run in parallel, depending on the hardware. Multiple tasks
* submitted to the same queue, however, always run sequentially, in the order
* they were enqueued.
*/
interface TaskRunner {

val listeners: MutableList<Listener>

val activeTaskCount: Int

fun runInBackground(task: () -> Unit)

fun runInForeground(task: () -> Unit)

interface Listener {
fun onTaskStarted()
fun onTaskFinished()
}
}

/**
* Sequential implementation of TaskRunner. Both background and foreground
* queues run in the same thread, so they block each other.
*/
class SequentialTaskRunner : TaskRunner {

override val listeners = mutableListOf<TaskRunner.Listener>()

override var activeTaskCount = 0

override fun runInBackground(task: () -> Unit) {
activeTaskCount += 1
for (l in listeners) l.onTaskStarted()
task()
activeTaskCount -= 1
for (l in listeners) l.onTaskFinished()
}

override fun runInForeground(task: () -> Unit) = runInBackground(task)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.isoron.uhabits.gui
package org.isoron.platform.gui

enum class TextAlign {
LEFT, CENTER, RIGHT
}


enum class Font {
REGULAR,
Expand All @@ -34,12 +39,13 @@ interface Canvas {
fun getHeight(): Double
fun getWidth(): Double
fun setFont(font: Font)
fun setTextSize(size: Double)
fun setFontSize(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)
fun setTextAlign(align: TextAlign)
}
45 changes: 45 additions & 0 deletions core/src/commonMain/kotlin/org/isoron/platform/gui/Colors.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <[email protected]>
*
* 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.platform.gui

data class PaletteColor(val index: Int)

data class Color(val red: Double,
val green: Double,
val blue: Double,
val alpha: Double) {

val luminosity: Double
get() {
return 0.21 * red + 0.72 * green + 0.07 * blue
}

constructor(rgb: Int) : this(((rgb shr 16) and 0xFF) / 255.0,
((rgb shr 8) and 0xFF) / 255.0,
((rgb shr 0) and 0xFF) / 255.0,
1.0)

fun blendWith(other: Color, weight: Double): Color {
return Color(red * (1 - weight) + other.red * weight,
green * (1 - weight) + other.green * weight,
blue * (1 - weight) + other.blue * weight,
alpha * (1 - weight) + other.alpha * weight)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.isoron.uhabits.gui.components

import org.isoron.uhabits.gui.*
package org.isoron.platform.gui

interface Component {
fun draw(canvas: Canvas)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.isoron.uhabits.gui
package org.isoron.platform.gui

class FontAwesome {
companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,37 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.isoron.uhabits.utils

enum class StepResult {
ROW,
DONE
}
package org.isoron.platform.io

interface PreparedStatement {
fun step(): StepResult
fun finalize()
fun getInt(index: Int): Int
fun getLong(index: Int): Long
fun getText(index: Int): String
fun getReal(index: Int): Double
fun bindInt(index: Int, value: Int)
fun bindLong(index: Int, value: Long)
fun bindText(index: Int, value: String)
fun bindReal(index: Int, value: Double)
fun reset()
}

interface Database {
fun prepareStatement(sql: String): PreparedStatement
fun close()
enum class StepResult {
ROW,
DONE
}

interface DatabaseOpener {
fun open(file: UserFile): Database
}

fun Database.execute(sql: String) {
interface Database {
fun prepareStatement(sql: String): PreparedStatement
fun close()
}

fun Database.runInBackground(sql: String) {
val stmt = prepareStatement(sql)
stmt.step()
stmt.finalize()
Expand All @@ -70,13 +72,13 @@ fun Database.nextId(tableName: String): Int {
}
}

fun Database.begin() = execute("begin")
fun Database.begin() = runInBackground("begin")

fun Database.commit() = execute("commit")
fun Database.commit() = runInBackground("commit")

fun Database.getVersion() = queryInt("pragma user_version")

fun Database.setVersion(v: Int) = execute("pragma user_version = $v")
fun Database.setVersion(v: Int) = runInBackground("pragma user_version = $v")

fun Database.migrateTo(newVersion: Int, fileOpener: FileOpener, log: Log) {
val currentVersion = getVersion()
Expand All @@ -90,12 +92,11 @@ fun Database.migrateTo(newVersion: Int, fileOpener: FileOpener, log: Log) {

begin()
for (v in (currentVersion + 1)..newVersion) {
log.debug("Database", "Running migration $v")
val filename = sprintf("migrations/%03d.sql", v)
val migrationFile = fileOpener.openResourceFile(filename)
for (line in migrationFile.readLines()) {
if (line.isEmpty()) continue
execute(line)
runInBackground(line)
}
setVersion(v)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.isoron.uhabits.utils

/**
* Represents a file that was shipped with the application, such as migration
* files or translations. These files cannot be deleted.
*/
interface ResourceFile {
fun readLines(): List<String>
}

/**
* Represents a file that was created after the application was installed, as a
* result of some user action, such as databases and logs. These files can be
* deleted.
*/
interface UserFile {
fun delete()
fun exists(): Boolean
}
package org.isoron.platform.io

interface FileOpener {
/**
Expand All @@ -58,3 +40,22 @@ interface FileOpener {
*/
fun openUserFile(filename: String): UserFile
}

/**
* Represents a file that was created after the application was installed, as a
* result of some user action, such as databases and logs. These files can be
* deleted.
*/
interface UserFile {
fun delete()
fun exists(): Boolean
}

/**
* Represents a file that was shipped with the application, such as migration
* files or translations. These files cannot be deleted.
*/
interface ResourceFile {
fun readLines(): List<String>
fun copyTo(dest: UserFile)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.isoron.uhabits.utils
package org.isoron.platform.io

interface Log {
fun info(tag: String, msg: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.isoron.uhabits.utils
package org.isoron.platform.io

expect fun sprintf(format: String, vararg args: Any?): String
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,37 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.isoron.uhabits.utils
package org.isoron.platform.time

data class Timestamp(val unixTime: Long)
import kotlin.math.*

enum class DayOfWeek(val index: Int) {
SUNDAY(0),
MONDAY(1),
TUESDAY(2),
WEDNESDAY(3),
THURSDAY(4),
FRIDAY(5),
SATURDAY(6),
}

data class Timestamp(val unixTimeInMillis: Long)

data class LocalDate(val year: Int,
val month: Int,
val day: Int) {

fun isOlderThan(other: LocalDate): Boolean {
if (other.year != year) return other.year > year
if (other.month != month) return other.month > month
return other.day > day
}

fun isNewerThan(other: LocalDate): Boolean {
if (this == other) return false
return other.isOlderThan(this)
}

init {
if ((month <= 0) or (month >= 13)) throw(IllegalArgumentException())
if ((day <= 0) or (day >= 32)) throw(IllegalArgumentException())
Expand All @@ -32,11 +56,23 @@ data class LocalDate(val year: Int,

interface LocalDateCalculator {
fun plusDays(date: LocalDate, days: Int): LocalDate
fun minusDays(date: LocalDate, days: Int): LocalDate {
return plusDays(date, -days)
}
fun dayOfWeek(date: LocalDate): DayOfWeek
fun toTimestamp(date: LocalDate): Timestamp
fun fromTimestamp(timestamp: Timestamp): LocalDate
}

fun LocalDateCalculator.distanceInDays(d1: LocalDate, d2: LocalDate): Int {
val t1 = toTimestamp(d1)
val t2 = toTimestamp(d2)
val dayLength = 24 * 60 * 60 * 1000
return abs((t2.unixTimeInMillis - t1.unixTimeInMillis) / dayLength).toInt()
}

fun LocalDateCalculator.minusDays(date: LocalDate, days: Int): LocalDate {
return plusDays(date, -days)
}

interface LocalDateFormatter {
fun shortWeekdayName(date: LocalDate): String
fun shortMonthName(date: LocalDate): String
}
Loading

0 comments on commit 70a7985

Please sign in to comment.