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

View File

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

View File

@@ -0,0 +1,131 @@
/*
* 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
import SQLite3
internal let SQLITE_STATIC = unsafeBitCast(0, to: sqlite3_destructor_type.self)
internal let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self)
class IosPreparedStatement : NSObject, PreparedStatement {
var db: OpaquePointer
var statement: OpaquePointer
init(withStatement statement: OpaquePointer, withDb db: OpaquePointer) {
self.statement = statement
self.db = db
}
func step() -> StepResult {
let result = sqlite3_step(statement)
if result == SQLITE_ROW {
return StepResult.row
} else if result == SQLITE_DONE {
return StepResult.done
} else {
let errMsg = String(cString: sqlite3_errmsg(db)!)
fatalError("Database error: \(errMsg) (\(result))")
}
}
func getInt(index: Int32) -> Int32 {
return sqlite3_column_int(statement, index)
}
func getText(index: Int32) -> String {
return String(cString: sqlite3_column_text(statement, index))
}
func getReal(index: Int32) -> Double {
return sqlite3_column_double(statement, index)
}
func bindInt(index: Int32, value: Int32) {
sqlite3_bind_int(statement, index + 1, value)
}
func bindText(index: Int32, value: String) {
sqlite3_bind_text(statement, index + 1, value, -1, SQLITE_TRANSIENT)
}
func bindReal(index: Int32, value: Double) {
sqlite3_bind_double(statement, index + 1, value)
}
func reset() {
sqlite3_reset(statement)
}
override func finalize() {
sqlite3_finalize(statement)
}
}
class IosDatabase : NSObject, Database {
var db: OpaquePointer
var log: Log
init(withDb db: OpaquePointer, withLog log: Log) {
self.db = db
self.log = log
}
func prepareStatement(sql: String) -> PreparedStatement {
if sql.isEmpty {
fatalError("Provided SQL query is empty")
}
log.debug(tag: "IosDatabase", msg: "Preparing: \(sql)")
var statement : OpaquePointer?
let result = sqlite3_prepare_v2(db, sql, -1, &statement, nil)
if result == SQLITE_OK {
return IosPreparedStatement(withStatement: statement!, withDb: db)
} else {
let errMsg = String(cString: sqlite3_errmsg(db)!)
fatalError("Database error: \(errMsg)")
}
}
func close() {
sqlite3_close(db)
}
}
class IosDatabaseOpener : NSObject, DatabaseOpener {
var log: Log
init(withLog log: Log) {
self.log = log
}
func open(file: UserFile) -> Database {
let dbPath = (file as! IosUserFile).path
let version = String(cString: sqlite3_libversion())
log.info(tag: "IosDatabaseOpener", msg: "SQLite \(version)")
log.info(tag: "IosDatabaseOpener", msg: "Opening database: \(dbPath)")
var db: OpaquePointer?
let result = sqlite3_open(dbPath, &db)
if result == SQLITE_OK {
return IosDatabase(withDb: db!, withLog: log)
} else {
fatalError("Error opening database (code \(result))")
}
}
}

View File

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

View File

@@ -0,0 +1,33 @@
/*
* 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
extension Color {
var uicolor: UIColor {
return UIColor(red: CGFloat(self.red),
green: CGFloat(self.green),
blue: CGFloat(self.blue),
alpha: 1.0)
}
var cgcolor : CGColor {
return uicolor.cgColor
}
}

View File

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