From 293fab1df23821b5f131603c4db56ab71681ce59 Mon Sep 17 00:00:00 2001
From: Joris Guyonvarch
Date: Sun, 12 Apr 2015 22:22:50 +0200
Subject: The listener will not show results at init step for now, only the new
 results

---
 src/AdListener.hs | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)

(limited to 'src/AdListener.hs')

diff --git a/src/AdListener.hs b/src/AdListener.hs
index 1f97e30..d676b61 100644
--- a/src/AdListener.hs
+++ b/src/AdListener.hs
@@ -25,24 +25,33 @@ import Parser.Detail
 import Config (Config)
 import qualified Config as C
 
-listenToNewAds :: Config -> [Ad] -> IO ()
-listenToNewAds config viewedAds = do
+listenToNewAds :: Config -> IO ()
+listenToNewAds config = do
   eitherResumes <- getResumes (C.url config)
   case eitherResumes of
     Left error ->
-      listenError config viewedAds error
+      listenError config [] error
     Right resumes ->
-      listenToNewAdsWithResumes config viewedAds resumes
+      let newURLs = map url resumes
+      in  listenToNewAdsWithViewedURLs config newURLs
 
-listenToNewAdsWithResumes :: Config -> [Ad] -> [Resume] -> IO ()
-listenToNewAdsWithResumes config viewedAds resumes =
-  let viewedURLs = getURLs $ map resume viewedAds
-      newResumes = getNewResumes viewedURLs resumes
+listenToNewAdsWithViewedURLs :: Config -> [URL] -> IO ()
+listenToNewAdsWithViewedURLs config viewedURLs = do
+  eitherResumes <- getResumes (C.url config)
+  case eitherResumes of
+    Left error ->
+      listenError config viewedURLs error
+    Right resumes ->
+      listenToNewAdsWithResumes config viewedURLs resumes
+
+listenToNewAdsWithResumes :: Config -> [URL] -> [Resume] -> IO ()
+listenToNewAdsWithResumes config viewedURLs resumes =
+  let (newURLs, newResumes) = getNewResumes viewedURLs resumes
   in  do
         eitherNewAds <- getAds newResumes
         case eitherNewAds of
           Left error ->
-            listenError config viewedAds error
+            listenError config viewedURLs error
           Right newAds ->
             do
               if not (null newAds)
@@ -51,7 +60,7 @@ listenToNewAdsWithResumes config viewedAds resumes =
                 else
                   return ()
               waitOneMinute
-              listenToNewAds config (viewedAds ++ newAds)
+              listenToNewAdsWithViewedURLs config (viewedURLs ++ newURLs)
 
 newAdsMessage :: [Ad] -> Text
 newAdsMessage newAds =
@@ -69,11 +78,11 @@ newAdsMessage newAds =
         , renderAds newAds
         ]
 
-listenError :: Config -> [Ad] -> Text -> IO ()
-listenError config viewedAds error = do
+listenError :: Config -> [URL] -> Text -> IO ()
+listenError config viewedURLs error = do
   T.putStrLn error
   waitOneMinute
-  listenToNewAds config viewedAds
+  listenToNewAdsWithViewedURLs config viewedURLs
 
 waitOneMinute :: IO ()
 waitOneMinute = threadDelay (1000 * 1000 * 60)
-- 
cgit v1.2.3