mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Remove react-native; rewrite main screen in (native) swift
This commit is contained in:
42
ios/Application/AppDelegate.swift
Normal file
42
ios/Application/AppDelegate.swift
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/>.
|
||||
*/
|
||||
|
||||
import UIKit
|
||||
|
||||
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
|
||||
var window: UIWindow?
|
||||
var backend = Backend(databaseOpener: IosDatabaseOpener(withLog: StandardLog()),
|
||||
fileOpener: IosFileOpener(),
|
||||
log: StandardLog())
|
||||
|
||||
func application(_ application: UIApplication,
|
||||
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
||||
|
||||
window = UIWindow(frame: UIScreen.main.bounds)
|
||||
if let window = window {
|
||||
let nav = UINavigationController()
|
||||
nav.viewControllers = [ListHabitsController(withBackend: backend)]
|
||||
window.backgroundColor = UIColor.white
|
||||
window.rootViewController = nav
|
||||
window.makeKeyAndVisible()
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "20x20",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "20x20",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "29x29",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "29x29",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "40x40",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "40x40",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "60x60",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "60x60",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "20x20",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "20x20",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "29x29",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "29x29",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "40x40",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "40x40",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "76x76",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "76x76",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "83.5x83.5",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ios-marketing",
|
||||
"size" : "1024x1024",
|
||||
"scale" : "1x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
6
ios/Application/Assets.xcassets/Contents.json
Normal file
6
ios/Application/Assets.xcassets/Contents.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
20
ios/Application/BridgingHeader.h
Normal file
20
ios/Application/BridgingHeader.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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 <core.h>
|
||||
87
ios/Application/EditHabitController.swift
Normal file
87
ios/Application/EditHabitController.swift
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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 EditHabitTableViewController: NSObject, UITableViewDataSource, UITableViewDelegate {
|
||||
func disclosure(title: String, subtitle: String) -> UITableViewCell {
|
||||
let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
|
||||
cell.textLabel?.text = title
|
||||
cell.detailTextLabel?.text = subtitle
|
||||
cell.accessoryType = .disclosureIndicator
|
||||
return cell
|
||||
}
|
||||
|
||||
func input(title: String) -> UITableViewCell {
|
||||
let cell = UITableViewCell()
|
||||
let field = UITextField(frame: cell.bounds.insetBy(dx: 20, dy: 0))
|
||||
field.placeholder = title
|
||||
field.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth,
|
||||
UIView.AutoresizingMask.flexibleHeight]
|
||||
cell.contentView.addSubview(field)
|
||||
return cell
|
||||
}
|
||||
|
||||
var primary = [UITableViewCell]()
|
||||
var secondary = [UITableViewCell]()
|
||||
var parentController: EditHabitController
|
||||
|
||||
init(withParentController parentController: EditHabitController) {
|
||||
self.parentController = parentController
|
||||
super.init()
|
||||
primary.append(input(title: "Name"))
|
||||
primary.append(input(title: "Question (e.g. Did you wake up early today?)"))
|
||||
secondary.append(disclosure(title: "Color", subtitle: "Blue"))
|
||||
secondary.append(disclosure(title: "Repeat", subtitle: "Daily"))
|
||||
secondary.append(disclosure(title: "Reminder", subtitle: "Disabled"))
|
||||
}
|
||||
|
||||
func numberOfSections(in tableView: UITableView) -> Int {
|
||||
return 2
|
||||
}
|
||||
|
||||
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
return section == 0 ? primary.count : secondary.count
|
||||
}
|
||||
|
||||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||
return indexPath.section == 0 ? primary[indexPath.item] : secondary[indexPath.item]
|
||||
}
|
||||
|
||||
// func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||
// let alert = UIAlertController(title: "Hello", message: "You selected something", preferredStyle: .alert)
|
||||
// parentController.present(alert, animated: true)
|
||||
// }
|
||||
}
|
||||
|
||||
class EditHabitController: UIViewController {
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
let bounds = UIScreen.main.bounds
|
||||
let tableController = EditHabitTableViewController(withParentController: self)
|
||||
let table = UITableView(frame: bounds, style: .grouped)
|
||||
table.dataSource = tableController
|
||||
table.delegate = tableController
|
||||
self.view = table
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
self.title = "Edit Habit"
|
||||
}
|
||||
}
|
||||
47
ios/Application/Info.plist
Normal file
47
ios/Application/Info.plist
Normal file
@@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>UIAppFonts</key>
|
||||
<array>
|
||||
<string>fonts/FontAwesome.ttf</string>
|
||||
</array>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
<true/>
|
||||
<key>UIRequiredDeviceCapabilities</key>
|
||||
<array>
|
||||
<string>armv7</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
<key>UILaunchStoryboardName</key>
|
||||
<string>Launch.storyboard</string>
|
||||
<key>UISupportedInterfaceOrientations~ipad</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationPortraitUpsideDown</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
7
ios/Application/Launch.storyboard
Normal file
7
ios/Application/Launch.storyboard
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13142" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12042"/>
|
||||
</dependencies>
|
||||
<scenes/>
|
||||
</document>
|
||||
151
ios/Application/ListHabitsController.swift
Normal file
151
ios/Application/ListHabitsController.swift
Normal file
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
141
ios/Application/Platform/IosCanvas.swift
Normal file
141
ios/Application/Platform/IosCanvas.swift
Normal 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))
|
||||
}
|
||||
}
|
||||
131
ios/Application/Platform/IosDatabase.swift
Normal file
131
ios/Application/Platform/IosDatabase.swift
Normal 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))")
|
||||
}
|
||||
}
|
||||
}
|
||||
57
ios/Application/Platform/IosDates.swift
Normal file
57
ios/Application/Platform/IosDates.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
33
ios/Application/Platform/IosExtensions.swift
Normal file
33
ios/Application/Platform/IosExtensions.swift
Normal 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
|
||||
}
|
||||
}
|
||||
79
ios/Application/Platform/IosFiles.swift
Normal file
79
ios/Application/Platform/IosFiles.swift
Normal 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")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user