Advertisement
multifacs

WeatherApp

Mar 25th, 2024 (edited)
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 11.31 KB | Source Code | 0 0
  1. import QtQuick 2.0
  2. import Sailfish.Silica 1.0
  3. import QtQuick.LocalStorage 2.0
  4.  
  5. Page {
  6.  
  7.     id: page
  8.  
  9.     property double lat: 52.52
  10.     property double lon: 13.41
  11.     property string city: "Берлин"
  12.     property var weather
  13.     property var records: []
  14.  
  15.     property var db: LocalStorage.openDatabaseSync("QDeclarativeExampleDB", "1.0", "The Example QML SQL!", 1000000)
  16.  
  17.     Column {
  18.         spacing: 100
  19.         anchors.verticalCenter: parent.verticalCenter
  20.  
  21.         Column { // Колонка - контейнер для расположения элементов в столбец
  22.             width: 720
  23.             height: 300
  24.             spacing: 20
  25.  
  26.             Label {
  27.                 id: cityLabel
  28.                 text: city
  29.                 font.family: "Helvetica"; font.pixelSize: 40; font.bold: true
  30.                 anchors.horizontalCenter: parent.horizontalCenter
  31.             }
  32.             Row {
  33.                 Image {
  34.                     id: weatherIcon
  35.  
  36.                     anchors.verticalCenter: parent.verticalCenter
  37.                     source: "https://openweathermap.org/img/wn/01d@2x.png"
  38.                     width: 300
  39.                     height: 300
  40.                 }
  41.  
  42.                 Column {
  43.                     width: 420
  44.                     spacing: 10
  45.                     anchors.verticalCenter: parent.verticalCenter
  46.  
  47.                     Label {
  48.                         id: dateLabel
  49.                         font.family: "Helvetica"; font.pixelSize: 30
  50.                         anchors.horizontalCenter: parent.horizontalCenter
  51.                     }
  52.                     Row {
  53.                         anchors.horizontalCenter: parent.horizontalCenter
  54.                         spacing: 10
  55.                         Label {
  56.                             id: timeLabel
  57.                             font.family: "Helvetica"; font.pixelSize: 70
  58.                         }
  59.                         Label {
  60.                             text: "GMT"
  61.                             anchors.bottom: timeLabel.bottom
  62.                             font.family: "Helvetica"; font.pixelSize: 20
  63.                         }
  64.                     }
  65.  
  66.                     Rectangle {
  67.                         height: 5
  68.                         width: 300
  69.                         color: "black"
  70.                         anchors.horizontalCenter: parent.horizontalCenter
  71.                     }
  72.                     Row {
  73.                         anchors.horizontalCenter: parent.horizontalCenter
  74.                         spacing: 20
  75.  
  76.                         Label {
  77.                             id: temperatureLabel
  78.                             font.family: "Helvetica"; font.pixelSize: 70
  79.                         }
  80.  
  81.                         Column {
  82.                             Label {
  83.                                 id: windSpeedLabel
  84.                                 anchors.horizontalCenter: parent.horizontalCenter
  85.                             }
  86.                             Image {
  87.                                 anchors.horizontalCenter: parent.horizontalCenter
  88.                                 id: windDirImage
  89.                                 source: "https://www.svgrepo.com/show/533632/arrow-up.svg"
  90.                                 width: 50
  91.                                 height: width
  92.                             }
  93.                         }
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.  
  99.         Column {
  100.             anchors.horizontalCenter: parent.horizontalCenter
  101.             width: 600
  102.             spacing: 10
  103.             Row {
  104.                 anchors.horizontalCenter: parent.horizontalCenter
  105.                 spacing: 10
  106.                 Button {
  107.                     text: "Берлин"
  108.                     width: 300
  109.                     onClicked: {
  110.                         lat = 52.52
  111.                         lon = 13.41
  112.                         city = "Берлин"
  113.                         loadWeather()
  114.                     }
  115.                 }
  116.                 Button {
  117.                     text: "Сидней"
  118.                     width: 300
  119.                     onClicked: {
  120.                         lat = -33.8678
  121.                         lon = 151.2073
  122.                         city = "Сидней"
  123.                         loadWeather()
  124.                     }
  125.                 }
  126.             }
  127.  
  128.             Row {
  129.                 anchors.horizontalCenter: parent.horizontalCenter
  130.                 spacing: 10
  131.                 Button {
  132.                     text: "Вашингтон"
  133.                     width: 300
  134.                     onClicked: {
  135.                         lat = 38.8951
  136.                         lon = -77.0364
  137.                         city = "Вашингтон"
  138.                         loadWeather()
  139.                     }
  140.                 }
  141.                 Button {
  142.                     text: "Буэнос-Айрес"
  143.                     width: 300
  144.                     onClicked: {
  145.                         lat = -34.6131
  146.                         lon = -58.3772
  147.                         city = "Буэнос-Айрес"
  148.                         loadWeather()
  149.                     }
  150.                 }
  151.             }
  152.         }
  153.  
  154.         SilicaListView {
  155.             width: 720
  156.             height: 300
  157.             model: records
  158.             delegate: Label {
  159.                 width: parent.width
  160.                 height: 100
  161.                 Button {
  162.                     text: JSON.parse(modelData.weather).city + " " + JSON.parse(modelData.weather).current_weather.time
  163.                     anchors.horizontalCenter: parent.horizontalCenter
  164.                     onClicked: {
  165.                         console.log(modelData.weather)
  166.                         weather = JSON.parse(modelData.weather)
  167.                         city = weather.city
  168.  
  169.                         var current_weather_units = weather.current_weather_units
  170.                         var current_weather = weather.current_weather
  171.                         console.log(current_weather.time)
  172.                         var date = new Date(current_weather.time);
  173.  
  174.                         weatherIcon.source = getWeatherIcon(current_weather.weathercode, current_weather.is_day);
  175.                         dateLabel.text = date.toDateString()
  176.                         timeLabel.text = date.toTimeString().slice(0,5)
  177.                         temperatureLabel.text = current_weather.temperature + " " + current_weather_units.temperature
  178.                         windSpeedLabel.text = current_weather.windspeed + " " + current_weather_units.windspeed
  179.                         windDirImage.rotation = current_weather.winddirection
  180.                     }
  181.                 }
  182.             }
  183.             spacing: 5
  184.         }
  185.  
  186.         Button {
  187.             text: "Очистить"
  188.             onClicked: {
  189.                 clearDB()
  190.             }
  191.         }
  192.     }
  193.  
  194.     Component.onCompleted: { // такая штука чтобы весь код ниже запустился с загрузкой проги
  195.         loadWeather()
  196.         createDB()
  197.     }
  198.  
  199.     function loadWeather() {
  200.         var xhr = new XMLHttpRequest(); // всё из презентации
  201.  
  202.         xhr.open('GET', 'https://api.open-meteo.com/v1/forecast?latitude=' + lat + '&longitude=' + lon + '&current_weather=true', true); // адрес по которому получаем погоду
  203.  
  204.         xhr.onreadystatechange = function() {
  205.             if (xhr.readyState === XMLHttpRequest.DONE) {
  206.  
  207.                 weather = JSON.parse(xhr.responseText) // текст по ссылке превращаем в javascript объект
  208.                 var current_weather_units = weather.current_weather_units
  209.                 var current_weather = weather.current_weather
  210.                 console.log(current_weather.time)
  211.                 var date = new Date(current_weather.time);
  212.  
  213.                 weatherIcon.source = getWeatherIcon(current_weather.weathercode, current_weather.is_day);
  214.                 dateLabel.text = date.toDateString()
  215.                 timeLabel.text = date.toTimeString().slice(0,5)
  216.                 temperatureLabel.text = current_weather.temperature + " " + current_weather_units.temperature
  217.                 windSpeedLabel.text = current_weather.windspeed + " " + current_weather_units.windspeed
  218.                 windDirImage.rotation = current_weather.winddirection
  219.  
  220.                 var weatherCopy = JSON.parse(JSON.stringify(weather))
  221.                 weatherCopy.city = city
  222.  
  223.                 addRecord(JSON.stringify(weatherCopy))
  224.  
  225.             }
  226.         }
  227.         xhr.send();
  228.     }
  229.  
  230.     function getWeatherIcon(code, isDay) {
  231.         var iconUrl = "https://openweathermap.org/img/wn/"
  232.  
  233.         var codesDict = {
  234.             0: "01",
  235.             1: "01",
  236.             2: "02",
  237.             3: "03",
  238.             45: "50", 48: "50",
  239.             51: "10", 53: "10", 55: "10", 56: "10", 57: "10",
  240.             61: "10", 63: "10", 65: "10", 66: "10", 67: "10",
  241.             71: "13", 73: "13", 75: "13", 77: "13", 85: "13", 86: "13",
  242.             80: "09", 81: "09", 82: "09",
  243.             95: "11", 96: "11", 99: "11"
  244.         }
  245.  
  246.         if (codesDict[code] !== null) {
  247.             iconUrl += codesDict[code];
  248.         } else {
  249.             iconUrl += "11";
  250.         }
  251.  
  252.         if (isDay === 1) {
  253.             iconUrl += "d";
  254.         } else {
  255.             iconUrl += "n";
  256.         }
  257.  
  258.         iconUrl += "@2x.png";
  259.  
  260.         return iconUrl;
  261.     }
  262.  
  263.     function createDB() {
  264.         db.transaction(
  265.                     function(tx) {
  266.                         // Create the database if it doesn't already exist
  267.                         tx.executeSql('CREATE TABLE IF NOT EXISTS records(weather TEXT)');
  268.  
  269.                         // Show all added greetings
  270.                         var rs = tx.executeSql('SELECT * FROM records');
  271.  
  272.                         var r = []
  273.                         for (var i = 0; i < rs.rows.length; i++) {
  274.                             r.push(rs.rows.item(i))
  275.                         }
  276.                         console.log(r)
  277.                         records = r
  278.                     }
  279.                     )
  280.     }
  281.  
  282.     function addRecord(text) {
  283.         db.transaction(function(tx) {
  284.             tx.executeSql("INSERT INTO records (weather) VALUES(?);", [text]);
  285.  
  286.             // Show all added greetings
  287.             var rs = tx.executeSql('SELECT * FROM records');
  288.  
  289.             var r = []
  290.             for (var i = 0; i < rs.rows.length; i++) {
  291.                 r.push(rs.rows.item(i))
  292.             }
  293.             console.log(r)
  294.             records = r
  295.         });
  296.     }
  297.  
  298.     function clearDB() {
  299.         db.transaction(
  300.                     function(tx) {
  301.                         // Create the database if it doesn't already exist
  302.                         tx.executeSql('DROP TABLE IF EXISTS records');
  303.                         tx.executeSql('CREATE TABLE IF NOT EXISTS records(weather TEXT)');
  304.  
  305.                         // Show all added greetings
  306.                         var rs = tx.executeSql('SELECT * FROM records');
  307.  
  308.                         var r = []
  309.                         for (var i = 0; i < rs.rows.length; i++) {
  310.                             r.push(rs.rows.item(i))
  311.                         }
  312.                         console.log(r)
  313.                         records = r
  314.                     }
  315.                     )
  316.     }
  317. }
  318.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement