-- | Constructors and updaters for all hledger-ui screens.
--
-- Constructors (*New) create and initialise a new screen with valid state,
-- based on the provided options, reporting date, journal, and screen-specific parameters.
--
-- Updaters (*Update) recalculate an existing screen's state, 
-- based on new options, reporting date, journal, and the old screen state.
--
-- These are gathered in this low-level module so that any screen's handler 
-- can create or regenerate all other screens.
-- Drawing and event-handling code is elsewhere, in per-screen modules.

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NamedFieldPuns #-}

module Hledger.UI.UIScreens
(screenUpdate
,esNew
,esUpdate
,msNew
,msUpdate
,asNew
,asUpdate
,asItemIndex
,csItemIndex
,bsItemIndex
,isItemIndex
,accountsScreenRoptsMod
,accountsScreenQuery
,rsNew
,rsUpdate
,tsNew
,tsUpdate
,listMoveToIfDisplayItems
,uiDisplayAccount
)
where

import Brick.Widgets.List (listMoveTo, listSelectedElement, list, listSelectedL, GenericList)
import Data.List
import Data.Maybe
import Data.Text qualified as T
import Data.Time.Calendar (Day, diffDays)
import Lens.Micro (over)
import Safe
import Data.Vector qualified as V

import Hledger.Utils.I18n (i18n)
import Hledger.Cli hiding (mode, progname,prognameandversion)
import Hledger.UI.UIOptions
import Hledger.UI.UITypes
import Hledger.UI.UIUtils
import Data.Function ((&))


