starwisp.scm 12 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
;;;;;;;;;;;;; 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"))
Dave Griffiths's avatar
Dave Griffiths committed
60
   (list '+ (list "+" "+" "+"))
61 62 63 64 65 66 67 68 69 70 71 72 73
   ))

(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
74
(define (mbutton id title fn)
75 76 77
  (button (make-id id)
          (mtext-lookup title)
          40 (layout 'fill-parent 'wrap-content -1 'left 5) fn))
78 79

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

(define (mtoggle-button id title fn)
85 86 87
  (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
88 89

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

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

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

(define (mtext id text)
105 106 107 108 109 110 111 112
  (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
113 114

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

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

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
(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
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 272
(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
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
   "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
289 290 291
   (lambda (fragment arg)
     (activity-layout fragment))
   (lambda (fragment arg)
292
     (list))
Dave Griffiths's avatar
Dave Griffiths committed
293 294 295 296 297 298
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '())
   (lambda (fragment) '()))


299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
  (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
316 317 318 319 320 321 322 323 324 325 326 327 328 329


  )

(msg "one")

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


(define-activity-list

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

331
   (vert-fill
332
    (relative
333
     '(("parent-top"))
334 335 336 337 338 339 340 341 342 343
     (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
       (mtitle "" 'title)
       (horiz
        (medit-text "user-id" 'user-id "normal" (lambda () (list)))
Dave Griffiths's avatar
Dave Griffiths committed
344
        (mbutton-scale "sync-button" 'sync (lambda () (list))))
345

346
       (mspinner "languages" 'languages (list 'english 'khasi 'hindi) (lambda (c) (list)))
Dave Griffiths's avatar
Dave Griffiths committed
347 348 349 350 351
       (horiz
        (mtext "" 'villages)
        (mbutton "new-village" '+
                 (lambda ()
                   (list (start-activity "village" 0 ""))))))))
352 353

    (relative
354
     '(("parent-bottom"))
355 356 357 358 359 360 361 362 363
     (list 100 100 255 127)
     (vert
      (spacer 5)
      (build-fragment "bottom" (make-id "bottom") fillwrap)))
    )

   (lambda (activity arg)
     (activity-layout activity))
   (lambda (activity arg)
Dave Griffiths's avatar
Dave Griffiths committed
364
     (list))
365 366 367 368 369 370 371 372 373
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity) '())
   (lambda (activity requestcode resultcode) '()))


  (activity
   "village"
Dave Griffiths's avatar
Dave Griffiths committed
374

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
   (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)))
       )))

    (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
398
    )
Dave Griffiths's avatar
Dave Griffiths committed
399 400 401 402 403 404 405 406 407 408 409 410
   (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) '()))

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
  (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
431

Dave Griffiths's avatar
Dave Griffiths committed
432 433


Dave Griffiths's avatar
Dave Griffiths committed
434
  )