Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
fix x3
Browse files Browse the repository at this point in the history
  • Loading branch information
FoPPi committed Dec 10, 2022
1 parent 1aa7413 commit cb31f4f
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 62 deletions.
10 changes: 5 additions & 5 deletions WorkingWithAPI/Faculty.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

func TakeFacultyID(value string) {
json, _ := TakeFaculty()
faculty, _ := TakeFaculty()

FacultyName = value
for _, rec := range json {
for _, rec := range faculty {
if value == rec.Name {
FacultyID = rec.Id
}
Expand All @@ -21,11 +21,11 @@ func TakeFacultyID(value string) {
}

func FacultyJSONtoString() []string {
json, _ := TakeFaculty()
faculty, _ := TakeFaculty()

sArr := make([]string, len(json))
sArr := make([]string, len(faculty))
counter := 0
for _, rec := range json {
for _, rec := range faculty {
sArr[counter] = rec.Name
counter++
}
Expand Down
23 changes: 11 additions & 12 deletions WorkingWithAPI/UserCfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@ func ReadUserConf() {
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
return
}

var UserCFG UserJSON

byteValue, _ := ioutil.ReadAll(jsonFile)

json.Unmarshal(byteValue, &UserCFG)
err = json.Unmarshal(byteValue, &UserCFG)
if err != nil {
fmt.Println(err)
return
}

jsonFile.Close()
err = jsonFile.Close()
if err != nil {
fmt.Println(err)
return
}

FacultyName = UserCFG.Faculty.FacultyName
FacultyID = UserCFG.Faculty.FacultyID
Expand Down Expand Up @@ -59,16 +68,6 @@ func WriteUserConf() {
return
}

//_, erro := os.Stat("files")
//
//if os.IsNotExist(err) {
// errDir := os.Mkdir("./files", os.FileMode(0522))
// if errDir != nil {
// log.Fatal(erro)
// }
//
//}

var nestedDir = "files"
path := filepath.Join(".", nestedDir)
erro := os.MkdirAll(path, 0777)
Expand Down
2 changes: 2 additions & 0 deletions WorkingWithAPI/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ var (
GroupID int = 0

LastUpdate string = ""

Version string = "v1.0.0"
)
53 changes: 39 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import (
"time"
)

var (
InternetExist bool = true
FacultySelector = widget.NewSelect([]string{}, func(value string) {})
CourseSelector = widget.NewSelect([]string{}, func(value string) {})
GroupSelector = widget.NewSelect([]string{}, func(value string) {})
)

// UpdateTime Show time to pare
func UpdateTime(ParaNameLabel *widget.Label, TimerLabel *widget.Label, app fyne.App) {
paraExist, paraName, diff := TakeTime(app)
Expand All @@ -26,10 +33,17 @@ func UpdateTime(ParaNameLabel *widget.Label, TimerLabel *widget.Label, app fyne.
func CheckConn(OnlineLabel *widget.Label, LastUpdateLabel *widget.Label, w fyne.Window, SendError bool) {
updated, err := UpdateOfflineJSON()
if updated {
time := time.Now().String()
api.LastUpdate = time[0:16]
t := time.Now().String()
api.LastUpdate = t[0:16]
LastUpdateLabel.SetText("Updated: " + api.LastUpdate)
OnlineLabel.SetText("Online")

if !InternetExist {
FacultySelector.Options = api.FacultyJSONtoString()
CourseSelector.Options = api.CourseJSONtoString(api.FacultyName)
GroupSelector.Options = api.GroupJSONtoString(api.CourseName)
InternetExist = true
}
} else {
if SendError {
dialog.ShowError(err, w)
Expand All @@ -40,7 +54,7 @@ func CheckConn(OnlineLabel *widget.Label, LastUpdateLabel *widget.Label, w fyne.
}

func main() {
a := app.New()
a := app.NewWithID("DUTclock")
w := a.NewWindow("DUTclock")

// set icon
Expand All @@ -58,8 +72,13 @@ func main() {
ParaNameLabel := widget.NewLabel("")
TimerLabel := widget.NewLabel("")

// check internet connection
status, _ := Ping("google.com")
if status != 200 {
InternetExist = false
}

//update kit
FacultySelector := widget.NewSelect([]string{}, func(value string) {})
OnlineLabel := widget.NewLabel("")
LastUpdateLabel := widget.NewLabel("")
UpdateButton := widget.NewButton("Update", func() {
Expand All @@ -86,14 +105,14 @@ func main() {

// start checking connection every hour
go func() {
for range time.Tick(time.Hour) {
for range time.Tick(time.Hour / 2) {
CheckConn(OnlineLabel, LastUpdateLabel, w, false)
}
}()

// add selectors
GroupLabel := widget.NewLabel("Group")
GroupSelector := widget.NewSelect([]string{}, func(value string) {
GroupSelector = widget.NewSelect([]string{}, func(value string) {
if value != api.GroupName {
api.TakeGroupID(value)

Expand All @@ -105,15 +124,13 @@ func main() {
})

CourseLabel := widget.NewLabel("Course")
CourseSelector := widget.NewSelect([]string{}, func(value string) {
if value != api.CourseName {
GroupSelector.Selected = ""
GroupSelector.Options = api.GroupJSONtoString(value)
}
CourseSelector = widget.NewSelect([]string{}, func(value string) {
GroupSelector.Selected = ""
GroupSelector.Options = api.GroupJSONtoString(value)
})

FacultyLabel := widget.NewLabel("Faculty")
FacultySelector = widget.NewSelect(api.FacultyJSONtoString(), func(value string) {
FacultySelector = widget.NewSelect([]string{}, func(value string) {
if value != api.FacultyName {
GroupSelector.Options = []string{}
CourseSelector.Selected = ""
Expand All @@ -122,12 +139,19 @@ func main() {
}
})

if InternetExist {
FacultySelector.Options = api.FacultyJSONtoString()
}

// add selectors names if is not first start
if api.GroupID != 0 {
GroupSelector.Selected = api.GroupName
CourseSelector.Selected = api.CourseName
FacultySelector.Selected = api.FacultyName
CourseSelector.Options = api.CourseJSONtoString(api.FacultyName)
GroupSelector.Options = api.GroupJSONtoString(api.CourseName)
if InternetExist {
CourseSelector.Options = api.CourseJSONtoString(api.FacultyName)
GroupSelector.Options = api.GroupJSONtoString(api.CourseName)
}
}

// add content
Expand Down Expand Up @@ -162,4 +186,5 @@ func main() {

// show window
w.ShowAndRun()

}
89 changes: 58 additions & 31 deletions TakeTime.go → mind.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,15 @@ func ReadOfflineJSON(jsonName string) []WeekJSON {

byteValue, _ := ioutil.ReadAll(jsonFile)

json.Unmarshal(byteValue, &Week)
err = json.Unmarshal(byteValue, &Week)
if err != nil {
fmt.Println(err)
}

jsonFile.Close()
err = jsonFile.Close()
if err != nil {
fmt.Println(err)
}

return Week
}
Expand Down Expand Up @@ -161,6 +167,56 @@ func UpdateOfflineJSON() (Updated bool, error error) {
return true, nil
}

// TakeWeek читает WeekJSON из url
func TakeWeek(Faculty, Course, Group int, Week string) ([]WeekJSON, error) {

// 1/1/1576/NEXT
url := "https://dutcalendar-tracker.lwjerri.ml/v1/calendar/" + strconv.Itoa(Faculty) + "/" + strconv.Itoa(Course) + "/" + strconv.Itoa(Group) + "/" + Week

// Get request
resp, err := http.Get(url)
if err != nil {
fmt.Println("No response from request")
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Println("Error", err)
}
}(resp.Body)
body, err := ioutil.ReadAll(resp.Body) // response body is []byte

var result []WeekJSON
if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to the go struct pointer
fmt.Println("Can not unmarshal JSON")
return nil, err
}

return result, err
}

// Ping site and return status code
func Ping(domain string) (int, error) {
var client = http.Client{}

url := "http://" + domain
req, err := http.NewRequest("HEAD", url, nil)
if err != nil {
return 0, err
}
resp, err := client.Do(req)
if err != nil {
return 0, err
}

err = resp.Body.Close()
if err != nil {
return 0, err
}
return resp.StatusCode, nil
}

// isWeekend проверка на выходной
func isWeekend(t time.Time) bool {
t = t.UTC()
Expand Down Expand Up @@ -191,35 +247,6 @@ func PrettyPrint(text any) string {
return res
}

// TakeWeek читает WeekJSON из url
func TakeWeek(Faculty, Course, Group int, Week string) ([]WeekJSON, error) {

// 1/1/1576/NEXT
url := "https://dutcalendar-tracker.lwjerri.ml/v1/calendar/" + strconv.Itoa(Faculty) + "/" + strconv.Itoa(Course) + "/" + strconv.Itoa(Group) + "/" + Week

// Get request
resp, err := http.Get(url)
if err != nil {
fmt.Println("No response from request")
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Println("Error", err)
}
}(resp.Body)
body, err := ioutil.ReadAll(resp.Body) // response body is []byte

var result []WeekJSON
if err := json.Unmarshal(body, &result); err != nil { // Parse []byte to the go struct pointer
fmt.Println("Can not unmarshal JSON")
return nil, err
}

return result, err
}

// WeekJSON структура json-а
type WeekJSON struct {
AddedAt *string `json:"addedAt"`
Expand Down

0 comments on commit cb31f4f

Please sign in to comment.