-- | Regenerate the content of any screen from new options, reporting date and journal.
screenUpdate :: UIOpts -> Day -> Journal -> Screen -> Screen
screenUpdate :: UIOpts -> Day -> Journal -> Screen -> Screen
screenUpdate UIOpts
opts Day
d Journal
j = \case
  -- Force each regenerated screen state to WHNF so the strict selection-index
  -- binding in each updater fires, severing references to the previous generation (#1825).
  MS MenuScreenState
sst -> MenuScreenState -> Screen
MS (MenuScreenState -> Screen) -> MenuScreenState -> Screen
forall a b. (a -> b) -> a -> b
$! MenuScreenState -> MenuScreenState
msUpdate MenuScreenState
sst  -- opts d j ass
  AS AccountsScreenState
sst -> AccountsScreenState -> Screen
AS (AccountsScreenState -> Screen) -> AccountsScreenState -> Screen
forall a b. (a -> b) -> a -> b
$! UIOpts
-> Day -> Journal -> AccountsScreenState -> AccountsScreenState
asUpdate UIOpts
opts Day
d Journal
j AccountsScreenState
sst
  RS RegisterScreenState
sst -> RegisterScreenState -> Screen
RS (RegisterScreenState -> Screen) -> RegisterScreenState -> Screen
forall a b. (a -> b) -> a -> b
$! UIOpts
-> Day -> Journal -> RegisterScreenState -> RegisterScreenState
rsUpdate UIOpts
opts Day
d Journal
j RegisterScreenState
sst
  TS TransactionScreenState
sst -> TransactionScreenState -> Screen
TS (TransactionScreenState -> Screen)
-> TransactionScreenState -> Screen
forall a b. (a -> b) -> a -> b
$! UIOpts
-> Day
-> Journal
-> TransactionScreenState
-> TransactionScreenState
tsUpdate UIOpts
opts Day
d Journal
j TransactionScreenState
sst
  ES ErrorScreenState
sst -> ErrorScreenState -> Screen
ES (ErrorScreenState -> Screen) -> ErrorScreenState -> Screen
forall a b. (a -> b) -> a -> b
$  ErrorScreenState -> ErrorScreenState
esUpdate ErrorScreenState
sst

-- | Construct an error screen.
-- Screen-specific arguments: the error message to show.
esNew :: String -> Screen
esNew :: String -> Screen
esNew String
msg =
  String -> Screen -> Screen
forall a. String -> a -> a
dbgui String
"esNew" (Screen -> Screen) -> Screen -> Screen
forall a b. (a -> b) -> a -> b
$
  ErrorScreenState -> Screen
ES ESS {
    _essError :: String
_essError = String
msg
    ,_essUnused :: ()
_essUnused = ()
    }

-- | Update an error screen. Currently a no-op since error screen
-- depends only on its screen-specific state.
esUpdate :: ErrorScreenState -> ErrorScreenState
esUpdate :: ErrorScreenState -> ErrorScreenState
esUpdate = String -> ErrorScreenState -> ErrorScreenState
forall a. String -> a -> a
dbgui String
"esUpdate`"

-- | Construct a menu screen, with the first item selected.
-- Screen-specific arguments: none.
msNew :: Screen
msNew :: Screen
msNew =
  String -> Screen -> Screen
forall a. String -> a -> a
dbgui String
"msNew" (Screen -> Screen) -> Screen -> Screen
forall a b. (a -> b) -> a -> b
$
  MenuScreenState -> Screen
MS MSS { _mssList :: List Name MenuScreenItem
_mssList = Name -> Vector MenuScreenItem -> Int -> List Name MenuScreenItem
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
MenuList ([MenuScreenItem] -> Vector MenuScreenItem
forall a. [a] -> Vector a
V.fromList [MenuScreenItem]
items ) Int
1, _mssUnused :: ()
_mssUnused = () }
  where
    -- keep synced with: indexes below, initial screen stack setup in UI.Main
    -- TRANSLATORS: the menu screen's entries; translated when drawn.
    items :: [MenuScreenItem]
items = [
       AccountName -> AccountsScreenKind -> MenuScreenItem
MenuScreenItem (AccountName -> AccountName
i18n AccountName
"Cash accounts") AccountsScreenKind
CashAccounts
      ,AccountName -> AccountsScreenKind -> MenuScreenItem
MenuScreenItem (AccountName -> AccountName
i18n AccountName
"Balance sheet accounts") AccountsScreenKind
BalancesheetAccounts
      ,AccountName -> AccountsScreenKind -> MenuScreenItem
MenuScreenItem (AccountName -> AccountName
i18n AccountName
"Income statement accounts") AccountsScreenKind
IncomestatementAccounts
      ,AccountName -> AccountsScreenKind -> MenuScreenItem
MenuScreenItem (AccountName -> AccountName
i18n AccountName
"All accounts") AccountsScreenKind
AllAccounts
      ]

-- keep synced with items above.
-- | Positions of menu screen items, so we can move selection to them.
[
  Int
csItemIndex,
  Int
bsItemIndex,
  Int
isItemIndex,
  Int
asItemIndex
  ] = [Int
0..Int
3] :: [Int]

-- | Update a menu screen. Currently a no-op since menu screen
-- has unchanging content.
msUpdate :: MenuScreenState -> MenuScreenState
msUpdate :: MenuScreenState -> MenuScreenState
msUpdate = String -> MenuScreenState -> MenuScreenState
forall a. String -> a -> a
dbgui String
"msUpdate"

nullass :: AccountsScreenKind -> Maybe AccountName -> AccountsScreenState
nullass AccountsScreenKind
kind Maybe AccountName
macct = ASS {
   _assKind :: AccountsScreenKind
_assKind            = AccountsScreenKind
kind
  ,_assSelectedAccount :: AccountName
_assSelectedAccount = AccountName -> Maybe AccountName -> AccountName
forall a. a -> Maybe a -> a
fromMaybe AccountName
"" Maybe AccountName
macct
  ,_assList :: List Name AccountsScreenItem
_assList            = Name
-> Vector AccountsScreenItem -> Int -> List Name AccountsScreenItem
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
AccountsList ([AccountsScreenItem] -> Vector AccountsScreenItem
forall a. [a] -> Vector a
V.fromList []) Int
1
  }

-- | Construct an accounts-like screen (all accounts, cash, balance sheet or income
-- statement, selected by the AccountsScreenKind), listing the appropriate set of accounts,
-- with the appropriate one selected.
-- Screen-specific arguments: the kind of screen, and the account to select if any.
asNew :: AccountsScreenKind -> UIOpts -> Day -> Journal -> Maybe AccountName -> Screen
asNew :: AccountsScreenKind
-> UIOpts -> Day -> Journal -> Maybe AccountName -> Screen
asNew AccountsScreenKind
kind UIOpts
uopts Day
d Journal
j Maybe AccountName
macct = String -> Screen -> Screen
forall a. String -> a -> a
dbgui String
"asNew" (Screen -> Screen) -> Screen -> Screen
forall a b. (a -> b) -> a -> b
$ AccountsScreenState -> Screen
AS (AccountsScreenState -> Screen) -> AccountsScreenState -> Screen
forall a b. (a -> b) -> a -> b
$ UIOpts
-> Day -> Journal -> AccountsScreenState -> AccountsScreenState
asUpdate UIOpts
uopts Day
d Journal
j (AccountsScreenState -> AccountsScreenState)
-> AccountsScreenState -> AccountsScreenState
forall a b. (a -> b) -> a -> b
$ AccountsScreenKind -> Maybe AccountName -> AccountsScreenState
nullass AccountsScreenKind
kind Maybe AccountName
macct

-- | Update an accounts-like screen's state from these options, reporting date, and journal.
-- The report is restricted and accumulated according to the screen's kind (_assKind).
asUpdate :: UIOpts -> Day -> Journal -> AccountsScreenState -> AccountsScreenState
asUpdate :: UIOpts
-> Day -> Journal -> AccountsScreenState -> AccountsScreenState
asUpdate UIOpts
uopts Day
d Journal
j AccountsScreenState
ass = String -> AccountsScreenState -> AccountsScreenState
forall a. String -> a -> a
dbgui String
"asUpdate" (AccountsScreenState -> AccountsScreenState)
-> AccountsScreenState -> AccountsScreenState
forall a b. (a -> b) -> a -> b
$
  ReportSpec
-> Day
-> CliOpts
-> (ReportOpts -> ReportOpts)
-> Query
-> Journal
-> AccountsScreenState
-> AccountsScreenState
asUpdateHelper ReportSpec
rspec Day
d CliOpts
copts (AccountsScreenKind -> ReportOpts -> ReportOpts
accountsScreenRoptsMod AccountsScreenKind
kind) (AccountsScreenKind -> Query
accountsScreenQuery AccountsScreenKind
kind) Journal
j AccountsScreenState
ass
  where
    UIOpts{uoCliOpts :: UIOpts -> CliOpts
uoCliOpts=copts :: CliOpts
copts@CliOpts{reportspec_ :: CliOpts -> ReportSpec
reportspec_=ReportSpec
rspec}} = UIOpts
uopts
    kind :: AccountsScreenKind
kind = AccountsScreenState -> AccountsScreenKind
_assKind AccountsScreenState
ass

-- | The report-options modifier for an accounts-like screen of the given kind:
-- which balance accumulation it forces, if any.
accountsScreenRoptsMod :: AccountsScreenKind -> ReportOpts -> ReportOpts
accountsScreenRoptsMod :: AccountsScreenKind -> ReportOpts -> ReportOpts
accountsScreenRoptsMod AccountsScreenKind
kind ReportOpts
ropts = case AccountsScreenKind
kind of
  AccountsScreenKind
AllAccounts             -> ReportOpts
ropts
  AccountsScreenKind
CashAccounts            -> ReportOpts
ropts{balanceaccum_=Historical}  -- always show historical end balances
  AccountsScreenKind
BalancesheetAccounts    -> ReportOpts
ropts{balanceaccum_=Historical}  -- always show historical end balances
  AccountsScreenKind
IncomestatementAccounts -> ReportOpts
ropts{balanceaccum_=PerPeriod}   -- always show period changes

-- | The extra query restricting an accounts-like screen of the given kind to its account types.
accountsScreenQuery :: AccountsScreenKind -> Query
accountsScreenQuery :: AccountsScreenKind -> Query
accountsScreenQuery AccountsScreenKind
kind = case AccountsScreenKind
kind of
  AccountsScreenKind
AllAccounts             -> Query
Any
  AccountsScreenKind
CashAccounts            -> [AccountType] -> Query
Type [AccountType
Cash]
  AccountsScreenKind
BalancesheetAccounts    -> [AccountType] -> Query
Type [AccountType
Asset,AccountType
Liability,AccountType
Equity]
  AccountsScreenKind
IncomestatementAccounts -> [AccountType] -> Query
Type [AccountType
Revenue,AccountType
Expense]

-- | Update an accounts-like screen's state from this report spec, reporting date,
-- cli options, report options modifier, extra query, and journal.
asUpdateHelper :: ReportSpec -> Day -> CliOpts -> (ReportOpts -> ReportOpts) -> Query -> Journal -> AccountsScreenState -> AccountsScreenState
asUpdateHelper :: ReportSpec
-> Day
-> CliOpts
-> (ReportOpts -> ReportOpts)
-> Query
-> Journal
-> AccountsScreenState
-> AccountsScreenState
asUpdateHelper ReportSpec
rspec0 Day
d CliOpts
copts ReportOpts -> ReportOpts
roptsModify Query
extraquery Journal
j AccountsScreenState
ass = String -> AccountsScreenState -> AccountsScreenState
forall a. String -> a -> a
dbgui String
"asUpdateHelper" (AccountsScreenState -> AccountsScreenState)
-> AccountsScreenState -> AccountsScreenState
forall a b. (a -> b) -> a -> b
$
  -- Force the new list, which forces the bang-patterned selidx below, severing the
  -- reference to the previous generation's state (ass) so it can be GC'd (#1825).
  List Name AccountsScreenItem
l List Name AccountsScreenItem
-> AccountsScreenState -> AccountsScreenState
forall a b. a -> b -> b
`seq` AccountsScreenState
ass{_assList=l}
  where
    ropts :: ReportOpts
ropts = ReportOpts -> ReportOpts
roptsModify (ReportOpts -> ReportOpts) -> ReportOpts -> ReportOpts
forall a b. (a -> b) -> a -> b
$ ReportSpec -> ReportOpts
_rsReportOpts ReportSpec
rspec0
    rspec :: ReportSpec
rspec =
      ReportOpts -> ReportSpec -> Either String ReportSpec
updateReportSpec
        ReportOpts
ropts
        ReportSpec
rspec0{_rsDay=d}  -- update to the current date, might have changed since program start
      Either String ReportSpec
-> (Either String ReportSpec -> ReportSpec) -> ReportSpec
forall a b. a -> (a -> b) -> b
& (String -> ReportSpec)
-> (ReportSpec -> ReportSpec)
-> Either String ReportSpec
-> ReportSpec
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> String -> ReportSpec
forall a. String -> a
error' String
"asUpdateHelper: adjusting the query, should not have failed") ReportSpec -> ReportSpec
forall a. a -> a
id -- PARTIAL:
      ReportSpec -> (ReportSpec -> ReportSpec) -> ReportSpec
forall a b. a -> (a -> b) -> b
& Maybe DateSpan -> ReportSpec -> ReportSpec
reportSpecSetFutureAndForecast (InputOpts -> Maybe DateSpan
forecast_ (InputOpts -> Maybe DateSpan) -> InputOpts -> Maybe DateSpan
forall a b. (a -> b) -> a -> b
$ CliOpts -> InputOpts
inputopts_ CliOpts
copts)  -- include/exclude future & forecast transactions
      ReportSpec -> (ReportSpec -> ReportSpec) -> ReportSpec
forall a b. a -> (a -> b) -> b
& Query -> ReportSpec -> ReportSpec
reportSpecAddQuery Query
extraquery  -- add any extra restrictions

    l :: List Name AccountsScreenItem
l = Int
-> [AccountsScreenItem]
-> List Name AccountsScreenItem
-> List Name AccountsScreenItem
forall item.
Int
-> [item]
-> GenericList Name Vector item
-> GenericList Name Vector item
listMoveToIfDisplayItems Int
selidx [AccountsScreenItem]
displayitems (List Name AccountsScreenItem -> List Name AccountsScreenItem)
-> List Name AccountsScreenItem -> List Name AccountsScreenItem
forall a b. (a -> b) -> a -> b
$ Name
-> Vector AccountsScreenItem -> Int -> List Name AccountsScreenItem
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
AccountsList ([AccountsScreenItem] -> Vector AccountsScreenItem
forall a. [a] -> Vector a
V.fromList ([AccountsScreenItem] -> Vector AccountsScreenItem)
-> [AccountsScreenItem] -> Vector AccountsScreenItem
forall a b. (a -> b) -> a -> b
$ [AccountsScreenItem]
displayitems [AccountsScreenItem]
-> [AccountsScreenItem] -> [AccountsScreenItem]
forall a. [a] -> [a] -> [a]
++ [AccountsScreenItem]
blankitems) Int
1
      where
        -- which account should be selected ?
        -- Strict: forced via `l `seq`` above, so the new list holds no thunk over ass (#1825).
        !selidx :: Int
selidx = Int -> [Int] -> Int
forall a. a -> [a] -> a
headDef Int
0 ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ [Maybe Int] -> [Int]
forall a. [Maybe a] -> [a]
catMaybes [
           AccountName -> [AccountName] -> Maybe Int
forall a. Eq a => a -> [a] -> Maybe Int
elemIndex AccountName
a [AccountName]
as                               -- the one previously selected, if it can be found
          ,(AccountName -> Bool) -> [AccountName] -> Maybe Int
forall a. (a -> Bool) -> [a] -> Maybe Int
findIndex (AccountName
a AccountName -> AccountName -> Bool
`isAccountNamePrefixOf`) [AccountName]
as     -- or the first account found with the same prefix
          ,Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 ([AccountName] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ((AccountName -> Bool) -> [AccountName] -> [AccountName]
forall a. (a -> Bool) -> [a] -> [a]
filter (AccountName -> AccountName -> Bool
forall a. Ord a => a -> a -> Bool
< AccountName
a) [AccountName]
as) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)  -- otherwise, the alphabetically preceding account.
          ]
          where
            a :: AccountName
a = AccountsScreenState -> AccountName
_assSelectedAccount AccountsScreenState
ass
            as :: [AccountName]
as = (AccountsScreenItem -> AccountName)
-> [AccountsScreenItem] -> [AccountName]
forall a b. (a -> b) -> [a] -> [b]
map AccountsScreenItem -> AccountName
asItemAccountName [AccountsScreenItem]
displayitems

        displayitems :: [AccountsScreenItem]
displayitems = ((AccountName, AccountName, Int, MixedAmount)
 -> AccountsScreenItem)
-> [(AccountName, AccountName, Int, MixedAmount)]
-> [AccountsScreenItem]
forall a b. (a -> b) -> [a] -> [b]
map (AccountName, AccountName, Int, MixedAmount) -> AccountsScreenItem
displayitem [(AccountName, AccountName, Int, MixedAmount)]
items
          where
            -- run the report
            ([(AccountName, AccountName, Int, MixedAmount)]
items, MixedAmount
_) = Map AccountName AmountStyle -> BalanceReport -> BalanceReport
forall a. HasAmounts a => Map AccountName AmountStyle -> a -> a
styleAmounts Map AccountName AmountStyle
styles (BalanceReport -> BalanceReport) -> BalanceReport -> BalanceReport
forall a b. (a -> b) -> a -> b
$ ReportSpec -> Journal -> BalanceReport
balanceReport ReportSpec
rspec Journal
j
              where
                styles :: Map AccountName AmountStyle
styles = Rounding -> Journal -> Map AccountName AmountStyle
journalCommodityStylesWith Rounding
HardRounding Journal
j

            -- pre-render a list item
            displayitem :: (AccountName, AccountName, Int, MixedAmount) -> AccountsScreenItem
displayitem (AccountName
fullacct, AccountName
shortacct, Int
indent, MixedAmount
bal) =
              AccountsScreenItem{asItemIndentLevel :: Int
asItemIndentLevel        = Int
indent
                                ,asItemAccountName :: AccountName
asItemAccountName        = AccountName
fullacct
                                ,asItemDisplayAccountName :: AccountName
asItemDisplayAccountName = AccountName -> AccountName -> AccountName
replaceHiddenAccountsNameWith AccountName
"All" (AccountName -> AccountName) -> AccountName -> AccountName
forall a b. (a -> b) -> a -> b
$ if ReportOpts -> Bool
tree_ ReportOpts
ropts then AccountName
shortacct else AccountName
fullacct
                                ,asItemMixedAmount :: Maybe MixedAmount
asItemMixedAmount        = MixedAmount -> Maybe MixedAmount
forall a. a -> Maybe a
Just MixedAmount
bal
                                }

        -- blanks added for scrolling control, cf RegisterScreen.
        blankitems :: [AccountsScreenItem]
blankitems = Int -> AccountsScreenItem -> [AccountsScreenItem]
forall a. Int -> a -> [a]
replicate Int
uiNumBlankItems  -- XXX ugly hard-coded value. When debugging, changing to 0 reduces noise.
          AccountsScreenItem{asItemIndentLevel :: Int
asItemIndentLevel        = Int
0
                            ,asItemAccountName :: AccountName
asItemAccountName        = AccountName
""
                            ,asItemDisplayAccountName :: AccountName
asItemDisplayAccountName = AccountName
""
                            ,asItemMixedAmount :: Maybe MixedAmount
asItemMixedAmount        = Maybe MixedAmount
forall a. Maybe a
Nothing
                            }

-- | Construct a register screen listing the appropriate set of transactions,
-- with the appropriate one selected.
-- Screen-specific arguments: the account whose register this is,
-- whether to force inclusive balances.
rsNew :: UIOpts -> Day -> Journal -> AccountName -> Bool -> Screen
rsNew :: UIOpts -> Day -> Journal -> AccountName -> Bool -> Screen
rsNew UIOpts
uopts Day
d Journal
j AccountName
acct Bool
forceinclusive =  -- XXX forcedefaultselection - whether to force selecting the last transaction.
  String -> Screen -> Screen
forall a. String -> a -> a
dbgui String
"rsNew" (Screen -> Screen) -> Screen -> Screen
forall a b. (a -> b) -> a -> b
$
  RegisterScreenState -> Screen
RS (RegisterScreenState -> Screen) -> RegisterScreenState -> Screen
forall a b. (a -> b) -> a -> b
$
  UIOpts
-> Day -> Journal -> RegisterScreenState -> RegisterScreenState
rsUpdate UIOpts
uopts Day
d Journal
j (RegisterScreenState -> RegisterScreenState)
-> RegisterScreenState -> RegisterScreenState
forall a b. (a -> b) -> a -> b
$
  RSS {
     _rssAccount :: AccountName
_rssAccount        = AccountName -> AccountName -> AccountName
replaceHiddenAccountsNameWith AccountName
"*" AccountName
acct
    ,_rssForceInclusive :: Bool
_rssForceInclusive = Bool
forceinclusive
    ,_rssList :: List Name RegisterScreenItem
_rssList           = Name
-> Vector RegisterScreenItem -> Int -> List Name RegisterScreenItem
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
RegisterList ([RegisterScreenItem] -> Vector RegisterScreenItem
forall a. [a] -> Vector a
V.fromList []) Int
1
    }

-- | Update a register screen from these options, reporting date, and journal.
rsUpdate :: UIOpts -> Day -> Journal -> RegisterScreenState -> RegisterScreenState
rsUpdate :: UIOpts
-> Day -> Journal -> RegisterScreenState -> RegisterScreenState
rsUpdate UIOpts
uopts Day
d Journal
j rss :: RegisterScreenState
rss@RSS{AccountName
_rssAccount :: RegisterScreenState -> AccountName
_rssAccount :: AccountName
_rssAccount, Bool
_rssForceInclusive :: RegisterScreenState -> Bool
_rssForceInclusive :: Bool
_rssForceInclusive, _rssList :: RegisterScreenState -> List Name RegisterScreenItem
_rssList=List Name RegisterScreenItem
oldlist} =
  String -> RegisterScreenState -> RegisterScreenState
forall a. String -> a -> a
dbgui String
"rsUpdate" (RegisterScreenState -> RegisterScreenState)
-> RegisterScreenState -> RegisterScreenState
forall a b. (a -> b) -> a -> b
$
  -- Force the new list, which forces the bang-patterned newselidx below, severing the
  -- reference to the previous generation's list (oldlist) so it can be GC'd (#1825).
  List Name RegisterScreenItem
l' List Name RegisterScreenItem
-> RegisterScreenState -> RegisterScreenState
forall a b. a -> b -> b
`seq` RegisterScreenState
rss{_rssList=l'}
  where
    -- the rendered register items (one per transaction); shared with the transaction screen
    displayitems :: [RegisterScreenItem]
displayitems = UIOpts
-> Day -> Journal -> AccountName -> Bool -> [RegisterScreenItem]
registerScreenDisplayItems UIOpts
uopts Day
d Journal
j AccountName
_rssAccount Bool
_rssForceInclusive

    -- blank items are added to allow more control of scroll position; we won't allow movement over these.
    -- XXX Ugly. Changing to 0 helps when debugging.
    blankitems :: [RegisterScreenItem]
blankitems = Int -> RegisterScreenItem -> [RegisterScreenItem]
forall a. Int -> a -> [a]
replicate Int
uiNumBlankItems
          RegisterScreenItem{rsItemDate :: AccountName
rsItemDate          = AccountName
""
                            ,rsItemStatus :: Status
rsItemStatus        = Status
Unmarked
                            ,rsItemDescription :: AccountName
rsItemDescription   = AccountName
""
                            ,rsItemOtherAccounts :: AccountName
rsItemOtherAccounts = AccountName
""
                            ,rsItemChangeAmount :: WideBuilder
rsItemChangeAmount  = WideBuilder
forall a. Monoid a => a
mempty
                            ,rsItemBalanceAmount :: WideBuilder
rsItemBalanceAmount = WideBuilder
forall a. Monoid a => a
mempty
                            ,rsItemTransaction :: Transaction
rsItemTransaction   = Transaction
nulltransaction
                            }

    -- build the new list widget
    l :: List Name RegisterScreenItem
l = Name
-> Vector RegisterScreenItem -> Int -> List Name RegisterScreenItem
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list Name
RegisterList ([RegisterScreenItem] -> Vector RegisterScreenItem
forall a. [a] -> Vector a
V.fromList ([RegisterScreenItem] -> Vector RegisterScreenItem)
-> [RegisterScreenItem] -> Vector RegisterScreenItem
forall a b. (a -> b) -> a -> b
$ [RegisterScreenItem]
displayitems [RegisterScreenItem]
-> [RegisterScreenItem] -> [RegisterScreenItem]
forall a. [a] -> [a] -> [a]
++ [RegisterScreenItem]
blankitems) Int
1

    -- ensure the appropriate list item is selected:
    -- if forcedefaultselection is true, the last (latest) transaction;  XXX still needed ?
    -- otherwise, the previously selected transaction if possible;
    -- otherwise, the transaction nearest in date to it;
    -- or if there's several with the same date, the nearest in journal order;
    -- otherwise, the last (latest) transaction.
    l' :: List Name RegisterScreenItem
l' = Int
-> [RegisterScreenItem]
-> List Name RegisterScreenItem
-> List Name RegisterScreenItem
forall item.
Int
-> [item]
-> GenericList Name Vector item
-> GenericList Name Vector item
listMoveToIfDisplayItems Int
newselidx [RegisterScreenItem]
displayitems List Name RegisterScreenItem
l
      where
        endidx :: Int
endidx = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ [RegisterScreenItem] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [RegisterScreenItem]
displayitems Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
        -- Strict: forced via `l' `seq`` above, so the new list holds no thunk over oldlist (#1825).
        !newselidx :: Int
newselidx =
          -- case (forcedefaultselection, listSelectedElement _rssList) of
          --   (True, _)    -> endidx
          --   (_, Nothing) -> endidx
          --   (_, Just (_, RegisterScreenItem{rsItemTransaction=Transaction{tindex=prevselidx, tdate=prevseld}})) ->
          --     headDef endidx $ catMaybes [
          --       findIndex ((==prevselidx) . tindex . rsItemTransaction) displayitems
          --       ,findIndex ((==nearestidbydatethenid) . Just . tindex . rsItemTransaction) displayitems
          --       ]
          --     where
          --       nearestidbydatethenid = third3 <$> (headMay $ sort
          --         [(abs $ diffDays (tdate t) prevseld, abs (tindex t - prevselidx), tindex t) | t <- ts])
          --       ts = map rsItemTransaction displayitems
          case List Name RegisterScreenItem -> Maybe (Int, RegisterScreenItem)
forall (t :: * -> *) e n.
(Splittable t, Traversable t, Semigroup (t e)) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement List Name RegisterScreenItem
oldlist of
            Maybe (Int, RegisterScreenItem)
Nothing -> Int
endidx
            Just (Int
_, RegisterScreenItem{rsItemTransaction :: RegisterScreenItem -> Transaction
rsItemTransaction=Transaction{tindex :: Transaction -> Integer
tindex=Integer
prevselidx, tdate :: Transaction -> Day
tdate=Day
prevseld}}) ->
              Int -> [Int] -> Int
forall a. a -> [a] -> a
headDef Int
endidx ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ [Maybe Int] -> [Int]
forall a. [Maybe a] -> [a]
catMaybes [
                (RegisterScreenItem -> Bool) -> [RegisterScreenItem] -> Maybe Int
forall a. (a -> Bool) -> [a] -> Maybe Int
findIndex ((Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
==Integer
prevselidx) (Integer -> Bool)
-> (RegisterScreenItem -> Integer) -> RegisterScreenItem -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Integer
tindex (Transaction -> Integer)
-> (RegisterScreenItem -> Transaction)
-> RegisterScreenItem
-> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegisterScreenItem -> Transaction
rsItemTransaction) [RegisterScreenItem]
displayitems
                ,(RegisterScreenItem -> Bool) -> [RegisterScreenItem] -> Maybe Int
forall a. (a -> Bool) -> [a] -> Maybe Int
findIndex ((Maybe Integer -> Maybe Integer -> Bool
forall a. Eq a => a -> a -> Bool
==Maybe Integer
nearestidbydatethenid) (Maybe Integer -> Bool)
-> (RegisterScreenItem -> Maybe Integer)
-> RegisterScreenItem
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Maybe Integer
forall a. a -> Maybe a
Just (Integer -> Maybe Integer)
-> (RegisterScreenItem -> Integer)
-> RegisterScreenItem
-> Maybe Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Integer
tindex (Transaction -> Integer)
-> (RegisterScreenItem -> Transaction)
-> RegisterScreenItem
-> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegisterScreenItem -> Transaction
rsItemTransaction) [RegisterScreenItem]
displayitems
                ]
              where
                nearestidbydatethenid :: Maybe Integer
nearestidbydatethenid = (Integer, Integer, Integer) -> Integer
forall {a} {b} {c}. (a, b, c) -> c
third3 ((Integer, Integer, Integer) -> Integer)
-> Maybe (Integer, Integer, Integer) -> Maybe Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([(Integer, Integer, Integer)] -> Maybe (Integer, Integer, Integer)
forall a. [a] -> Maybe a
headMay ([(Integer, Integer, Integer)]
 -> Maybe (Integer, Integer, Integer))
-> [(Integer, Integer, Integer)]
-> Maybe (Integer, Integer, Integer)
forall a b. (a -> b) -> a -> b
$ [(Integer, Integer, Integer)] -> [(Integer, Integer, Integer)]
forall a. Ord a => [a] -> [a]
sort
                  [(Integer -> Integer
forall a. Num a => a -> a
abs (Integer -> Integer) -> Integer -> Integer
forall a b. (a -> b) -> a -> b
$ Day -> Day -> Integer
diffDays (Transaction -> Day
tdate Transaction
t) Day
prevseld, Integer -> Integer
forall a. Num a => a -> a
abs (Transaction -> Integer
tindex Transaction
t Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
prevselidx), Transaction -> Integer
tindex Transaction
t) | Transaction
t <- [Transaction]
ts])
                ts :: [Transaction]
ts = (RegisterScreenItem -> Transaction)
-> [RegisterScreenItem] -> [Transaction]
forall a b. (a -> b) -> [a] -> [b]
map RegisterScreenItem -> Transaction
rsItemTransaction [RegisterScreenItem]
displayitems

-- | The account a register-like screen should display: normally the screen's stored
-- account, but when that is a lot subaccount and lot detail is collapsed (so the
-- account doesn't exist in the display journal), its base account, where the
-- collapsed postings now appear. Keep the collapse condition synced with uiDisplayJournal.
uiDisplayAccount :: UIOpts -> AccountName -> AccountName
uiDisplayAccount :: UIOpts -> AccountName -> AccountName
uiDisplayAccount UIOpts
uopts AccountName
a
  | String -> RawOpts -> Bool
boolopt String
"lots" RawOpts
ro        = AccountName
a
  | String -> RawOpts -> Bool
boolopt String
"ignore-lots" RawOpts
ro = AccountName
a
  | Bool
otherwise                = AccountName -> AccountName
lotBaseAccount AccountName
a
  where ro :: RawOpts
ro = CliOpts -> RawOpts
rawopts_ (CliOpts -> RawOpts) -> CliOpts -> RawOpts
forall a b. (a -> b) -> a -> b
$ UIOpts -> CliOpts
uoCliOpts UIOpts
uopts

-- | The rendered register items (one per transaction) for an account's register,
-- from these options, reporting date, journal, account name, and whether to include
-- subaccount transactions. Shared by the register screen and the transaction screen
-- so they show the same set of transactions.
-- The account is remapped with uiDisplayAccount, so a lot subaccount's register
-- shows its base account's transactions while lot detail is collapsed.
registerScreenDisplayItems :: UIOpts -> Day -> Journal -> AccountName -> Bool -> [RegisterScreenItem]
registerScreenDisplayItems :: UIOpts
-> Day -> Journal -> AccountName -> Bool -> [RegisterScreenItem]
registerScreenDisplayItems UIOpts
uopts Day
d Journal
j AccountName
acct0 Bool
forceinclusive = ((Transaction, Transaction, Bool, [AccountName], MixedAmount,
  MixedAmount)
 -> RegisterScreenItem)
-> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)]
-> [RegisterScreenItem]
forall a b. (a -> b) -> [a] -> [b]
map (Transaction, Transaction, Bool, [AccountName], MixedAmount,
 MixedAmount)
-> RegisterScreenItem
forall {b} {c}.
(Transaction, b, c, [AccountName], MixedAmount, MixedAmount)
-> RegisterScreenItem
displayitem [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
  MixedAmount)]
items'
  where
    acct :: AccountName
acct = UIOpts -> AccountName -> AccountName
uiDisplayAccount UIOpts
uopts AccountName
acct0
    UIOpts{uoCliOpts :: UIOpts -> CliOpts
uoCliOpts=copts :: CliOpts
copts@CliOpts{reportspec_ :: CliOpts -> ReportSpec
reportspec_=rspec :: ReportSpec
rspec@ReportSpec{_rsReportOpts :: ReportSpec -> ReportOpts
_rsReportOpts=ReportOpts
ropts}}} = UIOpts
uopts
    inclusive :: Bool
inclusive = ReportOpts -> Bool
tree_ ReportOpts
ropts Bool -> Bool -> Bool
|| Bool
forceinclusive
    thisacctq :: Query
thisacctq = Regexp -> Query
Acct (Regexp -> Query) -> Regexp -> Query
forall a b. (a -> b) -> a -> b
$ AccountName -> Regexp
mkregex AccountName
acct
      where
        mkregex :: AccountName -> Regexp
mkregex = if Bool
inclusive then AccountName -> Regexp
accountNameToAccountRegex else AccountName -> Regexp
accountNameToAccountOnlyRegex

    -- adjust the report options and report spec, carefully as usual to avoid screwups (#1523)
    ropts' :: ReportOpts
ropts' = ReportOpts
ropts {
        -- ignore any depth limit, as in postingsReport; allows register's total to match accounts screen
        depth_=mempty
        -- do not strip prices so we can toggle costs within the ui
      , show_costs_=True
      }
    rspec' :: ReportSpec
rspec' =
      ReportOpts -> ReportSpec -> Either String ReportSpec
updateReportSpec ReportOpts
ropts' ReportSpec
rspec{_rsDay=d}
      Either String ReportSpec
-> (Either String ReportSpec -> ReportSpec) -> ReportSpec
forall a b. a -> (a -> b) -> b
& (String -> ReportSpec)
-> (ReportSpec -> ReportSpec)
-> Either String ReportSpec
-> ReportSpec
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> String -> ReportSpec
forall a. String -> a
error' String
"registerScreenDisplayItems: adjusting the query for register, should not have failed") ReportSpec -> ReportSpec
forall a. a -> a
id -- PARTIAL:
      ReportSpec -> (ReportSpec -> ReportSpec) -> ReportSpec
forall a b. a -> (a -> b) -> b
& Maybe DateSpan -> ReportSpec -> ReportSpec
reportSpecSetFutureAndForecast (InputOpts -> Maybe DateSpan
forecast_ (InputOpts -> Maybe DateSpan) -> InputOpts -> Maybe DateSpan
forall a b. (a -> b) -> a -> b
$ CliOpts -> InputOpts
inputopts_ CliOpts
copts)

    -- gather transactions to display
    items :: [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
  MixedAmount)]
items = Map AccountName AmountStyle
-> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)]
-> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)]
forall a. HasAmounts a => Map AccountName AmountStyle -> a -> a
styleAmounts Map AccountName AmountStyle
styles ([(Transaction, Transaction, Bool, [AccountName], MixedAmount,
   MixedAmount)]
 -> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
      MixedAmount)])
-> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)]
-> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)]
forall a b. (a -> b) -> a -> b
$ ReportSpec
-> Journal
-> Query
-> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)]
accountTransactionsReport ReportSpec
rspec' Journal
j Query
thisacctq
              where
                styles :: Map AccountName AmountStyle
styles = Rounding -> Journal -> Map AccountName AmountStyle
journalCommodityStylesWith Rounding
HardRounding Journal
j
    items' :: [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
  MixedAmount)]
items' =
      (if ReportOpts -> Bool
empty_ ReportOpts
ropts then [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
  MixedAmount)]
-> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)]
forall a. a -> a
id else ((Transaction, Transaction, Bool, [AccountName], MixedAmount,
  MixedAmount)
 -> Bool)
-> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)]
-> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> ((Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)
    -> Bool)
-> (Transaction, Transaction, Bool, [AccountName], MixedAmount,
    MixedAmount)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MixedAmount -> Bool
mixedAmountLooksZero (MixedAmount -> Bool)
-> ((Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)
    -> MixedAmount)
-> (Transaction, Transaction, Bool, [AccountName], MixedAmount,
    MixedAmount)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Transaction, Transaction, Bool, [AccountName], MixedAmount,
 MixedAmount)
-> MixedAmount
forall {a} {b} {c} {d} {e} {f}. (a, b, c, d, e, f) -> e
fifth6)) ([(Transaction, Transaction, Bool, [AccountName], MixedAmount,
   MixedAmount)]
 -> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
      MixedAmount)])
-> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)]
-> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)]
forall a b. (a -> b) -> a -> b
$  -- without --empty, exclude no-change txns
      [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
  MixedAmount)]
-> [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
     MixedAmount)]
forall a. [a] -> [a]
reverse  -- most recent last
      [(Transaction, Transaction, Bool, [AccountName], MixedAmount,
  MixedAmount)]
items

    displayitem :: (Transaction, b, c, [AccountName], MixedAmount, MixedAmount)
-> RegisterScreenItem
displayitem (Transaction
t, b
_, c
_issplit, [AccountName]
otheraccts, MixedAmount
change, MixedAmount
bal) =
      RegisterScreenItem{rsItemDate :: AccountName
rsItemDate          = Day -> AccountName
showDate (Day -> AccountName) -> Day -> AccountName
forall a b. (a -> b) -> a -> b
$ WhichDate -> Query -> Query -> Transaction -> Day
transactionRegisterDate WhichDate
wd (ReportSpec -> Query
_rsQuery ReportSpec
rspec') Query
thisacctq Transaction
t
                        ,rsItemStatus :: Status
rsItemStatus        = Transaction -> Status
tstatus Transaction
t
                        ,rsItemDescription :: AccountName
rsItemDescription   = Transaction -> AccountName
tdescription Transaction
t
                        ,rsItemOtherAccounts :: AccountName
rsItemOtherAccounts = AccountName -> [AccountName] -> AccountName
T.intercalate AccountName
", " ([AccountName] -> AccountName)
-> ([AccountName] -> [AccountName]) -> [AccountName] -> AccountName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AccountName -> AccountName) -> [AccountName] -> [AccountName]
forall a b. (a -> b) -> [a] -> [b]
map AccountName -> AccountName
accountSummarisedName ([AccountName] -> AccountName) -> [AccountName] -> AccountName
forall a b. (a -> b) -> a -> b
$ [AccountName] -> [AccountName]
forall a. Eq a => [a] -> [a]
nub [AccountName]
otheraccts
                                                -- _   -> "<split>"  -- should do this if accounts field width < 30
                        ,rsItemChangeAmount :: WideBuilder
rsItemChangeAmount  = MixedAmount -> WideBuilder
showamt MixedAmount
change
                        ,rsItemBalanceAmount :: WideBuilder
rsItemBalanceAmount = MixedAmount -> WideBuilder
showamt MixedAmount
bal
                        ,rsItemTransaction :: Transaction
rsItemTransaction   = Transaction
t
                        }
      where
        showamt :: MixedAmount -> WideBuilder
showamt = AmountFormat -> MixedAmount -> WideBuilder
showMixedAmountB AmountFormat
oneLineNoCostFmt{displayMaxWidth=Just 3}
        wd :: WhichDate
wd = ReportOpts -> WhichDate
whichDate ReportOpts
ropts'

-- | Construct a transaction screen showing one of a given list of transactions,
-- with the ability to step back and forth through the list.
-- Screen-specific arguments: the account whose transactions are being shown,
-- whether that register included subaccounts, the list of showable transactions,
-- the currently shown transaction.
tsNew :: AccountName -> Bool -> [NumberedTransaction] -> NumberedTransaction -> Screen
tsNew :: AccountName
-> Bool -> [NumberedTransaction] -> NumberedTransaction -> Screen
tsNew AccountName
acct Bool
forceinclusive [NumberedTransaction]
nts NumberedTransaction
nt =
  String -> Screen -> Screen
forall a. String -> a -> a
dbgui String
"tsNew" (Screen -> Screen) -> Screen -> Screen
forall a b. (a -> b) -> a -> b
$
  TransactionScreenState -> Screen
TS TSS{
     _tssAccount :: AccountName
_tssAccount        = AccountName
acct
    ,_tssForceInclusive :: Bool
_tssForceInclusive = Bool
forceinclusive
    ,_tssTransactions :: [NumberedTransaction]
_tssTransactions   = [NumberedTransaction]
nts
    ,_tssTransaction :: NumberedTransaction
_tssTransaction    = NumberedTransaction
nt
    }

-- | Update a transaction screen: rebuild its transaction list from the (possibly reloaded)
-- journal, and reselect the same transaction by its journal index, so the screen refreshes
-- in place on reload. If that transaction is gone, fall back to the one at the same position,
-- else the last, else a blank.
tsUpdate :: UIOpts -> Day -> Journal -> TransactionScreenState -> TransactionScreenState
tsUpdate :: UIOpts
-> Day
-> Journal
-> TransactionScreenState
-> TransactionScreenState
tsUpdate UIOpts
uopts Day
d Journal
j tss :: TransactionScreenState
tss@TSS{AccountName
_tssAccount :: TransactionScreenState -> AccountName
_tssAccount :: AccountName
_tssAccount, Bool
_tssForceInclusive :: TransactionScreenState -> Bool
_tssForceInclusive :: Bool
_tssForceInclusive, _tssTransaction :: TransactionScreenState -> NumberedTransaction
_tssTransaction=(Integer
oldpos, Transaction
oldtxn)} =
  String -> TransactionScreenState -> TransactionScreenState
forall a. String -> a -> a
dbgui String
"tsUpdate" (TransactionScreenState -> TransactionScreenState)
-> TransactionScreenState -> TransactionScreenState
forall a b. (a -> b) -> a -> b
$
  [NumberedTransaction] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [NumberedTransaction]
