Skip to content

Commit

Permalink
added store + cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraasi committed Jun 30, 2018
1 parent 136ffac commit dadf90d
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 32 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
### Sää

Electron app to show city temperature in the notification area.
Right click for more weather info.
Right click icon for more weather info and to change city.

#### todo
[ ] option to change city and store it
[ ] timer
[ ] better 'font'
94 changes: 81 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"electron-compile": "^6.4.3",
"electron-prompt": "^0.5.0",
"electron-squirrel-startup": "^1.0.0",
"electron-store": "^2.0.0",
"merge-img": "^2.1.2",
"node-fetch": "^2.1.2"
},
Expand Down
48 changes: 31 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,48 @@ import path from 'path'
import mergeImg from 'merge-img'
import fetch from 'node-fetch'
import prompt from 'electron-prompt'
import Store from 'electron-store'
import dotenv from 'dotenv'

dotenv.config()

const store = new Store({
name: 'saeae-city',
defaults: {
storePath: app.getPath('userData'),
weatherCity: 'tampere',
},
})

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit()
}

let mainWindow
let tray = null
let city = 'tampere'
let city = store.get('weatherCity')

function parseTime(time) {
return (time < 10) ? `0${time}` : time
}

function promptCity() {
prompt({
allwaysOnTop: true,
alwaysOnTop: true,
title: 'Sää',
label: `Current city: ${city}`,
type: 'input',
inputAttrs: {
type: 'text',
placeholder: 'Choose a city',
placeholder: 'City',
},
})
.then((input) => {
// null if window was closed or user clicked Cancel
if (input === null) return
city = input
store.set('weatherCity', city)
fetchWeather()
})
.catch(console.error);
Expand All @@ -51,24 +61,27 @@ function buildContextMenu(json) {
{ label: `Wind: ${json.wind.speed} m/s @ ${json.wind.deg}°` },
{ label: `Sunrise: ${parseTime(new Date(json.sys.sunrise * 1000).getHours())}:${parseTime(new Date(json.sys.sunrise * 1000).getMinutes())}` },
{ label: `Sunset: ${parseTime(new Date(json.sys.sunset * 1000).getHours())}:${parseTime(new Date(json.sys.sunset * 1000).getMinutes())}` },
{ type: 'separator' },
{
label: 'Data from openweathermap.org',
click() {
shell.openExternal(`https://openweathermap.org/city/${city}`)
},
},
{
label: 'Sää authored by Fraasi',
label: 'Change city',
click() {
shell.openExternal('https://github.com/Fraasi')
promptCity()
},
},
{ type: 'separator' },
{
label: 'Change city',
label: 'Sää authored by Fraasi',
click() {
promptCity()
shell.openExternal('https://github.com/Fraasi')
},
},
{ type: 'separator' },
{
label: 'Quit app',
click() {
Expand All @@ -92,14 +105,16 @@ function updateTrayIcon(numString) {

mergeImg(numberPaths, { margin: '0 5 0 0' })
.then((img) => {
const numericalIcon = path.join(__dirname, 'assets/numerical-icon.png')
// maybe put numicon at app.getPath('userData') ?
const numericalIcon = path.join(app.getPath('userData'), 'numerical-icon.png')
img.write(numericalIcon, () => {
tray.setImage(numericalIcon)
})
})
}

function fetchWeather() {
tray.setImage(path.join(__dirname, './assets/weather-cloudy.png'))
const url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${process.env.OPENWEATHER_APIKEY}&units=metric`
fetch(url)
.then((response) => {
Expand All @@ -110,13 +125,14 @@ function fetchWeather() {
updateTrayIcon(Math.round(json.main.temp).toString())
buildContextMenu(json)
})
.catch((err) => {
.catch(() => {
const contextMenu = Menu.buildFromTemplate([
{ label: 'Bad Weather at the intertubes' },
{ label: 'Something went terribly wrong fetching weather data' },
{ label: err.message },
// { label: err.message }, // needed?
{ label: 'Try restarting the app and/or check your internet connection' },
{ label: `Or maybe you just misspelled your city (${city}) wrong` },
{ label: `Or maybe you just misspelled your city wrong (${city})` },
{ type: 'separator' },
{
label: 'You can file a bug report at github.com/Fraasi/Saeae',
click() {
Expand Down Expand Up @@ -150,8 +166,9 @@ function createTray() {
width: 100,
height: 100,
show: false,
icon: path.join(__dirname, 'assets/numerical-icon.png'), // not working?
})
// mainWindow.loadURL(`file://${__dirname}/index.html`)
// mainWindow.loadURL(`file://${__dirname}/index.html`) // not needed!
// mainWindow.webContents.openDevTools()
mainWindow.on('closed', () => {
mainWindow = null
Expand All @@ -168,8 +185,5 @@ function createTray() {
app.on('ready', createTray)

app.on('activate', () => {
if (mainWindow === null) {
createTray()
}
});

if (mainWindow === null) createTray()
})

0 comments on commit dadf90d

Please sign in to comment.