starwisp.scm 13 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 21 22 23 24 25 26 27
;; 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


(define trans-col (list 0 0 0 0))

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

Dave Griffiths's avatar
Dave Griffiths committed
28
(define db "/sdcard/starwisp/local-symbai.db")
Dave Griffiths's avatar
Dave Griffiths committed
29 30 31 32 33 34 35 36 37 38
(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
39
;;(display (db-all db "local" "app-settings"))(newline)
Dave Griffiths's avatar
Dave Griffiths committed
40 41 42 43 44


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

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
;;;;;;;;;;;;; i18n ;;;;;;;;;;;;;;;;;;;;;;

(define i18n-lang 0)

(define i18n-text
  (list
   (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"))
   (list 'user-id (list "User ID" "User ID" "User ID"))
   (list 'ok (list "Ok" "Ok" "Ok"))
   (list 'cancel (list "Cancel" "Cancel" "Cancel"))
   ))

(define (mtext-lookup id)
  (alog id)
  (define (_ l)
    (cond
     ((null? l) (string-append (symbol->string id) " not translated"))
     ((eq? (car (car l)) id) (list-ref (cadr (car l)) i18n-lang))
     (else (_ (cdr l)))))
  (_ i18n-text))

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

Dave Griffiths's avatar
Dave Griffiths committed
73
(define (mbutton id title fn)
74 75 76
  (button (make-id id)
          (mtext-lookup title)
          40 (layout 'fill-parent 'wrap-content -1 'left 5) fn))
77 78

(define (mbutton-scale id title fn)
79 80 81
  (button (make-id id)
          (mtext-lookup title)
          40 (layout 'fill-parent 'wrap-content 1 'left 5) fn))
Dave Griffiths's avatar
Dave Griffiths committed
82 83

(define (mtoggle-button id title fn)
84 85 86
  (toggle-button (make-id id)
                 (mtext-lookup title)
                 30 (layout 'fill-parent 'wrap-content 1 'left 0) "fancy" fn))
Dave Griffiths's avatar
Dave Griffiths committed
87 88

(define (mtoggle-button-yes id title fn)
89 90 91
  (toggle-button (make-id id)
                 (mtext-lookup title)
                 30 (layout 49 43 1 'left 0) "yes" fn))
Dave Griffiths's avatar
Dave Griffiths committed
92 93

(define (mtoggle-button-maybe id title fn)
94 95 96
  (toggle-button (make-id id)
                 (mtext-lookup title)
                 30 (layout  49 43 1 'left 0) "maybe" fn))
Dave Griffiths's avatar
Dave Griffiths committed
97 98

(define (mtoggle-button-no id title fn)
99 100 101
  (toggle-button (make-id id)
                 (mtext-lookup title)
                 30 (layout  49 43 1 'left 0) "no" fn))
Dave Griffiths's avatar
Dave Griffiths committed
102 103

(define (mtext id text)
104 105 106 107 108 109 110 111
  (text-view (make-id id)
             (mtext-lookup text)
             30 (layout 'wrap-content 'wrap-content -1 'left 0)))

(define (mtext-scale id text)
  (text-view (make-id id)
             (mtext-lookup text)
             30 (layout 'wrap-content 'wrap-content 1 'left 0)))
Dave Griffiths's avatar
Dave Griffiths committed
112 113

(define (mtitle id text)
114 115 116
  (text-view (make-id id)
             (mtext-lookup text)
             50 (layout 'fill-parent 'wrap-content -1 'left 0)))
Dave Griffiths's avatar
Dave Griffiths committed
117 118 119 120 121 122

(define (medit-text id text type fn)
  (vert
   (mtext (string-append id "-title") text)
   (edit-text (make-id id) "" 30 type fillwrap fn)))

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
(define (mspinner id name types fn)
   (vert
    (mtext "" name)
    (spinner (make-id (string-append id "-spinner")) (map mtext-lookup types) fillwrap
             (lambda (c) (fn c)))))

(define (mspinner-other id name types fn)
  (horiz
   (mspinner id name types fn (lambda (c) (fn c)))
   (vert
    (mtext "" 'other)
    (edit-text (make-id (string-append id "-edit-text")) "" 30 "normal" fillwrap
               (lambda (t) (fn t))))))


Dave Griffiths's avatar
Dave Griffiths committed
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 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
(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))

;;;;

(define (db-mongooses-by-pack)
  (db-all-where
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))))

(define (db-mongooses-by-pack-male)
  (db-all-where2or
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))
   (ktv "gender" "varchar" "Male") "Unknown"))

(define (db-mongooses-by-pack-female)
  (db-all-where2or
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))
   (ktv "gender" "varchar" "Female") "Unknown"))


;; (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 (db-mongooses-by-pack-pups)
  (db-all-newer
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))
   (ktv "dob" "varchar" (date->string (date-minus-months (date-time) 6)))))

(define (db-mongooses-by-pack-adults)
  (db-all-older
   db "sync" "mongoose"
   (ktv "pack-id" "varchar" (ktv-get (get-current 'pack '()) "unique_id"))
   (ktv "dob" "varchar" (date->string (date-minus-months (date-time) 6)))))

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

(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 ()
        (append
         (list (toast "sync-cb"))
         (upload-dirty db)
         (suck-new db "sync")))))
    (else '()))
   (list
    (delayed "debug-timer" (+ 5000 (random 5000)) debug-timer-cb)
    (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
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
   "top"
   (horiz
    (image-view 0 "face" (layout 48 64 -1 'left 0))
    (text-view (make-id "") 'title 30
               (layout 'fill-parent 'fill-parent 0.25 'centre 10))

    (linear-layout
     0 'vertical
     (layout 'fill-parent 'wrap-content 0.75 'left 0)
     (list 0 0 0 0)

     (list
      (text-view (make-id "") 'name 20
                 (layout 'fill-parent 'wrap-content 1 'left 0))
      (text-view (make-id "") 'photoid 20
                 (layout 'fill-parent 'wrap-content 1 'left 0)))))
Dave Griffiths's avatar
Dave Griffiths committed
288 289 290
   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
291
     (list))
Dave Griffiths's avatar
Dave Griffiths committed
292 293 294 295 296 297
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))


298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
  (fragment
   "bottom"
   (linear-layout
    0 'horizontal
    (layout 'fill-parent 'fill-parent 1 'left 0)
    (list 0 0 0 0)
    (list
     (mbutton-scale "cancel" 'cancel (lambda () (list)))
     (mbutton-scale "ok" 'ok (lambda () (list)))))
   (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
315 316 317 318 319 320 321 322 323 324 325 326 327 328


  )

(msg "one")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; activities


(define-activity-list

  (activity
   "main"
Dave Griffiths's avatar
Dave Griffiths committed
329

330
   (vert-fill
331
    (relative
332
     '(("parent-top"))
333 334 335 336 337 338 339
     (list 100 100 255 127)
     (build-fragment "top" (make-id "top") fillwrap))

    (scroll-view-vert
     0 (layout 'fill-parent 'fill-parent 1 'left 0)
     (list
      (vert-fill
340

341
       (mtitle "" 'title)
342

343 344 345
       (horiz
        (medit-text "user-id" 'user-id "normal" (lambda () (list)))
        (mbutton-scale "sync" 'sync (lambda () (list))))
346

347 348 349 350 351 352
       (mspinner "languages" 'languages (list 'english 'khasi 'hindi) (lambda (c) (list)))
       (mbutton "new-village" 'new-village (lambda () (list (start-activity "village" 0 ""))))

       )))

    (relative
353
     '(("parent-bottom"))
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
     (list 100 100 255 127)
     (vert
      (spacer 5)
      (build-fragment "bottom" (make-id "bottom") fillwrap)))
    )

   (lambda (activity arg)
     (activity-layout activity))
   (lambda (activity arg)
     (let ((user-id (ktv-get (get-entity db "local" 1) "user-id")))
       (set-current! 'user-id user-id)
       (list)))
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))


  (activity
   "village"
Dave Griffiths's avatar
Dave Griffiths committed
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 405 406 407 408 409 410
   (vert-fill
    (relative
     '(("parent-top"))
     (list 100 100 255 127)
     (build-fragment "top" (make-id "top") fillwrap))

    (scroll-view-vert
     0 (layout 'fill-parent 'fill-parent 1 'left 0)
     (list
      (vert-fill
       ;;(image-view (make-id "face") "face" (layout 640 470 1 'left 0))
       (mtitle "" 'title)
       (mtext "" 'database)
       (mbutton "main-sync" 'sync (lambda () (list (start-activity "sync" 0 ""))))
       (mspinner-other "test" 'test (list 'one 'two 'three) (lambda (c) (list)))
       (mbutton "main-sync" 'sync (lambda () (list (start-activity "sync" 0 ""))))
       (mspinner-other "test" 'test (list 'one 'two 'three) (lambda (c) (list)))
       (mbutton "main-sync" 'sync (lambda () (list (start-activity "sync" 0 ""))))
       (mspinner-other "test" 'test (list 'one 'two 'three) (lambda (c) (list)))
       (mbutton "main-sync" 'sync (lambda () (list (start-activity "sync" 0 ""))))
       (mspinner-other "test" 'test (list 'one 'two 'three) (lambda (c) (list)))
       (mbutton "main-sync" 'sync (lambda () (list (start-activity "sync" 0 ""))))
       (mspinner-other "test" 'test (list 'one 'two 'three) (lambda (c) (list)))
       (mbutton "main-sync" 'sync (lambda () (list (start-activity "sync" 0 ""))))
       (mspinner-other "test" 'test (list 'one 'two 'three) (lambda (c) (list)))
       (mbutton "main-sync" 'sync (lambda () (list (start-activity "sync" 0 ""))))
       (mspinner-other "test" 'test (list 'one 'two 'three) (lambda (c) (list)))
       )))

    (relative
     '(("parent-bottom"))
     (list 100 100 255 127)
     (vert
      (spacer 5)
      (build-fragment "bottom" (make-id "bottom") fillwrap)))
Dave Griffiths's avatar
Dave Griffiths committed
411
    )
Dave Griffiths's avatar
Dave Griffiths committed
412 413 414 415 416 417 418 419 420 421 422 423
   (lambda (activity arg)
     (activity-layout activity))
   (lambda (activity arg)
     (let ((user-id (ktv-get (get-entity db "local" 1) "user-id")))
       (set-current! 'user-id user-id)
       (list)))
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
  (activity
   "sync"
   (vert
    )

   (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 ""))
       (update-widget 'text-view (get-id "sync-dirty") 'text (build-dirty))
       )))
   (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
444

Dave Griffiths's avatar
Dave Griffiths committed
445 446


Dave Griffiths's avatar
Dave Griffiths committed
447
  )