nts Int -> TransactionScreenState -> TransactionScreenState
forall a b. a -> b -> b
`seq` NumberedTransaction
selected NumberedTransaction
-> TransactionScreenState -> TransactionScreenState
forall a b. a -> b -> b
`seq`
  TransactionScreenState
tss{_tssTransactions=nts, _tssTransaction=selected}
  where
    displayitems :: [RegisterScreenItem]
displayitems = UIOpts
-> Day -> Journal -> AccountName -> Bool -> [RegisterScreenItem]
registerScreenDisplayItems UIOpts
uopts Day
d Journal
j AccountName
_tssAccount Bool
_tssForceInclusive
    numberedtxns :: [NumberedTransaction]
numberedtxns = (Integer -> RegisterScreenItem -> NumberedTransaction)
-> [Integer] -> [RegisterScreenItem] -> [NumberedTransaction]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Integer
i RegisterScreenItem
item -> (Integer
i, RegisterScreenItem -> Transaction
rsItemTransaction RegisterScreenItem
item)) [(Integer
1::Integer)..] [RegisterScreenItem]
displayitems
    ([NumberedTransaction]
nts, NumberedTransaction
selected)
      -- the same transaction is still in this account's register: keep it selected
      | Just NumberedTransaction
sel <- (NumberedTransaction -> Bool)
-> [NumberedTransaction] -> Maybe NumberedTransaction
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Transaction -> Integer
tindex Transaction
oldtxn) (Integer -> Bool)
-> (NumberedTransaction -> Integer) -> NumberedTransaction -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> Integer
tindex (Transaction -> Integer)
-> (NumberedTransaction -> Transaction)
-> NumberedTransaction
-> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NumberedTransaction -> Transaction
forall a b. (a, b) -> b
snd) [NumberedTransaction]
numberedtxns = ([NumberedTransaction]
numberedtxns, NumberedTransaction
sel)
      -- the register is now empty, eg because this screen's account was a lot subaccount
      -- and lot display was just toggled off: if the entry still exists in the journal,
      -- keep showing it (in its current form); otherwise show a blank transaction
      | [NumberedTransaction] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [NumberedTransaction]
