Commit 0611b540 authored by Dave Griffiths's avatar Dave Griffiths
Browse files

big refactor started

parent 260db9ab
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="foam.symbai" package="foam.symbai"
android:versionCode="4" android:versionCode="7"
android:versionName="1.0"> android:versionName="1.0">
<application android:label="@string/app_name" <application android:label="@string/app_name"
android:icon="@drawable/logo" android:icon="@drawable/logo"
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
<activity android:name="foam.symbai.CropActivity" android:configChanges="orientation"></activity> <activity android:name="foam.symbai.CropActivity" android:configChanges="orientation"></activity>
<activity android:name="foam.symbai.ChildActivity" android:configChanges="orientation"></activity> <activity android:name="foam.symbai.ChildActivity" android:configChanges="orientation"></activity>
<activity android:name="foam.symbai.MoveActivity" android:configChanges="orientation"></activity> <activity android:name="foam.symbai.MoveActivity" android:configChanges="orientation"></activity>
<activity android:name="foam.symbai.VillagesActivity" android:configChanges="orientation"></activity>
</application> </application>
......
...@@ -81,7 +81,7 @@ ...@@ -81,7 +81,7 @@
'entity-values 'entity-values
(ktv-set (ktv-set
(get-current 'entity-values '()) (get-current 'entity-values '())
(ktv-create key type value)))) (ktv key type value))))
(define (entity-set! ktv-list) (define (entity-set! ktv-list)
(set-current! 'entity-values ktv-list)) (set-current! 'entity-values ktv-list))
...@@ -128,11 +128,11 @@ ...@@ -128,11 +128,11 @@
(let ((values (let ((values
(append (append
(list (list
(ktv-create "user" "varchar" (get-current 'user-id "none")) (ktv "user" "varchar" (get-current 'user-id "none"))
(ktv-create "time" "varchar" (date-time->string (date-time))) (ktv "time" "varchar" (date-time->string (date-time)))
(ktv-create "lat" "real" (car (get-current 'location '(0 0)))) (ktv "lat" "real" (car (get-current 'location '(0 0))))
(ktv-create "lon" "real" (cadr (get-current 'location '(0 0)))) (ktv "lon" "real" (cadr (get-current 'location '(0 0))))
(ktv-create "deleted" "int" 0)) (ktv "deleted" "int" 0))
ktv-list))) ktv-list)))
(let ((r (insert-entity/get-unique (let ((r (insert-entity/get-unique
db table entity-type (get-current 'user-id "no id") db table entity-type (get-current 'user-id "no id")
...@@ -176,7 +176,7 @@ ...@@ -176,7 +176,7 @@
(define url "http://192.168.2.1:8889/symbai?") (define url "http://192.168.2.1:8889/symbai?")
(define (build-url-from-ktv ktv) (define (build-url-from-ktv ktv)
(string-append "&" (ktv-key ktv) ":" (ktv-type ktv) ":" (number->string (ktv-version ktv)) "=" (stringify-value-url ktv))) (string-append "&" (ktv-key ktv) ":" (ktv-type ktv) "=" (stringify-value-url ktv)))
(define (build-url-from-ktvlist ktvlist) (define (build-url-from-ktvlist ktvlist)
(foldl (foldl
...@@ -282,8 +282,8 @@ ...@@ -282,8 +282,8 @@
(ktvlist (list-ref data 1)) (ktvlist (list-ref data 1))
(unique-id (list-ref entity 1)) (unique-id (list-ref entity 1))
(exists (entity-exists? db table unique-id))) (exists (entity-exists? db table unique-id)))
;;(msg "from server...:") (msg "from server...:")
;;(msg ktvlist) (msg ktvlist)
;; need to check exists again here, due to delays back and forth ;; need to check exists again here, due to delays back and forth
(if (not exists) (if (not exists)
(insert-entity-wholesale (insert-entity-wholesale
...@@ -702,12 +702,14 @@ ...@@ -702,12 +702,14 @@
;; pull db data into list of button widgets ;; pull db data into list of button widgets
(define (update-list-widget db table entity-type edit-activity parent) (define (update-list-widget db table entity-type edit-activity parent)
(msg "ulw")
(let ((search-results (let ((search-results
(if parent (if parent
(db-filter-only db table entity-type (db-filter-only db table entity-type
(list (list "parent" "varchar" "=" parent)) (list (list "parent" "varchar" "=" parent))
(list (list "name" "varchar"))) (list (list "name" "varchar")))
(db-all db table entity-type)))) (db-all db table entity-type))))
(msg "ulw search results " search-results)
(update-widget (update-widget
'linear-layout 'linear-layout
(get-id (string-append entity-type "-list")) (get-id (string-append entity-type "-list"))
...@@ -750,6 +752,13 @@ ...@@ -750,6 +752,13 @@
(list) (list)
(list (list "name" "varchar"))))) (list (list "name" "varchar")))))
(define (find-index-from-name-array arr unique-id)
(define (_ l i)
(cond
((null? l) #f)
((equal? unique-id (cadr (car l))) i)
(else (_ (cdr l) (+ i 1)))))
(_ arr 0))
(define vowel (map symbol->string (list 'a 'e 'i 'o 'u))) (define vowel (map symbol->string (list 'a 'e 'i 'o 'u)))
(define consonant (map symbol->string (list 'b 'c 'd 'f 'g 'h 'j 'k 'l 'm 'n 'p 'q 'r 's 't 'v 'w 'x 'y 'z))) (define consonant (map symbol->string (list 'b 'c 'd 'f 'g 'h 'j 'k 'l 'm 'n 'p 'q 'r 's 't 'v 'w 'x 'y 'z)))
...@@ -795,130 +804,130 @@ ...@@ -795,130 +804,130 @@
(choose (choose
(list (list
(list (list
(ktv-create "name" "varchar" (ktv "name" "varchar"
(string-append "Abe-" (number->string n))) (string-append "Abe-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "abe.jpg")) (ktv "photo" "file" "abe.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Akira-" (number->string n))) "name" "varchar" (string-append "Akira-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "akira.jpg")) (ktv "photo" "file" "akira.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Apu-" (number->string n))) "name" "varchar" (string-append "Apu-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "apu.jpg")) (ktv "photo" "file" "apu.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Barney-" (number->string n))) "name" "varchar" (string-append "Barney-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "barney.jpg")) (ktv "photo" "file" "barney.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Bart-" (number->string n))) "name" "varchar" (string-append "Bart-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "bartsimpson.jpg")) (ktv "photo" "file" "bartsimpson.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Billy-" (number->string n))) "name" "varchar" (string-append "Billy-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "billy.jpg")) (ktv "photo" "file" "billy.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Carl-" (number->string n))) "name" "varchar" (string-append "Carl-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "carl.jpg")) (ktv "photo" "file" "carl.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Cletus-" (number->string n))) "name" "varchar" (string-append "Cletus-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "cletus.jpg")) (ktv "photo" "file" "cletus.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "ComicBookGuy-" (number->string n))) "name" "varchar" (string-append "ComicBookGuy-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "comicbookguy.jpg")) (ktv "photo" "file" "comicbookguy.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Homer-" (number->string n))) "name" "varchar" (string-append "Homer-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "homersimpson.jpg")) (ktv "photo" "file" "homersimpson.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Jasper-" (number->string n))) "name" "varchar" (string-append "Jasper-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "jasper.jpg")) (ktv "photo" "file" "jasper.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Kent-" (number->string n))) "name" "varchar" (string-append "Kent-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "kentbrockman.jpg")) (ktv "photo" "file" "kentbrockman.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Kodos-" (number->string n))) "name" "varchar" (string-append "Kodos-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "kodos.jpg")) (ktv "photo" "file" "kodos.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Lenny-" (number->string n))) "name" "varchar" (string-append "Lenny-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "lenny.jpg")) (ktv "photo" "file" "lenny.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Lisa-" (number->string n))) "name" "varchar" (string-append "Lisa-" (number->string n)))
(ktv-create "gender" "varchar" "female") (ktv "gender" "varchar" "female")
(ktv-create "photo" "file" "lisasimpson.jpg")) (ktv "photo" "file" "lisasimpson.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Marge-" (number->string n))) "name" "varchar" (string-append "Marge-" (number->string n)))
(ktv-create "gender" "varchar" "female") (ktv "gender" "varchar" "female")
(ktv-create "photo" "file" "margesimpson.jpg")) (ktv "photo" "file" "margesimpson.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Martin-" (number->string n))) "name" "varchar" (string-append "Martin-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "martinprince.jpg")) (ktv "photo" "file" "martinprince.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Milhouse-" (number->string n))) "name" "varchar" (string-append "Milhouse-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "milhouse.jpg")) (ktv "photo" "file" "milhouse.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "MrBurns-" (number->string n))) "name" "varchar" (string-append "MrBurns-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "mrburns.jpg")) (ktv "photo" "file" "mrburns.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Ned-" (number->string n))) "name" "varchar" (string-append "Ned-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "nedflanders.jpg")) (ktv "photo" "file" "nedflanders.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Nelson-" (number->string n))) "name" "varchar" (string-append "Nelson-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "nelson.jpg")) (ktv "photo" "file" "nelson.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Otto-" (number->string n))) "name" "varchar" (string-append "Otto-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "otto.jpg")) (ktv "photo" "file" "otto.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Ralph-" (number->string n))) "name" "varchar" (string-append "Ralph-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "ralphwiggum.jpg")) (ktv "photo" "file" "ralphwiggum.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "Santaslittlehelper-" (number->string n))) "name" "varchar" (string-append "Santaslittlehelper-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "santaslittlehelper.jpg")) (ktv "photo" "file" "santaslittlehelper.jpg"))
(list (list
(ktv-create (ktv
"name" "varchar" (string-append "SideshowBob-" (number->string n))) "name" "varchar" (string-append "SideshowBob-" (number->string n)))
(ktv-create "gender" "varchar" "male") (ktv "gender" "varchar" "male")
(ktv-create "photo" "file" "sideshowbob.jpg"))))))))) (ktv "photo" "file" "sideshowbob.jpg")))))))))
(define (looper! n fn) (define (looper! n fn)
(when (not (zero? n)) (when (not (zero? n))
......
...@@ -202,6 +202,8 @@ ...@@ -202,6 +202,8 @@
;; don't make dirty or update version here ;; don't make dirty or update version here
(define (update-value-from-sync db table entity-id ktv) (define (update-value-from-sync db table entity-id ktv)
;;(msg "update-value-from-sync")
;;(msg entity-id ktv)
(let ((s (select-first (let ((s (select-first
db (string-append db (string-append
"select value from " table "_value_" (ktv-type ktv) " where entity_id = ? and attribute_id = ?") "select value from " table "_value_" (ktv-type ktv) " where entity_id = ? and attribute_id = ?")
...@@ -534,6 +536,7 @@ ...@@ -534,6 +536,7 @@
ktvlist) ktvlist)
(for-each (for-each
(lambda (ktv) (lambda (ktv)
;;(msg ktv)
(when (not (equal? (ktv-key ktv) "unique_id")) (when (not (equal? (ktv-key ktv) "unique_id"))
(if dirty (if dirty
(update-value db table entity-id ktv) (update-value db table entity-id ktv)
...@@ -613,12 +616,12 @@ ...@@ -613,12 +616,12 @@
db (string-append db (string-append
"select entity_id, entity_type, unique_id, dirty, version from " "select entity_id, entity_type, unique_id, dirty, version from "
table "_entity where dirty=1 limit 5;")))) table "_entity where dirty=1 limit 5;"))))
(msg de) ;;(msg de)
(if (null? de) (if (null? de)
'() '()
(map (map
(lambda (i) (lambda (i)
(msg "dirty:" (vector-ref i 2)) ;;(msg "dirty:" (vector-ref i 2))
(list (list
;; build according to url ([table] entity-type unique-id dirty version) ;; build according to url ([table] entity-type unique-id dirty version)
(cdr (vector->list i)) (cdr (vector->list i))
......
...@@ -569,7 +569,10 @@ ...@@ -569,7 +569,10 @@
; r))) ; r)))
(define (get-id name) (define (get-id name)
(id-map-get name)) (let ((r (id-map-get name)))
(cond
((zero? r) (msg "no id found for" name) 0)
(else r))))
(define (make-id name) (define (make-id name)
;;(msg "making id for" name) ;;(msg "making id for" name)
......
(define lang #f)
(define scheme #f)
(define racket #f)
(define (planet n) #f)
(define jaymccarthy/sqlite:5:1/sqlite #f)
(define (require . args) #f)
(define (provide . args) #f)
(define (all-defined-out) #f)
(define (make-semaphore n) #f)
(define (semaphore-wait n) #f)
;; tinyscheme
(define db-select db-exec)
;; helper to return first instance from a select
(define (select-first db str . args)
(let ((s (apply db-select (append (list db str) args))))
(if (or (null? s) (eq? s #t))
'()
(vector-ref (cadr s) 0))))
;; get a unique hash for this user (used for all the unique-ids)
(define (get-unique user)
(let ((t (time)))
(string-append
user "-" (number->string (car t)) ":" (number->string (cadr t)))))
...@@ -33,26 +33,30 @@ ...@@ -33,26 +33,30 @@
(setup db "sync") (setup db "sync")
(setup db "stream") (setup db "stream")
(define settings-entity-id-version 2)
(insert-entity-if-not-exists (insert-entity-if-not-exists
db "local" "app-settings" "null" 1 db "local" "app-settings" "null" settings-entity-id-version
(list (list
(ktv "user-id" "varchar" "not set") (ktv "user-id" "varchar" "not set")
(ktv "language" "int" 0) (ktv "language" "int" 0)
(ktv "house-id" "int" 0) (ktv "house-id" "int" 0)
(ktv "photo-id" "int" 0))) (ktv "photo-id" "int" 0)
(ktv "current-village" "varchar" "none")))
(define (get-setting-value name) (define (get-setting-value name)
(ktv-get (get-entity db "local" 1) name)) (ktv-get (get-entity db "local" settings-entity-id-version) name))
(define (set-setting! key type value) (define (set-setting! key type value)
(update-entity (update-entity
db "local" 1 (list (ktv key type value)))) db "local" settings-entity-id-version (list (ktv key type value))))
(define (get/inc-setting key) (define (get/inc-setting key)
(let ((r (get-setting-value key))) (let ((r (get-setting-value key)))
(set-setting! key "int" (+ r 1)) (set-setting! key "int" (+ r 1))
r)) r))
(set-current! 'user-id (get-setting-value "user-id"))
(set! i18n-lang (get-setting-value "language")) (set! i18n-lang (get-setting-value "language"))
(define entity-types (list "village" "household" "individual")) (define entity-types (list "village" "household" "individual"))
...@@ -75,78 +79,78 @@ ...@@ -75,78 +79,78 @@
(define village-ktvlist (define village-ktvlist
(list (list
(ktv-create "name" "varchar" (mtext-lookup 'default-village-name)) (ktv "name" "varchar" (mtext-lookup 'default-village-name))
(ktv-create "block" "varchar" "") (ktv "block" "varchar" "")
(ktv-create "district" "varchar" "test") (ktv "district" "varchar" "test")
(ktv-create "car" "int" 0))) (ktv "car" "int" 0)))
(define household-ktvlist (define household-ktvlist
(list (list
(ktv-create "name" "varchar" (mtext-lookup 'default-household-name)) (ktv "name" "varchar" (mtext-lookup 'default-household-name))
(ktv-create "num-pots" "int" 0) (ktv "num-pots" "int" 0)
(ktv-create "house-lat" "real" 0) ;; get from current location? (ktv "house-lat" "real" 0) ;; get from current location?
(ktv-create "house-lon" "real" 0) (ktv "house-lon" "real" 0)
(ktv-create "toilet-lat" "real" 0) (ktv "toilet-lat" "real" 0)
(ktv-create "toilet-lon" "real" 0))) (ktv "toilet-lon" "real" 0)))
(define individual-ktvlist (define individual-ktvlist
(list (list
(ktv-create "name" "varchar" (mtext-lookup 'default-individual-name)) (ktv "name" "varchar" (mtext-lookup 'default-individual-name))
(ktv-create "family" "varchar" (mtext-lookup 'default-family-name)) (ktv "family" "varchar" (mtext-lookup 'default-family-name))
(ktv-create "photo-id" "varchar" (mtext-lookup 'default-photo-id)) (ktv "photo-id" "varchar" (mtext-lookup 'default-photo-id))
(ktv-create "photo" "file" "none") (ktv "photo" "file" "")
(ktv-create "tribe" "varchar" "none") (ktv "tribe" "varchar" "")
(ktv-create "subtribe" "varchar" "none") (ktv "subtribe" "varchar" "")
(ktv-create "child" "int" 0) (ktv "child" "int" 0)
(ktv-create "age" "int" 0) (ktv "age" "int" 0)
(ktv-create "gender" "varchar" "none") (ktv "gender" "varchar" "")
(ktv-create "education" "varchar" "none") (ktv "education" "varchar" "")
(ktv-create "head-of-house" "varchar" "none") (ktv "head-of-house" "varchar" "")
(ktv-create "marital-status" "varchar" "none") (ktv "marital-status" "varchar" "")
(ktv-create "times-married" "int" 0) (ktv "times-married" "int" 0)
(ktv-create "id-spouse" "varchar" "none") (ktv "id-spouse" "varchar" "")
(ktv-create "children-living" "int" 0) (ktv "children-living" "int" 0)
(ktv-create "children-dead" "int" 0) (ktv "children-dead" "int" 0)
(ktv-create "children-together" "int" 0) (ktv "children-together" "int" 0)
(ktv-create "children-apart" "int" 0) (ktv "children-apart" "int" 0)
(ktv-create "residence-after-marriage" "varchar" "none") (ktv "residence-after-marriage" "varchar" "")
(ktv-create "num-siblings" "int" 0) (ktv "num-siblings" "int" 0)
(ktv-create "birth-order" "int" 0) (ktv "birth-order" "int" 0)
(ktv-create "length-time" "int" 0) (ktv "length-time" "int" 0)
(ktv-create "place-of-birth" "varchar" "none") (ktv "place-of-birth" "varchar" "")
(ktv-create "num-residence-changes" "int" 0) (ktv "num-residence-changes" "int" 0)
(ktv-create "village-visits-month" "int" 0) (ktv "village-visits-month" "int" 0)
(ktv-create "village-visits-year" "int" 0) (ktv "village-visits-year" "int" 0)
(ktv-create "occupation" "varchar" "none") (ktv "occupation" "varchar" "")