module a5
-/*
- Mart Lubbers s4109503
- Charlie Gerhardus s3050009
-*/
+// Mart Lubbers s4109503
+// Charlie Gerhardus s3050009
import iTasks
-import Text // To check string length
-:: Idea = {idea :: String, details :: Maybe Note, user :: Name, number :: Int}
:: ProtoIdea = {idea :: String, details :: Maybe Note}
+:: Idea = {number :: Int, idea :: String, details :: Maybe Note, user :: Name,
+ likes :: Int}
:: Name:== String
-derive class iTask Idea, ProtoIdea // generic magic
+derive class iTask Idea, ProtoIdea
+
+instance == Idea where
+ (==) x y = x.Idea.number == y.Idea.number
ideas :: Shared [Idea]
ideas = sharedStore "Ideas" []
storeIdea :: Name ProtoIdea -> Idea
storeIdea n x=:{ProtoIdea | idea,details} =
- {Idea | user=n, idea=idea, details=details, number=42}
+ {Idea | user=n, idea=idea, details=details, likes=0, number=0}
addNewIdea :: Name -> Task [Idea]
addNewIdea name = enterInformation "Add new idea" [EnterWith (storeIdea name)]
>>* [OnAction ActionOk (hasValue (\v.return v))]
- >>= \n.upd (\is.[n:is]) ideas
-
-viewList = (enterChoiceWithShared "Idea collection" [] ideas)
- >&^ viewSharedInformation "Selected idea" []
+ >>= \n.upd (\is.is ++ [{n & number=length is}]) ideas
+
+likeIdea :: Idea [Idea] -> [Idea]
+likeIdea i x = let (ind, y) = removeIndex i x in
+ insertAt ind {i & likes=i.likes+1} y
+
+deleteIdea :: Idea [Idea] -> [Idea]
+deleteIdea i x = let (ind, y) = removeIndex i x in
+ let (b, g) = splitAt ind y in b ++ (map (\z.{z & number=z.number-1}) g)
+
+viewList :: Name -> Task [Idea]
+viewList name = (enterChoiceWithShared "Idea collection" [] ideas)
+ >&^ viewSharedInformation "Selected idea" []
+ >>* [OnAction ActionOk (hasValue (\_.get ideas)),
+ OnAction (Action "Delete All" [ActionIcon "delete"])
+ (always (upd (\_.[]) ideas)),
+ OnAction ActionDelete (hasValue (\v.upd (deleteIdea v) ideas)),
+ OnAction (Action "Like" [])
+ (ifValue (\x=:{user}.user <> name) (\v.upd (likeIdea v) ideas))]
mainTask = enterInformation "What is your username?" []
- >>= \name.forever (
- (addNewIdea name -|| viewList)
-// >>* [OnAction ActionOk (always (return (get ideas)))]
-// OnAction (Action "Delete All" [ActionIcon "delete"]) (always (upd (\_.[]) ideas))
-//// OnAction (Action "Delete" [ActionIcon "delete"]) (always (return Void)),
-//// OnAction (Action "Like" [ActionIcon "like"]) (always (return Void))
-// ]
- )
+ >>= \name.forever ((addNewIdea name -|| forever (viewList name)))
Start :: *World -> *World
Start world = startEngine mainTask world