numberedtxns = case Journal -> Integer -> Maybe Transaction
journalTransactionAt Journal
j (Transaction -> Integer
tindex Transaction
oldtxn) of
          Just Transaction
t  -> ([(Integer
1, Transaction
t)], (Integer
1, Transaction
t))
          Maybe Transaction
Nothing -> ([], (Integer
0, Transaction
nulltransaction))
      -- the transaction is gone but the register is not empty, eg after a file change:
      -- fall back to the transaction at the same position, or the last one
      | Bool
otherwise = ([NumberedTransaction]
numberedtxns, NumberedTransaction
-> Maybe NumberedTransaction -> NumberedTransaction
forall a. a -> Maybe a -> a
fromMaybe ([NumberedTransaction] -> NumberedTransaction
forall a. HasCallStack => [a] -> a
last [NumberedTransaction]
numberedtxns) (Maybe NumberedTransaction -> NumberedTransaction)
-> Maybe NumberedTransaction -> NumberedTransaction
forall a b. (a -> b) -> a -> b
$ (\Transaction
t -> (Integer
oldpos, Transaction
t)) (Transaction -> NumberedTransaction)
-> Maybe Transaction -> Maybe NumberedTransaction
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Integer -> [NumberedTransaction] -> Maybe Transaction
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Integer
oldpos [NumberedTransaction]
numberedtxns)

-- | Set selected index of a list if there are displayitems.
-- If there are no displayitems, remove the selected index of the list.
listMoveToIfDisplayItems :: Int -> [item] -> GenericList Name V.Vector item -> GenericList Name V.Vector item
listMoveToIfDisplayItems :: forall item.
Int
-> [item]
-> GenericList Name Vector item
-> GenericList Name Vector item
listMoveToIfDisplayItems Int
idx [item]
displayItems =
    if [item] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [item]
displayItems
        then ASetter
  (GenericList Name Vector item)
  (GenericList Name Vector item)
  (Maybe Int)
  (Maybe Int)
-> (Maybe Int -> Maybe Int)
-> GenericList Name Vector item
-> GenericList Name Vector item
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
  (GenericList Name Vector item)
  (GenericList Name Vector item)
  (Maybe Int)
  (Maybe Int)
forall n (t :: * -> *) e (f :: * -> *).
Functor f =>
(Maybe Int -> f (Maybe Int))
-> GenericList n t e -> f (GenericList n t e)
listSelectedL ((Maybe Int -> Maybe Int)
 -> GenericList Name Vector item -> GenericList Name Vector item)
-> (Maybe Int -> Maybe Int)
-> GenericList Name Vector item
-> GenericList Name Vector item
forall a b. (a -> b) -> a -> b
$ Maybe Int -> Maybe Int -> Maybe Int
forall a b. a -> b -> a
const Maybe Int
forall a. Maybe a
Nothing
        else Int -> GenericList Name Vector item -> GenericList Name Vector item
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveTo Int
idx