starwisp.scm 53.4 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
(define entity-types (list "village" "household" "individual"))
Dave Griffiths's avatar
Dave Griffiths committed
23 24

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

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

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

Dave Griffiths's avatar
Dave Griffiths committed
37 38
(define settings-entity-id-version 2)

Dave Griffiths's avatar
Dave Griffiths committed
39
(insert-entity-if-not-exists
Dave Griffiths's avatar
Dave Griffiths committed
40
 db "local" "app-settings" "null" settings-entity-id-version
Dave Griffiths's avatar
Dave Griffiths committed
41
 (list
Dave Griffiths's avatar
Dave Griffiths committed
42
  (ktv "user-id" "varchar" "not set")
43
  (ktv "language" "int" 0)
Dave Griffiths's avatar
Dave Griffiths committed
44
  (ktv "house-id" "int" 0)
Dave Griffiths's avatar
Dave Griffiths committed
45 46
  (ktv "photo-id" "int" 0)
  (ktv "current-village" "varchar" "none")))
Dave Griffiths's avatar
Dave Griffiths committed
47

Dave Griffiths's avatar
Dave Griffiths committed
48
(define (get-setting-value name)
Dave Griffiths's avatar
Dave Griffiths committed
49
  (ktv-get (get-entity db "local" settings-entity-id-version) name))
Dave Griffiths's avatar
Dave Griffiths committed
50 51 52

(define (set-setting! key type value)
  (update-entity
Dave Griffiths's avatar
Dave Griffiths committed
53
   db "local" settings-entity-id-version (list (ktv key type value))))
Dave Griffiths's avatar
Dave Griffiths committed
54 55 56 57 58 59

(define (get/inc-setting key)
  (let ((r (get-setting-value key)))
    (set-setting! key "int" (+ r 1))
    r))

