Skip to content

Commit

Permalink
enhancement(elm): Introduce time subscriptions for progress bar (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrapacz authored Dec 28, 2017
1 parent f6c64bf commit 5bd57d8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
2 changes: 2 additions & 0 deletions aion/web/elm/src/App.elm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Navigation exposing (Location, modifyUrl)
import Panel.Api exposing (fetchCategories)
import Phoenix.Socket
import Room.Api exposing (fetchRooms)
import Room.Subscriptions
import Routing
import Update exposing (setHomeUrl, update)
import User.Api exposing (fetchCurrentUser)
Expand Down Expand Up @@ -43,6 +44,7 @@ subscriptions model =
[ Phoenix.Socket.listen model.socket Msgs.PhoenixMsg
, Navbar.subscriptions model.navbarState NavbarMsg
, Sub.map Msgs.MultiselectMsg <| Multiselect.subscriptions model.panelData.categoryMultiSelect
, Room.Subscriptions.subscriptions model
]


Expand Down
2 changes: 1 addition & 1 deletion aion/web/elm/src/Msgs.elm
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Msg
| ReceiveUserList Encode.Value
| ReceiveQuestionSummary Encode.Value
| LeaveRoom
| Tick
| Tick Time
| OnTime Time
| OnInitialTime Time
| OnRankingCategoryChange String
10 changes: 0 additions & 10 deletions aion/web/elm/src/Room/Constants.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,3 @@ answerInputFieldId =
progressBarTimeout : Float
progressBarTimeout =
10000


progressBarTicks : Float
progressBarTicks =
1000


progressBarDelay : Float
progressBarDelay =
progressBarTimeout / progressBarTicks
16 changes: 16 additions & 0 deletions aion/web/elm/src/Room/Subscriptions.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Room.Subscriptions exposing (..)

import General.Models exposing (Model)
import Msgs
import Room.Models exposing (RoomState(QuestionBreak, QuestionDisplayed))
import Time


subscriptions : Model -> Sub Msgs.Msg
subscriptions model =
case model.roomState of
QuestionDisplayed ->
Time.every (20 * Time.millisecond) Msgs.Tick

QuestionBreak ->
Sub.none
33 changes: 11 additions & 22 deletions aion/web/elm/src/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Update exposing (..)
import Auth.Api exposing (registerUser, submitCredentials)
import Auth.Models exposing (Token, UnauthenticatedViewToggle(LoginView, RegisterView))
import Auth.Notifications exposing (loginErrorToast, registrationErrorToast)
import Delay
import Dom exposing (focus)
import Forms
import General.Constants exposing (loginFormMsg, registerFormMsg)
Expand All @@ -20,7 +19,7 @@ import Ports exposing (check)
import Ranking.Api exposing (fetchRanking)
import RemoteData
import Room.Api exposing (fetchRooms)
import Room.Constants exposing (answerInputFieldId, enterKeyCode, progressBarDelay, progressBarTimeout)
import Room.Constants exposing (answerInputFieldId, enterKeyCode, progressBarTimeout)
import Room.Decoders exposing (answerFeedbackDecoder, questionDecoder, questionSummaryDecoder, userJoinedInfoDecoder, userLeftDecoder, userListMessageDecoder)
import Room.Models exposing (Event(MkQuestionSummaryLog, MkUserJoinedLog, MkUserLeftLog), EventLog, ProgressBarState(Running, Stopped, Uninitialized), RoomState(QuestionBreak, QuestionDisplayed), asLogIn, withProgress, withRunning, withStart)
import Room.Notifications exposing (..)
Expand Down Expand Up @@ -529,7 +528,7 @@ update msg model =
| roomState = QuestionDisplayed
, progressBar = model.progressBar |> withProgress 0 |> withRunning Uninitialized |> withStart 0
}
! [ Delay.after progressBarDelay millisecond Tick ]
! []

ReceiveQuestionBreak raw ->
{ model
Expand Down Expand Up @@ -709,20 +708,16 @@ update msg model =
! []
|> roomFormValidationErrorToast

Tick ->
let
cmd =
case model.progressBar.running of
Uninitialized ->
[ Task.perform OnInitialTime Time.now ]
Tick time ->
case model.progressBar.running of
Uninitialized ->
update (OnInitialTime time) model

Running ->
[ Task.perform OnTime Time.now ]
Running ->
update (OnTime time) model

Stopped ->
[]
in
model ! cmd
Stopped ->
model ! []

OnInitialTime time ->
update
Expand All @@ -743,13 +738,7 @@ update msg model =
_ ->
model.progressBar
in
(progressBar |> asProgressBarIn model)
! case progressBar.running of
Running ->
[ Delay.after progressBarDelay millisecond Tick ]

_ ->
[]
(progressBar |> asProgressBarIn model) ! []

MultiselectMsg subMsg ->
let
Expand Down

0 comments on commit 5bd57d8

Please sign in to comment.