mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 01:28:52 -06:00
Move IosFiles implementation to Kotlin; setup tests for ios target
This commit is contained in:
@@ -26,34 +26,31 @@ import org.isoron.uhabits.components.*
|
||||
import org.isoron.uhabits.i18n.*
|
||||
import org.isoron.uhabits.models.*
|
||||
|
||||
//class Backend(databaseName: String,
|
||||
// databaseOpener: DatabaseOpener,
|
||||
// fileOpener: FileOpener,
|
||||
// localeHelper: LocaleHelper,
|
||||
// val log: Log,
|
||||
// val taskRunner: TaskRunner) {
|
||||
class Backend(databaseName: String,
|
||||
databaseOpener: DatabaseOpener,
|
||||
fileOpener: FileOpener,
|
||||
localeHelper: LocaleHelper,
|
||||
private val log: Log,
|
||||
private val taskRunner: TaskRunner) {
|
||||
|
||||
// private val database: Database
|
||||
//
|
||||
// val database: Database
|
||||
// private val habitsRepository: HabitRepository
|
||||
//
|
||||
// val habitsRepository: HabitRepository
|
||||
// private val checkmarkRepository: CheckmarkRepository
|
||||
//
|
||||
// val checkmarkRepository: CheckmarkRepository
|
||||
// private val habits = mutableMapOf<Int, Habit>()
|
||||
//
|
||||
// val habits = mutableMapOf<Int, Habit>()
|
||||
// private val checkmarks = mutableMapOf<Habit, CheckmarkList>()
|
||||
//
|
||||
// val checkmarks = mutableMapOf<Habit, CheckmarkList>()
|
||||
//
|
||||
// val scores = mutableMapOf<Habit, ScoreList>()
|
||||
//
|
||||
// val mainScreenDataSource: MainScreenDataSource
|
||||
//
|
||||
// val strings = localeHelper.getStringsForCurrentLocale()
|
||||
//
|
||||
// val preferences: Preferences
|
||||
//
|
||||
// var theme: Theme = LightTheme()
|
||||
//
|
||||
// init {
|
||||
// private val scores = mutableMapOf<Habit, ScoreList>()
|
||||
|
||||
val mainScreenDataSource: MainScreenDataSource? = null
|
||||
val strings = localeHelper.getStringsForCurrentLocale()
|
||||
val preferences: Preferences? = null
|
||||
var theme: Theme = LightTheme()
|
||||
|
||||
init {
|
||||
// val dbFile = fileOpener.openUserFile(databaseName)
|
||||
// if (!dbFile.exists()) {
|
||||
// val templateFile = fileOpener.openResourceFile("databases/template.db")
|
||||
@@ -78,9 +75,9 @@ import org.isoron.uhabits.models.*
|
||||
// checkmarks,
|
||||
// scores,
|
||||
// taskRunner)
|
||||
// }
|
||||
//
|
||||
// fun createHabit(habit: Habit) {
|
||||
}
|
||||
|
||||
fun createHabit(habit: Habit) {
|
||||
// val id = habitsRepository.nextId()
|
||||
// habit.id = id
|
||||
// habit.position = habits.size
|
||||
@@ -88,20 +85,20 @@ import org.isoron.uhabits.models.*
|
||||
// checkmarks[habit] = CheckmarkList(habit.frequency, habit.type)
|
||||
// habitsRepository.insert(habit)
|
||||
// mainScreenDataSource.requestData()
|
||||
// }
|
||||
//
|
||||
// fun deleteHabit(id: Int) {
|
||||
}
|
||||
|
||||
fun deleteHabit(id: Int) {
|
||||
// habits[id]?.let { habit ->
|
||||
// habitsRepository.delete(habit)
|
||||
// habits.remove(id)
|
||||
// mainScreenDataSource.requestData()
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fun updateHabit(modified: Habit) {
|
||||
}
|
||||
|
||||
fun updateHabit(modified: Habit) {
|
||||
// habits[modified.id]?.let { existing ->
|
||||
// modified.position = existing.position
|
||||
// habitsRepository.update(modified)
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,11 +26,16 @@ class FilesTest() : BaseTest() {
|
||||
suspend fun testLines() {
|
||||
val fileOpener = resolver.getFileOpener()
|
||||
|
||||
assertFalse(fileOpener.openUserFile("non-existing.txt").exists())
|
||||
assertFalse(fileOpener.openResourceFile("non-existing.txt").exists())
|
||||
assertFalse(fileOpener.openUserFile("non-existing-usr.txt").exists(),
|
||||
"non-existing-usr.txt shouldn't exist")
|
||||
|
||||
assertFalse(fileOpener.openResourceFile("non-existing-res.txt").exists(),
|
||||
"non-existing-res.txt shouldn't exist")
|
||||
|
||||
val hello = fileOpener.openResourceFile("hello.txt")
|
||||
assertTrue(hello.exists(), "hello.txt should exist")
|
||||
var lines = hello.lines()
|
||||
assertEquals(2, lines.size)
|
||||
assertEquals("Hello World!", lines[0])
|
||||
assertEquals("This is a resource.", lines[1])
|
||||
|
||||
@@ -40,13 +45,13 @@ class FilesTest() : BaseTest() {
|
||||
assertEquals("Hello World!", lines[0])
|
||||
assertEquals("This is a resource.", lines[1])
|
||||
|
||||
assertTrue(helloCopy.exists())
|
||||
assertTrue(helloCopy.exists(), "helloCopy should exist")
|
||||
helloCopy.delete()
|
||||
assertFalse(helloCopy.exists())
|
||||
assertFalse(helloCopy.exists(), "helloCopy shouldn't exist")
|
||||
|
||||
|
||||
val migration = fileOpener.openResourceFile("migrations/012.sql")
|
||||
assertTrue(migration.exists())
|
||||
assertTrue(migration.exists(), "migrations/012.sql should exist")
|
||||
lines = migration.lines()
|
||||
assertEquals("delete from Score", lines[0])
|
||||
}
|
||||
|
||||
59
core/src/iosMain/kotlin/org/isoron/platform/io/IosFiles.kt
Normal file
59
core/src/iosMain/kotlin/org/isoron/platform/io/IosFiles.kt
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
@file:Suppress("UNCHECKED_CAST")
|
||||
|
||||
package org.isoron.platform.io
|
||||
|
||||
import platform.Foundation.*
|
||||
|
||||
class IosFileOpener : FileOpener {
|
||||
override fun openResourceFile(path: String): ResourceFile {
|
||||
val resPath = NSBundle.mainBundle.resourcePath!!
|
||||
return IosFile("$resPath/$path")
|
||||
}
|
||||
|
||||
override fun openUserFile(path: String): UserFile {
|
||||
val manager = NSFileManager.defaultManager
|
||||
val basePath = manager.URLsForDirectory(NSDocumentDirectory,
|
||||
NSUserDomainMask) as List<NSURL>
|
||||
val filePath = basePath.first().URLByAppendingPathComponent(path)!!.path!!
|
||||
return IosFile(filePath)
|
||||
}
|
||||
}
|
||||
|
||||
class IosFile(val path: String) : UserFile, ResourceFile {
|
||||
override suspend fun delete() {
|
||||
NSFileManager.defaultManager.removeItemAtPath(path, null)
|
||||
}
|
||||
|
||||
override suspend fun exists(): Boolean {
|
||||
return NSFileManager.defaultManager.fileExistsAtPath(path)
|
||||
}
|
||||
|
||||
override suspend fun lines(): List<String> {
|
||||
if (!exists()) throw Exception("File not found: $path")
|
||||
val contents = NSString.stringWithContentsOfFile(path)
|
||||
return contents.toString().lines()
|
||||
}
|
||||
|
||||
override suspend fun copyTo(dest: UserFile) {
|
||||
NSFileManager.defaultManager.copyItemAtPath(path, (dest as IosFile).path, null)
|
||||
}
|
||||
}
|
||||
@@ -17,14 +17,34 @@
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
@file:Suppress("UNCHECKED_CAST")
|
||||
|
||||
package org.isoron
|
||||
|
||||
import org.isoron.platform.gui.*
|
||||
import org.isoron.platform.io.*
|
||||
import platform.CoreGraphics.*
|
||||
import platform.Foundation.*
|
||||
import platform.UIKit.*
|
||||
|
||||
actual class DependencyResolver {
|
||||
actual fun getFileOpener(): FileOpener = TODO()
|
||||
actual fun getDatabase(): Database = TODO()
|
||||
actual fun createCanvas(width: Int, height: Int): Canvas = TODO()
|
||||
actual fun exportCanvas(canvas: Canvas, filename: String): Unit = TODO()
|
||||
actual suspend fun getFileOpener(): FileOpener {
|
||||
return IosFileOpener()
|
||||
}
|
||||
|
||||
actual suspend fun getDatabase(): Database = TODO()
|
||||
|
||||
actual fun createCanvas(width: Int, height: Int): Canvas {
|
||||
UIGraphicsBeginImageContext(CGSizeMake(width=500.0, height=600.0))
|
||||
return IosCanvas()
|
||||
}
|
||||
|
||||
actual fun exportCanvas(canvas: Canvas, filename: String): Unit {
|
||||
val image = UIGraphicsGetImageFromCurrentImageContext()!!
|
||||
val manager = NSFileManager.defaultManager
|
||||
val paths = manager.URLsForDirectory(NSDocumentDirectory, NSUserDomainMask) as List<NSURL>
|
||||
val filePath = paths.first().URLByAppendingPathComponent("IosCanvasTest.png")!!.path!!
|
||||
val data = UIImagePNGRepresentation(image)!!
|
||||
data.writeToFile(filePath, false)
|
||||
}
|
||||
}
|
||||
42
core/src/iosTest/kotlin/org/isoron/IosAsyncTests.kt
Normal file
42
core/src/iosTest/kotlin/org/isoron/IosAsyncTests.kt
Normal 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
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
import org.isoron.platform.io.*
|
||||
import org.isoron.uhabits.models.*
|
||||
import kotlin.test.*
|
||||
|
||||
class IosAsyncTests {
|
||||
@Test
|
||||
fun testFiles() = runBlocking { FilesTest().testLines() }
|
||||
|
||||
// @Test
|
||||
// fun testDatabase() = runBlocking { DatabaseTest().testUsage() }
|
||||
//
|
||||
// @Test
|
||||
// fun testCheckmarkRepository() = runBlocking { CheckmarkRepositoryTest().testCRUD() }
|
||||
//
|
||||
// @Test
|
||||
// fun testHabitRepository() = runBlocking { HabitRepositoryTest().testCRUD() }
|
||||
//
|
||||
// @Test
|
||||
// fun testPreferencesRepository() = runBlocking { PreferencesRepositoryTest().testUsage() }
|
||||
}
|
||||
Reference in New Issue
Block a user