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

big refactor started

parent 260db9ab
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
<application android:label="@string/app_name"
......@@ -33,6 +33,7 @@
<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.MoveActivity" android:configChanges="orientation"></activity>
<activity android:name="foam.symbai.VillagesActivity" android:configChanges="orientation"></activity>
......@@ -81,7 +81,7 @@
(get-current 'entity-values '())
(ktv-create key type value))))
(ktv key type value))))
(define (entity-set! ktv-list)
(set-current! 'entity-values ktv-list))
......@@ -128,11 +128,11 @@
(let ((values
(ktv-create "user" "varchar" (get-current 'user-id "none"))
(ktv-create "time" "varchar" (date-time->string (date-time)))
(ktv-create "lat" "real" (car (get-current 'location '(0 0))))
(ktv-create "lon" "real" (cadr (get-current 'location '(0 0))))
(ktv-create "deleted" "int" 0))
(ktv "user" "varchar" (get-current 'user-id "none"))
(ktv "time" "varchar" (date-time->string (date-time)))
(ktv "lat" "real" (car (get-current 'location '(0 0))))
(ktv "lon" "real" (cadr (get-current 'location '(0 0))))
(ktv "deleted" "int" 0))
(let ((r (insert-entity/get-unique
db table entity-type (get-current 'user-id "no id")
......@@ -176,7 +176,7 @@
(define url "")
(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)
......@@ -282,8 +282,8 @@
(ktvlist (list-ref data 1))
(unique-id (list-ref entity 1))
(exists (entity-exists? db table unique-id)))
;;(msg "from server...:")
;;(msg ktvlist)
(msg "from server...:")
(msg ktvlist)
;; need to check exists again here, due to delays back and forth
(if (not exists)
......@@ -702,12 +702,14 @@
;; pull db data into list of button widgets
(define (update-list-widget db table entity-type edit-activity parent)
(msg "ulw")
(let ((search-results
(if parent
(db-filter-only db table entity-type
(list (list "parent" "varchar" "=" parent))
(list (list "name" "varchar")))
(db-all db table entity-type))))
(msg "ulw search results " search-results)
(get-id (string-append entity-type "-list"))
......@@ -750,6 +752,13 @@
(list (list "name" "varchar")))))
(define (find-index-from-name-array arr unique-id)
(define (_ l i)
((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 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 @@
(ktv-create "name" "varchar"
(ktv "name" "varchar"
(string-append "Abe-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "abe.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "abe.jpg"))
"name" "varchar" (string-append "Akira-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "akira.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "akira.jpg"))
"name" "varchar" (string-append "Apu-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "apu.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "apu.jpg"))
"name" "varchar" (string-append "Barney-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "barney.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "barney.jpg"))
"name" "varchar" (string-append "Bart-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "bartsimpson.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "bartsimpson.jpg"))
"name" "varchar" (string-append "Billy-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "billy.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "billy.jpg"))
"name" "varchar" (string-append "Carl-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "carl.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "carl.jpg"))
"name" "varchar" (string-append "Cletus-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "cletus.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "cletus.jpg"))
"name" "varchar" (string-append "ComicBookGuy-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "comicbookguy.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "comicbookguy.jpg"))
"name" "varchar" (string-append "Homer-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "homersimpson.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "homersimpson.jpg"))
"name" "varchar" (string-append "Jasper-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "jasper.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "jasper.jpg"))
"name" "varchar" (string-append "Kent-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "kentbrockman.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "kentbrockman.jpg"))
"name" "varchar" (string-append "Kodos-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "kodos.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "kodos.jpg"))
"name" "varchar" (string-append "Lenny-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "lenny.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "lenny.jpg"))
"name" "varchar" (string-append "Lisa-" (number->string n)))
(ktv-create "gender" "varchar" "female")
(ktv-create "photo" "file" "lisasimpson.jpg"))
(ktv "gender" "varchar" "female")
(ktv "photo" "file" "lisasimpson.jpg"))
"name" "varchar" (string-append "Marge-" (number->string n)))
(ktv-create "gender" "varchar" "female")
(ktv-create "photo" "file" "margesimpson.jpg"))
(ktv "gender" "varchar" "female")
(ktv "photo" "file" "margesimpson.jpg"))
"name" "varchar" (string-append "Martin-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "martinprince.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "martinprince.jpg"))
"name" "varchar" (string-append "Milhouse-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "milhouse.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "milhouse.jpg"))
"name" "varchar" (string-append "MrBurns-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "mrburns.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "mrburns.jpg"))
"name" "varchar" (string-append "Ned-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "nedflanders.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "nedflanders.jpg"))
"name" "varchar" (string-append "Nelson-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "nelson.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "nelson.jpg"))
"name" "varchar" (string-append "Otto-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "otto.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "otto.jpg"))
"name" "varchar" (string-append "Ralph-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "ralphwiggum.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "ralphwiggum.jpg"))
"name" "varchar" (string-append "Santaslittlehelper-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "santaslittlehelper.jpg"))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "santaslittlehelper.jpg"))
"name" "varchar" (string-append "SideshowBob-" (number->string n)))
(ktv-create "gender" "varchar" "male")
(ktv-create "photo" "file" "sideshowbob.jpg")))))))))
(ktv "gender" "varchar" "male")
(ktv "photo" "file" "sideshowbob.jpg")))))))))
(define (looper! n fn)
(when (not (zero? n))
......@@ -202,6 +202,8 @@
;; don't make dirty or update version here
(define (update-value-from-sync db table entity-id ktv)
;;(msg "update-value-from-sync")
;;(msg entity-id ktv)
(let ((s (select-first
db (string-append
"select value from " table "_value_" (ktv-type ktv) " where entity_id = ? and attribute_id = ?")
......@@ -534,6 +536,7 @@
(lambda (ktv)
;;(msg ktv)
(when (not (equal? (ktv-key ktv) "unique_id"))
(if dirty
(update-value db table entity-id ktv)
......@@ -613,12 +616,12 @@
db (string-append
"select entity_id, entity_type, unique_id, dirty, version from "
table "_entity where dirty=1 limit 5;"))))
(msg de)
;;(msg de)
(if (null? de)
(lambda (i)
(msg "dirty:" (vector-ref i 2))
;;(msg "dirty:" (vector-ref i 2))
;; build according to url ([table] entity-type unique-id dirty version)
(cdr (vector->list i))
......@@ -569,7 +569,10 @@
; r)))
(define (get-id name)
(id-map-get name))
(let ((r (id-map-get name)))
((zero? r) (msg "no id found for" name) 0)
(else r))))
(define (make-id 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)))
user "-" (number->string (car t)) ":" (number->string (cadr t)))))
......@@ -33,26 +33,30 @@
(setup db "sync")
(setup db "stream")
(define settings-entity-id-version 2)
db "local" "app-settings" "null" 1
db "local" "app-settings" "null" settings-entity-id-version
(ktv "user-id" "varchar" "not set")
(ktv "language" "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)
(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)
db "local" 1 (list (ktv key type value))))
db "local" settings-entity-id-version (list (ktv key type value))))
(define (get/inc-setting key)
(let ((r (get-setting-value key)))
(set-setting! key "int" (+ r 1))
(set-current! 'user-id (get-setting-value "user-id"))
(set! i18n-lang (get-setting-value "language"))
(define entity-types (list "village" "household" "individual"))
......@@ -75,78 +79,78 @@
(define village-ktvlist
(ktv-create "name" "varchar" (mtext-lookup 'default-village-name))
(ktv-create "block" "varchar" "")
(ktv-create "district" "varchar" "test")
(ktv-create "car" "int" 0)))
(ktv "name" "varchar" (mtext-lookup 'default-village-name))
(ktv "block" "varchar" "")
(ktv "district" "varchar" "test")
(ktv "car" "int" 0)))
(define household-ktvlist
(ktv-create "name" "varchar" (mtext-lookup 'default-household-name))
(ktv-create "num-pots" "int" 0)
(ktv-create "house-lat" "real" 0) ;; get from current location?
(ktv-create "house-lon" "real" 0)
(ktv-create "toilet-lat" "real" 0)
(ktv-create "toilet-lon" "real" 0)))
(ktv "name" "varchar" (mtext-lookup 'default-household-name))
(ktv "num-pots" "int" 0)
(ktv "house-lat" "real" 0) ;; get from current location?
(ktv "house-lon" "real" 0)
(ktv "toilet-lat" "real" 0)
(ktv "toilet-lon" "real" 0)))
(define individual-ktvlist
(ktv-create "name" "varchar" (mtext-lookup 'default-individual-name))
(ktv-create "family" "varchar" (mtext-lookup 'default-family-name))
(ktv-create "photo-id" "varchar" (mtext-lookup 'default-photo-id))
(ktv-create "photo" "file" "none")
(ktv-create "tribe" "varchar" "none")
(ktv-create "subtribe" "varchar" "none")
(ktv-create "child" "int" 0)
(ktv-create "age" "int" 0)
(ktv-create "gender" "varchar" "none")
(ktv-create "education" "varchar" "none")
(ktv-create "head-of-house" "varchar" "none")
(ktv-create "marital-status" "varchar" "none")
(ktv-create "times-married" "int" 0)
(ktv-create "id-spouse" "varchar" "none")
(ktv-create "children-living" "int" 0)
(ktv-create "children-dead" "int" 0)
(ktv-create "children-together" "int" 0)
(ktv-create "children-apart" "int" 0)
(ktv-create "residence-after-marriage" "varchar" "none")
(ktv-create "num-siblings" "int" 0)
(ktv-create "birth-order" "int" 0)
(ktv-create "length-time" "int" 0)
(ktv-create "place-of-birth" "varchar" "none")
(ktv-create "num-residence-changes" "int" 0)
(ktv-create "village-visits-month" "int" 0)
(ktv-create "village-visits-year" "int" 0)
(ktv-create "occupation" "varchar" "none")
(ktv-create "contribute" "int" 0)
(ktv-create "own-land" "int" 0)
(ktv-create "rent-land" "int" 0)
(ktv-create "hire-land" "int" 0)
(ktv-create "house-type" "varchar" "none")
(ktv-create "loan" "int" 0)
(ktv-create "earning" "int" 0)
(ktv-create "radio" "int" 0)
(ktv-create "tv" "int" 0)
(ktv-create "mobile" "int" 0)
(ktv-create "visit-market" "int" 0)
(ktv-create "town-sell" "int" 0)
(ktv "name" "varchar" (mtext-lookup 'default-individual-name))
(ktv "family" "varchar" (mtext-lookup 'default-family-name))
(ktv "photo-id" "varchar" (mtext-lookup 'default-photo-id))
(ktv "photo" "file" "")
(ktv "tribe" "varchar" "")
(ktv "subtribe" "varchar" "")
(ktv "child" "int" 0)
(ktv "age" "int" 0)
(ktv "gender" "varchar" "")
(ktv "education" "varchar" "")
(ktv "head-of-house" "varchar" "")
(ktv "marital-status" "varchar" "")
(ktv "times-married" "int" 0)
(ktv "id-spouse" "varchar" "")
(ktv "children-living" "int" 0)
(ktv "children-dead" "int" 0)
(ktv "children-together" "int" 0)
(ktv "children-apart" "int" 0)
(ktv "residence-after-marriage" "varchar" "")
(ktv "num-siblings" "int" 0)
(ktv "birth-order" "int" 0)
(ktv "length-time" "int" 0)
(ktv "place-of-birth" "varchar" "")
(ktv "num-residence-changes" "int" 0)
(ktv "village-visits-month" "int" 0)
(ktv "village-visits-year" "int" 0)
(ktv "occupation" "varchar" "")
(ktv "contribute" "int" 0)
(ktv "own-land" "int" 0)
(ktv "rent-land" "int" 0)
(ktv "hire-land" "int" 0)
(ktv "house-type" "varchar" "")
(ktv "loan" "int" 0)
(ktv "earning" "int" 0)
(ktv "radio" "int" 0)
(ktv "tv" "int" 0)
(ktv "mobile" "int" 0)
(ktv "visit-market" "int" 0)
(ktv "town-sell" "int" 0)
(define crop-ktvlist
(ktv-create "name" "varchar" (mtext-lookup 'default-crop-name))
(ktv-create "unit" "varchar" "unit")
(ktv-create "used" "real" 0)
(ktv-create "sold" "real" 0)
(ktv-create "seed" "varchar" "none")))
(ktv "name" "varchar" (mtext-lookup 'default-crop-name))
(ktv "unit" "varchar" "unit")
(ktv "used" "real" 0)
(ktv "sold" "real" 0)
(ktv "seed" "varchar" "")))
(define child-ktvlist
(ktv-create "name" "varchar" (mtext-lookup 'default-child-name))
(ktv-create "alive" "int" 1)
(ktv-create "gender" "varchar" "none")
(ktv-create "age" "int" 0)
(ktv-create "living-at-home" "int" 0)))
(ktv "name" "varchar" (mtext-lookup 'default-child-name))
(ktv "alive" "int" 1)
(ktv "gender" "varchar" "")
(ktv "age" "int" 0)
(ktv "living-at-home" "int" 0)))
......@@ -223,10 +227,6 @@
;; fragments
(define (update-top-bar name photo-id)
(update-widget 'text-view (get-id "top-name") 'text name)
(update-widget 'text-view (get-id "top-photo-id") 'text photo-id)))
......@@ -235,28 +235,23 @@
(image-view 0 "face" (layout 48 64 -1 'centre 0))
(text-view (make-id "title") "" 30
(layout 'fill-parent 'fill-parent 0.25 'centre 10))
(layout 'fill-parent 'fill-parent 0.5 'centre 10))
0 'vertical
(layout 'fill-parent 'wrap-content 0.75 'centre 0)
(layout 'fill-parent 'wrap-content 0.5 'centre 0)
(list 0 0 0 0)
(text-view (make-id "top-name") 'name 20
(layout 'fill-parent 'wrap-content 1 'centre 0))
(text-view (make-id "top-village") 'name 20
(layout 'wrap-content 'wrap-content 1 'right 0))
(text-view (make-id "top-household") 'name 20
(layout 'wrap-content 'wrap-content 1 'right 0))
(text-view (make-id "top-photo-id") 'photo-id 20
(layout 'fill-parent 'wrap-content 1 'centre 0)))))
(layout 'wrap-content 'wrap-content 1 'right 0)))))
(lambda (fragment arg)
(activity-layout fragment))
(lambda (fragment arg)
(update-widget 'text-view (get-id "title") 'text
(get-current 'activity-title "Title not set"))
(update-widget 'text-view (get-id "top-name") 'text
(get-current 'activity-name "Name"))
(update-widget 'text-view (get-id "top-photo-id") 'text
(get-current 'activity-photo-id "Photo ID"))))
(lambda (fragment arg) '())
(lambda (fragment) '())
(lambda (fragment) '())
(lambda (fragment) '())
......@@ -297,6 +292,21 @@
(define (update-top-bar)
(let ((village (get-entity-name db "sync" (get-current 'village #f)))
(household (get-entity-name db "sync" (get-current 'household #f)))
(individual (get-entity-name db "sync" (get-current 'individual #f))))
(msg (get-current 'village "no village"))
(msg "top bar update--->" village household individual)
(update-widget 'text-view (get-id "title") 'text
(get-current 'activity-title "Title not set"))