From 097e4e19fc9f06fbc6cffb2ecf9074d72193eb41 Mon Sep 17 00:00:00 2001 From: ndwarshuis Date: Sun, 1 Jan 2023 23:09:23 -0500 Subject: [PATCH] REF clean up state functions in bluetooth --- lib/Xmobar/Plugins/Bluetooth.hs | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/Xmobar/Plugins/Bluetooth.hs b/lib/Xmobar/Plugins/Bluetooth.hs index 0436b61..805713b 100644 --- a/lib/Xmobar/Plugins/Bluetooth.hs +++ b/lib/Xmobar/Plugins/Bluetooth.hs @@ -157,6 +157,11 @@ readState = do c <- readDevices return (p, anyDevicesConnected c) +modifyState :: (BtState -> BTIO (BtState, a)) -> BTIO a +modifyState f = do + m <- asks btState + modifyMVar m f + -------------------------------------------------------------------------------- -- Object manager @@ -281,9 +286,7 @@ callGetPowered adapter = T.unpack adaptorPowered putPowered :: Maybe Bool -> BTIO () -putPowered ds = do - m <- asks btState - modifyMVar_ m (\s -> return s {btPowered = ds}) +putPowered ds = modifyState $ \s -> return (s {btPowered = ds}, ()) readPowered :: BTIO (Maybe Bool) readPowered = fmap btPowered $ readMVar =<< asks btState @@ -334,28 +337,22 @@ callGetConnected p = memberName_ (T.unpack devConnected) insertDevice :: ObjectPath -> BTDevice -> BTIO Bool -insertDevice device dev = do - m <- asks btState - modifyMVar m $ \s -> do - let new = M.insert device dev $ btDevices s - return (s {btDevices = new}, anyDevicesConnected new) +insertDevice device dev = modifyState $ \s -> do + let new = M.insert device dev $ btDevices s + return (s {btDevices = new}, anyDevicesConnected new) updateDevice :: ObjectPath -> Maybe Bool -> BTIO Bool -updateDevice device status = do - m <- asks btState - modifyMVar m $ \s -> do - let new = M.update (\d -> Just d {btDevConnected = status}) device $ btDevices s - return (s {btDevices = new}, anyDevicesConnected new) +updateDevice device status = modifyState $ \s -> do + let new = M.update (\d -> Just d {btDevConnected = status}) device $ btDevices s + return (s {btDevices = new}, anyDevicesConnected new) anyDevicesConnected :: ConnectedDevices -> Bool anyDevicesConnected = or . mapMaybe btDevConnected . M.elems removeDevice :: ObjectPath -> BTIO (Maybe BTDevice) -removeDevice device = do - m <- asks btState - modifyMVar m $ \s -> do - let devs = btDevices s - return (s {btDevices = M.delete device devs}, M.lookup device devs) +removeDevice device = modifyState $ \s -> do + let devs = btDevices s + return (s {btDevices = M.delete device devs}, M.lookup device devs) readDevices :: BTIO ConnectedDevices readDevices = fmap btDevices $ readMVar =<< asks btState