-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de7d810
commit f6d08ab
Showing
7 changed files
with
117 additions
and
89 deletions.
There are no files selected for viewing
24 changes: 16 additions & 8 deletions
24
core/coroutines-ext/src/main/kotlin/xyz/ksharma/krail/coroutines/ext/CoroutinesExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
core/data/src/main/kotlin/xyz/ksharma/krail/data/FilesHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package xyz.ksharma.krail.data | ||
|
||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.nio.file.StandardCopyOption | ||
import java.util.zip.ZipInputStream | ||
|
||
/** | ||
* Extracts a ZIP entry to a specified cache path. | ||
* | ||
* If the entry is a directory, it creates the directory structure. | ||
* If the entry is a file, it copies the file contents to the cache path. | ||
* | ||
* **Note:** If the target file already exists, it will be overwritten. | ||
* | ||
* @param isDirectory Indicates whether the entry is a directory. | ||
* @param path The target path in the cache directory. | ||
* @param inputStream The input stream containing the ZIP entry data. | ||
*/ | ||
internal fun writeToCacheFromZip( | ||
isDirectory: Boolean, | ||
path: Path, | ||
inputStream: ZipInputStream | ||
) { | ||
if (isDirectory) { | ||
Files.createDirectories(path) | ||
} else { | ||
// Handle creation of parent directories | ||
if (path.parent != null && Files.notExists(path.parent)) { | ||
Files.createDirectories(path.parent) | ||
} | ||
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING) | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
core/data/src/main/kotlin/xyz/ksharma/krail/data/ResponseExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package xyz.ksharma.krail.data | ||
|
||
import android.content.Context | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import okhttp3.Response | ||
import xyz.ksharma.krail.coroutines.ext.safeResult | ||
import xyz.ksharma.krail.network.files.toPath | ||
import java.io.File | ||
import java.io.IOException | ||
import java.nio.file.Path | ||
import java.util.zip.ZipInputStream | ||
|
||
/** | ||
* Caches the content of a successful ZIP response in the cache directory associated with the | ||
* provided context. This function operates on a specified coroutine dispatcher for asynchronous | ||
* execution. | ||
* | ||
* @throws IOException If an I/O error occurs during the caching process, | ||
* or if the response code is unexpected. | ||
* @param dispatcher The coroutine dispatcher to use for suspending operations. | ||
* @param context The context that provides the cache directory path. | ||
*/ | ||
@Throws(IOException::class) | ||
suspend fun Response.cacheZipResponse(dispatcher: CoroutineDispatcher, context: Context) = | ||
safeResult(dispatcher) { | ||
if (!isSuccessful) { | ||
throw IOException("Unexpected code $code") | ||
} | ||
|
||
val responseBody = body!! | ||
|
||
ZipInputStream(responseBody.byteStream()).use { inputStream -> | ||
// List files in zip | ||
var zipEntry = inputStream.nextEntry | ||
|
||
while (zipEntry != null) { | ||
val isDirectory = zipEntry.name.endsWith(File.separator) | ||
val path: Path = context.toPath(zipEntry.name) | ||
|
||
println("zipEntry: $zipEntry") | ||
|
||
writeToCacheFromZip(isDirectory, path, inputStream) | ||
|
||
zipEntry = inputStream.nextEntry | ||
} | ||
inputStream.closeEntry() | ||
} | ||
close() | ||
}.getOrElse { error -> | ||
println("cacheZipResponse: $error") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 1 addition & 64 deletions
65
core/network/src/main/kotlin/xyz/ksharma/krail/network/GtfsServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,25 @@ | ||
package xyz.ksharma.krail.network | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import xyz.ksharma.krail.network.di.NetworkModule.Companion.BASE_URL | ||
import xyz.ksharma.krail.network.files.toPath | ||
import java.io.File | ||
import java.io.IOException | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.nio.file.StandardCopyOption | ||
import java.util.zip.ZipInputStream | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class GtfsServiceImpl @Inject constructor( | ||
private val okHttpClient: OkHttpClient, | ||
@ApplicationContext private val context: Context, | ||
) : GtfsService { | ||
|
||
private val TAG = "SydneyTrainsServiceImpl" | ||
|
||
override suspend fun getSydneyTrainSchedule(): Response { | ||
val request = Request.Builder() | ||
.url("$BASE_URL/v1/gtfs/schedule/sydneytrains") // Replace with your API endpoint | ||
.url("$BASE_URL/v1/gtfs/schedule/sydneytrains") | ||
.build() | ||
|
||
val response = okHttpClient.newCall(request).execute() | ||
// don't log it's entire response body,which is huge. | ||
// Log.d(TAG, "fetchSydneyTrains: ${response.body?.string()}") | ||
|
||
val map = getHTMLZipOk(response) | ||
Log.d(TAG, "filesMap: $map") | ||
|
||
return response | ||
} | ||
|
||
@Throws(IOException::class) | ||
fun getHTMLZipOk(response: Response) { | ||
if (!response.isSuccessful) { | ||
throw IOException("Unexpected code ${response.code}") | ||
} | ||
|
||
val responseBody = response.body!! | ||
|
||
ZipInputStream(responseBody.byteStream()).use { inputStream -> | ||
// List files in zip | ||
var zipEntry = inputStream.nextEntry | ||
|
||
while (zipEntry != null) { | ||
val isDirectory = zipEntry.name.endsWith(File.separator) | ||
val path: Path = context.toPath(zipEntry.name) | ||
|
||
Log.d(TAG, "zipEntry: $zipEntry") | ||
|
||
writeToCacheFromZip(isDirectory, path, inputStream) | ||
|
||
zipEntry = inputStream.nextEntry | ||
} | ||
inputStream.closeEntry() | ||
} | ||
response.close() | ||
} | ||
|
||
/** | ||
* Extract files from zip and save to a file in cache directory. | ||
*/ | ||
private fun writeToCacheFromZip( | ||
isDirectory: Boolean, | ||
path: Path, | ||
inputStream: ZipInputStream | ||
) { | ||
if (isDirectory) { | ||
Files.createDirectories(path) | ||
} else { | ||
// Handle creation of parent directories | ||
if (path.parent != null && Files.notExists(path.parent)) { | ||
Files.createDirectories(path.parent) | ||
} | ||
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters