Skip to content

Instantly share code, notes, and snippets.

@szabba
Forked from helderjnpinto/filter messages
Last active December 16, 2016 11:38
Show Gist options
  • Save szabba/a428711efd75049d6076982fa72c7e87 to your computer and use it in GitHub Desktop.
Save szabba/a428711efd75049d6076982fa72c7e87 to your computer and use it in GitHub Desktop.
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Platform.Sub as Sub
import Html.Events exposing (..)
import Dict exposing (..)
import Debug as D exposing (..)
type Msg
= NoOp
| Click
type alias ConvoMsg =
{ convoId : String
, content : String
, date : Int
}
type alias LastMessages =
{ convoId : String
, content : String
}
type alias Convos =
String
type alias Model =
{ list_convoMsg : List ConvoMsg
, list_convos : List Convos
, list_lastmessages : List LastMessages
, dict_lastmessages : Dict String String
}
model : Model
model =
-- List ConvoMsg
{ list_convoMsg =
[
{ convoId = "id-1", content = "first v2 message", date = 2 }
, { convoId = "id-1", content = "four message", date = 9 }
, { convoId = "id-1", content = "third message", date = 3 }
, { convoId = "id-999", content = "second message", date = 2 }
, { convoId = "id-1", content = "22 message", date = 22 }
, { convoId = "id-1", content = "first message", date = 1 }
, { convoId = "id-1", content = "second message", date = 2 }
, { convoId = "id-3", content = "first message", date = 1 }
, { convoId = "id-2", content = "first message", date = 3 }
, { convoId = "id-999", content = "first message", date = 1 }
, { convoId = "id-4", content = "first message", date = 1 }
, { convoId = "id-2", content = "second message", date = 4 }
, { convoId = "id-1", content = "first v2 message", date = 2 }
]
, list_convos = [
"id-1"
,"id-2"
,"id-999"
]
, list_lastmessages = []
, dict_lastmessages = Dict.empty
}
view : Model -> Html Msg
view model =
div []
[ div []
[ text "TEST PAGE."
, button [ onClick Click ] [ text "Test" ]
]
, hr [] []
, div [ id "hi" ] [ text "" ]
, model |> toString |> text
]
lastMessage: List ConvoMsg -> List ConvoMsg
lastMessage list =
let
sort_list =
List.sortBy .date list
lastMessage =
List.drop ((List.length sort_list) - 1 ) sort_list
in
lastMessage
getLastMessages : List ConvoMsg -> List Convos -> List LastMessages
getLastMessages list_convoMsg list_convos =
let
-- sort all messages by date
-- sort_list_convoMsg = List.sortBy .date list_convoMsg
-- getLast =
-- List.foldr folding dicLastMsg list_convoMsg
-- filtrar e obter a lista que so elementos que so quero
checkIfMember : ConvoMsg -> Bool
checkIfMember m =
List.member m.convoId list_convos
filterMyConvos =
List.filter (\ m -> checkIfMember m) list_convoMsg
listByDateAndConvoID =
filterMyConvos
|> List.sortBy .date
|> List.sortBy .convoId
lastMsgs : List LastMessages
lastMsgs =
[]
-- get top messages from all types of convoId
--
-- (a -> b -> result)
in
lastMsgs
getLastMessagesDict : List ConvoMsg -> List Convos -> Dict String String
getLastMessagesDict list_convoMsg list_convos =
let
dicLastMsg : Dict String String
dicLastMsg =
Dict.empty
-- convoId, msg
checkIfMember : ConvoMsg -> Bool
checkIfMember m =
(List.member m.convoId list_convos)
filterMyConvos =
List.filter (\ m -> checkIfMember m) list_convoMsg
_ = D.log "myfilter" filterMyConvos
listByDateAndConvoID =
filterMyConvos
|> List.sortBy .convoId
|> List.sortBy .date
--(a -> b -> b)
folding : ConvoMsg -> Dict String String -> Dict String String
folding m lastMsgDic =
let
getDate =
Dict.get m.convoId lastMsgDic
parseMaybe =
case getDate of
Just date ->
Result.withDefault 0 (String.toInt date)
_ ->
0
dict =
if m.date < (parseMaybe) then
Dict.insert m.convoId m.content lastMsgDic
else
Dict.empty
in
dict
lastMessages =
List.foldl folding dicLastMsg listByDateAndConvoID
in
lastMessages
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp ->
( model, Cmd.none )
Click ->
( { model
| list_lastmessages = (getLastMessages model.list_convoMsg model.list_convos)
, dict_lastmessages = (getLastMessagesDict model.list_convoMsg model.list_convos)
}, Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
main : Program Never Model Msg
main =
Html.program
{ init = ( model, Cmd.none )
, view = view
, update = update
, subscriptions = subscriptions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment