Bimba.git

ref: 717e18b931e82fe600f5f8fb98869623a34d4790

app/src/main/java/xyz/apiote/bimba/czwek/repo/OfflineRepository.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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.repo

import android.content.Context
import android.net.ConnectivityManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import xyz.apiote.bimba.czwek.api.Server
import xyz.apiote.bimba.czwek.api.responses.FeedsResponse
import xyz.apiote.bimba.czwek.api.responses.FeedsResponseDev
import xyz.apiote.bimba.czwek.api.responses.FeedsResponseV1
import xyz.apiote.bimba.czwek.api.responses.FeedsResponseV2
import java.io.File
import java.io.FileInputStream
import java.net.URLEncoder

class OfflineRepository : Repository {
	override suspend fun getFeeds(
		server: Server,
		context: Context
	): Map<String, FeedInfo>? {
		val file = File(
			context.cacheDir, URLEncoder.encode(server.apiPath, "utf-8")
		)
		if (!file.exists()) {
			return emptyMap()
		}
		return when (val response =
			withContext(Dispatchers.IO) { FeedsResponse.unmarshal(FileInputStream(file)) }) {
			is FeedsResponseDev -> response.feeds.associate {
				Pair(
					it.id,
					FeedInfo(it).copy(cached = true)
				)
			}

			is FeedsResponseV2 -> response.feeds.associate {
				Pair(
					it.id,
					FeedInfo(it).copy(cached = true)
				)
			}

			is FeedsResponseV1 -> response.feeds.associate {
				Pair(
					it.id,
					FeedInfo(it).copy(cached = true)
				)
			}

			else -> null
		}
	}

	override suspend fun getDepartures(
		cm: ConnectivityManager,
		feedID: String,
		stop: String,
		line: String?,
		context: Context,
		limit: Int?
	): StopDepartures? {
		TODO("Not yet implemented")
	}

	override suspend fun getLocatablesIn(
		cm: ConnectivityManager,
		bl: Position,
		tr: Position,
		context: Context
	): List<Locatable>? {
		TODO("Not yet implemented")
	}

	override suspend fun getLine(
		cm: ConnectivityManager,
		feedID: String,
		line: String,
		context: Context
	): Line? {
		TODO("Not yet implemented")
	}

	override suspend fun queryQueryables(
		cm: ConnectivityManager,
		query: String,
		context: Context
	): List<Queryable>? {
		TODO("Not yet implemented")
	}

	override suspend fun locateQueryables(
		cm: ConnectivityManager,
		position: Position,
		context: Context
	): List<Queryable>? {
		TODO("Not yet implemented")
	}

}