starwisp.scm 37.9 KB
Newer Older
Dave Griffiths's avatar
Dave Griffiths committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
;; Starwisp Copyright (C) 2013 Dave Griffiths
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program 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 Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; strings


;; colours
21
(msg "starting up....")
Dave Griffiths's avatar
Dave Griffiths committed
22 23

(define trans-col (list 0 0 0 0))
24 25
(define colour-one (list 0 0 255 100))
(define colour-two (list  127 127 255 100))
Dave Griffiths's avatar
Dave Griffiths committed
26 27 28 29

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; persistent database

30
(define db "/sdcard/symbai/local-symbai.db")
Dave Griffiths's avatar
Dave Griffiths committed
31 32 33 34 35 36 37 38 39 40
(db-open db)
(setup db "local")
(setup db "sync")
(setup db "stream")

(insert-entity-if-not-exists
 db "local" "app-settings" "null" 1
 (list
  (ktv "user-id" "varchar" "No name yet...")))

Dave Griffiths's avatar
Dave Griffiths committed
41
(define entity-types (list "village"))
42

Dave Griffiths's avatar
Dave Griffiths committed
43
;;(display (db-all db "local" "app-settings"))(newline)
Dave Griffiths's avatar
Dave Griffiths committed
44 45 46 47 48


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; user interface abstraction

49 50 51 52 53 54
;;;;;;;;;;;;; i18n ;;;;;;;;;;;;;;;;;;;;;;

(define i18n-lang 0)

(define i18n-text
  (list
Dave Griffiths's avatar
Dave Griffiths committed
55
   (list 'test-num (list "1.0000000" "1.0000000" "1.0000000"))
56 57 58 59 60 61
   (list 'test-text (list "I am test text" "I am test text" "I am test text"))
   (list 'one (list "one"))
   (list 'two (list "two"))
   (list 'three (list "three"))
   (list 'village (list "Village"))
   (list 'household (list "Household"))
62
   (list 'households (list "Households"))
63
   (list 'individual (list "Individual"))
64
   (list 'individuals (list "Individuals"))
65

66 67
   (list 'add-item (list "+"))
   (list 'default-village-name (list "New village"))
Dave Griffiths's avatar
Dave Griffiths committed
68

69 70 71 72 73 74
   (list 'title (list "Symbai" "Symbai" "Symbai"))
   (list 'sync (list "Sync" "Sync" "Sync"))
   (list 'languages (list "Choose language" "Choose language" "Choose language"))
   (list 'english (list "English" "English" "English"))
   (list 'khasi (list "Khasi" "Khasi" "Khasi"))
   (list 'hindi (list "Hindi" "Hindi" "Hindi"))
Dave Griffiths's avatar
Dave Griffiths committed
75
   (list 'user-id (list "Your user ID" "User ID" "User ID"))
76 77
   (list 'ok (list "Ok" "Ok" "Ok"))
   (list 'cancel (list "Cancel" "Cancel" "Cancel"))
Dave Griffiths's avatar
Dave Griffiths committed
78
   (list 'villages (list "Villages" "Villages" "Villages"))
79 80 81 82
   (list 'list-empty (list "List empty"))
   (list 'delete (list "Delete"))
   (list 'delete-are-you-sure (list "Are you sure you want to delete this?"))
   (list 'save-are-you-sure (list "Are you sure you want to save changes?"))
Dave Griffiths's avatar
Dave Griffiths committed
83

84 85 86 87 88 89 90 91 92 93 94
   ;; sync
   (list 'sync-all (list "Sync me!"))
   (list 'sync-syncall (list "Sync everything"))
   (list 'export-data (list "Exporting data"))
   (list 'sync-download (list "Download main DB"))
   (list 'sync-export (list "Email main DB"))
   (list 'email-local (list "Email local DB"))
   (list 'debug (list "Debug"))
   (list 'sync-back (list "Back"))
   (list 'sync-prof (list "Profile"))

Dave Griffiths's avatar
Dave Griffiths committed
95
   ;; village screen
96
   (list 'village-name (list "Village name" "Village name" "Village name"))
Dave Griffiths's avatar
Dave Griffiths committed
97 98 99 100 101 102 103
   (list 'block (list "Block" "Block" "Block"))
   (list 'district (list "District" "District" "District"))
   (list 'car (list "Accessible by car"))
   (list 'household-list (list "Household list"))
   (list 'amenities (list "Amenities"))
   (list 'school (list "School"))
   (list 'present (list "Present"))
Dave Griffiths's avatar
Dave Griffiths committed
104
   (list 'closest-access (list "Closest place of access"))
Dave Griffiths's avatar
Dave Griffiths committed
105 106 107 108 109 110 111 112 113 114 115
   (list 'gps (list "GPS"))
   (list 'school (list "School"))
   (list 'hospital (list "Hospital/Health care centre"))
   (list 'post-office (list "Post Office"))
   (list 'railway-station (list "Railway station"))
   (list 'state-bus-service (list "Inter-state bus service"))
   (list 'district-bus-service (list "Inter-village/district bus service"))
   (list 'panchayat (list "Village Panchayat Office"))
   (list 'NGO (list "Presence of NGO's working with them"))
   (list 'market (list "Market"))

116
   ;; household
Dave Griffiths's avatar
Dave Griffiths committed
117 118
   (list 'household-name (list "Household name"))
   (list 'default-household-name (list "A household"))
119 120 121 122 123 124 125 126 127 128 129
   (list 'location (list "House location"))
   (list 'elevation (list "Elevation"))
   (list 'toilet-location (list "Toilet location"))
   (list 'children (list "Children"))
   (list 'male (list "Male"))
   (list 'female (list "Female"))
   (list 'num-pots (list "Number of pots"))
   (list 'adults (list "Adults"))
   (list 'add-individual (list "Add individual"))

   ;; individual
Dave Griffiths's avatar
Dave Griffiths committed
130 131 132
   (list 'default-individual-name (list "A person"))
   (list 'default-family-name (list "A family"))
   (list 'default-photo-id (list "???"))
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
   (list 'details (list "Details"))
   (list 'family (list "Family"))
   (list 'migration (list "Migration"))
   (list 'income (list "Income"))
   (list 'geneaology (list "Geneaology"))
   (list 'social (list "Social"))
   (list 'agreement (list "Agreement"))

   ;; details
   (list 'change-photo (list "Change photo"))
   (list 'name (list "Name"))
   (list 'photo-id (list "Photo ID"))
   (list 'tribe (list "Tribe"))
   (list 'sub-tribe (list "Sub tribe"))
   (list 'other (list "Other"))
   (list 'age (list "Age"))
   (list 'gender (list "Gender"))
   (list 'education (list "Education"))

   ;; family
   (list 'spouse (list "Spouse"))
   (list 'head-of-house (list "Head of house"))
   (list 'marital-status (list "Marital status"))
   (list 'ever-married (list "Ever married"))
   (list 'currently-married (list "Currently married"))
   (list 'currently-single (list "Currently single"))
   (list 'seperated (list "Seperated/divorced"))
   (list 'times-married (list "How many times married"))
   (list 'change-spouse (list "Change/add spouse"))
   (list 'living (list "Living"))
   (list 'dead (list "Dead"))
   (list 'together (list "Living together"))
   (list 'apart (list "Living apart"))
   (list 'residence-after-marriage (list "Residence after marriage"))
   (list 'birthplace (list "Birthplace"))
   (list 'spouse-village (list "Spouses natal village"))
   (list 'num-siblings (list "Number of living siblings of the same sex born from same mother"))
   (list 'birth-order (list "Birth order amoung currently living same sex siblings born from same mother"))

   ;; migration
   (list 'length-time (list "Length of time lived in this village (years)"))
   (list 'place-of-birth (list "Place of birth"))
   (list 'num-residence-changes (list "Number of time place of residence changed since birth"))
   (list 'village-visits-month (list "Number of times you have visited another village in the last month"))
   (list 'village-visits-year (list "Number of times you have visited another village in the last year (i.e. betwen last summer and this summer)"))

   ;; income
   (list 'occupation (list "Occupation"))
   (list 'occupation (list "Occupation"))
   (list 'agriculture (list "Agriculture"))
   (list 'gathering (list "Gathering"))
   (list 'labour (list "Labour"))
   (list 'cows (list "Cows"))
   (list 'fishing (list "Fishing"))
   (list 'num-people-in-house (list "Number of people living in this house"))
   (list 'contribute (list "Do you contribute to the family earnings?"))
   (list 'own-land (list "Do you own land?"))
   (list 'rent-land (list "Do you rent out your land?"))
   (list 'hire-land (list "Do you hire someone else's land?"))
   (list 'crops (list "Crops"))
   (list 'crop (list "Crop"))
   (list 'unit (list "Unit"))
   (list 'quantity (list "Quantity"))
   (list 'used-or-eaten (list "Used/Eaten"))
   (list 'sold (list "Sold"))
   (list 'seed (list "Seed (hybrid/local)"))
   (list 'house-type (list "House type"))
   (list 'concrete (list "Concrete"))
   (list 'tin (list "Tin"))
   (list 'thatched (list "Thatched"))
   (list 'loan (list "How much outstanding loan money have you taken in all from any source at this time?"))
   (list 'earning (list "How much do you earn for one day's labour?"))
   (list 'in-the-home (list "In the home"))
   (list 'radio (list "Radio"))
   (list 'tv (list "TV"))
   (list 'mobile (list "Mobile phone"))
   (list 'visit-market (list "How many times a month do you visit the tribal market?"))
   (list 'town-sell (list "How many times a month do you visit the local town or city to sell something?"))

   ;; geneaology

   (list 'mother (list "Mother"))
   (list 'father (list "Father"))
   (list 'change-mother (list "Change mother"))
   (list 'change-father (list "Change father"))
   (list 'alive (list "Alive"))
   (list 'sex (list "Sex"))
220 221 222 223 224 225
   ))

(define (mtext-lookup id)
  (define (_ l)
    (cond
     ((null? l) (string-append (symbol->string id) " not translated"))
Dave Griffiths's avatar
Dave Griffiths committed
226 227 228 229 230
     ((eq? (car (car l)) id)
      (let ((translations (cadr (car l))))
        (if (<= (length translations) i18n-lang)
            (string-append (symbol->string id) " not translated")
            (list-ref translations i18n-lang))))
231 232 233 234 235
     (else (_ (cdr l)))))
  (_ i18n-text))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Dave Griffiths's avatar
Dave Griffiths committed
236 237 238 239 240
(define (symbol->id id)
  (when (not (symbol? id))
        (msg "symbol->id: [" id "] is not a symbol"))
  (make-id (symbol->string id)))

241 242 243 244
(define (get-symbol-id id)
  (when (not (symbol? id))
        (msg "symbol->id: [" id "] is not a symbol"))
  (get-id (symbol->string id)))
Dave Griffiths's avatar
Dave Griffiths committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

(define (mbutton id fn)
  (button (symbol->id id)
          (mtext-lookup id)
          40 (layout 'fill-parent 'wrap-content -1 'centre 5) fn))

(define (mbutton-scale id fn)
  (button (symbol->id id)
          (mtext-lookup id)
          40 (layout 'fill-parent 'wrap-content 1 'centre 5) fn))

(define (mtoggle-button id fn)
  (toggle-button (symbol->id id)
                 (mtext-lookup id)
                 30 (layout 'fill-parent 'wrap-content -1 'centre 0) "fancy" fn))

(define (mtoggle-button-scale id fn)
  (toggle-button (symbol->id id)
                 (mtext-lookup id)
                 30 (layout 'fill-parent 'wrap-content 1 'centre 0) "fancy" fn))

(define (mtext id)
  (text-view (symbol->id id)
             (mtext-lookup id)
             30 (layout 'wrap-content 'wrap-content -1 'centre 0)))

(define (mtext-fixed w id)
  (text-view (symbol->id id)
             (mtext-lookup id)
             30 (layout w 'wrap-content -1 'centre 0)))

276 277 278 279
(define (mtext-small id)
  (text-view (symbol->id id)
             (mtext-lookup id)
             20 (layout 'wrap-content 'wrap-content -1 'centre 0)))
Dave Griffiths's avatar
Dave Griffiths committed
280 281 282 283 284 285 286 287 288

(define (mtext-scale id)
  (text-view (symbol->id id)
             (mtext-lookup id)
             30 (layout 'wrap-content 'wrap-content 1 'centre 0)))

(define (mtitle id)
  (text-view (symbol->id id)
             (mtext-lookup id)
289
             50 (layout 'fill-parent 'wrap-content -1 'centre 5)))
Dave Griffiths's avatar
Dave Griffiths committed
290

291 292 293
(define (mtitle-scale id)
  (text-view (symbol->id id)
             (mtext-lookup id)
294
             50 (layout 'fill-parent 'wrap-content 1 'centre 5)))
295

Dave Griffiths's avatar
Dave Griffiths committed
296
(define (medit-text id type fn)
Dave Griffiths's avatar
Dave Griffiths committed
297
  (vert
298 299
   (text-view 0 (mtext-lookup id)
              30 (layout 'wrap-content 'wrap-content -1 'centre 0))
Dave Griffiths's avatar
Dave Griffiths committed
300 301 302
   (edit-text (symbol->id id) "" 30 type
              (layout 'fill-parent 'wrap-content -1 'centre 0)
              fn)))
Dave Griffiths's avatar
Dave Griffiths committed
303

Dave Griffiths's avatar
Dave Griffiths committed
304 305
(define (medit-text-scale id type fn)
  (vert
306 307
   (text-view 0 (mtext-lookup id)
              30 (layout 'wrap-content 'wrap-content 1 'centre 0))
Dave Griffiths's avatar
Dave Griffiths committed
308 309 310
   (edit-text (symbol->id id) "" 30 type
              (layout 'fill-parent 'wrap-content 1 'centre 0)
              fn)))
311

Dave Griffiths's avatar
Dave Griffiths committed
312
(define (mspinner id types fn)
313
  (vert
Dave Griffiths's avatar
Dave Griffiths committed
314 315
   (text-view (symbol->id id)
              (mtext-lookup id)
316
              30 (layout 'wrap-content 'wrap-content 1 'centre 10))
Dave Griffiths's avatar
Dave Griffiths committed
317
   (spinner (make-id (string-append (symbol->string id) "-spinner"))
318 319
            (map mtext-lookup types)
            (layout 'wrap-content 'wrap-content 1 'centre 0)
Dave Griffiths's avatar
Dave Griffiths committed
320 321 322 323
            (lambda (c) (fn c)))))

(define (mspinner-other id types fn)
  (horiz
324
   (vert
325 326 327 328 329 330 331 332 333
    (text-view (symbol->id id)
               (mtext-lookup id)
               30 (layout 'wrap-content 'wrap-content 1 'centre 10))
    (spinner (make-id (string-append (symbol->string id) "-spinner"))
             (map mtext-lookup types)
             (layout 'wrap-content 'wrap-content 1 'centre 0)
             (lambda (c) (fn c))))
   (vert
    (mtext-scale 'other)
Dave Griffiths's avatar
Dave Griffiths committed
334
    (edit-text (make-id (string-append (symbol->string id) "-edit-text"))
335 336
               "" 30 "normal"
               (layout 'fill-parent 'wrap-content 1 'centre 0)
337 338 339
               (lambda (t) (fn t))))))


Dave Griffiths's avatar
Dave Griffiths committed
340 341 342 343 344 345 346 347 348 349 350 351 352
(define (mclear-toggles id-list)
  (map
   (lambda (id)
     (update-widget 'toggle-button (get-id id) 'checked 0))
   id-list))

(define (mclear-toggles-not-me me id-list)
  (foldl
   (lambda (id r)
     (if (equal? me id)
         r (cons (update-widget 'toggle-button (get-id id) 'checked 0) r)))
   '() id-list))

353 354 355 356 357

;; fill out the widget from the current entity in the memory store
;; dispatches based on widget type
(define (mupdate widget-type id-symbol key)
  (cond
Dave Griffiths's avatar
Dave Griffiths committed
358
   ((or (eq? widget-type 'edit-text) (eq? widget-type 'text-view))
359 360 361 362 363
    (update-widget widget-type (get-symbol-id id-symbol) 'text
                   (entity-get-value key)))
   ((eq? widget-type 'toggle-button)
    (update-widget widget-type (get-symbol-id id-symbol) 'selected
                   (entity-get-value key)))
364 365 366 367 368 369 370
   ((eq? widget-type 'image-view)
    (let ((image-name (entity-get-value key)))
      (msg "updating widget: " image-name)
      (if (equal? image-name "none")
          (update-widget widget-type (get-symbol-id id-symbol) 'image "face")
          (update-widget widget-type (get-symbol-id id-symbol) 'external-image
                         (string-append dirname "files/" image-name)))))
371 372
   (else (msg "mupdate-widget unhandled widget type" widget-type))))

Dave Griffiths's avatar
Dave Griffiths committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
;;;;
;; (y m d h m s)
(define (date-minus-months d ms)
  (let ((year (list-ref d 0))
        (month (- (list-ref d 1) 1)))
    (let ((new-month (- month ms)))
      (list
       (if (< new-month 0) (- year 1) year)
       (+ (if (< new-month 0) (+ new-month 12) new-month) 1)
       (list-ref d 2)
       (list-ref d 3)
       (list-ref d 4)
       (list-ref d 5)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;

(define (debug! txt)
  (set-current! 'debug-text (string-append txt "\n" (get-current 'debug-text ""))))

(define (update-debug)
  (update-widget 'debug-text-view (get-id "sync-debug") 'text
                 (get-current 'debug-text "")))

(define (debug-timer-cb)
  (append
   (cond
    ((get-current 'sync-on #f)
     (set-current! 'upload 0)
     (set-current! 'download 0)
     (connect-to-net
      (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
405
        (msg "connected, going in...")
Dave Griffiths's avatar
Dave Griffiths committed
406 407 408 409 410 411
        (append
         (list (toast "sync-cb"))
         (upload-dirty db)
         (suck-new db "sync")))))
    (else '()))
   (list
Dave Griffiths's avatar
Dave Griffiths committed
412
    (delayed "debug-timer" (+ 10000 (random 5000)) debug-timer-cb)
Dave Griffiths's avatar
Dave Griffiths committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    (update-debug))))


(define pf-length 20) ;; minutes...

(define (timer-cb)
  (set-current!
   'timer-seconds
   (- (get-current 'timer-seconds 59) 1))
  (append
   (cond
    ((< (get-current 'timer-seconds 59) 0)
     (set-current! 'timer-minutes (- (get-current 'timer-minutes pf-length) 1))
     (set-current! 'timer-seconds 59)
     (cond ((< (get-current 'timer-minutes pf-length) 1)
            (list
             (alert-dialog
              "pup-focal-end"
              "Pup focal time is up, have you finished?"
              (lambda (v)
                (cond
                 ((eqv? v 1)
                  (list (finish-activity 1)))
                 (else
                  (set-current! 'timer-minutes 1)
                  (list)))))))
           (else
            (list (replace-fragment (get-id "pf-top") "pf-scan1")))))
    (else '()))
   (list
    (delayed "timer" 1000 timer-cb)
    (update-widget
     'text-view (get-id "pf-timer-time-minutes") 'text
     (string-append (number->string (get-current 'timer-minutes pf-length))))
    (update-widget
     'text-view (get-id "pf-timer-time") 'text
     (string-append (number->string (get-current 'timer-seconds 59))))
    )))


(define (force-pause)
  (list
   (delayed "timer" 1000 (lambda () '()))
   (update-widget 'toggle-button (get-id "pf-pause") 'checked 1)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; fragments

(define-fragment-list

  (fragment
464 465
   "top"
   (horiz
Dave Griffiths's avatar
Dave Griffiths committed
466
    (image-view 0 "face" (layout 48 64 -1 'centre 0))
467
    (text-view (make-id "title") "" 30
468 469 470 471
               (layout 'fill-parent 'fill-parent 0.25 'centre 10))

    (linear-layout
     0 'vertical
Dave Griffiths's avatar
Dave Griffiths committed
472
     (layout 'fill-parent 'wrap-content 0.75 'centre 0)
473 474 475 476
     (list 0 0 0 0)

     (list
      (text-view (make-id "") 'name 20
Dave Griffiths's avatar
Dave Griffiths committed
477
                 (layout 'fill-parent 'wrap-content 1 'centre 0))
478
      (text-view (make-id "") 'photo-id 20
Dave Griffiths's avatar
Dave Griffiths committed
479
                 (layout 'fill-parent 'wrap-content 1 'centre 0)))))
Dave Griffiths's avatar
Dave Griffiths committed
480 481 482
   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
483 484 485
     (list
      (update-widget 'text-view (get-id "title") 'text
                     (get-current 'activity-title "Title not set"))))
Dave Griffiths's avatar
Dave Griffiths committed
486 487 488 489 490 491
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))


492 493 494 495
  (fragment
   "bottom"
   (linear-layout
    0 'horizontal
Dave Griffiths's avatar
Dave Griffiths committed
496
    (layout 'fill-parent 'fill-parent 1 'centre 0)
497 498
    (list 0 0 0 0)
    (list
499 500 501 502 503 504
     (mbutton-scale
      'ok
      (lambda ()
        (list
         (alert-dialog
          "ok-check"
505
          (mtext-lookup 'save-are-you-sure)
506 507 508 509 510 511 512 513
          (lambda (v)
            (cond
             ((eqv? v 1)
              (entity-update-values!)
              (list (finish-activity 1)))
             (else
              (list))))))))
     (mbutton-scale 'cancel (lambda () (list (finish-activity 1))))))
514 515 516 517 518 519 520 521
   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
     (list))
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))
Dave Griffiths's avatar
Dave Griffiths committed
522 523 524 525 526 527


  )

(msg "one")

Dave Griffiths's avatar
Dave Griffiths committed
528 529 530 531
(define (build-activity . contents)
  (vert-fill
   (relative
    '(("parent-top"))
532
    colour-one ;;(list 100 100 255 127)
Dave Griffiths's avatar
Dave Griffiths committed
533 534 535 536 537 538 539 540 541
    (build-fragment "top" (make-id "top") fillwrap))

   (scroll-view-vert
    0 (layout 'fill-parent 'fill-parent 1 'centre 0)
    (list
     (apply vert-fill contents)))

   (relative
    '(("parent-bottom"))
542
    colour-one
Dave Griffiths's avatar
Dave Griffiths committed
543 544 545 546 547
    (vert
     (spacer 5)
     (build-fragment "bottom" (make-id "bottom") fillwrap)))))


548 549
;; a standard builder for list widgets of entities and a
;; make new button, to add defaults to the list
Dave Griffiths's avatar
Dave Griffiths committed
550
(define (build-list-widget db table title entity-type edit-activity parent-fn ktv-default)
551 552 553
    (vert-colour
     colour-two
     (horiz
554
      (mtitle-scale title)
Dave Griffiths's avatar
Dave Griffiths committed
555 556 557 558
      (button
       (make-id (string-append (symbol->string title) "-add"))
       (mtext-lookup title)
       40 (layout 'fill-parent 'wrap-content 1 'centre 5)
559 560
       (lambda ()
         (entity-init! db table entity-type ktv-default)
Dave Griffiths's avatar
Dave Griffiths committed
561
         (entity-add-value! "parent" "varchar" (parent-fn))
562
         (entity-record-values!)
Dave Griffiths's avatar
Dave Griffiths committed
563
         (list (update-list-widget db table entity-type edit-activity (parent-fn))))))
564 565 566 567 568 569 570 571
     (linear-layout
      (make-id (string-append entity-type "-list"))
      'vertical
      (layout 'fill-parent 'wrap-content 1 'centre 20)
      (list 0 0 0 0)
      (list))))

;; pull db data into list of button widgets
Dave Griffiths's avatar
Dave Griffiths committed
572 573 574 575 576
(define (update-list-widget db table entity-type edit-activity parent)
  (let ((search-results
         (if parent
             (db-with-parent db table entity-type parent)
             (db-all db table entity-type))))
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
    (update-widget
     'linear-layout
     (get-id (string-append entity-type "-list"))
     'contents
     (if (null? search-results)
         (list (mtext 'list-empty))
         (map
          (lambda (e)
            (button
             (make-id (string-append "list-button-" (ktv-get e "unique_id")))
             (or (ktv-get e "name") "Unamed item")
             40 (layout 'fill-parent 'wrap-content 1 'centre 5)
             (lambda ()
               (msg "sending start act" (ktv-get e "unique_id"))
               (list (start-activity edit-activity 0 (ktv-get e "unique_id"))))))
          search-results)))))

(define (delete-button)
  (mbutton
   'delete
   (lambda ()
     (list
      (alert-dialog
       "delete-check"
       (mtext-lookup 'delete-are-you-sure)
       (lambda (v)
         (cond
          ((eqv? v 1)
           (entity-set-value! "deleted" "int" 1)
           (entity-update-values!)
           (list (finish-activity 1)))
          (else
           (list)))))))))
610

Dave Griffiths's avatar
Dave Griffiths committed
611 612 613
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; activities

614
(define photo-code 999)
Dave Griffiths's avatar
Dave Griffiths committed
615 616 617 618 619

(define-activity-list

  (activity
   "main"
620 621 622 623
   (build-activity
    (mtitle 'title)
    (horiz
     (medit-text 'user-id "normal" (lambda () (list)))
624
     (mbutton-scale 'sync (lambda () (list (start-activity "sync" 0 "")))))
625 626

    (mspinner 'languages (list 'english 'khasi 'hindi) (lambda (c) (list)))
Dave Griffiths's avatar
Dave Griffiths committed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
    (mbutton 'test-upload (lambda ()
                            (list
                             (network-connect
                              "network"
                              "mongoose-web"
                              (lambda (state)
                                (msg state)
                                (if (equal? state "Connected")
                                    (list
                                     (http-upload
                                      "test-upload"
                                      "http://192.168.2.1:8889/symbai?fn=upload"
                                      "/sdcard/symbai/photo.jpg"))
                                    '()))
                              ))))
642
    (build-list-widget
Dave Griffiths's avatar
Dave Griffiths committed
643
     db "sync" 'villages "village" "village" (lambda () #f)
644 645 646 647
     (list
      (ktv "name" "varchar" (mtext-lookup 'default-village-name))
      (ktv "block" "varchar" "")
      (ktv "district" "varchar" "test")
648 649
      (ktv "car" "int" 0)
      (ktv "photo" "file" "none"))))
650

651
   (lambda (activity arg)
652
     (set-current! 'activity-title "Main screen")
653 654
     (activity-layout activity))
   (lambda (activity arg)
Dave Griffiths's avatar
Dave Griffiths committed
655
     (list (update-list-widget db "sync" "village" "village" #f)))
656 657 658 659
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
660 661 662 663 664 665 666 667 668
   (lambda (activity requestcode resultcode)
     (cond
      ((eqv? requestcode photo-code)
       (msg "camera returned" resultcode)
       (list (update-widget
              'image-view (get-id "image")
              'external-image (string-append dirname "photo.jpg"))))
      (else
       '()))))
669 670 671 672


  (activity
   "village"
Dave Griffiths's avatar
Dave Griffiths committed
673 674 675
   (let ((place-widgets
          (lambda (id shade)
            (horiz-colour
676
             (if shade colour-one colour-two)
Dave Griffiths's avatar
Dave Griffiths committed
677 678 679 680
             (mtoggle-button-scale id (lambda (v) '()))
             (medit-text-scale 'closest-access "normal" (lambda (v) '()))
             (vert
              (mbutton-scale 'gps (lambda () '()))
681 682
              (mtext-small 'test-num)
              (mtext-small 'test-num))))))
Dave Griffiths's avatar
Dave Griffiths committed
683 684
     (build-activity
      (horiz
685 686
       (medit-text 'village-name "normal"
                   (lambda (v) (entity-set-value! "name" "varchar" v) '()))
Dave Griffiths's avatar
Dave Griffiths committed
687 688 689 690
       (medit-text 'block "normal" (lambda () '())))
      (horiz
       (medit-text 'district "normal" (lambda () '()))
       (mtoggle-button-scale 'car (lambda () '())))
691 692 693 694 695 696 697 698 699 700

      (vert
       (image-view (make-id "photo") "face" (layout 240 320 -1 'centre 10))
       (mbutton
        'change-photo
        (lambda ()
          (list
           (take-photo (string-append dirname "files/" (entity-get-value "unique_id") "-face.jpg") photo-code))
          )))

Dave Griffiths's avatar
Dave Griffiths committed
701 702 703 704
      (mbutton 'household-list
               (lambda ()
                 (list (start-activity "household-list" 0
                                       (get-current 'village #f)))))
705

Dave Griffiths's avatar
Dave Griffiths committed
706 707 708 709 710 711 712 713 714
      (mtitle 'amenities)
      (place-widgets 'school #t)
      (place-widgets 'hospital #f)
      (place-widgets 'post-office #t)
      (place-widgets 'railway-station #f)
      (place-widgets 'state-bus-service #t)
      (place-widgets 'district-bus-service #f)
      (place-widgets 'panchayat #t)
      (place-widgets 'NGO #f)
715 716
      (place-widgets 'market #t)
      (delete-button)))
Dave Griffiths's avatar
Dave Griffiths committed
717
   (lambda (activity arg)
718
     (set-current! 'activity-title "Village")
Dave Griffiths's avatar
Dave Griffiths committed
719 720
     (activity-layout activity))
   (lambda (activity arg)
721
     (entity-init! db "sync" "village" (get-entity-by-unique db "sync" arg))
722
     (set-current! 'village arg)
723 724 725 726 727
     (list
      (mupdate 'edit-text 'village-name "name")
      (mupdate 'edit-text 'block "block")
      (mupdate 'edit-text 'district "district")
      (mupdate 'toggle-button 'car "car")
Dave Griffiths's avatar
Dave Griffiths committed
728
      (mupdate 'image-view 'photo "photo")))
Dave Griffiths's avatar
Dave Griffiths committed
729 730 731 732
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
733 734 735 736 737 738 739 740 741 742 743 744 745
   (lambda (activity requestcode resultcode)
     (msg "back from camera")
     (cond
      ((eqv? requestcode photo-code)
       ;; todo: means we save when the camera happens
       ;; need to do this before init is called again in on-start,
       ;; which happens next
       (entity-set-value! "photo" "file" (string-append (entity-get-value "unique_id") "-face.jpg"))
       (entity-update-values!)
       (list
        (mupdate 'image-view 'photo "photo")))
      (else
       '()))))
Dave Griffiths's avatar
Dave Griffiths committed
746

Dave Griffiths's avatar
Dave Griffiths committed
747 748 749 750

  (activity
   "household-list"
   (build-activity
751
    (build-list-widget
Dave Griffiths's avatar
Dave Griffiths committed
752
     db "sync" 'households "household" "household" (lambda () (get-current 'village #f))
753 754 755 756 757 758
     (list
      (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)
Dave Griffiths's avatar
Dave Griffiths committed
759
      (ktv "toilet-lon" "real" 0))))
Dave Griffiths's avatar
Dave Griffiths committed
760
   (lambda (activity arg)
761
     (set-current! 'activity-title "Household List")
Dave Griffiths's avatar
Dave Griffiths committed
762
     (activity-layout activity))
763
   (lambda (activity arg)
Dave Griffiths's avatar
Dave Griffiths committed
764
     (msg "rebuilding household list with" arg)
Dave Griffiths's avatar
Dave Griffiths committed
765 766
     (list (update-list-widget
            db "sync" "household" "household" arg)))
Dave Griffiths's avatar
Dave Griffiths committed
767 768 769 770 771 772 773 774 775
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))

  (activity
   "household"
   (build-activity
776 777 778
    (horiz
     (medit-text 'household-name "normal" (lambda (v) '()))
     (medit-text 'num-pots "numeric" (lambda (v) '())))
779 780 781 782 783 784 785 786 787 788 789 790 791 792
    (horiz
     (mtext 'location)
     (vert
      (mbutton 'GPS (lambda () '()))
      (mtext-small 'test-num)
      (mtext-small 'test-num))
     (medit-text 'elevation "numeric" (lambda (v) '())))
    (horiz
     (mtext 'toilet-location)
     (vert
      (mbutton 'GPS (lambda () '()))
      (mtext-small 'test-num)
      (mtext-small 'test-num))
     (medit-text 'elevation "numeric" (lambda (v) '())))
793 794

    (build-list-widget
Dave Griffiths's avatar
Dave Griffiths committed
795
     db "sync" 'individuals "individual" "individual" (lambda () (get-current 'household #f))
796
     (list
Dave Griffiths's avatar
Dave Griffiths committed
797
      (ktv "name" "varchar" (mtext-lookup 'default-individual-name))
798
      (ktv "family" "varchar" (mtext-lookup 'default-family-name))
Dave Griffiths's avatar
Dave Griffiths committed
799
      (ktv "photo-id" "varchar" (mtext-lookup 'default-photo-id)))))
Dave Griffiths's avatar
Dave Griffiths committed
800
   (lambda (activity arg)
801
     (set-current! 'activity-title "Household")
Dave Griffiths's avatar
Dave Griffiths committed
802
     (activity-layout activity))
803 804 805 806
   (lambda (activity arg)
     (entity-init! db "sync" "household" (get-entity-by-unique db "sync" arg))
     (set-current! 'household arg)
     (list
Dave Griffiths's avatar
Dave Griffiths committed
807
      (update-list-widget db "sync" "individual" "individual" arg)
808 809 810
      (mupdate 'edit-text 'household-name "name")
      (mupdate 'edit-text 'num-pots "num-pots")))

Dave Griffiths's avatar
Dave Griffiths committed
811 812 813 814 815 816 817 818 819
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))

  (activity
   "individual"
   (build-activity
820 821 822 823 824 825
    (horiz
     (image-view (make-id "photo") "face" (layout 240 320 -1 'centre 10))
     (vert
      (mtext 'name)
      (mtext 'family)
      (mtext 'photo-id)))
Dave Griffiths's avatar
Dave Griffiths committed
826
    (mbutton 'agreement (lambda () (list (start-activity "agreement" 0 ""))))
827 828 829 830 831 832 833 834
    (horiz
     (mbutton-scale 'details (lambda () (list (start-activity "details" 0 ""))))
     (mbutton-scale 'family (lambda () (list (start-activity "family" 0 "")))))
    (horiz
     (mbutton-scale 'migration (lambda () (list (start-activity "migration" 0 ""))))
     (mbutton-scale 'income (lambda () (list (start-activity "income" 0 "")))))
    (horiz
     (mbutton-scale 'geneaology (lambda () (list (start-activity "geneaology" 0 ""))))
Dave Griffiths's avatar
Dave Griffiths committed
835
     (mbutton-scale 'social (lambda () (list (start-activity "social" 0 ""))))))
836

Dave Griffiths's avatar
Dave Griffiths committed
837
   (lambda (activity arg)
838
     (set-current! 'activity-title "Individual")
Dave Griffiths's avatar
Dave Griffiths committed
839
     (activity-layout activity))
Dave Griffiths's avatar
Dave Griffiths committed
840 841 842 843 844 845 846
   (lambda (activity arg)
     (entity-init! db "sync" "individual" (get-entity-by-unique db "sync" arg))
     (set-current! 'individual arg)
     (list
      (mupdate 'text-view 'name "name")
      (mupdate 'text-view 'family "family")
      (mupdate 'text-view 'photo-id "photo-id")))
Dave Griffiths's avatar
Dave Griffiths committed
847 848 849 850 851 852 853 854 855
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))

  (activity
   "details"
   (build-activity
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
    (horiz
     (vert
      (image-view (make-id "image") "face" (layout 240 320 -1 'centre 10))
      (mbutton
       'change-photo
       (lambda ()
         (list (take-photo (string-append dirname "photo.jpg") photo-code)))))
     (vert
      (medit-text 'name "normal" (lambda (v) '()))
      (medit-text 'family "normal" (lambda (v) '()))
      (medit-text 'photo-id "normal" (lambda (v) '()))))
    (mspinner-other 'tribe '(one two three) (lambda (v) '()))
    (mspinner-other 'sub-tribe '(one two three) (lambda (v) '()))
    (horiz
     (medit-text 'age "numeric" (lambda (v) '()))
     (mspinner 'gender '(male female) (lambda (v) '()))
     (mspinner 'education '(one two three) (lambda (v) '())))
Dave Griffiths's avatar
Dave Griffiths committed
873 874
    )
   (lambda (activity arg)
875
     (set-current! 'activity-title "Individual details")
Dave Griffiths's avatar
Dave Griffiths committed
876 877 878 879 880 881 882 883 884 885 886
     (activity-layout activity))
   (lambda (activity arg) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))

  (activity
   "family"
   (build-activity
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
    (horiz
     (vert
      (mspinner 'head-of-house '(male female) (lambda (v) '()))
      (mspinner 'marital-status '(ever-married currently-married currently-single seperated) (lambda (v) '()))
      (medit-text 'times-married "numeric" (lambda (v) '())))
     (vert
      (mtitle 'spouse)
      (image-view (make-id "spouse-image") "face" (layout 240 320 -1 'centre 0))
      (mbutton 'change-spouse (lambda () '()))))

    (mtitle 'children)
    (horiz
     (medit-text 'living "numeric" (lambda (v) '()))
     (medit-text 'dead "numeric" (lambda (v) '())))
    (horiz
     (medit-text 'together "numeric" (lambda (v) '()))
     (medit-text 'apart "numeric" (lambda (v) '())))
    (mspinner-other 'residence-after-marriage '(birthplace spouse-village) (lambda (v) '()))
    (medit-text 'num-siblings "numeric" (lambda (v) '()))
    (medit-text 'birth-order "numeric" (lambda (v) '())))
Dave Griffiths's avatar
Dave Griffiths committed
907
   (lambda (activity arg)
908
     (set-current! 'activity-title "Individual family")
Dave Griffiths's avatar
Dave Griffiths committed
909 910 911 912 913 914 915 916 917 918 919 920
     (activity-layout activity))
   (lambda (activity arg) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))


  (activity
   "migration"
   (build-activity
921 922 923 924 925
    (medit-text 'length-time "numeric" (lambda (v) '()))
    (medit-text 'place-of-birth "normal" (lambda (v) '()))
    (medit-text 'num-residence-changes "numeric" (lambda (v) '()))
    (medit-text 'village-visits-month "numeric" (lambda (v) '()))
    (medit-text 'village-visits-year "numeric" (lambda (v) '()))
Dave Griffiths's avatar
Dave Griffiths committed
926 927
    )
   (lambda (activity arg)
928
     (set-current! 'activity-title "Individual migration")
Dave Griffiths's avatar
Dave Griffiths committed
929 930 931 932 933 934 935 936 937 938 939
     (activity-layout activity))
   (lambda (activity arg) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))

  (activity
   "income"
   (build-activity
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
    (horiz
     (mspinner 'occupation '(agriculture gathering labour cows fishing other)
               (lambda (v) '()))
     (medit-text 'num-people-in-house "numeric" (lambda (v) '())))
    (horiz
     (mtoggle-button-scale 'contribute (lambda (v) '()))
     (mtoggle-button-scale 'own-land (lambda (v) '())))
    (horiz
     (mtoggle-button-scale 'rent-land (lambda (v) '()))
     (mtoggle-button-scale 'hire-land (lambda (v) '())))
    (mtitle 'crops)
    (horiz
     (mtext-scale 'crop) (mtext-scale 'unit) (mtext-scale 'quantity)
     (mtext-scale 'used-or-eaten) (mtext-scale 'sold) (mtext-scale 'seed))
    (mspinner-other 'house-type '(concrete tin thatched) (lambda (v) '()))
    (horiz
     (medit-text 'loan "numeric" (lambda (v) '()))
     (medit-text 'earning "numeric" (lambda (v) '())))
    (mtext 'in-the-home)
    (horiz
     (mtoggle-button-scale 'radio (lambda (v) '()))
     (mtoggle-button-scale 'tv (lambda (v) '()))
     (mtoggle-button-scale 'mobile (lambda (v) '())))
    (horiz
     (medit-text 'visit-market "numeric" (lambda (v) '()))
     (medit-text 'town-sell "numeric" (lambda (v) '())))
Dave Griffiths's avatar
Dave Griffiths committed
966 967
    )
   (lambda (activity arg)
968
     (set-current! 'activity-title "Individual income")
Dave Griffiths's avatar
Dave Griffiths committed
969 970 971 972 973 974 975 976 977 978 979
     (activity-layout activity))
   (lambda (activity arg) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))

  (activity
   "geneaology"
   (build-activity
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
    (horiz
     (vert
      (mtitle 'mother)
      (image-view (make-id "image") "face" (layout 240 320 -1 'centre 0))
      (mbutton 'change-mother (lambda () '())))
     (vert
      (mtitle 'father)
      (image-view (make-id "image") "face" (layout 240 320 -1 'centre 0))
      (mbutton 'change-father (lambda () '()))))
    (mtitle 'children)
    (horiz
     (medit-text 'name "normal" (lambda (v) '()))
     (mtoggle-button-scale 'alive (lambda (v) '()))
     (mspinner 'sex '(female male) (lambda (v) '()))
     (medit-text 'age "numeric" (lambda (v) '())))
Dave Griffiths's avatar
Dave Griffiths committed
995 996
    )
   (lambda (activity arg)
997
     (set-current! 'activity-title "Individual geneaology")
Dave Griffiths's avatar
Dave Griffiths committed
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
     (activity-layout activity))
   (lambda (activity arg) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))

  (activity
   "social"
   (build-activity
    )
   (lambda (activity arg)
1011
     (set-current! 'activity-title "Individual social network")
Dave Griffiths's avatar
Dave Griffiths committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
     (activity-layout activity))
   (lambda (activity arg) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))

  (activity
   "agreement"
   (build-activity
    )
   (lambda (activity arg)
1025
     (set-current! 'activity-title "Individual agreement")
Dave Griffiths's avatar
Dave Griffiths committed
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
     (activity-layout activity))
   (lambda (activity arg) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))

  (activity
   "individual-chooser"
   (build-activity
    )
   (lambda (activity arg)
1039
     (set-current! 'activity-title "Individual chooser")
Dave Griffiths's avatar
Dave Griffiths committed
1040 1041 1042 1043 1044 1045 1046 1047 1048
     (activity-layout activity))
   (lambda (activity arg) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))


1049 1050 1051
  (activity
   "sync"
   (vert
1052 1053 1054
    (text-view (make-id "sync-title") "Sync database" 40 fillwrap)
    (mtext 'sync-dirty "...")
    (horiz
Dave Griffiths's avatar
Dave Griffiths committed
1055
     (mtoggle-button-scale 'sync-all (lambda (v) (set-current! 'sync-on v) '()))
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
     (mbutton-scale 'sync-syncall
               (lambda ()
                 (let ((r (append
                           (spit db "sync" (dirty-and-all-entities db "sync"))
                           (spit db "stream" (dirty-and-all-entities db "stream")))))
                   (cons (toast "Uploading data...") r)))))
    (mtitle 'export-data)
    (horiz
     (mbutton-scale 'sync-download
               (lambda ()
                 (debug! (string-append "Downloading whole db"))
                 (append
                 (foldl
                  (lambda (e r)
                    (debug! (string-append "Downloading /sdcard/symbai/" e ".csv"))
                    (cons
                     (http-download
                      (string-append "getting-" e)
                      (string-append url "fn=entity-csv&table=stream&type=" e)
                      (string-append "/sdcard/mongoose/" e ".csv"))
                     r))
                  (list
                   (http-download
                    "getting-db"
                    "http://192.168.2.1:8889/symbai.db"
                    (string-append "/sdcard/symbai/symbai.db"))
                   )
                  entity-types)
                 (list))))
     (mbutton-scale 'sync-export
               (lambda ()
                 (debug! "Sending mail")
                 (list
                  (send-mail
                   ""
                   "From Symbai" "Please find attached your mongoose data"
                   (cons
                    "/sdcard/symbai/symbai.db"
                    (map
                     (lambda (e)
                       (string-append "/sdcard/symbai/" e ".csv"))
                     entity-types))))))
     (mbutton-scale 'email-local
               (lambda ()
                 (debug! "Sending mail")
                 (list
                  (send-mail
                   ""
                   "From symbai" "Please find attached your local data"
                   (list "/sdcard/symbai/local-symbai.db")))))
     )
    (spacer 10)
    (mtitle 'debug)
    (scroll-view-vert
     0 (layout 'fill-parent 200 1 'left 0)
     (list
      (vert
       (debug-text-view (make-id "sync-debug") "..." 15 (layout 'fill-parent 400 1 'left 0)))))
    (spacer 10)
    (horiz
     (mbutton-scale 'sync-back (lambda () (list (finish-activity 1))))
     (mbutton-scale 'sync-prof (lambda () (prof-print) (list))))
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
    )

   (lambda (activity arg)
     (activity-layout activity))
   (lambda (activity arg)
     (set-current! 'sync-on #f)
     (append
      (debug-timer-cb)
      (list
       (update-widget 'debug-text-view (get-id "sync-debug") 'text (get-current 'debug-text ""))
1128
       (update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty db))
1129 1130 1131 1132 1133 1134
       )))
   (lambda (activity) '())
   (lambda (activity) (list (delayed "debug-timer" 1000 (lambda () '()))))
   (lambda (activity) (list (delayed "debug-timer" 1000 (lambda () '()))))
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))
Dave Griffiths's avatar
Dave Griffiths committed
1135

Dave Griffiths's avatar
Dave Griffiths committed
1136 1137


1138 1139 1140



Dave Griffiths's avatar
Dave Griffiths committed
1141
  )