Bimba.git

ref: 67f5b3aad52d3fcabf3f0abe99afeaf8a2ed7203

app/src/main/java/xyz/apiote/bimba/czwek/api/structs/FeedInfo.kt


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.api.structs

import xyz.apiote.fruchtfleisch.Reader
import java.io.InputStream
import java.time.LocalDate
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.Locale

data class FeedInfoV2(
	val name: String,
	val id: String,
	val attribution: String,
	val description: String,
	val lastUpdate: LocalDate,
	val qrHost: String,
	val qrIn: QrLocationV1,
	val qrSelector: String,
	val validSince: LocalDate,
	val validTill: LocalDate
) {
	companion object {
		fun unmarshal(stream: InputStream): FeedInfoV2 {
			val reader = Reader(stream)
			return FeedInfoV2(
				reader.readString(),
				reader.readString(),
				reader.readString(),
				reader.readString(),
				LocalDate.parse(reader.readString(), DateTimeFormatter.ISO_LOCAL_DATE),
				reader.readString(),
				QrLocationV1.of(reader.readUInt().toULong().toUInt()),
				reader.readString(),
				LocalDate.parse(reader.readString(), DateTimeFormatter.BASIC_ISO_DATE),
				LocalDate.parse(reader.readString(), DateTimeFormatter.BASIC_ISO_DATE)
			)
		}
	}

	fun formatDate(): String {
		return lastUpdate.format(
			DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(Locale.getDefault())
		)
	}
}

data class FeedInfoV1(
	val name: String,
	val id: String,
	val attribution: String,
	val description: String,
	val lastUpdate: ZonedDateTime
) {
	companion object {
		fun unmarshal(stream: InputStream): FeedInfoV1 {
			val reader = Reader(stream)
			return FeedInfoV1(
				reader.readString(),
				reader.readString(),
				reader.readString(),
				reader.readString(),
				ZonedDateTime.parse(reader.readString(), DateTimeFormatter.ISO_DATE_TIME)
			)
		}
	}

	fun formatDate(): String {
		return lastUpdate.format(
			DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(Locale.getDefault())
		)
	}
}