From 56ff42fac5e7ead7f13373b65732db9a1090d6e8 Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Thu, 13 Jun 2024 12:49:42 -0400 Subject: [PATCH] Rename and move file/http related classes --- core/src/main/AndroidManifest.xml | 4 ++-- .../dev/hotwire/core/config/HotwireConfig.kt | 8 +++---- .../delegates/BrowseFilesDelegate.kt} | 10 ++++----- .../delegates/CameraCaptureDelegate.kt} | 12 +++++----- .../delegates/FileChooserDelegate.kt} | 16 +++++++------- .../hotwire/core/files/util/FileConstants.kt | 3 +++ .../util/HotwireFileProvider.kt} | 14 +++++++----- .../util/UriAttributes.kt} | 4 ++-- .../util/UriHelper.kt} | 22 ++++++++++--------- .../config/PathConfigurationRepository.kt | 4 ++-- ...urboHttpClient.kt => HotwireHttpClient.kt} | 4 ++-- .../OfflineCacheStrategy.kt} | 4 ++-- .../OfflineHttpRepository.kt} | 17 +++++++------- .../OfflinePreCacheRequest.kt} | 4 ++-- .../OfflineRequestHandler.kt} | 6 ++--- .../OfflineWebViewRequestInterceptor.kt} | 6 ++--- .../dev/hotwire/core/turbo/session/Session.kt | 17 +++++++------- .../hotwire/core/turbo/util/CoreConstants.kt | 3 --- ...tcherProvider.kt => DispatcherProvider.kt} | 4 ++-- .../delegates/BrowseFilesDelegateTest.kt} | 8 +++---- .../delegates/CameraCaptureDelegateTest.kt} | 10 ++++----- .../delegates/FileChooserDelegateTest.kt} | 8 +++---- .../util/HotwireFileProviderTest.kt} | 10 ++++----- .../util/UriHelperTest.kt} | 15 +++++++------ .../config/PathConfigurationRepositoryTest.kt | 4 ++-- .../HotwireWebBottomSheetFragment.kt | 4 ++-- .../fragments/HotwireWebFragment.kt | 4 ++-- 27 files changed, 116 insertions(+), 109 deletions(-) rename core/src/main/kotlin/dev/hotwire/core/{turbo/delegates/TurboBrowseFilesDelegate.kt => files/delegates/BrowseFilesDelegate.kt} (87%) rename core/src/main/kotlin/dev/hotwire/core/{turbo/delegates/TurboCameraCaptureDelegate.kt => files/delegates/CameraCaptureDelegate.kt} (81%) rename core/src/main/kotlin/dev/hotwire/core/{turbo/delegates/TurboFileChooserDelegate.kt => files/delegates/FileChooserDelegate.kt} (87%) create mode 100644 core/src/main/kotlin/dev/hotwire/core/files/util/FileConstants.kt rename core/src/main/kotlin/dev/hotwire/core/{turbo/util/TurboFileProvider.kt => files/util/HotwireFileProvider.kt} (75%) rename core/src/main/kotlin/dev/hotwire/core/{turbo/util/TurboUriAttributes.kt => files/util/UriAttributes.kt} (53%) rename core/src/main/kotlin/dev/hotwire/core/{turbo/util/TurboUriHelper.kt => files/util/UriHelper.kt} (90%) rename core/src/main/kotlin/dev/hotwire/core/turbo/http/{TurboHttpClient.kt => HotwireHttpClient.kt} (94%) rename core/src/main/kotlin/dev/hotwire/core/turbo/{http/TurboOfflineCacheStrategy.kt => offline/OfflineCacheStrategy.kt} (53%) rename core/src/main/kotlin/dev/hotwire/core/turbo/{http/TurboHttpRepository.kt => offline/OfflineHttpRepository.kt} (92%) rename core/src/main/kotlin/dev/hotwire/core/turbo/{http/TurboPreCacheRequest.kt => offline/OfflinePreCacheRequest.kt} (84%) rename core/src/main/kotlin/dev/hotwire/core/turbo/{http/TurboOfflineRequestHandler.kt => offline/OfflineRequestHandler.kt} (75%) rename core/src/main/kotlin/dev/hotwire/core/turbo/{http/TurboWebViewRequestInterceptor.kt => offline/OfflineWebViewRequestInterceptor.kt} (91%) delete mode 100644 core/src/main/kotlin/dev/hotwire/core/turbo/util/CoreConstants.kt rename core/src/main/kotlin/dev/hotwire/core/turbo/util/{TurboDispatcherProvider.kt => DispatcherProvider.kt} (70%) rename core/src/test/kotlin/dev/hotwire/core/{turbo/delegates/TurboBrowseFilesDelegateTest.kt => files/delegates/BrowseFilesDelegateTest.kt} (92%) rename core/src/test/kotlin/dev/hotwire/core/{turbo/delegates/TurboCameraCaptureDelegateTest.kt => files/delegates/CameraCaptureDelegateTest.kt} (92%) rename core/src/test/kotlin/dev/hotwire/core/{turbo/delegates/TurboFileChooserDelegateTest.kt => files/delegates/FileChooserDelegateTest.kt} (89%) rename core/src/test/kotlin/dev/hotwire/core/{turbo/util/TurboFileProviderTest.kt => files/util/HotwireFileProviderTest.kt} (72%) rename core/src/test/kotlin/dev/hotwire/core/{turbo/util/TurboUriHelperTest.kt => files/util/UriHelperTest.kt} (81%) diff --git a/core/src/main/AndroidManifest.xml b/core/src/main/AndroidManifest.xml index 911b2ef..09b98cb 100644 --- a/core/src/main/AndroidManifest.xml +++ b/core/src/main/AndroidManifest.xml @@ -4,8 +4,8 @@ ? { val file = cameraImagePath?.let { File(it) } ?: return null - val uri = TurboFileProvider.contentUriForFile(context, file) + val uri = HotwireFileProvider.contentUriForFile(context, file) return when (file.length()) { 0L -> null @@ -45,7 +45,7 @@ internal class TurboCameraCaptureDelegate(val context: Context) { private fun createEmptyImageFile(): File? { return try { - val directory: File = TurboFileProvider.directory(context) + val directory: File = HotwireFileProvider.directory(context) return File.createTempFile("Capture_", ".jpg", directory) } catch (e: IOException) { logError("createTempFileError", e) diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/delegates/TurboFileChooserDelegate.kt b/core/src/main/kotlin/dev/hotwire/core/files/delegates/FileChooserDelegate.kt similarity index 87% rename from core/src/main/kotlin/dev/hotwire/core/turbo/delegates/TurboFileChooserDelegate.kt rename to core/src/main/kotlin/dev/hotwire/core/files/delegates/FileChooserDelegate.kt index c9482cd..a28e06a 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/delegates/TurboFileChooserDelegate.kt +++ b/core/src/main/kotlin/dev/hotwire/core/files/delegates/FileChooserDelegate.kt @@ -1,4 +1,4 @@ -package dev.hotwire.core.turbo.delegates +package dev.hotwire.core.files.delegates import android.app.Activity import android.content.Context @@ -10,19 +10,19 @@ import androidx.activity.result.ActivityResult import dev.hotwire.core.R import dev.hotwire.core.logging.logError import dev.hotwire.core.turbo.session.Session -import dev.hotwire.core.turbo.util.TURBO_REQUEST_CODE_FILES -import dev.hotwire.core.turbo.util.TurboFileProvider +import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_FILES +import dev.hotwire.core.files.util.HotwireFileProvider import dev.hotwire.core.turbo.util.dispatcherProvider import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.coroutines.launch import kotlin.coroutines.CoroutineContext -class TurboFileChooserDelegate(val session: Session) : CoroutineScope { +class FileChooserDelegate(val session: Session) : CoroutineScope { private val context: Context = session.context private var uploadCallback: ValueCallback>? = null - private val browseFilesDelegate = TurboBrowseFilesDelegate(context) - private val cameraCaptureDelegate = TurboCameraCaptureDelegate(context) + private val browseFilesDelegate = BrowseFilesDelegate(context) + private val cameraCaptureDelegate = CameraCaptureDelegate(context) override val coroutineContext: CoroutineContext get() = dispatcherProvider.io + Job() @@ -47,7 +47,7 @@ class TurboFileChooserDelegate(val session: Session) : CoroutineScope { fun deleteCachedFiles() { launch { - TurboFileProvider.deleteAllFiles(context) + HotwireFileProvider.deleteAllFiles(context) } } @@ -69,7 +69,7 @@ class TurboFileChooserDelegate(val session: Session) : CoroutineScope { val destination = session.currentVisit?.callback?.visitDestination() ?: return false return try { - destination.activityResultLauncher(TURBO_REQUEST_CODE_FILES)?.launch(intent) + destination.activityResultLauncher(HOTWIRE_REQUEST_CODE_FILES)?.launch(intent) true } catch (e: Exception) { logError("startIntentError", e) diff --git a/core/src/main/kotlin/dev/hotwire/core/files/util/FileConstants.kt b/core/src/main/kotlin/dev/hotwire/core/files/util/FileConstants.kt new file mode 100644 index 0000000..ef06fab --- /dev/null +++ b/core/src/main/kotlin/dev/hotwire/core/files/util/FileConstants.kt @@ -0,0 +1,3 @@ +package dev.hotwire.core.files.util + +const val HOTWIRE_REQUEST_CODE_FILES = 37 diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/util/TurboFileProvider.kt b/core/src/main/kotlin/dev/hotwire/core/files/util/HotwireFileProvider.kt similarity index 75% rename from core/src/main/kotlin/dev/hotwire/core/turbo/util/TurboFileProvider.kt rename to core/src/main/kotlin/dev/hotwire/core/files/util/HotwireFileProvider.kt index a131c93..aaf3dfa 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/util/TurboFileProvider.kt +++ b/core/src/main/kotlin/dev/hotwire/core/files/util/HotwireFileProvider.kt @@ -1,18 +1,20 @@ -package dev.hotwire.core.turbo.util +package dev.hotwire.core.files.util import android.content.Context import android.net.Uri import androidx.core.content.FileProvider +import dev.hotwire.core.turbo.util.deleteAllFilesInDirectory +import dev.hotwire.core.turbo.util.dispatcherProvider import kotlinx.coroutines.withContext import java.io.File import java.io.IOException -class TurboFileProvider : FileProvider() { +class HotwireFileProvider : FileProvider() { companion object { private const val sharedDir = "shared" fun authority(context: Context): String { - return "${context.packageName}.turbo.fileprovider" + return "${context.packageName}.hotwire.fileprovider" } fun directory(context: Context, dirName: String = sharedDir): File { @@ -30,13 +32,13 @@ class TurboFileProvider : FileProvider() { } @Suppress("unused") - fun uriAttributes(context: Context, uri: Uri): TurboUriAttributes? { - val uriHelper = TurboUriHelper(context) + fun uriAttributes(context: Context, uri: Uri): UriAttributes? { + val uriHelper = UriHelper(context) return uriHelper.getAttributes(uri) } suspend fun writeUriToFile(context: Context, uri: Uri, dirName: String = sharedDir): File? { - val uriHelper = TurboUriHelper(context) + val uriHelper = UriHelper(context) return uriHelper.writeFileTo(uri, directory(context, dirName)) } diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/util/TurboUriAttributes.kt b/core/src/main/kotlin/dev/hotwire/core/files/util/UriAttributes.kt similarity index 53% rename from core/src/main/kotlin/dev/hotwire/core/turbo/util/TurboUriAttributes.kt rename to core/src/main/kotlin/dev/hotwire/core/files/util/UriAttributes.kt index 7d02ff0..46cbead 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/util/TurboUriAttributes.kt +++ b/core/src/main/kotlin/dev/hotwire/core/files/util/UriAttributes.kt @@ -1,6 +1,6 @@ -package dev.hotwire.core.turbo.util +package dev.hotwire.core.files.util -data class TurboUriAttributes( +data class UriAttributes( val fileName: String, val mimeType: String, val fileSize: Long diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/util/TurboUriHelper.kt b/core/src/main/kotlin/dev/hotwire/core/files/util/UriHelper.kt similarity index 90% rename from core/src/main/kotlin/dev/hotwire/core/turbo/util/TurboUriHelper.kt rename to core/src/main/kotlin/dev/hotwire/core/files/util/UriHelper.kt index fe7f0cd..bd1d78c 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/util/TurboUriHelper.kt +++ b/core/src/main/kotlin/dev/hotwire/core/files/util/UriHelper.kt @@ -1,4 +1,4 @@ -package dev.hotwire.core.turbo.util +package dev.hotwire.core.files.util import android.content.Context import android.database.Cursor @@ -6,11 +6,13 @@ import android.net.Uri import android.provider.OpenableColumns import android.webkit.MimeTypeMap import dev.hotwire.core.logging.logError +import dev.hotwire.core.turbo.util.dispatcherProvider +import dev.hotwire.core.turbo.util.extract import kotlinx.coroutines.withContext import java.io.File import java.io.IOException -internal class TurboUriHelper(val context: Context) { +internal class UriHelper(val context: Context) { @Suppress("BlockingMethodInNonBlockingContext") // https://youtrack.jetbrains.com/issue/KT-39684 suspend fun writeFileTo(uri: Uri, destDirectory: File): File? { val uriAttributes = getAttributes(uri) ?: return null @@ -39,7 +41,7 @@ internal class TurboUriHelper(val context: Context) { } } - fun getAttributes(uri: Uri): TurboUriAttributes? { + fun getAttributes(uri: Uri): UriAttributes? { return when (uri.scheme) { "file" -> getFileUriAttributes(uri) "content" -> getContentUriAttributes(context, uri) @@ -47,21 +49,21 @@ internal class TurboUriHelper(val context: Context) { } } - private fun getFileUriAttributes(uri: Uri): TurboUriAttributes? { + private fun getFileUriAttributes(uri: Uri): UriAttributes? { val file = uri.getFile() ?: return null if (file.originIsAppResource()) { return null } - return TurboUriAttributes( + return UriAttributes( fileName = file.name, mimeType = uri.mimeType(), fileSize = file.length() ) } - private fun getContentUriAttributes(context: Context, uri: Uri): TurboUriAttributes? { + private fun getContentUriAttributes(context: Context, uri: Uri): UriAttributes? { val projection = arrayOf(OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE) val mimeType: String? = context.contentResolver.getType(uri) val cursor = try { @@ -80,7 +82,7 @@ internal class TurboUriHelper(val context: Context) { return cursorAttributes ?: uriAttributesDerivedFromUri(uri, mimeType) } - private fun uriAttributesFromContentQuery(uri: Uri, mimeType: String?, cursor: Cursor): TurboUriAttributes? { + private fun uriAttributesFromContentQuery(uri: Uri, mimeType: String?, cursor: Cursor): UriAttributes? { val columnName = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME).takeIf { it >= 0 } val columnSize = cursor.getColumnIndex(OpenableColumns.SIZE).takeIf { it >= 0 } val fileName: String? = columnName?.let { cursor.getString(it) } @@ -90,21 +92,21 @@ internal class TurboUriHelper(val context: Context) { return null } - return TurboUriAttributes( + return UriAttributes( fileName = fileName ?: "attachment", mimeType = mimeType ?: uri.mimeType(), fileSize = fileSize ?: 0L ) } - private fun uriAttributesDerivedFromUri(uri: Uri, mimeType: String?): TurboUriAttributes? { + private fun uriAttributesDerivedFromUri(uri: Uri, mimeType: String?): UriAttributes? { val fileName: String? = uri.lastPathSegment if (fileName == null && mimeType == null) { return null } - return TurboUriAttributes( + return UriAttributes( fileName = fileName ?: "attachment", mimeType = mimeType ?: uri.mimeType(), fileSize = 0 diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/config/PathConfigurationRepository.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/config/PathConfigurationRepository.kt index e7284a3..bafa6bc 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/config/PathConfigurationRepository.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/config/PathConfigurationRepository.kt @@ -4,7 +4,7 @@ import android.content.Context import android.content.SharedPreferences import androidx.core.content.edit import dev.hotwire.core.logging.logError -import dev.hotwire.core.turbo.http.TurboHttpClient +import dev.hotwire.core.turbo.http.HotwireHttpClient import dev.hotwire.core.turbo.util.dispatcherProvider import dev.hotwire.core.turbo.util.toJson import kotlinx.coroutines.withContext @@ -37,7 +37,7 @@ internal class PathConfigurationRepository { private fun issueRequest(request: Request): String? { return try { - val call = TurboHttpClient.instance.newCall(request) + val call = HotwireHttpClient.instance.newCall(request) call.execute().use { response -> if (response.isSuccessful) { diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboHttpClient.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/http/HotwireHttpClient.kt similarity index 94% rename from core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboHttpClient.kt rename to core/src/main/kotlin/dev/hotwire/core/turbo/http/HotwireHttpClient.kt index 5e3066f..512c06a 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboHttpClient.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/http/HotwireHttpClient.kt @@ -13,7 +13,7 @@ import java.util.concurrent.TimeUnit /** * Experimental: API may change, not ready for production use. */ -object TurboHttpClient { +object HotwireHttpClient { private var cache: Cache? = null private var httpCacheSize = 100L * 1024L * 1024L // 100 MBs private val loggingInterceptor = HttpLoggingInterceptor().apply { @@ -43,7 +43,7 @@ object TurboHttpClient { internal fun enableCachingWith(context: Context) { if (cache == null) { cache = Cache( - directory = File(context.cacheDir, "turbo_cache"), + directory = File(context.cacheDir, "hotwire_cache"), maxSize = httpCacheSize ) reset() diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboOfflineCacheStrategy.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflineCacheStrategy.kt similarity index 53% rename from core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboOfflineCacheStrategy.kt rename to core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflineCacheStrategy.kt index 2fc6e17..b1fccec 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboOfflineCacheStrategy.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflineCacheStrategy.kt @@ -1,8 +1,8 @@ -package dev.hotwire.core.turbo.http +package dev.hotwire.core.turbo.offline /** * Experimental: API may change, not ready for production use. */ -enum class TurboOfflineCacheStrategy { +enum class OfflineCacheStrategy { APP, NONE } diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboHttpRepository.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflineHttpRepository.kt similarity index 92% rename from core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboHttpRepository.kt rename to core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflineHttpRepository.kt index 8659119..df9aa00 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboHttpRepository.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflineHttpRepository.kt @@ -1,9 +1,10 @@ -package dev.hotwire.core.turbo.http +package dev.hotwire.core.turbo.offline import android.webkit.CookieManager import android.webkit.WebResourceRequest import android.webkit.WebResourceResponse import dev.hotwire.core.logging.logError +import dev.hotwire.core.turbo.http.HotwireHttpClient import dev.hotwire.core.turbo.util.dispatcherProvider import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch @@ -20,7 +21,7 @@ import java.io.InputStream /** * Experimental: API may change, not ready for production use. */ -internal class TurboHttpRepository(private val coroutineScope: CoroutineScope) { +internal class OfflineHttpRepository(private val coroutineScope: CoroutineScope) { private val cookieManager = CookieManager.getInstance() // Limit pre-cache requests to 2 concurrently @@ -33,7 +34,7 @@ internal class TurboHttpRepository(private val coroutineScope: CoroutineScope) { ) internal fun preCache( - requestHandler: TurboOfflineRequestHandler, + requestHandler: OfflineRequestHandler, resourceRequest: WebResourceRequest ) { coroutineScope.launch { @@ -46,19 +47,19 @@ internal class TurboHttpRepository(private val coroutineScope: CoroutineScope) { } internal fun fetch( - requestHandler: TurboOfflineRequestHandler, + requestHandler: OfflineRequestHandler, resourceRequest: WebResourceRequest ): Result { val url = resourceRequest.url.toString() return when (requestHandler.getCacheStrategy(url)) { - TurboOfflineCacheStrategy.APP -> fetchAppCacheRequest(requestHandler, resourceRequest) - TurboOfflineCacheStrategy.NONE -> Result(null, false) + OfflineCacheStrategy.APP -> fetchAppCacheRequest(requestHandler, resourceRequest) + OfflineCacheStrategy.NONE -> Result(null, false) } } private fun fetchAppCacheRequest( - requestHandler: TurboOfflineRequestHandler, + requestHandler: OfflineRequestHandler, resourceRequest: WebResourceRequest ): Result { val url = resourceRequest.url.toString() @@ -126,7 +127,7 @@ internal class TurboHttpRepository(private val coroutineScope: CoroutineScope) { private fun getResponse(request: Request): Response? { val location = request.url.toString() - val call = TurboHttpClient.instance.newCall(request) + val call = HotwireHttpClient.instance.newCall(request) return call.execute().let { response -> if (response.isSuccessful) { diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboPreCacheRequest.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflinePreCacheRequest.kt similarity index 84% rename from core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboPreCacheRequest.kt rename to core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflinePreCacheRequest.kt index 113fe91..c1dd928 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboPreCacheRequest.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflinePreCacheRequest.kt @@ -1,4 +1,4 @@ -package dev.hotwire.core.turbo.http +package dev.hotwire.core.turbo.offline import android.net.Uri import android.webkit.CookieManager @@ -7,7 +7,7 @@ import android.webkit.WebResourceRequest /** * Experimental: API may change, not ready for production use. */ -internal class TurboPreCacheRequest(val url: String, val userAgent: String) : WebResourceRequest { +internal class OfflinePreCacheRequest(val url: String, val userAgent: String) : WebResourceRequest { private val cookieManager = CookieManager.getInstance() override fun getUrl(): Uri { diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboOfflineRequestHandler.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflineRequestHandler.kt similarity index 75% rename from core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboOfflineRequestHandler.kt rename to core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflineRequestHandler.kt index 5e91008..1902aad 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboOfflineRequestHandler.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflineRequestHandler.kt @@ -1,12 +1,12 @@ -package dev.hotwire.core.turbo.http +package dev.hotwire.core.turbo.offline import android.webkit.WebResourceResponse /** * Experimental: API may change, not ready for production use. */ -interface TurboOfflineRequestHandler { - fun getCacheStrategy(url: String): TurboOfflineCacheStrategy +interface OfflineRequestHandler { + fun getCacheStrategy(url: String): OfflineCacheStrategy fun getCachedResponseHeaders(url: String): Map? fun getCachedResponse(url: String, allowStaleResponse: Boolean = false): WebResourceResponse? fun getCachedSnapshot(url: String): WebResourceResponse? diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboWebViewRequestInterceptor.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflineWebViewRequestInterceptor.kt similarity index 91% rename from core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboWebViewRequestInterceptor.kt rename to core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflineWebViewRequestInterceptor.kt index dde5682..3d159ea 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/http/TurboWebViewRequestInterceptor.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/offline/OfflineWebViewRequestInterceptor.kt @@ -1,4 +1,4 @@ -package dev.hotwire.core.turbo.http +package dev.hotwire.core.turbo.offline import android.webkit.WebResourceRequest import android.webkit.WebResourceResponse @@ -7,7 +7,7 @@ import dev.hotwire.core.logging.logEvent import dev.hotwire.core.turbo.session.Session import dev.hotwire.core.turbo.util.isHttpGetRequest -internal class TurboWebViewRequestInterceptor(val session: Session) { +internal class OfflineWebViewRequestInterceptor(val session: Session) { private val offlineRequestHandler get() = Hotwire.config.offlineRequestHandler private val httpRepository get() = session.httpRepository private val currentVisit get() = session.currentVisit @@ -43,7 +43,7 @@ internal class TurboWebViewRequestInterceptor(val session: Session) { return request.isHttpGetRequest() } - private fun logCurrentVisitResult(url: String, result: TurboHttpRepository.Result) { + private fun logCurrentVisitResult(url: String, result: OfflineHttpRepository.Result) { logEvent( "location" to url, "redirectToLocation" to result.redirectToLocation.toString(), diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/session/Session.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/session/Session.kt index ea7bfe6..581cd24 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/session/Session.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/session/Session.kt @@ -15,12 +15,13 @@ import androidx.webkit.WebViewFeature.VISUAL_STATE_CALLBACK import androidx.webkit.WebViewFeature.isFeatureSupported import dev.hotwire.core.config.Hotwire import dev.hotwire.core.logging.logEvent -import dev.hotwire.core.turbo.delegates.TurboFileChooserDelegate +import dev.hotwire.core.files.delegates.FileChooserDelegate import dev.hotwire.core.turbo.errors.HttpError import dev.hotwire.core.turbo.errors.LoadError import dev.hotwire.core.turbo.errors.WebError import dev.hotwire.core.turbo.errors.WebSslError -import dev.hotwire.core.turbo.http.* +import dev.hotwire.core.turbo.http.HotwireHttpClient +import dev.hotwire.core.turbo.offline.* import dev.hotwire.core.turbo.util.isHttpGetRequest import dev.hotwire.core.turbo.util.runOnUiThread import dev.hotwire.core.turbo.util.toJson @@ -50,8 +51,8 @@ class Session( internal var visitPending = false internal var restorationIdentifiers = SparseArray() internal val context: Context = activity.applicationContext - internal val httpRepository = TurboHttpRepository(activity.lifecycleScope) - internal val requestInterceptor = TurboWebViewRequestInterceptor(this) + internal val httpRepository = OfflineHttpRepository(activity.lifecycleScope) + internal val requestInterceptor = OfflineWebViewRequestInterceptor(this) // User accessible @@ -75,18 +76,18 @@ class Session( var isRenderProcessGone = false internal set - val fileChooserDelegate = TurboFileChooserDelegate(this) + val fileChooserDelegate = FileChooserDelegate(this) init { initializeWebView() - TurboHttpClient.enableCachingWith(context) + HotwireHttpClient.enableCachingWith(context) fileChooserDelegate.deleteCachedFiles() } // Public /** - * Fetches a given location and returns the response to the [TurboOfflineRequestHandler] + * Fetches a given location and returns the response to the [OfflineRequestHandler] * Allows an offline cache to contain specific items instead of solely relying on visited items. * * @param location Location to cache. @@ -97,7 +98,7 @@ class Session( } httpRepository.preCache( - requestHandler, TurboPreCacheRequest( + requestHandler, OfflinePreCacheRequest( url = location, userAgent = webView.settings.userAgentString ) ) diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/util/CoreConstants.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/util/CoreConstants.kt deleted file mode 100644 index fb62874..0000000 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/util/CoreConstants.kt +++ /dev/null @@ -1,3 +0,0 @@ -package dev.hotwire.core.turbo.util - -const val TURBO_REQUEST_CODE_FILES = 37 diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/util/TurboDispatcherProvider.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/util/DispatcherProvider.kt similarity index 70% rename from core/src/main/kotlin/dev/hotwire/core/turbo/util/TurboDispatcherProvider.kt rename to core/src/main/kotlin/dev/hotwire/core/turbo/util/DispatcherProvider.kt index 703b3fd..6dc9d5a 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/util/TurboDispatcherProvider.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/util/DispatcherProvider.kt @@ -3,12 +3,12 @@ package dev.hotwire.core.turbo.util import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers -internal data class TurboDispatcherProvider( +internal data class DispatcherProvider( val main: CoroutineDispatcher, var io: CoroutineDispatcher ) -internal val dispatcherProvider = TurboDispatcherProvider( +internal val dispatcherProvider = DispatcherProvider( main = Dispatchers.Main, io = Dispatchers.IO ) diff --git a/core/src/test/kotlin/dev/hotwire/core/turbo/delegates/TurboBrowseFilesDelegateTest.kt b/core/src/test/kotlin/dev/hotwire/core/files/delegates/BrowseFilesDelegateTest.kt similarity index 92% rename from core/src/test/kotlin/dev/hotwire/core/turbo/delegates/TurboBrowseFilesDelegateTest.kt rename to core/src/test/kotlin/dev/hotwire/core/files/delegates/BrowseFilesDelegateTest.kt index 776fd6a..c0285a2 100644 --- a/core/src/test/kotlin/dev/hotwire/core/turbo/delegates/TurboBrowseFilesDelegateTest.kt +++ b/core/src/test/kotlin/dev/hotwire/core/files/delegates/BrowseFilesDelegateTest.kt @@ -1,4 +1,4 @@ -package dev.hotwire.core.turbo.delegates +package dev.hotwire.core.files.delegates import android.content.Context import android.content.Intent @@ -17,15 +17,15 @@ import org.robolectric.annotation.Config @RunWith(RobolectricTestRunner::class) @Config(sdk = [Build.VERSION_CODES.R]) -class TurboBrowseFilesDelegateTest : BaseUnitTest() { +class BrowseFilesDelegateTest : BaseUnitTest() { private lateinit var context: Context - private lateinit var delegate: TurboBrowseFilesDelegate + private lateinit var delegate: BrowseFilesDelegate @Before override fun setup() { super.setup() context = ApplicationProvider.getApplicationContext() - delegate = TurboBrowseFilesDelegate(context) + delegate = BrowseFilesDelegate(context) } @Test diff --git a/core/src/test/kotlin/dev/hotwire/core/turbo/delegates/TurboCameraCaptureDelegateTest.kt b/core/src/test/kotlin/dev/hotwire/core/files/delegates/CameraCaptureDelegateTest.kt similarity index 92% rename from core/src/test/kotlin/dev/hotwire/core/turbo/delegates/TurboCameraCaptureDelegateTest.kt rename to core/src/test/kotlin/dev/hotwire/core/files/delegates/CameraCaptureDelegateTest.kt index fdfd15b..1f9e9f1 100644 --- a/core/src/test/kotlin/dev/hotwire/core/turbo/delegates/TurboCameraCaptureDelegateTest.kt +++ b/core/src/test/kotlin/dev/hotwire/core/files/delegates/CameraCaptureDelegateTest.kt @@ -1,4 +1,4 @@ -package dev.hotwire.core.turbo.delegates +package dev.hotwire.core.files.delegates import android.content.Context import android.content.Intent @@ -17,15 +17,15 @@ import org.robolectric.annotation.Config @RunWith(RobolectricTestRunner::class) @Config(sdk = [Build.VERSION_CODES.R]) -class TurboCameraCaptureDelegateTest : BaseUnitTest() { +class CameraCaptureDelegateTest : BaseUnitTest() { private lateinit var context: Context - private lateinit var delegate: TurboCameraCaptureDelegate + private lateinit var delegate: CameraCaptureDelegate @Before override fun setup() { super.setup() context = ApplicationProvider.getApplicationContext() - delegate = TurboCameraCaptureDelegate(context) + delegate = CameraCaptureDelegate(context) } @Test @@ -46,7 +46,7 @@ class TurboCameraCaptureDelegateTest : BaseUnitTest() { assertThat(intent).isNotNull() assertThat(intent?.action).isEqualTo(MediaStore.ACTION_IMAGE_CAPTURE) - assertThat(uri).startsWith("content://dev.hotwire.core.test.turbo.fileprovider/shared") + assertThat(uri).startsWith("content://dev.hotwire.core.test.hotwire.fileprovider/shared") assertThat(uri).contains("/Capture_") assertThat(uri).endsWith(".jpg") } diff --git a/core/src/test/kotlin/dev/hotwire/core/turbo/delegates/TurboFileChooserDelegateTest.kt b/core/src/test/kotlin/dev/hotwire/core/files/delegates/FileChooserDelegateTest.kt similarity index 89% rename from core/src/test/kotlin/dev/hotwire/core/turbo/delegates/TurboFileChooserDelegateTest.kt rename to core/src/test/kotlin/dev/hotwire/core/files/delegates/FileChooserDelegateTest.kt index 4b6a064..8e30765 100644 --- a/core/src/test/kotlin/dev/hotwire/core/turbo/delegates/TurboFileChooserDelegateTest.kt +++ b/core/src/test/kotlin/dev/hotwire/core/files/delegates/FileChooserDelegateTest.kt @@ -1,12 +1,12 @@ -package dev.hotwire.core.turbo.delegates +package dev.hotwire.core.files.delegates import android.content.Context import android.os.Build import androidx.appcompat.app.AppCompatActivity import androidx.test.core.app.ApplicationProvider +import dev.hotwire.core.files.util.HotwireFileProvider import dev.hotwire.core.turbo.BaseRepositoryTest import dev.hotwire.core.turbo.session.Session -import dev.hotwire.core.turbo.util.TurboFileProvider import dev.hotwire.core.turbo.views.TurboWebView import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.runBlocking @@ -24,7 +24,7 @@ import java.io.File @ExperimentalCoroutinesApi @RunWith(RobolectricTestRunner::class) @Config(sdk = [Build.VERSION_CODES.R]) -class TurboFileChooserDelegateTest : BaseRepositoryTest() { +class FileChooserDelegateTest : BaseRepositoryTest() { @Mock private lateinit var webView: TurboWebView private lateinit var activity: AppCompatActivity @@ -43,7 +43,7 @@ class TurboFileChooserDelegateTest : BaseRepositoryTest() { @Test fun fileProviderDirectoryIsCleared() { - val dir = TurboFileProvider.directory(context) + val dir = HotwireFileProvider.directory(context) File(dir, "testFile.txt").apply { writeText("text") diff --git a/core/src/test/kotlin/dev/hotwire/core/turbo/util/TurboFileProviderTest.kt b/core/src/test/kotlin/dev/hotwire/core/files/util/HotwireFileProviderTest.kt similarity index 72% rename from core/src/test/kotlin/dev/hotwire/core/turbo/util/TurboFileProviderTest.kt rename to core/src/test/kotlin/dev/hotwire/core/files/util/HotwireFileProviderTest.kt index f89a765..fd0d92d 100644 --- a/core/src/test/kotlin/dev/hotwire/core/turbo/util/TurboFileProviderTest.kt +++ b/core/src/test/kotlin/dev/hotwire/core/files/util/HotwireFileProviderTest.kt @@ -1,4 +1,4 @@ -package dev.hotwire.core.turbo.util +package dev.hotwire.core.files.util import android.content.Context import android.os.Build @@ -13,7 +13,7 @@ import org.robolectric.annotation.Config @RunWith(RobolectricTestRunner::class) @Config(sdk = [Build.VERSION_CODES.R]) -class TurboFileProviderTest : BaseUnitTest() { +class HotwireFileProviderTest : BaseUnitTest() { private lateinit var context: Context @Before @@ -24,13 +24,13 @@ class TurboFileProviderTest : BaseUnitTest() { @Test fun authority() { - val authority = TurboFileProvider.authority(context) - assertThat(authority).isEqualTo("dev.hotwire.core.test.turbo.fileprovider") + val authority = HotwireFileProvider.authority(context) + assertThat(authority).isEqualTo("dev.hotwire.core.test.hotwire.fileprovider") } @Test fun directory() { - val directory = TurboFileProvider.directory(context) + val directory = HotwireFileProvider.directory(context) assertThat(directory.path).endsWith("dev.hotwire.core.test-dataDir/files/shared") } } diff --git a/core/src/test/kotlin/dev/hotwire/core/turbo/util/TurboUriHelperTest.kt b/core/src/test/kotlin/dev/hotwire/core/files/util/UriHelperTest.kt similarity index 81% rename from core/src/test/kotlin/dev/hotwire/core/turbo/util/TurboUriHelperTest.kt rename to core/src/test/kotlin/dev/hotwire/core/files/util/UriHelperTest.kt index 3b591c1..5ff7ee9 100644 --- a/core/src/test/kotlin/dev/hotwire/core/turbo/util/TurboUriHelperTest.kt +++ b/core/src/test/kotlin/dev/hotwire/core/files/util/UriHelperTest.kt @@ -1,10 +1,11 @@ -package dev.hotwire.core.turbo.util +package dev.hotwire.core.files.util import android.content.Context import android.net.Uri import android.os.Build import androidx.test.core.app.ApplicationProvider import dev.hotwire.core.turbo.BaseUnitTest +import dev.hotwire.core.turbo.util.dispatcherProvider import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.TestCoroutineDispatcher @@ -23,12 +24,12 @@ import java.io.File @ExperimentalCoroutinesApi @RunWith(RobolectricTestRunner::class) @Config(sdk = [Build.VERSION_CODES.R]) -class TurboUriHelperTest : BaseUnitTest() { +class UriHelperTest : BaseUnitTest() { private val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher() private lateinit var context: Context - private lateinit var turboUriHelper: TurboUriHelper + private lateinit var uriHelper: UriHelper @Before override fun setup() { @@ -37,7 +38,7 @@ class TurboUriHelperTest : BaseUnitTest() { dispatcherProvider.io = Dispatchers.Main context = ApplicationProvider.getApplicationContext() - turboUriHelper = TurboUriHelper(context) + uriHelper = UriHelper(context) } override fun teardown() { @@ -52,7 +53,7 @@ class TurboUriHelperTest : BaseUnitTest() { val inputFileUri = Uri.fromFile(inputFile) Shadows.shadowOf(context.contentResolver).registerInputStream(inputFileUri, "fileContent".byteInputStream()) - val destFile = turboUriHelper.writeFileTo(inputFileUri, TurboFileProvider.directory(context)) + val destFile = uriHelper.writeFileTo(inputFileUri, HotwireFileProvider.directory(context)) assertThat(destFile).isNotNull() } @@ -62,7 +63,7 @@ class TurboUriHelperTest : BaseUnitTest() { val inputFileUri = Uri.parse("../../tmp/file.txt") Shadows.shadowOf(context.contentResolver).registerInputStream(inputFileUri, "fileContent".byteInputStream()) - val destFile = turboUriHelper.writeFileTo(inputFileUri, TurboFileProvider.directory(context)) + val destFile = uriHelper.writeFileTo(inputFileUri, HotwireFileProvider.directory(context)) assertThat(destFile).isNull() } @@ -72,7 +73,7 @@ class TurboUriHelperTest : BaseUnitTest() { val inputFileUri = Uri.parse("content://malicious.app?path=/data/data/malicious.app/files/file.txt&name=../../file.txt") Shadows.shadowOf(context.contentResolver).registerInputStream(inputFileUri, "fileContent".byteInputStream()) - val destFile = turboUriHelper.writeFileTo(inputFileUri, TurboFileProvider.directory(context)) + val destFile = uriHelper.writeFileTo(inputFileUri, HotwireFileProvider.directory(context)) assertThat(destFile).isNull() } diff --git a/core/src/test/kotlin/dev/hotwire/core/turbo/config/PathConfigurationRepositoryTest.kt b/core/src/test/kotlin/dev/hotwire/core/turbo/config/PathConfigurationRepositoryTest.kt index 5f0e751..5e1b48d 100644 --- a/core/src/test/kotlin/dev/hotwire/core/turbo/config/PathConfigurationRepositoryTest.kt +++ b/core/src/test/kotlin/dev/hotwire/core/turbo/config/PathConfigurationRepositoryTest.kt @@ -5,7 +5,7 @@ import android.os.Build import androidx.test.core.app.ApplicationProvider import com.google.gson.reflect.TypeToken import dev.hotwire.core.turbo.BaseRepositoryTest -import dev.hotwire.core.turbo.http.TurboHttpClient +import dev.hotwire.core.turbo.http.HotwireHttpClient import dev.hotwire.core.turbo.util.toObject import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -27,7 +27,7 @@ class PathConfigurationRepositoryTest : BaseRepositoryTest() { override fun setup() { super.setup() context = ApplicationProvider.getApplicationContext() - TurboHttpClient.instance = client() + HotwireHttpClient.instance = client() } @Test diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebBottomSheetFragment.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebBottomSheetFragment.kt index 5440e6a..bd7abfb 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebBottomSheetFragment.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebBottomSheetFragment.kt @@ -10,7 +10,7 @@ import android.view.ViewGroup import androidx.activity.result.ActivityResultLauncher import dev.hotwire.core.bridge.BridgeDelegate import dev.hotwire.core.turbo.errors.VisitError -import dev.hotwire.core.turbo.util.TURBO_REQUEST_CODE_FILES +import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_FILES import dev.hotwire.core.turbo.views.TurboWebChromeClient import dev.hotwire.core.turbo.views.TurboWebView import dev.hotwire.navigation.R @@ -58,7 +58,7 @@ open class HotwireWebBottomSheetFragment : HotwireBottomSheetFragment(), Hotwire override fun activityResultLauncher(requestCode: Int): ActivityResultLauncher? { return when (requestCode) { - TURBO_REQUEST_CODE_FILES -> webDelegate.fileChooserResultLauncher + HOTWIRE_REQUEST_CODE_FILES -> webDelegate.fileChooserResultLauncher else -> null } } diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt index 3305282..5834ac4 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt @@ -9,7 +9,7 @@ import android.view.ViewGroup import androidx.activity.result.ActivityResultLauncher import dev.hotwire.core.bridge.BridgeDelegate import dev.hotwire.core.turbo.errors.VisitError -import dev.hotwire.core.turbo.util.TURBO_REQUEST_CODE_FILES +import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_FILES import dev.hotwire.core.turbo.views.TurboWebChromeClient import dev.hotwire.core.turbo.views.TurboWebView import dev.hotwire.navigation.R @@ -95,7 +95,7 @@ open class HotwireWebFragment : HotwireFragment(), HotwireWebFragmentCallback { override fun activityResultLauncher(requestCode: Int): ActivityResultLauncher? { return when (requestCode) { - TURBO_REQUEST_CODE_FILES -> webDelegate.fileChooserResultLauncher + HOTWIRE_REQUEST_CODE_FILES -> webDelegate.fileChooserResultLauncher else -> null } }