Something went wrong. Try again.
An app for learning Chinese characters hanzi.bpev.me
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513module Components.Progress exposing (Category(..), Context, Data, Item, Model, Msg(..), System(..), changeLocale, defaultSystem, enter, fromJlpt, hskDecoder, initial, jlptKanjiDecoder, jlptVocabDecoder, key, update, view)
import Dict exposing (Dict)import Html exposing (Html, a, button, details, div, h2, h3, li, p, section, span, summary, text, ul)import Html.Attributes as Attr exposing (class, href, title, type_)import Html.Events exposing (onClick)import Httpimport Json.Decode as Decode exposing (Decoder)import LucideIconsimport Stringimport Svg.Attributes as SvgAttrimport Utils.Persistence exposing (Snapshot)import Utils.Subjects as Subjects exposing (Subject)
type System = Hsk | Tocfl | Jlpt
type Category = Characters | Vocabulary
type alias Item = { level : Int, slug : String }
type alias Data = { chars : List Item, vocab : List Item }
type alias Count = { learned : Int, total : Int }
type alias Model = { level : Maybe Int , system : System , selectedGrade : Maybe ( Category, Int ) , lists : Dict String (Result String Data) , jlptKanji : Maybe (Result String (List Item)) , jlptVocab : Maybe (Result String (List Item)) }
type Msg = SelectLevel Int | SelectSystem System | SelectGrade Category Int | Loaded System (Result Http.Error Data) | LoadedJlptKanji (Result Http.Error (List Item)) | LoadedJlptVocab (Result Http.Error (List Item))
initial : Modelinitial = { level = Nothing , system = Hsk , selectedGrade = Nothing , lists = Dict.empty , jlptKanji = Nothing , jlptVocab = Nothing }
{-| Ensure the selected list is loaded without discarding cached lists.Called when the dashboard becomes available, not during application startup.-}enter : (Msg -> msg) -> Model -> ( Model, Cmd msg )enter toMsg model = if Dict.member (key model.system) model.lists then ( model, Cmd.none )
else ( { model | lists = Dict.insert (key model.system) (Err "loading") model.lists } , load toMsg model.system )
{-| Reset locale-specific selections while retaining immutable list data.The application decides when the subject set changed; this feature decideshow its own state responds.-}changeLocale : String -> Model -> ModelchangeLocale locale model = { model | system = defaultSystem locale, level = Nothing, selectedGrade = Nothing }
update : (Msg -> msg) -> Int -> Msg -> Model -> ( Model, Cmd msg )update toMsg userLevel msg model = case msg of SelectLevel requested -> ( { model | level = Just (clamp 1 (max 1 userLevel) requested) }, Cmd.none ) SelectSystem system -> enter toMsg { model | system = system, selectedGrade = Nothing } SelectGrade category level -> ( { model | selectedGrade = if model.selectedGrade == Just ( category, level ) then Nothing else Just ( category, level ) } , Cmd.none ) Loaded system result -> ( { model | lists = Dict.insert (key system) (Result.mapError loadError result) model.lists } , Cmd.none ) LoadedJlptKanji result -> ( combineJlpt { model | jlptKanji = Just (Result.mapError loadError result) }, Cmd.none ) LoadedJlptVocab result -> ( combineJlpt { model | jlptVocab = Just (Result.mapError loadError result) }, Cmd.none )
combineJlpt : Model -> ModelcombineJlpt model = case ( model.jlptKanji, model.jlptVocab ) of ( Just kanji, Just vocab ) -> { model | lists = Dict.insert "JLPT" (Result.map2 fromJlpt kanji vocab) model.lists } _ -> model
loadError : Http.Error -> StringloadError _ = "progress_load_failed"
load : (Msg -> msg) -> System -> Cmd msgload toMsg system = case system of Hsk -> Http.get { url = "/static/gen/progress/hsk.json" , expect = Http.expectJson (Loaded Hsk >> toMsg) hskDecoder } Tocfl -> Http.get { url = "/static/gen/progress/tocfl.json" , expect = Http.expectJson (Loaded Tocfl >> toMsg) hskDecoder } Jlpt -> Cmd.batch [ Http.get { url = "/static/gen/progress/jlpt-kanji.json" , expect = Http.expectJson (LoadedJlptKanji >> toMsg) jlptKanjiDecoder } , Http.get { url = "/static/gen/progress/jlpt-vocab.json" , expect = Http.expectJson (LoadedJlptVocab >> toMsg) jlptVocabDecoder } ]
{-| Shared inputs are supplied, never copied into the feature's model.-}type alias Context = { locale : String , subjects : List Subject , snapshot : Snapshot , tr : String -> String }
view : (Msg -> msg) -> Context -> Model -> Html msgview toMsg context model = viewDetails { level = Maybe.withDefault (max 1 context.snapshot.level) model.level , userLevel = context.snapshot.level , locale = context.locale , subjects = context.subjects , snapshot = context.snapshot , system = model.system , progress = Dict.get (key model.system) model.lists |> Maybe.withDefault (Err "loading") , selectedGrade = model.selectedGrade , onLevel = SelectLevel >> toMsg , onSystem = SelectSystem >> toMsg , onGrade = \category level -> toMsg (SelectGrade category level) , tr = context.tr }
type alias Options msg = { level : Int , userLevel : Int , locale : String , subjects : List Subject , snapshot : Snapshot , system : System , progress : Result String Data , selectedGrade : Maybe ( Category, Int ) , onLevel : Int -> msg , onSystem : System -> msg , onGrade : Category -> Int -> msg , tr : String -> String }
key : System -> Stringkey system = case system of Hsk -> "HSK" Tocfl -> "TOCFL" Jlpt -> "JLPT"
defaultSystem : String -> SystemdefaultSystem locale = if locale == "ja" then Jlpt else if locale == "zh_TW" then Tocfl else Hsk
hskDecoder : Decoder DatahskDecoder = Decode.list (Decode.map2 Item (Decode.field "level" Decode.int) (Decode.field "traditional" Decode.string) ) |> Decode.map (\items -> { chars = List.filter (\item -> String.length item.slug == 1) items , vocab = List.filter (\item -> String.length item.slug > 1) items } )
jlptKanjiDecoder : Decoder (List Item)jlptKanjiDecoder = Decode.list (Decode.map2 Item (Decode.field "level" Decode.int) (Decode.field "kanji" Decode.string) )
jlptVocabDecoder : Decoder (List Item)jlptVocabDecoder = Decode.list (Decode.map2 Item (Decode.field "level" Decode.int) (Decode.field "chars" Decode.string) )
fromJlpt : List Item -> List Item -> DatafromJlpt chars vocab = { chars = chars, vocab = vocab }
viewDetails : Options msg -> Html msgviewDetails options = let bySlug = Dict.fromList (List.map (\subject -> ( subject.slug, subject )) options.subjects)
levelTiles kind = options.subjects |> List.filter (\subject -> subject.level == options.level && subject.kind == kind) |> List.sortBy .position |> List.map (tile options.snapshot)
systemButton system = button [ type_ "button" , class (if system == options.system then "selected" else "" ) , onClick (options.onSystem system) ] [ text (options.tr (String.toLower (key system))) ]
learned kind = options.subjects |> List.filter (\subject -> subject.kind == kind) |> List.filter (\subject -> Dict.get subject.id options.snapshot.assignments |> Maybe.map (\assignment -> assignment.startedAt /= Nothing) |> Maybe.withDefault False ) |> List.length in div [ class "progress-page" ] [ section [ class "level-stats" ] [ div [ class "level-heading" ] [ h2 [] [ text (options.tr "level" ++ " " ++ String.fromInt options.level) ] , div [] [ button [ type_ "button", Attr.attribute "aria-label" (options.tr "previous_level"), Attr.disabled (options.level <= 1), onClick (options.onLevel (options.level - 1)) ] [ arrow "arrow-left" ] , button [ type_ "button", Attr.attribute "aria-label" (options.tr "next_level"), Attr.disabled (options.level >= max 1 options.userLevel), onClick (options.onLevel (options.level + 1)) ] [ arrow "arrow-right" ] ] ] , div [ class "totals" ] [ h2 [] [ text (options.tr "total_learned") ] , if List.any (\subject -> subject.kind == "Radical") options.subjects then p [] [ text (options.tr "radicals" ++ ": " ++ String.fromInt (learned "Radical")) ] else text "" , p [] [ text (options.tr "characters" ++ ": " ++ String.fromInt (learned "Character")) ] , p [] [ text (options.tr "vocabulary" ++ ": " ++ String.fromInt (learned "Vocabulary")) ] ] , levelSection (options.tr "radicals") (levelTiles "Radical") , levelSection (options.tr "characters") (levelTiles "Character") , levelSection (options.tr "vocabulary") (levelTiles "Vocabulary") ] , section [ class "grade-system" ] [ h2 [ class "sr-only" ] [ text (options.tr "proficiency_lists") ] , div [ class "progress-tabs" ] (if options.locale == "ja" then [ systemButton Jlpt ] else [ systemButton Hsk, systemButton Tocfl ] ) , case options.progress of Err message -> p [] [ text (options.tr message) ] Ok data -> div [] [ div [ class "stats" ] [ div [ class "levels" ] [ h3 [] [ text (options.tr "characters") ] , gradeRows options bySlug Characters data.chars ] , div [ class "levels" ] [ h3 [] [ text (options.tr "vocabulary") ] , gradeRows options bySlug Vocabulary data.vocab ] ] , case options.selectedGrade of Nothing -> text "" Just ( category, level ) -> let items = if category == Characters then data.chars else data.vocab in levelSection "" (items |> List.filter (\item -> item.level == level) |> List.filterMap (\item -> Dict.get item.slug bySlug) |> List.map (tile options.snapshot) ) ] ] ]
arrow : String -> Html msgarrow name = let options = [ SvgAttr.class "ui-icon" ] in case name of "arrow-left" -> LucideIcons.arrowLeftIcon options "arrow-right" -> LucideIcons.arrowRightIcon options _ -> span [ class "ui-icon" ] []
gradeLabel : Options msg -> Int -> StringgradeLabel options level = (case options.system of Hsk -> options.tr "hsk" Tocfl -> options.tr "tocfl" Jlpt -> "N" ) ++ " " ++ String.fromInt level
levelSection : String -> List (Html msg) -> Html msglevelSection heading tiles = if List.isEmpty tiles then text "" else if heading /= "" then details [ class "progress-section", Attr.attribute "open" "" ] [ summary [] [ text heading ] , div [ class "subject-grid" ] tiles ] else div [ class "progress-section", Attr.attribute "open" "" ] [ div [ class "subject-grid" ] tiles ]
tile : Snapshot -> Subject -> Html msgtile snapshot subject = let assignment = Dict.get subject.id snapshot.assignments
status = case assignment of Just value -> if value.completedAt /= Nothing || value.markedCompleted || value.passedAt /= Nothing then "passed" else if value.startedAt /= Nothing then "started" else "locked" Nothing -> "locked" in a [ href ("#!/reference/subject/" ++ subject.id) , class ("subject-tile " ++ String.toLower subject.kind ++ " " ++ status) , title (Subjects.primaryMeaning subject) ] [ text subject.character ]
gradeRows : Options msg -> Dict String Subject -> Category -> List Item -> Html msggradeRows options bySlug category items = let tally item current = let prior = Dict.get item.level current |> Maybe.withDefault { learned = 0, total = 0 }
studied = Dict.get item.slug bySlug |> Maybe.andThen (\subject -> Dict.get subject.id options.snapshot.assignments) |> Maybe.map (\assignment -> assignment.startedAt /= Nothing) |> Maybe.withDefault False in Dict.insert item.level { learned = prior.learned + (if studied then 1 else 0 ) , total = prior.total + 1 } current
groups = List.foldl tally Dict.empty items |> Dict.toList
ordered = if options.system == Jlpt then List.reverse groups else groups in ul [ class "grade-list" ] (List.map (\( level, counts ) -> li [] [ button [ type_ "button" , class (if options.selectedGrade == Just ( category, level ) then "grade-level selected " ++ (if category == Characters then "character" else "vocabulary" ) else "grade-level" ) , onClick (options.onGrade category level) ] [ span [ class "bold" ] [ text (gradeLabel options level ++ ": ") ] , span [] [ text (String.fromInt (round (toFloat counts.learned / toFloat counts.total * 100)) ++ "% (" ++ String.fromInt counts.learned ++ "/" ++ String.fromInt counts.total ++ ")" ) ] ] ] ) ordered )