From 481c40ca2ff88d0ecc8b60a82c978306e5a91ff8 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 12 Dec 2020 11:51:25 -0800 Subject: [PATCH] ;ui: refactor toggleForecast --- hledger-ui/Hledger/UI/UIState.hs | 39 +++++++++++++++----------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/hledger-ui/Hledger/UI/UIState.hs b/hledger-ui/Hledger/UI/UIState.hs index 1cb95bbdb..0f4a4db2b 100644 --- a/hledger-ui/Hledger/UI/UIState.hs +++ b/hledger-ui/Hledger/UI/UIState.hs @@ -163,28 +163,25 @@ toggleHistorical ui@UIState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportspec b | balancetype_ ropts == HistoricalBalance = PeriodChange | otherwise = HistoricalBalance --- | Toggle hledger-ui's "forecast mode". In forecast mode, periodic --- transactions (generated by periodic rules) are enabled (as with --- hledger --forecast), and also future transactions in general --- (including non-periodic ones) are displayed. In normal mode, all --- future transactions (periodic or not) are suppressed (unlike --- command-line hledger). --- --- After toggling this, we do a full reload of the journal from disk --- to make it take effect; currently that's done in the callers (cf --- AccountsScreen, RegisterScreen) where it's easier. This is --- overkill, probably we should just hide/show the periodic --- transactions with a query for their special tag. --- +-- | Toggle hledger-ui's "forecast/future mode". When this mode is enabled, +-- hledger-shows regular transactions which have future dates, and +-- "forecast" transactions generated by periodic transaction rules +-- (which are usually but not necessarily future-dated). +-- In normal mode, both of these are hidden. toggleForecast :: Day -> UIState -> UIState -toggleForecast d ui@UIState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportspec_=rspec@ReportSpec{rsOpts=ropts}}}} = - ui{aopts=uopts{cliopts_=copts'}} - where - copts' = copts{reportspec_=rspec{rsOpts=ropts{forecast_=forecast'}}} - forecast' = - case forecast_ ropts of - Just _ -> Nothing - Nothing -> Just $ fromMaybe nulldatespan $ forecastPeriodFromRawOpts d $ rawopts_ copts +toggleForecast d ui@UIState{aopts=UIOpts{cliopts_=copts@CliOpts{reportspec_=ReportSpec{rsOpts=ropts}}}} = + uiSetForecast ui $ + case forecast_ ropts of + Just _ -> Nothing + Nothing -> Just $ fromMaybe nulldatespan $ forecastPeriodFromRawOpts d $ rawopts_ copts + +-- | Helper: set forecast mode (with the given forecast period) on or off in the UI state. +uiSetForecast :: UIState -> Maybe DateSpan -> UIState +uiSetForecast + ui@UIState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportspec_=rspec@ReportSpec{rsOpts=ropts}}}} + mforecast = + -- we assume forecast mode has no effect on ReportSpec's derived fields + ui{aopts=uopts{cliopts_=copts{reportspec_=rspec{rsOpts=ropts{forecast_=mforecast}}}}} -- | Toggle between showing all and showing only real (non-virtual) items. toggleReal :: UIState -> UIState