Compare commits

...
Sign in to create a new pull request.

2 commits
cotlin ... main

Author SHA1 Message Date
51a62c2e6d Changed keyboard back to numerical one 2023-03-18 02:07:36 +01:00
b38cb92788 can now display multiple vehicles
which have the same number
button which requests check
now is disabled when the data
is being fetched
minor text formatting changes
and small UI changes
new icon
2023-03-18 01:49:16 +01:00
4 changed files with 66 additions and 34 deletions

2
.gitignore vendored
View file

@ -1,2 +0,0 @@
fyne_metadata_init.go
ztm_vehicles_app.apk

BIN
icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 766 KiB

BIN
yui.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 942 KiB

View file

@ -3,7 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"strconv" "strings"
"fyne.io/fyne/v2/app" "fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container" "fyne.io/fyne/v2/container"
@ -11,6 +11,10 @@ import (
"github.com/gocolly/colly/v2" "github.com/gocolly/colly/v2"
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
"fyne.io/fyne/v2/layout"
"strconv"
"fyne.io/fyne/v2/driver/mobile" "fyne.io/fyne/v2/driver/mobile"
) )
@ -60,21 +64,39 @@ type vehicle struct {
equipment string equipment string
} }
func getVehicleByNum(vehicleNum string) string { func vehicleToString(inputVehicle *vehicle) string {
registrationPlate := ""
if len(inputVehicle.vehicle_registration_plate) > 0 {
registrationPlate = fmt.Sprintf(`,o rejestracji %s`, inputVehicle.vehicle_registration_plate)
}
output_string := fmt.Sprintf(
"%s o numerze %s,"+"%s %s,"+"z roku %s,"+"w posiadaniu %s,"+"z zajezdni %s%s",
inputVehicle.traction_type,
inputVehicle.vehicle_number,
inputVehicle.producer,
inputVehicle.model,
inputVehicle.production_year,
inputVehicle.operator,
inputVehicle.garage,
registrationPlate)
return strings.Replace(output_string, ",", ",\n", -1)
}
func getVehiclesByNum(vehicleNum string) string {
searchURL := fmt.Sprintf("https://www.ztm.waw.pl/baza-danych-pojazdow/?ztm_traction=&ztm_make=&ztm_model=&ztm_year=&ztm_registration=&ztm_vehicle_number=%s&ztm_carrier=&ztm_depot=", vehicleNum) searchURL := fmt.Sprintf("https://www.ztm.waw.pl/baza-danych-pojazdow/?ztm_traction=&ztm_make=&ztm_model=&ztm_year=&ztm_registration=&ztm_vehicle_number=%s&ztm_carrier=&ztm_depot=", vehicleNum)
vehicleURL := "" var vehicleURLs []string
c2 := colly.NewCollector( c2 := colly.NewCollector(
// Visit only domains: // Visit only domains:
colly.AllowedDomains("www.ztm.waw.pl"), colly.AllowedDomains("www.ztm.waw.pl"),
) )
c2.OnHTML(".grid-row-active", func(e *colly.HTMLElement) { c2.OnHTML(".grid-row-active", func(e *colly.HTMLElement) {
text := e.Attr("href") text := e.Attr("href")
vehicleURL = text vehicleURLs = append(vehicleURLs, text)
}) })
c2.Visit(searchURL) c2.Visit(searchURL)
if vehicleURL == "" { output_string := ""
return "" for _, vehicleURL := range vehicleURLs {
} else {
var retrievedData [10]string var retrievedData [10]string
// Instantiate default collector // Instantiate default collector
c := colly.NewCollector( c := colly.NewCollector(
@ -100,46 +122,58 @@ func getVehicleByNum(vehicleNum string) string {
ticket_machine: retrievedData[8], ticket_machine: retrievedData[8],
equipment: retrievedData[9], equipment: retrievedData[9],
} }
output_string += vehicleToString(&retrievedVehicle)
output_string := fmt.Sprintf( output_string += "\n"
`%s %s output_string += "\n"
z roku %s,
w posiadaniu %s,
z zajezdni %s,
o rejestracji %s`, retrievedVehicle.producer, retrievedVehicle.model, retrievedVehicle.production_year, retrievedVehicle.operator, retrievedVehicle.garage, retrievedVehicle.vehicle_registration_plate)
return output_string
} }
return output_string
} }
func main() { func main() {
a := app.New() a := app.New()
w := a.NewWindow("Hello") w := a.NewWindow("ZTM vehicles")
output := widget.NewLabel("") entryLabel := widget.NewLabel("Podaj numer taborowy pojazdu:")
entry := newNumericalEntry() entry := newNumericalEntry()
form := &widget.Form{ output := widget.NewLabel("")
Items: []*widget.FormItem{ // we can specify items in the constructor output.Wrapping = fyne.TextWrapWord
{Text: "Podaj numer taborowy pojazdu:", Widget: entry}},
} //outputContainer := container.NewVScroll(output)
form.OnSubmit = func() { //I create button earlier in order to reference it later inside function of it so I can easily disable it for the time data is fetching. There is probably better way to do it but I could't find it so
output_data := getVehicleByNum(entry.Text) executeButton := widget.NewButton("Sprawdź numer", func() {
output.Refresh()
})
executeButton = widget.NewButton("Sprawdź numer", func() {
executeButton.Disable()
executeButton.SetText("Wczytywanie")
if len(strings.TrimSpace(entry.Text)) != 0 {
output_data := getVehiclesByNum(entry.Text)
if output_data != "" { if output_data != "" {
output.Text = output_data output.Text = output_data
} else { } else {
output.Text = "Nie znaleziono pojazdu o podanym numerze taborowym w bazie pojazdów WTP" output.Text = "Nie znaleziono pojazdu o podanym numerze taborowym w bazie pojazdów WTP"
} }
output.Refresh() } else {
output.Text = "Nie wprowadzono prawidłowego numeru"
} }
output.Refresh()
executeButton.Enable()
executeButton.SetText("Sprawdź numer")
})
executeButton.Importance = widget.HighImportance
clearButton := widget.NewButton("Wyczyść", func() { clearButton := widget.NewButton("Wyczyść", func() {
entry.Text = "" entry.Text = ""
output.Text = "" output.Text = ""
output.Refresh() output.Refresh()
entry.Refresh() entry.Refresh()
}) })
buttons := container.New(layout.NewGridLayout(2), clearButton, executeButton)
inputs := container.New(layout.NewGridLayout(1), entryLabel, entry, buttons)
w.SetContent(container.NewVBox( w.SetContent(container.NewVBox(
form, inputs,
clearButton,
output, output,
)) ))