Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. namespace XoaDotNet
  2.  
  3. open Avalonia.Controls
  4. open Avalonia.Media
  5. open Avalonia.FuncUI.Types
  6. open Avalonia.FuncUI
  7. open Avalonia.Layout
  8. open FSharpx.Collections
  9. open Bogus
  10.  
  11. [<AutoOpen>]
  12. module Helpers =
  13. let pvSet = PersistentVector.update
  14. let pvGet = PersistentVector.nth
  15. let pvAdd = PersistentVector.conj
  16. type IndexedMessage<'Msg> = ( int*'Msg )
  17.  
  18.  
  19. module PersonModule =
  20.  
  21. type PersonState = {
  22. name : string
  23. age : int
  24. }
  25.  
  26. let f = Faker()
  27.  
  28. let personGen() : PersonState = {
  29. name = f.Name.FirstName()
  30. age = f.Random.Number(10,99)
  31. }
  32.  
  33. type PersonMsg =
  34. | EditName of string
  35. | EditAge of int
  36.  
  37. let update (msg:PersonMsg) (state:PersonState) : PersonState =
  38. match msg with
  39. | EditName name -> { state with name = name }
  40. | EditAge age -> {state with age = age}
  41.  
  42. let updateNth (msg:PersonMsg) id state =
  43. pvSet id (update msg (pvGet id state)) state
  44.  
  45. let view (state:PersonState) (dispatch) : View =
  46. Views.uniformGrid [
  47. Attrs.columns 2
  48. Attrs.children [
  49. Views.textBox [
  50. Attrs.text state.name
  51. Attrs.onKeyUp(fun sender args ->
  52. dispatch (EditName (sender :?> TextBox).Text)
  53. )
  54. ]
  55. Views.textBox [
  56. Attrs.text (sprintf "%d" state.age)
  57. Attrs.onKeyUp(fun sender args ->
  58. dispatch (EditAge ((sender :?> TextBox).Text |> int))
  59. )
  60. ]
  61. ]
  62. ]
  63.  
  64. module PersonsModule =
  65. type PersonsMsg =
  66. | Update of IndexedMessage<PersonModule.PersonMsg>
  67. | Delete of int
  68.  
  69. let update (personsMsg:PersonsMsg) state =
  70. match personsMsg with
  71. | Update (id, msg) -> pvSet id (PersonModule.update msg (pvGet id state)) state
  72. | Delete id ->
  73. state
  74. |> Seq.indexed
  75. |> Seq.where ( fun (_id,_)->id<>_id)
  76. |> Seq.map (fun (_,p)->p)
  77. |> PersistentVector.ofSeq
  78.  
  79. let view (state:PersonModule.PersonState PersistentVector) (dispatch) : View =
  80. // Set up a dispatcher for a person at a specific id
  81. let dispatchPerson id = (fun msg -> dispatch(Update(id,msg)))
  82. Views.scrollViewer [
  83. Attrs.content (
  84. Views.stackPanel [
  85. Attrs.children [
  86. for (id,person) in state |> Seq.indexed do
  87. yield Views.dockpanel [
  88. Attrs.children [
  89. Views.button [
  90. Attrs.content "X"
  91. Attrs.onClick ( fun sender args -> dispatch (PersonsMsg.Delete id) )
  92. ]
  93. PersonModule.view person (dispatchPerson id)
  94. ]
  95. ]
  96. ]
  97. ]
  98. )
  99. ]
  100.  
  101. module ParentView =
  102. open Bogus
  103.  
  104. // The model holds data that you want to keep track of while the application is running
  105. type ParentState = {
  106. people : PersonModule.PersonState PersistentVector
  107. }
  108.  
  109. let barConjGen state =
  110. state |> PersistentVector.conj (PersonModule.personGen())
  111.  
  112. //The initial state of of the application
  113. let initialState = {
  114. people = PersistentVector.empty
  115. |> barConjGen
  116. |> barConjGen
  117. |> barConjGen
  118. |> barConjGen
  119. |> barConjGen
  120. |> barConjGen
  121. |> barConjGen
  122. |> barConjGen
  123. }
  124.  
  125. type ParentViewMsg =
  126. | PersonsMsg of PersonsModule.PersonsMsg
  127. | NewPerson
  128.  
  129.  
  130. let update (msg: ParentViewMsg) (state: ParentState) : ParentState =
  131. match msg with
  132. | PersonsMsg msg -> { state with people = PersonsModule.update msg state.people }
  133. | NewPerson -> { state with people = state.people |> barConjGen }
  134.  
  135.  
  136. let view (state: ParentState) (dispatch): View =
  137. Views.dockpanel[
  138. Attrs.children [
  139. Views.button [
  140. Attrs.dockPanel_dock Dock.Bottom
  141. Attrs.content "new"
  142. Attrs.onClick (fun sender args -> dispatch ParentViewMsg.NewPerson)
  143.  
  144. ]
  145. PersonsModule.view state.people (PersonsMsg >> dispatch)
  146. ]
  147. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement