Skip to content

Commit

Permalink
new widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaneeyo committed Oct 21, 2023
1 parent 34289cb commit f14cb70
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 6 deletions.
24 changes: 20 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const fs = require('fs')
const path = require('path');
const icon = __dirname + '/favicon.ico'
const { spawn } = require('child_process');
const exeName = path.basename(process.execPath)

// starts the app at login
app.setLoginItemSettings({
openAtLogin: true,
path: path.join(app.getPath('exe').replace('samsung-widgets.exe', 'Samsung Widgets.exe'))
path: exeName
});

// starts the background Serivce which provides information for the music and device care widget
Expand All @@ -36,6 +37,7 @@ let countdownWidget = null;
let quickNotesWidget = null;
let untisWidget = null;
let digitalClockWidget = null;
let forecastWidget = null;

const folderPath = path.join(os.homedir(), 'AppData', 'Local', 'Samsung-Widgets');

Expand All @@ -58,6 +60,7 @@ const positionData = {
quickNotesWidget: { y: "750", x: "475" },
untisWidget: { y: "900", x: "75" },
digitalClockWidget: { y: "75", x: "875" },
forecastWidget: { y: "200", x: "875" },
};

const stateData = {
Expand All @@ -73,6 +76,7 @@ const stateData = {
quickNotesWidget: { show: "true" },
untisWidget: { show: "false" },
digitalClockWidget: { show: "true" },
forecastWidget: { show: "false" },
};

const weatherData = {
Expand All @@ -92,12 +96,23 @@ const colorData = {
};

function createJSONFile(filePath, data) {
if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, JSON.stringify(data, null, 4));
let existingData = {};

if (fs.existsSync(filePath)) {
const fileContent = fs.readFileSync(filePath);
existingData = JSON.parse(fileContent);
}

// Merge existing data with new data, adding missing keys and setting default values
const updatedData = {
...data,
...existingData,
};

fs.writeFileSync(filePath, JSON.stringify(updatedData, null, 4));
}

// creates all jsons needed for the app if they dont exist
// Creates or updates JSON files for the app
createJSONFile(path.join(folderPath, 'widgetPositions.json'), positionData);
createJSONFile(path.join(folderPath, 'widgetStates.json'), stateData);
createJSONFile(path.join(folderPath, 'weatherOptions.json'), weatherData);
Expand All @@ -120,6 +135,7 @@ const widgetsData = {
{ name: "quickNotesWidget", width: 390, height: 175, html: "./src/widgets/notes/quickNotes.html", "clickthrough": false },
{ name: "untisWidget", width: 390, height: 125, html: "./src/widgets/untis.html", "clickthrough": true },
{ name: "digitalClockWidget", width: 390, height: 100, html: "./src/widgets/clock/digitalClock.html", "clickthrough": true },
{ name: "forecastWidget", width: 390, height: 150, html: "./src/widgets/weather/forecast.html", "clickthrough": true },
],
};

Expand Down
48 changes: 48 additions & 0 deletions src/css/widgets/weather/forecast.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
body {
height: 100vh;
top: 0;
margin: 0;
}

#container-main {
height: 100%;
width: 100%;
border-radius: 33px;
display: flex;
padding: 20px 40px 20px 40px;
box-sizing: border-box;
align-items: center;
color: var(--text);
flex-direction: column;
justify-content: center;
gap: 10px;
}

.weather-icon {
width: 40px;
}

.container-day {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}

#container-days {
display: flex;
justify-content: center;
gap: 10px;
width: 100%;
}

#city {
font-size: 20px;
font-weight: 900;
}

#container-info {
display: flex;
flex-direction: column;
align-items: center;
}
2 changes: 1 addition & 1 deletion src/js/music/music.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ window.addEventListener("DOMContentLoaded", () => {

if (jsonData.CoverUrl == "") {
containerMain.style.background = `linear-gradient(135deg, rgb(${colorData.red}, ${colorData.green}, ${colorData.blue}) 0%, rgb(${colorData.red - 35}, ${colorData.green - 35}, ${colorData.blue - 35}) 100%)`;
document.getElementById("music-cover").src = "../res/generic-cover.png";
document.getElementById("music-cover").src = "../../res/generic-cover.png";
} else {
document.getElementById("music-cover").src = jsonData.CoverUrl + "?" + Date.now();
Vibrant.from(jsonData.CoverUrl).getPalette()
Expand Down
87 changes: 87 additions & 0 deletions src/js/weather/forecast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const os = require('os')
const fs = require('fs')
const path = require('path');
const contrast = require('wcag-contrast')

const folderPath = path.join(os.homedir(), 'AppData', 'Local', 'Samsung-Widgets');

const weatherOptions = JSON.parse(fs.readFileSync(folderPath + "\\weatherOptions.json"), 'utf8')
const weatherConditions = require('../../json/weather_conditions.json');

window.addEventListener("DOMContentLoaded", () => {
const containerMain = document.getElementById("container-main");

const secondaryColors = [
[179, 179, 179],
[142, 142, 142]
];

const textColors = [
[0, 0, 0],
[250, 250, 250]
];

// change color based on Setting
const colorData = JSON.parse(fs.readFileSync(path.join(folderPath, 'color.json'), 'utf8'));

function findBetterContrast(rgb1, rgb2) {
const contrast1 = contrast.rgb(rgb1, [colorData.red, colorData.green, colorData.blue])
const contrast2 = contrast.rgb(rgb2, [colorData.red, colorData.green, colorData.blue])
if (contrast1 > contrast2) {
return `rgb(${rgb1[0]}, ${rgb1[1]}, ${rgb1[2]})`;
} else {
return `rgb(${rgb2[0]}, ${rgb2[1]}, ${rgb2[2]})`;
}
}

const textColor = findBetterContrast(textColors[0], textColors[1])
const secondaryColor = findBetterContrast(secondaryColors[0], secondaryColors[1])

containerMain.style.color = textColor;

containerMain.style.background = `linear-gradient(135deg, rgb(${colorData.red}, ${colorData.green}, ${colorData.blue}) 0%, rgb(${colorData.red - 35}, ${colorData.green - 35}, ${colorData.blue - 35}) 100%)`;

document.getElementById('temparature').style.color = secondaryColor;

async function setWeatherInfo() {
const responseIP = await fetch('https://api.ipify.org?format=json');
const dataIP = await responseIP.json();
const publicIP = dataIP.ip;

const responseLocation = await fetch(`http://ip-api.com/json/${publicIP}`);
const dataLocation = await responseLocation.json();
const IPLocation = dataLocation.city;


if (weatherOptions.iplocation == true) {
var weatherURL = `https://api.weatherapi.com/v1/forecast.json?key=0d6f65c595974c82b19143504232708&q=${IPLocation}&days=6&aqi=no&alerts=no`
} else if (weatherOptions.iplocation == false && weatherOptions.country != "" && weatherOptions.name != "") {
const Location = weatherOptions.weather_name + "," + weatherOptions.weather_country
var weatherURL = `https://api.weatherapi.com/v1/forecast.json?key=0d6f65c595974c82b19143504232708&q=${Location}&days=6&aqi=no&alerts=no`
}

const responseForecast = await fetch(weatherURL);
const forecastData = await responseForecast.json();

console.log(forecastData)

document.getElementById('city').innerHTML = forecastData.location.name;
document.getElementById('temparature').innerHTML = forecastData.current.temp_c + "°";

for (i = 0; i < 3; i++) {
const date = new Date(forecastData.forecast.forecastday[i].date_epoch * 1000)
console.log(date.getDay())
const weatherIcons = document.getElementsByClassName('weather-icon')
const days = document.getElementsByClassName('day')

const weatherImageData = weatherConditions.find(codeObj => codeObj.code === forecastData.forecast.forecastday[i].day.condition.code);

weatherIcons[i].src = `../../res/weather/${weatherImageData.day}`

var dayNames = ['Sun', 'Mon', 'Tues', 'Wed', 'Thrus', 'Fri', 'Sat'];

days[i].innerHTML = dayNames[date.getDay()]
}
}
setWeatherInfo()
})
1 change: 0 additions & 1 deletion src/js/weather/weather.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ async function changeWeatherInfo() {
} else {
document.getElementById("weather-icon").src = `../../res/weather/${weatherImageData.night}`
document.getElementById("container-main").style.background = `linear-gradient(180deg, rgba(${dataWeather.current.temp_c * 4}, 70, 120, 1) 0%, rgba(${dataWeather.current.temp_c * 3},55,105, 1) 100%)`

}
}

Expand Down
41 changes: 41 additions & 0 deletions src/widgets/weather/forecast.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../../css/widgets/weather/forecast.css">
<link rel="stylesheet" href="../../css/fonts.css">
<link rel="stylesheet" href="../../css/colors.css">
<script src="../../js/weather/forecast.js"></script>
</head>

<body>
<container id="container-main">
<container id="container-info">
<span id="city">London</span>
<span id="temparature">17°</span>
</container>


<container id="container-days">
<container class="container-day">
<img src="../../res/weather/patchy_rain_day.webp" class="weather-icon">
<span class="day">Mon</span>
</container>

<container class="container-day">
<img src="../../res/weather/sunny_day.webp" class="weather-icon">
<span class="day">Tue</span>
</container>

<container class="container-day">
<img src="../../res/weather/cloudy.webp" class="weather-icon">
<span class="day">Wed</span>
</container>
</container>

</container>
</body>

</html>

0 comments on commit f14cb70

Please sign in to comment.