Skip to content

Commit 5125bf9

Browse files
committed
can save favorite rates
1 parent 982ea09 commit 5125bf9

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

ghcjs/currency-converter/src/App/Widgets/Fav.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ fav st =
119119
let nextFavName = makeFavName nextSt
120120
in nextSt
121121
& ( Misc.textPopupPure
122-
$ "Removed "
122+
$ "Removed"
123+
<> ( if nextFavName == mempty
124+
then mempty
125+
else " "
126+
)
123127
<> nextFavName
124128
<> "!"
125129
)
@@ -138,7 +142,8 @@ makeFavName st =
138142
^. #modelState
139143
. #stDoc
140144
. #stDocPreFavName
141-
. #fieldOutput
145+
. #fieldInput
146+
. #uniqueValue
142147

143148
favItems :: Model -> [View Action]
144149
favItems st =

ghcjs/currency-converter/src/App/Widgets/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ mainWidget st =
4444
-- . (: mempty)
4545
-- $ div_
4646
-- mempty
47-
-- [ inspect $ st ^. #modelFavMap
47+
-- [ text . inspect $ st ^. #modelFavMap
4848
-- ],
4949
tosWidget,
5050
Snackbar.snackbar (Snackbar.config Misc.textPopupClosed)

ghcjs/currency-converter/src/Functora/Miso/Prelude.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ instance ConvertUtf8 MisoString ByteString where
6161
encodeUtf8 = encodeUtf8 . from @MisoString @Prelude.Text
6262
decodeUtf8 = from @Prelude.Text @MisoString . decodeUtf8
6363
decodeUtf8Strict = fmap (from @Prelude.Text @MisoString) . decodeUtf8Strict
64+
65+
instance ToJSONKey MisoString where
66+
toJSONKey = contramap (fromMisoString @Prelude.Text) $ toJSONKey
67+
68+
instance FromJSONKey MisoString where
69+
fromJSONKey = fmap toMisoString $ fromJSONKey @Prelude.Text
6470
#endif
6571

6672
inspect :: (Show a, Data a) => a -> MisoString

ghcjs/currency-converter/src/Main.hs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,33 @@ updateModel (InitUpdate ext) prevSt = do
127127
{ modelProducerQueue = prod,
128128
modelConsumerQueue = cons
129129
}
130-
if isJust ext
131-
then Misc.pushActionQueue nextSt $ ChanItem 0 (& #modelLoading .~ False)
132-
else Storage.selectStorage ("current-" <> vsn) $ \case
133-
Nothing ->
134-
Misc.pushActionQueue nextSt $ ChanItem 0 (& #modelLoading .~ False)
135-
Just uri -> do
136-
finSt <- newModel (nextSt ^. #modelWebOpts) (Just nextSt) uri
137-
Misc.pushActionQueue nextSt
138-
$ ChanItem 0 (const $ finSt & #modelLoading .~ False)
130+
Storage.selectStorage ("favorite-" <> vsn) $ \mFav -> do
131+
let fav = mergeMap (const id) $ fromMaybe mempty mFav
132+
if isJust ext
133+
then
134+
Misc.pushActionQueue
135+
nextSt
136+
. ChanItem 0
137+
$ (& #modelFavMap %~ fav)
138+
. (& #modelLoading .~ False)
139+
else Storage.selectStorage ("current-" <> vsn) $ \case
140+
Nothing ->
141+
Misc.pushActionQueue nextSt
142+
. ChanItem 0
143+
$ (& #modelFavMap %~ fav)
144+
. (& #modelLoading .~ False)
145+
Just uri -> do
146+
finSt <- newModel (nextSt ^. #modelWebOpts) (Just nextSt) uri
147+
Misc.pushActionQueue nextSt
148+
$ ChanItem
149+
0
150+
( const
151+
$ finSt
152+
& #modelFavMap
153+
%~ fav
154+
& #modelLoading
155+
.~ False
156+
)
139157
pure
140158
$ ChanUpdate nextSt
141159
]
@@ -181,6 +199,8 @@ updateModel (ChanUpdate prevSt) _ = do
181199
)
182200
$ foldlM (\acc updater -> evalModel $ updater acc) prevSt actions
183201
uri <- URI.mkURI $ shareLink nextSt
202+
let fm = nextSt ^. #modelFavMap
203+
when (fm /= mempty) $ Storage.insertStorage ("favorite-" <> vsn) fm
184204
Storage.insertStorage ("current-" <> vsn) uri
185205
if nextSt ^. #modelLoading
186206
then do

0 commit comments

Comments
 (0)