Dave Griffiths's avatar
Dave Griffiths committed
60
(set-current! 'user-id (get-setting-value "user-id"))
Dave Griffiths's avatar
Dave Griffiths committed
61 62
(set! i18n-lang (get-setting-value "language"))

Dave Griffiths's avatar
Dave Griffiths committed
63
;;(display (db-all db "local" "app-settings"))(newline)
Dave Griffiths's avatar
Dave Griffiths committed
64

Dave Griffiths's avatar
Dave Griffiths committed
65 66 67 68
(define tribes-list '(khasi other))
(define subtribe-list '(khynriam pnar bhoi war other))
(define education-list   '(illiterate literate primary middle high secondary university))
(define married-list '(ever-married currently-married currently-single seperated))
69 70 71
(define residence-list '(birthplace spouse-village))
(define gender-list '(male female))
(define occupation-list '(agriculture gathering labour cows fishing other))
Dave Griffiths's avatar
Dave Griffiths committed
72
(define house-type-list '(concrete tin thatched other))
Dave Griffiths's avatar
Dave Griffiths committed
73

Dave Griffiths's avatar
Dave Griffiths committed
74
(define social-types-list '(friendship knowledge prestige))
Dave Griffiths's avatar
Dave Griffiths committed
75 76 77 78
(define social-relationship-list '(mother father sister brother spouse children co-wife spouse-mother spouse-father spouse-brother-wife spouse-sister-husband friend neighbour other))
(define social-residence-list '(same other))
(define social-strength-list '(daily weekly monthly less))

Dave Griffiths's avatar
Dave Griffiths committed
79 80
(define village-ktvlist
  (list
Dave Griffiths's avatar
Dave Griffiths committed
81 82 83 84
   (ktv "name" "varchar" (mtext-lookup 'default-village-name))
   (ktv "block" "varchar" "")
   (ktv "district" "varchar" "test")
   (ktv "car" "int" 0)))
Dave Griffiths's avatar
Dave Griffiths committed
85 86 87

(define household-ktvlist
  (list
Dave Griffiths's avatar
Dave Griffiths committed
88 89
   (ktv "name" "varchar" (mtext-lookup 'default-household-name))
   (ktv "num-pots" "int" 0)
90
   (ktv "num-children" "int" 0)
Dave Griffiths's avatar
Dave Griffiths committed
91 92 93 94
   (ktv "house-lat" "real" 0) ;; get from current location?
   (ktv "house-lon" "real" 0)
   (ktv "toilet-lat" "real" 0)
   (ktv "toilet-lon" "real" 0)))
Dave Griffiths's avatar
Dave Griffiths committed
95

96 97
(define individual-ktvlist
  (list
Dave Griffiths's avatar
Dave Griffiths committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
   (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)
137
   ))
Dave Griffiths's avatar
Dave Griffiths committed
138

139 140
(define crop-ktvlist
  (list
Dave Griffiths's avatar
Dave Griffiths committed
141 142 143 144 145
   (ktv "name" "varchar" (mtext-lookup 'default-crop-name))
   (ktv "unit" "varchar" "unit")
   (ktv "used" "real" 0)
   (ktv "sold" "real" 0)
   (ktv "seed" "varchar" "")))
146

147 148
(define child-ktvlist
  (list
Dave Griffiths's avatar
Dave Griffiths committed
149 150 151 152 153
   (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)))
154 155


Dave Griffiths's avatar
Dave Griffiths committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;

(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)
Dave Griffiths's avatar
Dave Griffiths committed
170 171 172
;     (when (zero? (random 10))
;           (msg "mangling...")
;           (mangle-test! db "sync" entity-types))
Dave Griffiths's avatar
Dave Griffiths committed
173
     (msg "one")
Dave Griffiths's avatar
Dave Griffiths committed
174 175 176 177
     (set-current! 'upload 0)
     (set-current! 'download 0)
     (connect-to-net
      (lambda ()
Dave Griffiths's avatar
Dave Griffiths committed
178
        (msg "connected, going in...")
Dave Griffiths's avatar
Dave Griffiths committed
179 180 181
        (append
         (list (toast "sync-cb"))
         (upload-dirty db)
Dave Griffiths's avatar
Dave Griffiths committed
182
         (if (have-dirty? db "sync") '() (suck-new db "sync"))))))
Dave Griffiths's avatar
Dave Griffiths committed
183 184
    (else '()))
   (list
Dave Griffiths's avatar
Dave Griffiths committed
185
    (delayed "debug-timer" (+ 10000 (random 5000)) debug-timer-cb)
Dave Griffiths's avatar
Dave Griffiths committed
186 187 188 189 190 191 192 193 194 195 196
    (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)
Dave Griffiths's avatar
Dave Griffiths committed
197

Dave Griffiths's avatar
Dave Griffiths committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
     (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

Dave Griffiths's avatar
Dave Griffiths committed
234

Dave Griffiths's avatar
Dave Griffiths committed
235 236 237
(define-fragment-list

  (fragment
238 239
   "top"
   (horiz
Dave Griffiths's avatar
Dave Griffiths committed
240
    (image-view 0 "face" (layout 48 64 -1 'centre 0))
241
    (text-view (make-id "title") "" 30
Dave Griffiths's avatar
Dave Griffiths committed
242
               (layout 'fill-parent 'fill-parent 0.5 'centre 10))
243 244 245

    (linear-layout
     0 'vertical
Dave Griffiths's avatar
Dave Griffiths committed
246
     (layout 'fill-parent 'wrap-content 0.5 'centre 0)
247 248 249
     (list 0 0 0 0)

     (list
Dave Griffiths's avatar
Dave Griffiths committed
250 251 252 253
      (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))
Dave Griffiths's avatar
Dave Griffiths committed
254
      (text-view (make-id "top-photo-id") 'photo-id 20
Dave Griffiths's avatar
Dave Griffiths committed
255
                 (layout 'wrap-content 'wrap-content 1 'right 0)))))
Dave Griffiths's avatar
Dave Griffiths committed
256 257
   (lambda (fragment arg)
     (activity-layout fragment))
Dave Griffiths's avatar
Dave Griffiths committed
258
   (lambda (fragment arg) '())
Dave Griffiths's avatar
Dave Griffiths committed
259 260 261 262 263 264
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))


265 266 267 268
  (fragment
   "bottom"
   (linear-layout
    0 'horizontal
Dave Griffiths's avatar
Dave Griffiths committed
269
    (layout 'fill-parent 'fill-parent 1 'centre 0)
270 271
    (list 0 0 0 0)
    (list
272
     (mbutton-scale
273
      'save
274 275 276 277
      (lambda ()
        (list
         (alert-dialog
          "ok-check"
278
          (mtext-lookup 'save-are-you-sure)
279 280 281 282
          (lambda (v)
            (cond
             ((eqv? v 1)
              (entity-update-values!)
283
              (list))
284 285
             (else
              (list))))))))
286
     (mbutton-scale 'back (lambda () (list (finish-activity 1))))))
287 288 289 290 291 292 293 294
   (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
295 296 297 298


  )

Dave Griffiths's avatar
Dave Griffiths committed
299 300 301 302 303 304 305 306 307 308 309 310 311
(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))))
    (list
     (update-widget 'text-view (get-id "title") 'text
                    (get-current 'activity-title "Title not set"))
     (update-widget 'text-view (get-id "top-village") 'text (if village (string-append "Village: " village) ""))
     (update-widget 'text-view (get-id "top-household") 'text (if household (string-append "Household: " household) ""))
     (update-widget 'text-view (get-id "top-photo-id") 'text (if individual (string-append "Individual: " individual) "")))))



Dave Griffiths's avatar
Dave Griffiths committed
312 313 314 315
(define (build-activity . contents)
  (vert-fill
   (relative
    '(("parent-top"))
316
    colour-one ;;(list 100 100 255 127)
Dave Griffiths's avatar
Dave Griffiths committed
317 318 319 320 321 322 323 324 325
    (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"))
326
    colour-one
Dave Griffiths's avatar
Dave Griffiths committed
327 328 329 330
    (vert
     (spacer 5)
     (build-fragment "bottom" (make-id "bottom") fillwrap)))))

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

(define (grid-ify widgets n)
  (map
   (lambda (w)
     (linear-layout
      0 'horizontal
      (layout 'wrap-content 'wrap-content 1 'left 0)
      (list 0 0 0 0)
      w))
   (chop widgets n)))

(define (filter-set! l)
  (set-current! 'individual-filter l))

(define (filter-clear!)
  (filter-set! '()))

(define (filter-add! f)
  (set-current!
   'individual-filter
   (merge-filter f (get-current 'individual-filter '()))))

(define (filter-remove! key)
  (set-current!
   'individual-filter
   (delete-filter key (get-current 'individual-filter '()))))

(define (filter-get)
  (get-current 'individual-filter '()))

(define button-size (list (inexact->exact (round (* 192 0.9)))
                          (inexact->exact (round (* 256 0.9)))))

364

365
(define (update-individual-filter)
Dave Griffiths's avatar
Dave Griffiths committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  (let ((search (db-filter-only db "sync" "individual" (filter-get)
                                (list
                                 (list "photo" "file")
                                 (list "name" "varchar")))))
    (update-widget
     'linear-layout (get-id "choose-pics") 'contents
     (grid-ify
      (map
       (lambda (e)
         (let* ((id (ktv-get e "unique_id"))
                (image-name (ktv-get e "photo"))
                (image (if (image-invalid? image-name)
                           "face" (string-append "/sdcard/symbai/files/" image-name))))
           (cond
            ((> (length search) 50)
381 382
             (button
              (make-id (string-append "chooser-" id))
Dave Griffiths's avatar
Dave Griffiths committed
383
              (ktv-get e "name") 30 (layout (car button-size) (/ (cadr button-size) 3) 1 'centre 5)
384 385
              (lambda ()
                (set-current! 'choose-result id)
Dave Griffiths's avatar
Dave Griffiths committed
386 387 388 389
                (list (finish-activity 0)))))

            ((equal? image "face")
             (button
390
              (make-id (string-append "chooser-" id))
Dave Griffiths's avatar
Dave Griffiths committed
391
              (ktv-get e "name") 30 (layout (car button-size) (cadr button-size) 1 'centre 5)
392 393
              (lambda ()
                (set-current! 'choose-result id)
Dave Griffiths's avatar
Dave Griffiths committed
394
                (list (finish-activity 0)))))
395

Dave Griffiths's avatar
Dave Griffiths committed
396 397 398 399 400 401 402 403 404 405 406 407 408 409
            (else
             (vert
              (image-button
               (make-id (string-append "chooser-" id))
               image (layout (car button-size) (cadr button-size) 1 'centre 5)
               (lambda ()
                 (set-current! 'choose-result id)
                 (list (finish-activity 0))))
              (text-view 0 (ktv-get e "name") 20 (layout 'wrap-content 'wrap-content -1 'centre 0)))
             ))))
       search)
     3))))

(define (image/name-from-unique-id db table unique-id)
410
  (let ((e (get-entity-by-unique db table unique-id)))
Dave Griffiths's avatar
Dave Griffiths committed
411 412 413
    (list
     (ktv-get e "name")
     (ktv-get e "photo"))))
414 415 416 417 418 419

(define (build-person-selector id key filter request-code)
  (vert
   (mtitle id)
   (image-view (make-id (string-append (symbol->string id) "-image"))
               "face" (layout 240 320 -1 'centre 0))
Dave Griffiths's avatar
Dave Griffiths committed
420
   (mtext-small (string->symbol (string-append (symbol->string id) "-text")))
421 422 423 424 425 426 427 428
   (button
    (make-id (string-append "change-" (symbol->string id)))
    (mtext-lookup 'change-id)
    40 (layout 'fill-parent 'wrap-content -1 'centre 5)
    (lambda ()
      (filter-set! filter)
      (list (start-activity "individual-chooser" request-code ""))))))

Dave Griffiths's avatar
Dave Griffiths committed
429
(define (build-small-person-selector id key filter request-code)
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
  (linear-layout
   0 'vertical
   (layout 300 'wrap-content 1 'centre 10)
   (list 0 0 0 0)
   (list
    (mtitle id)
    (image-view (make-id (string-append (symbol->string id) "-image"))
                "face" (layout 120 160 -1 'centre 0))
    (mtext-small (string->symbol (string-append (symbol->string id) "-text")))
    (button
     (make-id (string-append "change-" (symbol->string id)))
     (mtext-lookup 'change-id)
     40 (layout 'wrap-content 'wrap-content -1 'centre 5)
     (lambda ()
       (filter-set! filter)
       (list (start-activity "individual-chooser" request-code "")))))))
Dave Griffiths's avatar
Dave Griffiths committed
446 447


448 449 450 451
;; from activity on result with request id: choose-code
;; todo determine *which* selector this came from...
(define (person-selector-return request-code key choose-code)
  (when (eqv? request-code choose-code)
Dave Griffiths's avatar
Dave Griffiths committed
452 453
        (entity-set-value! key "varchar" (get-current 'choose-result "not set"))
        (entity-update-values!)))
454 455 456 457

;; need to load from across entities, so need db, table
(define (update-person-selector db table id key)
  (let ((entity-id (entity-get-value key)))
Dave Griffiths's avatar
Dave Griffiths committed
458 459 460 461 462 463
    (let ((image-name (image/name-from-unique-id db table entity-id))
          (id (get-id (string-append (symbol->string id) "-image")))
          (text-id (get-id (string-append (symbol->string id) "-text"))))
      (if (image-invalid? (cadr image-name))
          (list
           (update-widget 'image-view id 'image "face")
Dave Griffiths's avatar
Dave Griffiths committed
464
           (update-widget 'text-view text-id 'text (or (car image-name) "")))
Dave Griffiths's avatar
Dave Griffiths committed
465
          (list
Dave Griffiths's avatar
Dave Griffiths committed
466
           (update-widget 'text-view text-id 'text (or (car image-name) ""))
Dave Griffiths's avatar
Dave Griffiths committed
467 468
           (update-widget 'image-view id 'external-image
                          (string-append dirname "files/" (cadr image-name))))))))
469

470
(define (build-social-connection id key type request-code shade)
Dave Griffiths's avatar
Dave Griffiths committed
471
  (let ((id-text (string-append (symbol->string id))))
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    (linear-layout
     0 'horizontal
     (layout 'wrap-content 'wrap-content 1 'centre 20)
     (if shade colour-one colour-two)
     (list
      (build-small-person-selector id key (list) request-code)
      (vert
       (horiz
        (mspinner-other-vert
         (string->symbol (string-append id-text "-relationship"))
         'social-relationship
         social-relationship-list
         (lambda (v)
           (entity-set-value! (string-append key "-relationship") "varchar"
                              (spinner-choice social-relationship-list v))
           '()))
        (mspinner-other-vert
         (string->symbol (string-append id-text "-residence"))
         'social-residence
         social-residence-list
         (lambda (v)
           (entity-set-value! (string-append key "-residence") "varchar"
                              (spinner-choice social-residence-list v)) '())))
       (vert
        (text-view 0 (mtext-lookup 'social-strength)
                   30 (layout 'wrap-content 'wrap-content 1 'centre 10))
        (spinner
Dave Griffiths's avatar
Dave Griffiths committed
499
         (make-id (string-append id-text "-strength-spinner"))
500 501 502 503 504
         (map mtext-lookup social-strength-list)
         (layout 'wrap-content 'wrap-content 1 'centre 0)
         (lambda (v)
           (entity-set-value! (string-append key "-strength") "varchar"
                              (spinner-choice social-strength-list v)) '()))))))))
Dave Griffiths's avatar
Dave Griffiths committed
505 506 507 508 509 510 511

(define (social-connection-return request-code key choose-code)
  (when (eqv? request-code choose-code)
        (entity-set-value! key "varchar" (get-current 'choose-result "not set"))))

(define (update-social-connection db table id key type request-code)
  (let ((id-text (string-append (symbol->string id))))
Dave Griffiths's avatar
Dave Griffiths committed
512
    (append
513
     (update-person-selector db table id key)
Dave Griffiths's avatar
Dave Griffiths committed
514 515 516 517 518 519 520 521
     (mupdate-spinner-other
      (string->symbol (string-append id-text "-relationship"))
      (string-append key "-relationship")
      social-relationship-list)
     (mupdate-spinner-other
      (string->symbol (string-append id-text "-residence"))
      (string-append key "-residence")
      social-residence-list)
Dave Griffiths's avatar
Dave Griffiths committed
522 523
     (list
      (mupdate-spinner
Dave Griffiths's avatar
Dave Griffiths committed
524
       (string->symbol (string-append id-text "-strength"))
Dave Griffiths's avatar
Dave Griffiths committed
525 526
       (string-append key "-strength")
       social-strength-list))
Dave Griffiths's avatar
Dave Griffiths committed
527 528
     )))

Dave Griffiths's avatar
Dave Griffiths committed
529 530 531 532
(define (build-amenity-widgets id shade)
  (let ((id-text (symbol->string id)))
    (horiz-colour
     (if shade colour-one colour-two)
Dave Griffiths's avatar
Dave Griffiths committed
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
     (linear-layout
      0 'vertical (layout 200 'wrap-content -1 'left 0)
      (list 0 0 0 0)
      (list
       (text-view (symbol->id id)
                  (mtext-lookup id)
                  30 (layout 'wrap-content 'wrap-content -1 'left 0))
       (mtoggle-button-scale
        (string->symbol (string-append id-text "-in-village"))
        (lambda (v)
          (entity-set-value! id-text "int" v)
          (list (update-widget
                 'edit-text
                 (get-id (string-append id-text "-closest-access-container"))
                 (if (eqv? v 1) 'hide 'show) 0))))))
Dave Griffiths's avatar
Dave Griffiths committed
548 549 550 551 552 553 554 555 556 557 558
     (medit-text-scale
      (string->symbol (string-append id-text "-closest-access"))
      "normal" (lambda (v) (entity-set-value!
                            (string-append id-text "-closest-access")
                            "varchar" v) '()))
     (vert
      (mbutton-scale
       (string->symbol (string-append id-text "-gps"))
       (lambda ()  (do-gps
                    (string->symbol (string-append id-text "-gps"))
                    (string-append id-text "-gps"))))
Dave Griffiths's avatar
Dave Griffiths committed
559 560
      (mtext-small (string->symbol (string-append id-text "-gps-lat")))
      (mtext-small (string->symbol (string-append id-text "-gps-lon")))))))
Dave Griffiths's avatar
Dave Griffiths committed
561 562 563 564 565

(define (update-amenity-widgets id)
  (let ((id-text (symbol->string id)))
    (append
     (list
Dave Griffiths's avatar
Dave Griffiths committed
566
      (mupdate 'toggle-button (string->symbol (string-append id-text "-in-village")) id-text)
Dave Griffiths's avatar
Dave Griffiths committed
567 568
      (mupdate 'edit-text
               (string->symbol (string-append id-text "-closest-access"))
Dave Griffiths's avatar
Dave Griffiths committed
569 570 571 572 573 574
               (string-append id-text "-closest-access"))
      (update-widget
       'edit-text
       (get-id (string-append id-text "-closest-access-container"))
       (if (eqv? (entity-get-value id-text) 1)
           'hide 'show) 0))
Dave Griffiths's avatar
Dave Griffiths committed
575 576 577 578 579
     (mupdate-gps
      (string->symbol (string-append id-text "-gps"))
      (string-append id-text "-gps")))))


Dave Griffiths's avatar
Dave Griffiths committed
580 581 582
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; activities

583
(define photo-code 999)
584
(define choose-code 998)
585 586 587
(define spouse-request-code 997)
(define mother-request-code 996)
(define father-request-code 995)
Dave Griffiths's avatar
Dave Griffiths committed
588

Dave Griffiths's avatar
Dave Griffiths committed
589 590 591 592 593 594
(define social-request-code-one 994)
(define social-request-code-two 993)
(define social-request-code-three 992)
(define social-request-code-four 991)
(define social-request-code-five 990)

Dave Griffiths's avatar
Dave Griffiths committed
595 596 597 598
(define-activity-list

  (activity
   "main"
599 600 601
   (build-activity
    (mtitle 'title)
    (horiz
Dave Griffiths's avatar
Dave Griffiths committed
602 603 604 605 606
     (medit-text 'user-id "normal"
                 (lambda (v)
                   (set-setting! "user-id" "varchar" v)
                   (set-current! 'user-id v)
                   (list)))
607

Dave Griffiths's avatar
Dave Griffiths committed
608 609
     (mspinner 'languages (list 'english 'khasi 'hindi)
               (lambda (c)
610 611
                 (set-setting! "language" "int" c)
                 (set! i18n-lang c)
Dave Griffiths's avatar
Dave Griffiths committed
612
                 (list)))
613
     (mbutton-scale 'find-individual (lambda () (list (start-activity "individual-chooser" choose-code "")))))
Dave Griffiths's avatar
Dave Griffiths committed
614

615
    (build-list-widget
Dave Griffiths's avatar
Dave Griffiths committed
616 617 618 619 620 621 622 623 624 625
     db "sync" 'households "household" "household" (lambda () (get-setting-value "current-village"))
     (lambda ()
       ;; autogenerate the name from the current ID
       (ktvlist-merge
        household-ktvlist
        (list (ktv "name" "varchar"
                   (string-append
                    (mtext-lookup 'default-household-name) "-"
                    (get-setting-value "user-id") "-"
                    (number->string (get/inc-setting "house-id"))))))))
Dave Griffiths's avatar
Dave Griffiths committed
626

Dave Griffiths's avatar
Dave Griffiths committed
627 628 629 630 631 632 633 634 635

    (mbutton 'villages (lambda () (list (start-activity "villages" 0 ""))))

    (mbutton 'sync (lambda () (list (start-activity "sync" 0 ""))))

    (horiz
     (medit-text 'house-id "numeric" (lambda (v) (set-setting! "house-id" "int" (string->number v)) (list)))
     (medit-text 'photo-id "numeric" (lambda (v) (set-setting! "photo-id" "int" (string->number v)) (list))))
    )
636

637 638 639
   (lambda (activity arg)
     (activity-layout activity))
   (lambda (activity arg)
Dave Griffiths's avatar
Dave Griffiths committed
640 641 642 643
     (set-current! 'activity-title "Main screen")
     (set-current! 'village (get-setting-value "current-village"))
     (set-current! 'household #f)
     (set-current! 'individual #f)
Dave Griffiths's avatar
Dave Griffiths committed
644
     (append
Dave Griffiths's avatar
Dave Griffiths committed
645
      (update-top-bar)
Dave Griffiths's avatar
Dave Griffiths committed
646 647 648 649 650 651 652 653 654 655 656
      (list
       (update-widget 'edit-text (get-id "user-id") 'text (get-setting-value "user-id"))
       (update-widget 'edit-text (get-id "house-id") 'text (get-setting-value "house-id"))
       (update-widget 'edit-text (get-id "photo-id") 'text (get-setting-value "photo-id"))
       (update-widget 'spinner (get-id "languages-spinner") 'selection
                      (get-setting-value "language"))
       (gps-start "gps" (lambda (loc)
                          (set-current! 'location loc)
                          (list (toast (string-append
                                        (number->string (car loc)) ", "
                                        (number->string (cadr loc)))))))
Dave Griffiths's avatar
Dave Griffiths committed
657
       (update-list-widget
Dave Griffiths's avatar
Dave Griffiths committed
658
        db "sync" "household" "household" (get-setting-value "current-village")))))
659 660 661 662
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
663 664
   (lambda (activity requestcode resultcode)
     (cond
665 666
      ((eqv? requestcode choose-code)
       (list (start-activity "individual" 0 (get-current 'choose-result 0))))
667 668 669 670 671 672
      ((eqv? requestcode photo-code)
       (list (update-widget
              'image-view (get-id "image")
              'external-image (string-append dirname "photo.jpg"))))
      (else
       '()))))
673

Dave Griffiths's avatar
Dave Griffiths committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
  (activity
   "villages"
   (build-activity
    (mspinner 'current-village '()
              (lambda (v)
                (set-setting! "current-village" "varchar"
                              (cadr (list-ref (get-current 'villages-list '()) v)))
                '()))
    (build-list-widget
     db "sync" 'villages "village" "village" (lambda () #f)
     (lambda () village-ktvlist)))


   (lambda (activity arg)
     (activity-layout activity))
   (lambda (activity arg)
     (set-current! 'activity-title "Villages")
     (set-current! 'villages-list (build-array-from-names db "sync" "village"))
     (append
      (update-top-bar)
      (list
       (update-widget 'spinner (get-id "current-village-spinner") 'array
                      (map car (get-current 'villages-list '())))
       (update-widget 'spinner (get-id "current-village-spinner") 'selection
                      (find-index-from-name-array
                       (get-current 'villages-list '())
                       (get-current 'village #f)))
       (update-list-widget db "sync" "village" "village" #f))))
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))


709 710 711

  (activity
   "village"
Dave Griffiths's avatar
Dave Griffiths committed
712 713
     (build-activity
      (horiz
714 715
       (medit-text 'village-name "normal" (lambda (v) (entity-set-value! "name" "varchar" v) '()))
       (medit-text 'block "normal" (lambda (v) (entity-set-value! "block" "varchar" v) '())))
Dave Griffiths's avatar
Dave Griffiths committed
716
      (horiz
717 718
       (medit-text 'district "normal" (lambda (v) (entity-set-value! "district" "varchar" v) '()))
       (mtoggle-button-scale 'car (lambda (v) (entity-set-value! "car" "int" v) '())))
719

Dave Griffiths's avatar
Dave Griffiths committed
720 721 722 723
      (mbutton 'household-list
               (lambda ()
                 (list (start-activity "household-list" 0
                                       (get-current 'village #f)))))
724

Dave Griffiths's avatar
Dave Griffiths committed
725
      (mtitle 'amenities)
Dave Griffiths's avatar
Dave Griffiths committed
726 727 728 729 730 731 732 733 734 735
      (build-amenity-widgets 'school #t)
      (build-amenity-widgets 'hospital #f)
      (build-amenity-widgets 'post-office #t)
      (build-amenity-widgets 'railway-station #f)
      (build-amenity-widgets 'state-bus-service #t)
      (build-amenity-widgets 'district-bus-service #f)
      (build-amenity-widgets 'panchayat #t)
      (build-amenity-widgets 'NGO #f)
      (build-amenity-widgets 'market #t)
      (delete-button))
Dave Griffiths's avatar
Dave Griffiths committed
736 737 738
   (lambda (activity arg)
     (activity-layout activity))
   (lambda (activity arg)
Dave Griffiths's avatar
Dave Griffiths committed
739
     (set-current! 'activity-title "Village")
740
     (entity-init! db "sync" "village" (get-entity-by-unique db "sync" arg))
741
     (set-current! 'village arg)
Dave Griffiths's avatar
Dave Griffiths committed
742 743
     (set-current! 'household #f)
     (set-current! 'individual #f)
Dave Griffiths's avatar
Dave Griffiths committed
744
     (append
Dave Griffiths's avatar
Dave Griffiths committed
745
      (update-top-bar)
Dave Griffiths's avatar
Dave Griffiths committed
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
      (list
       (mupdate 'edit-text 'village-name "name")
       (mupdate 'edit-text 'block "block")
       (mupdate 'edit-text 'district "district")
       (mupdate 'toggle-button 'car "car"))
      (update-amenity-widgets 'school)
      (update-amenity-widgets 'hospital)
      (update-amenity-widgets 'post-office)
      (update-amenity-widgets 'railway-station)
      (update-amenity-widgets 'state-bus-service)
      (update-amenity-widgets 'district-bus-service)
      (update-amenity-widgets 'panchayat)
      (update-amenity-widgets 'NGO)
      (update-amenity-widgets 'market)))

Dave Griffiths's avatar
Dave Griffiths committed
761 762 763 764
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
Dave Griffiths's avatar
Dave Griffiths committed
765
   (lambda (activity requestcode resultcode) '()))
Dave Griffiths's avatar
Dave Griffiths committed
766

Dave Griffiths's avatar
Dave Griffiths committed
767 768 769 770

  (activity
   "household-list"
   (build-activity
771
    (build-list-widget
Dave Griffiths's avatar
Dave Griffiths committed
772
     db "sync" 'households "household" "household" (lambda () (get-current 'village #f))
Dave Griffiths's avatar
Dave Griffiths committed
773 774 775 776 777 778 779 780 781
     (lambda ()
       ;; autogenerate the name from the current ID
       (ktvlist-merge
        household-ktvlist
        (list (ktv "name" "varchar"
                   (string-append
                    (mtext-lookup 'default-household-name) "-"
                    (get-setting-value "user-id") "-"
                    (number->string (get/inc-setting "house-id")))))))))
Dave Griffiths's avatar
Dave Griffiths committed
782 783
   (lambda (activity arg)
     (activity-layout activity))
784
   (lambda (activity arg)
Dave Griffiths's avatar
Dave Griffiths committed
785 786 787 788 789
     (set-current! 'activity-title "Household List")
     (append
      (update-top-bar)
      (list (update-list-widget
             db "sync" "household" "household" arg))))
Dave Griffiths's avatar
Dave Griffiths committed
790 791 792 793 794 795 796 797 798
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))

  (activity
   "household"
   (build-activity
799
    (horiz
800
     (medit-text 'household-name "normal" (lambda (v) (entity-set-value! "name" "varchar" v) '()))
801 802 803
     (vert
      (medit-text 'num-pots "numeric" (lambda (v) (entity-set-value! "num-pots" "int" v) '()))
      (medit-text 'num-children "numeric" (lambda (v) (entity-set-value! "num-children" "int" v) '()))))
804 805
    (horiz
     (vert
Dave Griffiths's avatar
Dave Griffiths committed
806 807 808 809
      (mtext 'location)
      (mbutton 'house-gps (lambda () (do-gps 'house "house")))
      (mtext-small 'house-lat)
      (mtext-small 'house-lon))
810
     (vert
Dave Griffiths's avatar
Dave Griffiths committed
811 812 813 814 815
      (mtext 'toilet-location)
      (mbutton 'toilet-gps (lambda () (do-gps 'toilet "toilet")))
      (mtext-small 'toilet-lat)
      (mtext-small 'toilet-lon)))

816 817

    (build-list-widget
Dave Griffiths's avatar
Dave Griffiths committed
818 819
     db "sync" 'individuals "individual" "individual"
     (lambda () (get-current 'household #f))
Dave Griffiths's avatar
Dave Griffiths committed
820
     (lambda ()
821 822 823 824 825 826 827 828 829 830 831 832 833 834
       (let ((photo-id (get/inc-setting "photo-id")))
         (ktvlist-merge
          individual-ktvlist
          (list
           (ktv "photo-id" "varchar"
                (string-append
                 (get-setting-value "user-id")
                 "-"
                 (number->string photo-id)))
           (ktv "social-type" "varchar"
                (symbol->string
                 (list-ref social-types-list
                           (modulo photo-id (length social-types-list)))))
         )))))
Dave Griffiths's avatar
Dave Griffiths committed
835

Dave Griffiths's avatar
Dave Griffiths committed
836

Dave Griffiths's avatar
Dave Griffiths committed
837
    (delete-button))
Dave Griffiths's avatar
Dave Griffiths committed
838 839
   (lambda (activity arg)
     (activity-layout activity))
840
   (lambda (activity arg)
Dave Griffiths's avatar
Dave Griffiths committed
841
     (set-current! 'activity-title "Household")
842 843
     (entity-init! db "sync" "household" (get-entity-by-unique db "sync" arg))
     (set-current! 'household arg)
Dave Griffiths's avatar
Dave Griffiths committed
844
     (set-current! 'individual #f)
Dave Griffiths's avatar
Dave Griffiths committed
845
     (append
Dave Griffiths's avatar
Dave Griffiths committed
846
      (update-top-bar)
Dave Griffiths's avatar
Dave Griffiths committed
847 848 849
      (list
       (update-list-widget db "sync" "individual" "individual" arg)
       (mupdate 'edit-text 'household-name "name")
850 851
       (mupdate 'edit-text 'num-pots "num-pots")
       (mupdate 'edit-text 'num-children "num-children"))
Dave Griffiths's avatar
Dave Griffiths committed
852 853
      (mupdate-gps 'house "house")
      (mupdate-gps 'toilet "toilet")))
854

Dave Griffiths's avatar
Dave Griffiths committed
855 856 857 858 859 860 861 862 863
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))

  (activity
   "individual"
   (build-activity
864 865 866
    (horiz
     (image-view (make-id "photo") "face" (layout 240 320 -1 'centre 10))
     (vert
Dave Griffiths's avatar
Dave Griffiths committed
867
      (mtext 'name-display)
868
      (spacer 20)
Dave Griffiths's avatar
Dave Griffiths committed
869
      (mtext 'family-display)
870
      (spacer 20)
871 872
      (mtext 'photo-id-display)
      ))
873
    (mbutton 'agreement-button (lambda () (list (start-activity "agreement" 0 ""))))
874
    (horiz
Dave Griffiths's avatar
Dave Griffiths committed
875 876
     (mbutton-scale 'details-button (lambda () (list (start-activity "details" 0 ""))))
     (mbutton-scale 'family-button (lambda () (list (start-activity "family" 0 "")))))
877
    (horiz
Dave Griffiths's avatar
Dave Griffiths committed
878 879
     (mbutton-scale 'migration-button (lambda () (list (start-activity "migration" 0 ""))))
     (mbutton-scale 'income-button (lambda () (list (start-activity "income" 0 "")))))
880
    (horiz
Dave Griffiths's avatar
Dave Griffiths committed
881 882
     (mbutton-scale 'geneaology-button (lambda () (list (start-activity "geneaology" 0 ""))))
     (mbutton-scale 'social-button (lambda () (list (start-activity "social" 0 "")))))
883
    (mbutton-scale 'move-button (lambda () (list (start-activity "move" 0 ""))))
884
    (spacer 20)
Dave Griffiths's avatar
Dave Griffiths committed
885
    (delete-button))
886

Dave Griffiths's avatar
Dave Griffiths committed
887 888
   (lambda (activity arg)
     (activity-layout activity))
Dave Griffiths's avatar
Dave Griffiths committed
889
   (lambda (activity arg)
Dave Griffiths's avatar
Dave Griffiths committed
890
     (set-current! 'activity-title "Individual")
Dave Griffiths's avatar
Dave Griffiths committed
891 892
     (entity-init! db "sync" "individual" (get-entity-by-unique db "sync" arg))
     (set-current! 'individual arg)
Dave Griffiths's avatar
Dave Griffiths committed
893
     (append
Dave Griffiths's avatar
Dave Griffiths committed
894
      (update-top-bar)
Dave Griffiths's avatar
Dave Griffiths committed
895 896 897 898 899
      (list
       (mupdate 'text-view 'name-display "name")
       (mupdate 'text-view 'family-display "family")
       (mupdate 'text-view 'photo-id-display "photo-id")
       (mupdate 'image-view 'photo "photo"))))
Dave Griffiths's avatar
Dave Griffiths committed
900 901 902 903 904 905 906 907 908
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))

  (activity
   "details"
   (build-activity
909
    (horiz
Dave Griffiths's avatar
Dave Griffiths committed
910

911
     (vert
Dave Griffiths's avatar
Dave Griffiths committed
912
      (image-view (make-id "photo") "face" (layout 240 320 -1 'centre 10))
913 914 915
      (mbutton
       'change-photo
       (lambda ()
916 917
         (set-current!
          'photo-name (string-append (entity-get-value "unique_id") "-" (get-unique "p") "-face.jpg"))
Dave Griffiths's avatar
Dave Griffiths committed
918
         (list
919
          (take-photo (string-append dirname "files/" (get-current 'photo-name "")) photo-code))
Dave Griffiths's avatar
Dave Griffiths committed
920 921
         )))

922
     (vert
923 924 925
      (medit-text 'details-name "normal" (lambda (v) (entity-set-value! "name" "varchar" v) '()))
      (medit-text 'details-family "normal" (lambda (v) (entity-set-value! "family" "varchar" v) '()))
      (medit-text 'details-photo-id "normal" (lambda (v) (entity-set-value! "photo-id" "varchar" v) '()))))
Dave Griffiths's avatar
Dave Griffiths committed
926
    (mspinner-other 'tribe tribes-list (lambda (v) (entity-set-value! "tribe" "varchar" (spinner-choice tribes-list v)) '()))
927
    (mspinner-other 'sub-tribe subtribe-list (lambda (v) (entity-set-value! "subtribe" "varchar" (spinner-choice subtribe-list v)) '()))
928
    (horiz
929
     (medit-text 'age "numeric" (lambda (v) (entity-set-value! "age" "int" v) '()))
930
     (mspinner 'gender gender-list (lambda (v) (entity-set-value! "gender" "varchar" (spinner-choice gender-list v)) '()))
931
     (mspinner 'education education-list (lambda (v) (entity-set-value! "education" "varchar" v) '())))
Dave Griffiths's avatar
Dave Griffiths committed
932 933 934
    )
   (lambda (activity arg)
     (activity-layout activity))
Dave Griffiths's avatar
Dave Griffiths committed
935
   (lambda (activity arg)
Dave Griffiths's avatar
Dave Griffiths committed
936
     (set-current! 'activity-title "Individual details")
Dave Griffiths's avatar
Dave Griffiths committed
937
     (append
Dave Griffiths's avatar
Dave Griffiths committed
938
      (update-top-bar)
Dave Griffiths's avatar
Dave Griffiths committed
939 940
      (mupdate-spinner-other 'tribe "tribe" tribes-list)
      (mupdate-spinner-other 'sub-tribe "subtribe" subtribe-list)
Dave Griffiths's avatar
Dave Griffiths committed
941 942 943 944 945 946 947 948 949
      (list
       (mupdate 'edit-text 'details-name "name")
       (mupdate 'edit-text 'details-family "family")
       (mupdate 'edit-text 'details-photo-id "photo-id")
       (mupdate 'image-view 'photo "photo")
       (mupdate 'edit-text 'age "age")
       (mupdate-spinner 'gender "gender" gender-list)
       (mupdate-spinner 'education "education" education-list)
       )))
Dave Griffiths's avatar
Dave Griffiths committed
950 951 952 953
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
Dave Griffiths's avatar
Dave Griffiths committed
954 955 956 957 958 959
   (lambda (activity requestcode resultcode)
     (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
960
       (let ((unique-id (entity-get-value "unique_id")))
961
         (entity-set-value! "photo" "file" (get-current 'photo-name "error no photo name!!"))
962 963 964
         (entity-update-values!)
         ;; need to reset the individual from the db now (as update reset it)
         (entity-init! db "sync" "individual" (get-entity-by-unique db "sync" unique-id)))
Dave Griffiths's avatar
Dave Griffiths committed
965 966 967 968
       (list
        (mupdate 'image-view 'photo "photo")))
      (else
       '()))))
Dave Griffiths's avatar
Dave Griffiths committed
969 970 971 972

  (activity
   "family"
   (build-activity
973 974
    (horiz
     (vert
975 976
      (mspinner 'head-of-house gender-list (lambda (v) (entity-set-value! "head-of-house" "varchar" (spinner-choice gender-list v)) '()))
      (mspinner 'marital-status married-list (lambda (v) (entity-set-value! "marital-status" "varchar" (spinner-choice married-list v)) '()))
977
      (medit-text 'times-married "numeric" (lambda (v) (entity-set-value! "times-married" "int" v) '())))
978 979 980

     (build-person-selector 'spouse "id-spouse" (list) spouse-request-code)
     )
981

982 983 984 985 986 987 988 989 990 991 992
;;    (mtitle 'children)
;;    (horiz
;;     (medit-text 'children-living "numeric" (lambda (v) (entity-set-value! "children-living" "int" v) '()))
;;     (medit-text 'children-dead "numeric" (lambda (v) (entity-set-value! "children-dead" "int" v) '())))
;;    (horiz
;;     (medit-text 'children-together "numeric" (lambda (v) (entity-set-value! "children-together" "int" v) '()))
;;     (medit-text 'children-apart "numeric" (lambda (v) (entity-set-value! "children-apart" "int" v) '())))

    (mspinner-other 'residence-after-marriage residence-list (lambda (v) (entity-set-value!
                                                                          "residence-after-marriage" "varchar"
                                                                          (spinner-choice residence-list v)) '()))
993 994
    (medit-text 'num-siblings "numeric" (lambda (v) (entity-set-value! "num-siblings" "int" v) '()))
    (medit-text 'birth-order "numeric" (lambda (v) (entity-set-value! "birth-order" "int" v) '())))
Dave Griffiths's avatar
Dave Griffiths committed
995
   (lambda (activity arg)
Dave Griffiths's avatar
Dave Griffiths committed
996
     (activity-layout activity))
Dave Griffiths's avatar
Dave Griffiths committed
997
   (lambda (activity arg)
Dave Griffiths's avatar
Dave Griffiths committed
998
     (set-current! 'activity-title "Individual family")
Dave Griffiths's avatar
Dave Griffiths committed
999
     (append
Dave Griffiths's avatar
Dave Griffiths committed
1000
      (update-top-bar)
1001
      (update-person-selector db "sync" 'spouse "id-spouse")
Dave Griffiths's avatar
Dave Griffiths committed
1002
      (list
1003
       (mupdate-spinner 'head-of-house "head-of-house" gender-list)
Dave Griffiths's avatar
Dave Griffiths committed
1004 1005 1006
       (mupdate-spinner 'marital-status "marital-status" married-list)
       (mupdate 'edit-text 'times-married "times-married")
       ;;(mupdate 'id-spouse "id-spouse")
1007 1008 1009 1010 1011
;;       (mupdate 'edit-text 'children-living "children-living")
;;       (mupdate 'edit-text 'children-dead "children-dead")
;;       (mupdate 'edit-text 'children-together "children-together")
;;       (mupdate 'edit-text 'children-apart "children-apart")
       (mupdate-spinner 'residence-after-marriage "residence-after-marriage" residence-list)
Dave Griffiths's avatar
Dave Griffiths committed
1012 1013
       (mupdate 'edit-text 'num-siblings "num-siblings")
       (mupdate 'edit-text 'birth-order "birth-order"))))
Dave Griffiths's avatar
Dave Griffiths committed
1014 1015 1016 1017
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
1018 1019
   (lambda (activity requestcode resultcode)
     (person-selector-return requestcode "id-spouse" spouse-request-code)
1020 1021 1022 1023 1024

     ;; intercept a spouse setting and set the other individual
     ;; BIDIRECTIONAL AUTOSPOUSE
     (when (and (eqv? requestcode spouse-request-code)
                (get-current 'choose-result #f))
Dave Griffiths's avatar
Dave Griffiths committed
1025
           (update-entity db "sync" (entity-id-from-unique db "sync" (get-current 'choose-result #f))
Dave Griffiths's avatar
Dave Griffiths committed
1026
                          (list (ktv "id-spouse" "varchar" (entity-get-value "unique_id")))))
1027 1028 1029 1030 1031 1032 1033

     ;; save and reinit otherwise we can get out of sync here with the spouse :/
     (let ((unique-id (entity-get-value "unique_id")))
       (entity-update-values!)
       ;; need to reset the individual from the db now (as update reset it)
       (entity-init! db "sync" "individual" (get-entity-by-unique db "sync" unique-id)))

1034
     '()))
Dave Griffiths's avatar
Dave Griffiths committed
1035

1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
  (activity
   "move"
   (build-activity
    (mspinner 'move-household '()
              (lambda (v)
                (entity-set-value!
                 "parent" "varchar"
                 (cadr (list-ref (get-current 'move-household-list '()) v)))
                '())))
   (lambda (activity arg)
     (activity-layout activity))
   (lambda (activity arg)
Dave Griffiths's avatar
Dave Griffiths committed
1048
     (set-current! 'activity-title "Move individual")
1049
     (set-current! 'move-household-list (build-array-from-names db "sync" "household"))
Dave Griffiths's avatar
Dave Griffiths committed
1050
     (append
Dave Griffiths's avatar
Dave Griffiths committed
1051
      (update-top-bar)
Dave Griffiths's avatar
Dave Griffiths committed
1052 1053 1054
      (list
       (update-widget 'spinner (get-id "move-household-spinner") 'array
                      (map car (get-current 'move-household-list '()))))))
1055