Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed May 7, 2021
1 parent 848beec commit c7ccddc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
47 changes: 19 additions & 28 deletions src/OrgSelf/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import Data.Default (Default (..))
import qualified Data.Map.Strict as Map
import Data.Org (OrgFile)
import qualified Data.Org as Org
import qualified Data.Set as Set
import Data.Tagged (Tagged (Tagged))
import qualified Data.Text as T
import Data.Time (defaultTimeLocale, parseTimeM)
import Data.Time.Calendar (Day)
import OrgSelf.Data.Measure (Measures, parseMeasures)
import OrgSelf.Data.Tag (TagStore)
import qualified OrgSelf.Data.Tag as TagStore
import qualified OrgSelf.Data.Tag as Tag
import OrgSelf.Data.Task (Task)
import qualified OrgSelf.Data.Task as Task
import System.FilePath (takeFileName)
Expand Down Expand Up @@ -45,45 +43,38 @@ data DiaryUpdate
| DiaryDel Day
deriving (Eq, Show)

-- TODO: Use lens to make this nicer.
diaryUpdate :: DiaryUpdate -> Diary -> Diary
diaryUpdate action diary =
case action of
DiaryModify day orgFile ->
diary
{ diaryCal =
Map.insert
day
(orgFile, parseMeasures $ Org.orgMeta orgFile)
$ diaryCal diary,
diaryCal diary
& Map.insert
day
(orgFile, parseMeasures $ Org.orgMeta orgFile),
diaryTasks =
Map.insert
day
(Task.queryTasks orgFile)
$ diaryTasks diary,
diaryTasks diary
& Map.insert
day
(Task.queryTasks orgFile),
diaryTags =
let tags :: Set TagStore.Tag = (Set.map Tagged . Org.allDocTags . Org.orgDoc $ orgFile)
in diaryTags diary
& TagStore.delTagsForDay day
& TagStore.addTagsForDay day tags
diaryTags diary
& Tag.delTagsForDay day
& Tag.addTagsForDay day (Tag.tagsFrom orgFile)
}
DiaryDel day ->
diary
{ diaryCal =
Map.delete day $ diaryCal diary,
diaryCal diary
& Map.delete day,
diaryTasks =
Map.delete day $ diaryTasks diary,
diaryTasks diary
& Map.delete day,
diaryTags =
maybe
(diaryTags diary)
( foldl'
(flip (TagStore.delTagFromDay day))
(diaryTags diary)
. Set.map Tagged
. Org.allDocTags
. Org.orgDoc
. fst
)
(Map.lookup day $ diaryCal diary)
diaryTags diary
& Tag.delTagsForDay day
}

parseDay :: String -> Maybe Day
Expand Down
14 changes: 5 additions & 9 deletions src/OrgSelf/Data/Tag.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module OrgSelf.Data.Tag where

import qualified Data.Map.Strict as Map
import qualified Data.Org as Org
import qualified Data.Set as Set
import Data.Tagged
import Data.Time.Calendar (Day)
Expand All @@ -12,6 +13,10 @@ type Tag = Tagged "Tag" Text

type TagStore = Map Tag (Set Day)

tagsFrom :: Org.OrgFile -> Set Tag
tagsFrom =
Set.map Tagged . Org.allDocTags . Org.orgDoc

addTagsForDay :: Day -> Set Tag -> TagStore -> TagStore
addTagsForDay day =
flip $ foldl' (flip (addTagForDay day))
Expand All @@ -24,12 +29,3 @@ addTagsForDay day =
delTagsForDay :: Day -> TagStore -> TagStore
delTagsForDay day =
Map.map $ Set.delete day

delTagFromDay :: Day -> Tag -> TagStore -> TagStore
delTagFromDay v k m =
case Map.lookup k m of
Nothing -> m
Just vs ->
if Set.member v vs
then Map.insert k (Set.delete v vs) m
else m

0 comments on commit c7ccddc

Please sign in to comment.