Commit ac1884c0 authored by Cédric F.'s avatar Cédric F.

Allow to hide unproductive years

parent 1e0395a7
Pipeline #5416208 passed with stage
in 1 minute and 17 seconds
......@@ -13,6 +13,8 @@ type alias Model =
, kidUntil : Int
, oldFrom : Int
, lifeExpectancy : Int
, lifeExpectancyString : String
, hideUnproductiveYears : Bool
, events : List Event
, newEventOpen : Bool
, newEvent : Event
......@@ -26,6 +28,7 @@ type alias JsonModel =
, kidUntil : Int
, oldFrom : Int
, lifeExpectancy : Int
, hideUnproductiveYears : Bool
, events : List Event
}
......@@ -45,6 +48,8 @@ initialModel =
, kidUntil = 18
, oldFrom = 70
, lifeExpectancy = 80
, lifeExpectancyString = "80"
, hideUnproductiveYears = False
, events = []
, newEventOpen = False
, newEvent = initialEvent
......@@ -61,6 +66,8 @@ mergeJsonModel jsonModel =
, kidUntil = jsonModel.kidUntil
, oldFrom = jsonModel.oldFrom
, lifeExpectancy = jsonModel.lifeExpectancy
, lifeExpectancyString = toString jsonModel.lifeExpectancy
, hideUnproductiveYears = jsonModel.hideUnproductiveYears
, events = jsonModel.events
}
......@@ -101,6 +108,7 @@ type Msg
| NewUrl Navigation.Location
| NewDateOfBirth String
| NewLifeExpectancy String
| HideUnproductiveYears Bool
| ToggleNewEvent
| UpdateNewEvent NewEventField String
| SaveNewEvent
......@@ -108,9 +116,30 @@ type Msg
| ToggleModal
deathDate : Model -> Date
deathDate model =
partialDate model (Date.year model.birthDate + model.lifeExpectancy)
relativeBirthDate : Model -> Date
relativeBirthDate model =
if model.hideUnproductiveYears then
partialDate model (Date.year model.birthDate + model.kidUntil)
else
model.birthDate
relativeDeathDate : Model -> Date
relativeDeathDate model =
if model.hideUnproductiveYears then
partialDate model (Date.year model.birthDate + maxOldFrom model)
else
partialDate model (Date.year model.birthDate + model.lifeExpectancy)
maxKidUntil : Model -> Int
maxKidUntil model =
Basics.min model.lifeExpectancy model.kidUntil
maxOldFrom : Model -> Int
maxOldFrom model =
Basics.min model.lifeExpectancy model.oldFrom
partialDate : Model -> Int -> Date
......
......@@ -44,17 +44,19 @@ serializeJson model =
, ( "life-expectancy", Encode.int model.lifeExpectancy )
, ( "kid-until", Encode.int model.kidUntil )
, ( "old-from", Encode.int model.oldFrom )
, ( "hide-unproductive-years", Encode.bool model.hideUnproductiveYears )
, ( "events", Encode.list <| List.map encodeEvent model.events )
]
jsonDecoder : Decode.Decoder JsonModel
jsonDecoder =
Decode.map5 JsonModel
Decode.map6 JsonModel
(Decode.field "birth-date" dateDecoder)
(Decode.field "kid-until" Decode.int)
(Decode.field "old-from" Decode.int)
(Decode.field "life-expectancy" Decode.int)
(Decode.field "hide-unproductive-years" Decode.bool)
(Decode.field "events" <| Decode.list eventDecoder)
......
......@@ -44,7 +44,10 @@ update msg model =
Ok date ->
let
newModel =
{ model | birthDateString = string, birthDate = date }
{ model
| birthDateString = string
, birthDate = date
}
in
( newModel, updateUrl newModel )
......@@ -53,16 +56,31 @@ update msg model =
NewLifeExpectancy string ->
let
value =
case String.toInt string of
Ok int ->
clamp 1 500 int
int =
String.toInt string |> Result.toMaybe |> Maybe.withDefault 0
in
if int < minLifeExpectancy model || int > 500 then
( { model | lifeExpectancyString = string }, Cmd.none )
else
let
newModel =
{ model
| lifeExpectancyString = string
, lifeExpectancy = int
}
in
( newModel, updateUrl newModel )
Err _ ->
1
HideUnproductiveYears bool ->
let
tempModel =
{ model | hideUnproductiveYears = bool }
newModel =
{ model | lifeExpectancy = value }
{ tempModel
| lifeExpectancy =
Basics.max tempModel.lifeExpectancy (minLifeExpectancy tempModel)
}
in
( newModel, updateUrl newModel )
......@@ -140,3 +158,11 @@ updateUrl model =
"#" ++ base64
in
Navigation.newUrl url
minLifeExpectancy : Model -> Int
minLifeExpectancy model =
if model.hideUnproductiveYears then
model.kidUntil
else
1
......@@ -9,7 +9,7 @@ import Color.Interpolate
import Date exposing (Date)
import Time exposing (Time)
import DateExtra
import Lifechart.Model exposing (..)
import Lifechart.Model as Model exposing (..)
lineHeight : number
......@@ -73,7 +73,10 @@ canvas model =
|> Collage.moveX (canvasWidth / 2 - legendWidth + lineHeight)
form =
Collage.group [ grid, theLegend ]
if model.hideUnproductiveYears then
Collage.group [ grid ]
else
Collage.group [ grid, theLegend ]
in
Collage.collage canvasWidth (ceiling height) [ form ] |> Element.toHtml
......@@ -96,7 +99,7 @@ legend model =
)
maxKidUntil =
Basics.min lifeExpectancy kidUntil
Model.maxKidUntil model |> toFloat
kid =
makeText "kid"
......@@ -111,7 +114,7 @@ legend model =
(canvasHeight (lifeExpectancy / 2 - kidUntil) - markOffset)
maxOldFrom =
Basics.min lifeExpectancy oldFrom
Model.maxOldFrom model |> toFloat
productive =
if maxOldFrom - kidUntil < 8 then
......@@ -145,9 +148,15 @@ years model =
makeYear i =
Collage.group (year model i)
|> Collage.moveY
((toFloat -i - 1) * (weekWidth + weekBorder * 2) - lineHeight)
((0 - toFloat (i - from + 1)) * (weekWidth + weekBorder * 2) - lineHeight)
( from, to ) =
if model.hideUnproductiveYears then
( model.kidUntil, maxOldFrom model )
else
( 0, model.lifeExpectancy )
in
weekIndexes :: (List.range 0 model.lifeExpectancy |> List.map makeYear)
weekIndexes :: (List.range from to |> List.map makeYear)
weekIndexes : Form
......@@ -246,7 +255,8 @@ week model year week =
outOfBounds : Model -> Time -> Bool
outOfBounds model time =
time < Date.toTime model.birthDate || time > Date.toTime (deathDate model)
(time < Date.toTime (relativeBirthDate model))
|| (time > Date.toTime (relativeDeathDate model))
isCurrentWeek : Model -> Time -> Bool
......@@ -270,18 +280,24 @@ isKid : Model -> Time -> Bool
isKid model time =
let
kidDate =
partialDate model (Date.year model.birthDate + model.kidUntil)
partialDate model (Date.year (relativeBirthDate model) + model.kidUntil)
in
Date.toTime kidDate >= time
if model.hideUnproductiveYears then
False
else
Date.toTime kidDate >= time
isOld : Model -> Time -> Bool
isOld model time =
let
oldDate =
partialDate model (Date.year model.birthDate + model.oldFrom)
partialDate model (Date.year (relativeBirthDate model) + model.oldFrom)
in
Date.toTime oldDate <= time
if model.hideUnproductiveYears then
False
else
Date.toTime oldDate <= time
yearWeekToTime : Model -> ( Int, Int ) -> Time
......
......@@ -67,7 +67,7 @@ links : Model -> List (Html Msg)
links model =
let
demo =
"ewogICJiaXJ0aC1kYXRlIjogIjE5ODgtMDctMjQiLAogICJsaWZlLWV4cGVjdGFuY3kiOiA4MCwKICAia2lkLXVudGlsIjogMTgsCiAgIm9sZC1mcm9tIjogNzAsCiAgImV2ZW50cyI6IFsKICAgIHsKICAgICAgImZyb20iOiAiMjAxMC0wOS0xNCIsCiAgICAgICJ0byI6ICIyMDE0LTAyLTAxIiwKICAgICAgImNvbG9yIjogIiNmNTc5MDAiLAogICAgICAibGFiZWwiOiAiY29sbGVnZSIKICAgIH0sCiAgICB7CiAgICAgICJmcm9tIjogIjIwMTUtMDYtMDEiLAogICAgICAidG8iOiAiMjAxNi0wNS0zMSIsCiAgICAgICJjb2xvciI6ICIjNzNkMjE2IiwKICAgICAgImxhYmVsIjogInRyaXAiCiAgICB9CiAgXQp9Cg=="
"ewogICJiaXJ0aC1kYXRlIjogIjE5ODgtMDctMjQiLAogICJsaWZlLWV4cGVjdGFuY3kiOiA4MCwKICAia2lkLXVudGlsIjogMTgsCiAgIm9sZC1mcm9tIjogNzAsCiAgImhpZGUtdW5wcm9kdWN0aXZlLXllYXJzIjogZmFsc2UsCiAgImV2ZW50cyI6IFsKICAgIHsKICAgICAgImZyb20iOiAiMjAxMC0wOS0xNCIsCiAgICAgICJ0byI6ICIyMDE0LTAyLTAxIiwKICAgICAgImNvbG9yIjogIiNmNTc5MDAiLAogICAgICAibGFiZWwiOiAiY29sbGVnZSIKICAgIH0sCiAgICB7CiAgICAgICJmcm9tIjogIjIwMTUtMDYtMDEiLAogICAgICAidG8iOiAiMjAxNi0wNS0zMSIsCiAgICAgICJjb2xvciI6ICIjNzNkMjE2IiwKICAgICAgImxhYmVsIjogInRyaXAiCiAgICB9CiAgXQp9Cg=="
current =
Serializer.serialize model
......@@ -83,8 +83,8 @@ links model =
config : Model -> List (Html Msg)
config model =
[ div [ class "row form-group" ]
[ label [ class "col-xs-4 col-form-label col-form-label-lg" ] [ text "Date of Birth" ]
, div [ class "col-xs-8" ]
[ label [ class "col-xs-5 col-form-label col-form-label-lg" ] [ text "Date of Birth" ]
, div [ class "col-xs-7" ]
[ input
(List.append dateInputAttributes
[ class "form-control form-control-lg"
......@@ -96,18 +96,61 @@ config model =
]
]
, div [ class "row form-group" ]
[ label [ class "col-xs-4 col-form-label col-form-label-lg" ] [ text "Life Expectancy" ]
, div [ class "col-xs-8" ]
[ label [ class "col-xs-5 col-form-label col-form-label-lg" ]
[ text "Life Expectancy "
, a
[ href
"https://en.wikipedia.org/wiki/List_of_countries_by_life_expectancy"
, target "_blank"
]
[ text "(?)" ]
]
, div [ class "col-xs-7" ]
[ input
[ class "form-control form-control-lg"
, type_ "number"
, required True
, value <| toString model.lifeExpectancy
, value model.lifeExpectancyString
, Html.Attributes.min "1"
, Html.Attributes.max "500"
, onInput NewLifeExpectancy
]
[]
]
]
, div [ class "row form-group" ]
[ label [ class "col-xs-5 col-form-label col-form-label-lg" ]
[ text "Unproductive years "
, a
[ href
"https://www.reddit.com/r/GetMotivated/comments/1vyf9r/made_for_myself_thought_of_you_weeks_left/cexas8u/"
, target "_blank"
]
[ text "(?)" ]
]
, div [ class "col-xs-7" ]
[ label [ class "form-check-inline form-control-lg" ]
[ input
[ class "form-check-input"
, type_ "radio"
, checked <| not model.hideUnproductiveYears
, onClick <| HideUnproductiveYears False
]
[]
, text " Show"
]
, label [ class "form-check-inline form-control-lg" ]
[ input
[ class "form-check-input"
, type_ "radio"
, checked model.hideUnproductiveYears
, onClick <| HideUnproductiveYears True
]
[]
, text " Hide"
]
]
]
]
......@@ -235,7 +278,7 @@ events model =
eventPercentage event model =
100
* (Date.toTime event.to - Date.toTime event.from)
/ (Date.toTime (deathDate model) - Date.toTime model.birthDate)
/ (Date.toTime (relativeDeathDate model) - Date.toTime (relativeBirthDate model))
|> roundToPadded 1
list =
......@@ -262,8 +305,8 @@ metrics model =
percentage =
100
* (model.now - Date.toTime model.birthDate)
/ (Date.toTime (deathDate model) - Date.toTime model.birthDate)
* (model.now - Date.toTime (relativeBirthDate model))
/ (Date.toTime (relativeDeathDate model) - Date.toTime (relativeBirthDate model))
|> clamp 0 100
|> roundToPadded 6
in
......@@ -301,8 +344,6 @@ footer =
[ text "Inspired by "
, a [ href "https://i.imgur.com/67aHKhF.jpg", target "_blank" ] [ text "this chart" ]
, text " - "
, a [ href "https://www.reddit.com/r/GetMotivated/comments/1vyf9r/made_for_myself_thought_of_you_weeks_left/cexas8u/", target "_blank" ] [ text "I'm terrified" ]
, text " - "
, a [ href "https://github.com/infertux/lifechart", target "_blank" ] [ text "Source code" ]
, text " - "
, a [ href "javascript:void(0)", onClick ToggleModal ] [ text "Show raw data" ]
......@@ -331,7 +372,7 @@ weeksLeft : Model -> String
weeksLeft model =
let
left =
Date.toTime (deathDate model) - model.now
Date.toTime (relativeDeathDate model) - model.now
weeks =
(Time.inHours left) / 24 / 7 |> Basics.max 0
......
- UI to edit events
- hideUnproductiveYears : Bool
https://www.reddit.com/r/GetMotivated/comments/1vyf9r/made_for_myself_thought_of_you_weeks_left/cexas8u
- responsive
- store location / countries
- store goals
......@@ -3,6 +3,7 @@
"life-expectancy": 80,
"kid-until": 18,
"old-from": 70,
"hide-unproductive-years": false,
"events": [
{
"from": "2010-09-14",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment