DataModel.qml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import QtQuick
  2. import Felgo
  3. Item {
  4. // property to configure target dispatcher / logic
  5. property alias dispatcher: logicConnection.target
  6. // whether api is busy (ongoing network requests)
  7. readonly property bool isBusy: api.busy
  8. // whether a user is logged in
  9. readonly property bool userLoggedIn: _.userLoggedIn
  10. // model data properties
  11. readonly property alias todos: _.todos
  12. readonly property alias todoDetails: _.todoDetails
  13. // action success signals
  14. signal todoStored(var todo)
  15. // action error signals
  16. signal fetchTodosFailed(var error)
  17. signal fetchTodoDetailsFailed(int id, var error)
  18. signal storeTodoFailed(var todo, var error)
  19. // listen to actions from dispatcher
  20. Connections {
  21. id: logicConnection
  22. // action 1 - fetchTodos
  23. function onFetchTodos() {
  24. // check cached value first
  25. var cached = cache.getValue("todos")
  26. if(cached)
  27. _.todos = cached
  28. // load from api
  29. api.getTodos(
  30. function(data) {
  31. // cache data before updating model property
  32. cache.setValue("todos",data)
  33. _.todos = data
  34. },
  35. function(error) {
  36. // action failed if no cached data
  37. if(!cached)
  38. fetchTodosFailed(error)
  39. })
  40. }
  41. // action 2 - fetchTodoDetails
  42. function onFetchTodoDetails(id) {
  43. // check cached todo details first
  44. var cached = cache.getValue("todo_"+id)
  45. if(cached) {
  46. _.todoDetails[id] = cached
  47. todoDetailsChanged() // signal change within model to update UI
  48. }
  49. // load from api
  50. api.getTodoById(id,
  51. function(data) {
  52. // cache data first
  53. cache.setValue("todo_"+id, data)
  54. _.todoDetails[id] = data
  55. todoDetailsChanged()
  56. },
  57. function(error) {
  58. // action failed if no cached data
  59. if(!cached) {
  60. fetchTodoDetailsFailed(id, error)
  61. }
  62. })
  63. }
  64. // action 3 - storeTodo
  65. function onStoreTodo(todo) {
  66. // store with api
  67. api.addTodo(todo,
  68. function(data) {
  69. // NOTE: Dummy REST API always returns 201 as id of new todo
  70. // To simulate a new todo, we set correct local id based on current model
  71. data.id = _.todos.length + 1
  72. // cache newly added item details
  73. cache.setValue("todo_"+data.id, data)
  74. // add new item to todos
  75. _.todos.unshift(data)
  76. // cache updated todo list
  77. cache.setValue("todos", _.todos)
  78. todosChanged()
  79. todoStored(data)
  80. },
  81. function(error) {
  82. storeTodoFailed(todo, error)
  83. })
  84. }
  85. // action 4 - clearCache
  86. function onClearCache() {
  87. cache.clearAll()
  88. }
  89. // action 5 - login
  90. function onLogin(password) {
  91. if(password==="8888")
  92. {
  93. _.userLoggedIn = true
  94. }else if(password==="156715"){
  95. if(wifService && wifService.connected) {
  96. wifService.setKioskMode(false)
  97. wifService.startHome()
  98. } else {
  99. console.warn("wifService not connected, skipping kiosk mode operations")
  100. }
  101. serial.Maichong_Exit()
  102. }
  103. else
  104. {
  105. nativeUtils.displayMessageBox("密码错误!", "", 1)
  106. _.userLoggedIn = false
  107. }
  108. }
  109. // action 6 - logout
  110. function onLogout() {
  111. _.userLoggedIn = false
  112. }
  113. }
  114. // you can place getter functions here that do not modify the data
  115. // pages trigger write operations through logic signals only
  116. // rest api for data access
  117. RestAPI {
  118. id: api
  119. maxRequestTimeout: 3000 // use max request timeout of 3 sec
  120. }
  121. // storage for caching
  122. Storage {
  123. id: cache
  124. }
  125. // private
  126. Item {
  127. id: _
  128. // data properties
  129. property var todos: [] // Array
  130. property var todoDetails: ({}) // Map
  131. // auth
  132. property bool userLoggedIn: false
  133. }
  134. }