60fb0a8da1568f2517df735ab401d447d753d1d0
[mTask.git] / Shares / mTaskShare.icl
1 implementation module Shares.mTaskShare
2
3 import dynamic_string
4 import Utils.SDS
5 import Utils.Devices
6 import iTasks
7 import mTask
8 import Data.List
9 from Data.Func import $
10 from StdFunc import flip
11
12 derive class iTask MTaskShareType, MTaskShare
13
14 manageShares :: [MTaskShare] -> Task ()
15 manageShares shares = withShared Nothing $ \cs->forever $
16 (viewSharesGrid cs shares -|| updateShares shares <<@ ArrangeVertical)
17 @! ()
18 updateShares :: [MTaskShare] -> Task BCValue
19 updateShares shares = anyTask (map updateS shares) <<@ ArrangeWithTabs
20
21 updateS :: MTaskShare -> Task BCValue
22 updateS sh = flip (<<@) (Title $ toString sh.identifier) $ forever $
23 viewSharedInformation "Current value" [] (getSDSShare sh)
24 ||- (
25 updateSharedInformation "New value" [] (getSDSShare sh)
26 >>= \nv->allTasks (map getDevice sh.withDevice)
27 >>= \devs->allTasks (map (sendMessages [MTUpd sh.identifier nv]) devs)
28 >>| treturn nv
29 )
30 <<@ ArrangeHorizontal
31
32 viewSharesGrid :: (Shared (Maybe MTaskShare)) [MTaskShare] -> Task [BCValue]
33 viewSharesGrid _ [] = viewInformation "No shares yet" [] []
34 viewSharesGrid cs sh = (allTasks [watch (getSDSShare m)\\m<-sh] <<@ NoUserInterface)
35 >&^ \st->flip (<<@) NoUserInterface $ whileUnchanged st $ \mshs->enterChoice "" [ChooseFromGrid id]
36 [{MTaskShare|ss&value=s}\\s<-fromJust mshs & ss<-sh]
37 // >>* [OnValue (withValue $ \s->Just (set (Just s) cs))]
38 @! fromJust mshs
39
40 viewShare :: MTaskShare -> Task BCValue
41 viewShare m = viewSharedInformation "" [] (getSDSShare m)
42 <<@ Title ("SDS: " +++ toString m.identifier)
43
44 getSDSShare :: MTaskShare -> Shared BCValue
45 getSDSShare s=:{realShare=(MTaskWithShare id),value} = memoryShare id value
46
47 makeShare :: String String Int BCValue -> Task MTaskShare
48 makeShare withTask withDevice identifier value = treturn
49 {MTaskShare
50 |withTask=[withTask]
51 ,withDevice=[withDevice]
52 ,identifier=identifier
53 ,value=value
54 ,realShare=MTaskWithShare $ "mTaskSDS-" +++ toString identifier
55 } >>= \sh->set value (getSDSShare sh) >>| treturn sh
56
57 updateShare :: Int BCValue -> Task ()
58 updateShare ident val = get sdsStore
59 >>= \sh->(case find (\s->s.identifier==ident) sh of
60 Nothing = abort "Help, no share found with this ident"
61 Just mts = set val (getSDSShare mts))
62 >>| traceValue "Updated" @! ()