Advertisement
frek

Untitled

Jul 14th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Felgo 3.0
  2. import QtQuick 2.12
  3.  
  4. Item {
  5.     id: dataModel
  6.     property var productItems: ({})
  7.     readonly property string dbKeyAllProductItems: "products-admin"
  8.  
  9.     signal loggedIn
  10.     signal loggedOut
  11.  
  12.     FirebaseConfig {
  13.         id: fbConfig
  14.  
  15.         projectId: "..."
  16.         databaseUrl: "..."
  17.         apiKey: Qt.platform.os === "android"
  18.                 ? "..."
  19.                 : ""
  20.  
  21.         applicationId: Qt.platform.os === "android"
  22.                        ? "..."
  23.                        : ""
  24.     }
  25.  
  26.     FirebaseAuth {
  27.         id: auth
  28.         config: fbConfig
  29.  
  30.         onLoggedIn: if(!success)
  31.                      nativeUtils.displayMessageBox(qsTr("Login failed"), qsTr("Reason: %1").arg(message), 1)
  32.         onUserRegistered: if(!success)
  33.                      nativeUtils.displayMessageBox(qsTr("Register failed"), qsTr("Reason: %1").arg(message), 1)
  34.  
  35.         onAuthenticatedChanged: if(authenticated) dataModel.loggedIn()
  36.     }
  37.  
  38.     function registerAdmin(email, password) {
  39.         auth.registerUser(email, password)
  40.     }
  41.  
  42.     function loginAdmin(email, password) {
  43.         auth.loginUser(email, password)
  44.     }
  45.  
  46.     function customer() {
  47.         dataModel.loggedIn()
  48.     }
  49.  
  50.     function logoutAdmin() {
  51.         auth.logoutUser()
  52.     }
  53.  
  54.     FirebaseDatabase {
  55.         id: database
  56.         config: fbConfig
  57.  
  58.         realtimeUserValueKeys: [dbKeyAllProductItems]
  59.         onRealtimeUserValueChanged: if(key === dbKeyAllProductItems) productItemsLoaded(value)
  60.     }
  61.  
  62.     function productItemsLoaded(value) {
  63.         dataModel.productItems = value || {}
  64.         console.log(JSON.stringify(dataModel.productItems))
  65.     }
  66.    
  67.     function addProductItem(text) {
  68.         var time = new Date().getTime()
  69.         var productItem = {
  70.             date: time,
  71.             text: text
  72.         }
  73.         database.setUserValue(dbKeyAllProductItems + "/" + time, productItem)
  74.     }
  75.  
  76.     function saveProductItem(productItem) {
  77.         database.setUserValue(dbKeyAllProductItems + "/" + productItem.date, productItem)
  78.     }
  79.  
  80.     function deleteProductItem(id) {
  81.         database.setUserValue(dbKeyAllProductItems + "/" + id, null)
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement