add auto change wallpaper

This commit is contained in:
Данил 2022-09-19 16:55:29 +03:00
parent 41507c8fc5
commit b9d775eec9
4 changed files with 111 additions and 10 deletions

View file

@ -2,6 +2,7 @@ package controllers
import (
"database/sql"
"github.com/Redume/EveryNasa/functions"
"net/http"
"github.com/Redume/EveryNasa/api/utils"
@ -27,14 +28,14 @@ var SettingsGet = func(w http.ResponseWriter, r *http.Request) {
}(query)
var lang string
var autostart, autoupdate int
var autostart, autoupdate, autochangewallpaper int
for query.Next() {
err := query.Scan(&lang, &autostart, &autoupdate)
err := query.Scan(&lang, &autostart, &autoupdate, &autochangewallpaper)
if err != nil {
panic(err)
}
var data = map[string]interface{}{"lang": lang, "autostart": autostart, "autoupdate": autoupdate}
var data = map[string]interface{}{"lang": lang, "autostart": autostart, "autoupdate": autoupdate, "autochangewallpaper": autochangewallpaper}
utils.Respond(w, data)
}
}
@ -48,8 +49,9 @@ var SettingsUpdate = func(w http.ResponseWriter, r *http.Request) {
lang := r.FormValue("lang")
autostart := r.FormValue("autostart")
autoupdate := r.FormValue("autoupdate")
autochangewallpaper := r.FormValue("autochangewallpaper")
if lang == "" && autostart == "" && autoupdate == "" {
if lang == "" && autostart == "" && autoupdate == "" && autochangewallpaper == "" {
utils.Respond(w, utils.Message(false, "All fields are required"))
return
}
@ -75,5 +77,16 @@ var SettingsUpdate = func(w http.ResponseWriter, r *http.Request) {
}
}
if autochangewallpaper != "" {
_, err := db.Exec("UPDATE settings SET autochangewallpaper = ?", autochangewallpaper)
if err != nil {
panic(err)
}
if autochangewallpaper == "1" {
go functions.StartWallpaper()
}
}
utils.Respond(w, utils.Message(true, "Settings updated"))
}

View file

@ -2,6 +2,7 @@ package functions
import (
"encoding/json"
"fmt"
"github.com/jasonlvhit/gocron"
"github.com/reujab/wallpaper"
"github.com/rodkranz/fetch"
@ -61,12 +62,43 @@ func SetWallpaper() {
}
}
func x() {
fmt.Println("F")
}
func StartWallpaper() {
SetWallpaper()
times := time.Now()
t := time.Date(times.Year(), times.Month(), times.Day(), 4, 50, times.Second(), times.Nanosecond(), time.UTC)
err := gocron.Every(1).Day().From(&t).Do(SetWallpaper)
if err != nil {
panic(err)
type Autostart struct {
Autochangewallpaper int `json:"autochangewallpaper"`
}
client := fetch.NewDefault()
res, getErr := client.Get("http://localhost:8080/api/get/settings", nil)
if getErr != nil {
panic(getErr)
}
body, StringErr := res.ToString()
if StringErr != nil {
panic(StringErr)
}
var AutostartSetWallpaper Autostart
jsonErr := json.Unmarshal([]byte(body), &AutostartSetWallpaper)
if jsonErr != nil {
panic(jsonErr)
}
if AutostartSetWallpaper.Autochangewallpaper == 1 {
times := time.Now()
t := time.Date(times.Year(), times.Month(), times.Day(), 4, 50, times.Second(), times.Nanosecond(), time.UTC)
SetWallpaper()
err := gocron.Every(1).Day().From(&t).Do(SetWallpaper)
if err != nil {
panic(err)
}
gocron.Start()
}
}

View file

@ -34,6 +34,14 @@
<h1 id="title">Application Settings</h1>
<div class="container">
<header class="header-row">
<div class="settings">
<p class="title"><strong>Auto-change of desktop wallpaper</strong></p>
<p class="desc">EveryNasa will automatically change the desktop wallpaper at startup as well as every day</p>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="autosetWallpaperSwitch">
<label class="form-check-label" for="autorunText" id="autosetWallpaperText">Off</label>
</div>
</div>
<div class="settings">
<p class="title"><strong>Automatic updates</strong></p>
<p class="desc">EveryNasa will automatically perform an update at startup.</p>
@ -52,6 +60,16 @@
</div>
</header>
</div>
<div class="toast-container position-fixed bottom-0 end-0 p-3">
<div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="/static/image/icons/favicon.png" class="rounded me-2" alt="EveryNasa" width="30">
<strong class="me-auto" style="color: black;">EveryNasa</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body" style="color: black;"></div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.min.js" integrity="sha384-ODmDIVzN+pFdexxHEHFBQH3/9/vQ9uori45z4JjnFsRydbmQbmL5t1tQ0culUzyK" crossorigin="anonymous"></script>
<script src="/static/scripts/switch.js"></script>

View file

@ -10,6 +10,10 @@ $.ajax({
$("#autorunSwitch").attr("checked", "true");
$("#autorunText").text("On");
}
if(data["autochangewallpaper"] === 1) {
$("#autosetWallpaperSwitch").attr("checked", "true");
$("#autosetWallpaperText").text("On");
}
}
})
@ -82,3 +86,37 @@ $("#autorunSwitch").click(function(){
}
});
})
$("#autosetWallpaperSwitch").click(function(){
$.ajax({
url: "http://localhost:8080/api/get/settings",
type: "GET",
success: function(data){
if(data["autochangewallpaper"] === 1){
$.ajax({
url: "http://localhost:8080/api/update/settings",
type: "POST",
data: {
autochangewallpaper: 0
},
success: function(){
$("#autosetWallpaperSwitch").removeAttr("checked");
$("#autosetWallpaperText").text("Off");
}
})
} else {
$.ajax({
url: "http://localhost:8080/api/update/settings",
type: "POST",
data: {
autochangewallpaper: 1
},
success: function(){
$("#autosetWallpaperSwitch").attr("checked", "true");
$("#autosetWallpaperText").text("On");
}
})
}
}
});
})