diff --git a/api/controllers/settings.go b/api/controllers/settings.go index c245084..3afdca6 100644 --- a/api/controllers/settings.go +++ b/api/controllers/settings.go @@ -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")) } diff --git a/functions/wallpaper.go b/functions/wallpaper.go index 630a303..11cd36a 100644 --- a/functions/wallpaper.go +++ b/functions/wallpaper.go @@ -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() } } diff --git a/web/src/settings.html b/web/src/settings.html index 3e75c3d..5149634 100644 --- a/web/src/settings.html +++ b/web/src/settings.html @@ -34,6 +34,14 @@
Auto-change of desktop wallpaper
+EveryNasa will automatically change the desktop wallpaper at startup as well as every day
+Automatic updates
EveryNasa will automatically perform an update at startup.
@@ -52,6 +60,16 @@