Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.14 KB | None | 0 0
  1. import QtQuick 2.11
  2. import Shared 1.0
  3. import "controls"
  4. import "delegates"
  5.  
  6. SharedStackPage {
  7. id: buddiesPage
  8.  
  9. property int buttonWidth: (width - 4 * theme.textMargins) / 3
  10. property var model: controller.buddyList.sortedBuddyModel
  11. property bool refreshWhenActivated: true
  12.  
  13. readonly property JsonRequester invites: controller.pendingInvites
  14. readonly property JsonRequester requests: controller.buddyInvites
  15.  
  16. function fetchBuddyLists() {
  17. invites.request();
  18. requests.request();
  19. controller.buddyList.request();
  20. }
  21.  
  22. headerTitle: qsTr("Buddies")
  23. pageUrl: "buddies"
  24.  
  25. onPageActivated: {
  26. if (refreshWhenActivated) {
  27. fetchBuddyLists();
  28. if (!model) {
  29. model = Qt.binding(function () {
  30. return controller.buddyList.model;
  31. });
  32. }
  33. refreshWhenActivated = false;
  34. }
  35. }
  36.  
  37. onHeaderClicked: {
  38. fetchBuddyLists();
  39. }
  40.  
  41. JsonRequester {
  42. id: buddyInviteRequester
  43. method: "POST"
  44. path: "/buddy/invitation?token=" + token
  45. onSuccess: invites.request()
  46. onFail: notifyError(error, qsTr("Failed to invite buddy"))
  47. }
  48.  
  49. JsonRequester {
  50. id: deleteBuddyInvitation
  51. method: "DELETE"
  52. onFail: confirmError(error, qsTr("Failed to remove buddy"))
  53. onSuccess: invites.request()
  54. }
  55.  
  56. JsonRequester {
  57. id: handleInvite
  58. method: "PUT"
  59. onSuccess: {
  60. requests.request();
  61. controller.buddyList.request();
  62. }
  63. }
  64.  
  65. onPlayerReady: {
  66. var playerNumber = playerAdder.playerNumber, clubNumber = playerAdder.clubNumber;
  67. var data = [{
  68. "number": playerNumber,
  69. "club": {
  70. "number": clubNumber
  71. }
  72. }];
  73. buddyInviteRequester.request(data);
  74. }
  75.  
  76. function canAddBuddy(number, clubnumber) {
  77. var pending = invites.model;
  78. for (var a in pending) {
  79. var buddy = pending[a];
  80. if (number === buddy.number && clubnumber === buddy.club.number) {
  81. return true;
  82. }
  83. }
  84.  
  85. var buddies = controller.buddyList.model;
  86. for (a in buddies) {
  87. buddy = buddies[a];
  88. if (number === buddy.number && clubnumber === buddy.club.number) {
  89. return true;
  90. }
  91. }
  92.  
  93. if (number === session.playerNumber && clubnumber === session.clubNumber) {
  94. return true;
  95. }
  96. return false;
  97. }
  98.  
  99. sourceComponent: SharedFlickable {
  100. id: flickArea
  101. anchors.fill: parent
  102. contentHeight: column.height + theme.textMargins
  103.  
  104. pullToRefresh: true
  105. onRefreshEvent: fetchBuddyLists()
  106.  
  107. Column {
  108. id: column
  109. width: parent.width
  110. anchors.margins: theme.textMargins
  111. anchors.left: parent.left
  112. anchors.right: parent.right
  113. spacing: 20 * scaleFactor
  114.  
  115. SharedButton {
  116. id: searchAndAddButton
  117. width: parent.width
  118. text: qsTr("Add buddy")
  119. labelColor: theme.accentForeground
  120. onClicked: {
  121. playerAdder.title = qsTr("Add buddy");
  122. playerAdder.setPlayerFilter(canAddBuddy);
  123. playerAdder.showBuddies = false;
  124. playerAdder.show();
  125. }
  126. }
  127.  
  128. Item {
  129. width: parent.width
  130. height: requests.model.length ? pendingRequests.height : 0
  131. Behavior on height {
  132. NumberAnimation {
  133. easing.type: Easing.OutSine
  134. }
  135. }
  136. Column {
  137. id: pendingRequests
  138. width: parent.width
  139. spacing: 10 * scaleFactor
  140.  
  141. SectionHeader {
  142. text: qsTr("Requests")
  143. height: pendingRequests.height
  144. && pendingRequests.model.length ? implicitHeight : 0
  145. Behavior on height {
  146. NumberAnimation {
  147. easing.type: Easing.OutSine
  148. }
  149. }
  150. Behavior on opacity {
  151. OpacityAnimator {
  152. duration: 100
  153. }
  154. }
  155. opacity: requests.model.length > 0 ? 1 : 0
  156. }
  157.  
  158. Repeater {
  159. model: requests.model
  160. Item {
  161. width: parent ? parent.width : 100
  162. height: 190 * scaleFactor
  163.  
  164. StyledText {
  165. id: header
  166. text: qsTr("Buddy request from ") + modelData.firstName + ' ' + modelData.lastName
  167. anchors.left: parent.left
  168. anchors.top: parent.top
  169. anchors.margins: 16 * scaleFactor
  170. anchors.right: parent.right
  171. }
  172. StyledText {
  173. text: modelData.club.name
  174. anchors.left: parent.left
  175. anchors.top: header.bottom
  176. font.pixelSize: 24 * scaleFactor
  177. color: theme.borderColor
  178. anchors.margins: 14 * scaleFactor
  179. wrapMode: Text.WrapAtWordBoundaryOrAnywhere
  180. }
  181. RectButton {
  182. id: acceptButton
  183. anchors.left: parent.left
  184. anchors.bottom: parent.bottom
  185. anchors.margins: 14 * scaleFactor
  186. text: qsTr("Accept")
  187. foregroundColor: theme.accentForeground
  188. border.color: Qt.darker(color)
  189. checkable: false
  190. color: Qt.darker(theme.accentColor, pressed ? 1.2 : 1.0)
  191. onClicked: {
  192. handleInvite.path = "/buddy/invitation?token=" + session.token + "&accept=true";
  193. handleInvite.request(modelData);
  194. }
  195. }
  196. RectButton {
  197. anchors.right: parent.right
  198. anchors.bottom: parent.bottom
  199. anchors.margins: 14 * scaleFactor
  200. text: qsTr("Decline")
  201. checkable: false
  202. color: theme.buttonColor
  203. onClicked: {
  204. handleInvite.path = "/buddy/invitation?token=" + session.token + "&accept=false";
  205. handleInvite.request(modelData);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212.  
  213. Item {
  214. width: parent.width
  215. height: invites.model.length ? pendingInvites.height : 0
  216. Behavior on height {
  217. NumberAnimation {
  218. easing.type: Easing.OutSine
  219. }
  220. }
  221. Column {
  222. id: pendingInvites
  223. width: parent.width
  224. spacing: 20 * scaleFactor
  225.  
  226. SectionHeader {
  227. text: qsTr("Pending invitations")
  228. height: invites.model && invites.model.length ? implicitHeight : 0
  229. Behavior on height {
  230. NumberAnimation {
  231. easing.type: Easing.OutSine
  232. }
  233. }
  234. Behavior on opacity {
  235. OpacityAnimator {
  236. duration: 100
  237. }
  238. }
  239. opacity: invites.model.length > 0 ? 1 : 0
  240. }
  241.  
  242. Repeater {
  243. model: invites.model
  244. Item {
  245. width: parent ? parent.width : 100
  246. height: 90 * scaleFactor
  247.  
  248. Column {
  249. anchors.left: parent.left
  250. anchors.right: cancelButton.left
  251. anchors.margins: 16 * scaleFactor
  252. spacing: 4 * scaleFactor
  253. anchors.verticalCenter: parent.verticalCenter
  254. StyledText {
  255. id: header2
  256. anchors.left: parent.left
  257. text: modelData.firstName + ' ' + modelData.lastName + qsTr(
  258. " invited")
  259. width: parent.width
  260. anchors.margins: 10 * scaleFactor
  261. wrapMode: Text.WrapAtWordBoundaryOrAnywhere
  262. }
  263. StyledText {
  264. text: modelData.club.name
  265. width: parent.width
  266. anchors.left: parent.left
  267. font.pixelSize: 24 * scaleFactor
  268. color: theme.borderColor
  269. anchors.margins: 14 * scaleFactor
  270. }
  271. }
  272. RectButton {
  273. id: cancelButton
  274. anchors.right: parent.right
  275. anchors.margins: 10 * scaleFactor
  276.  
  277. text: qsTr("Cancel")
  278. color: theme.buttonColor
  279.  
  280. anchors.verticalCenter: parent.verticalCenter
  281. anchors.verticalCenterOffset: -4 * scaleFactor
  282.  
  283. checkable: false
  284. onClicked: {
  285. deleteBuddyInvitation.path = "/buddy/" + modelData.id + "?token=" + session.token;
  286. deleteBuddyInvitation.request();
  287. }
  288. }
  289. }
  290. }
  291. }
  292. }
  293.  
  294. Item {
  295. height: 10 * scaleFactor
  296. width: 1
  297. }
  298.  
  299. HeaderText {
  300. text: qsTr("Buddies")
  301. visible: controller.buddyList.model.length
  302. }
  303.  
  304. Item {
  305. height: 20 * scaleFactor
  306. width: 1
  307. }
  308.  
  309. Grid {
  310. id: grid
  311. anchors.left: parent.left
  312. anchors.right: parent.right
  313. columns: 2
  314. rowSpacing: 52 * scaleFactor
  315. columnSpacing: 20 * scaleFactor
  316. Repeater {
  317. model: buddiesPage.model
  318. BuddyDelegate {
  319. width: parent ? grid.width / 2 - 10 * scaleFactor : 0
  320. }
  321. }
  322. }
  323. }
  324. }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement