Add and implement UrlLauncher

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-09-13 23:48:37 +05:30
parent 34ba71e229
commit 1a1410eb1c
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
4 changed files with 39 additions and 2 deletions

View file

@ -4,5 +4,4 @@ import android.app.Application
import dagger.hilt.android.HiltAndroidApp import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp @HiltAndroidApp
class Application : Application() { class Application : Application()
}

View file

@ -0,0 +1,19 @@
package dev.msfjarvis.todo.di
import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent
import dagger.hilt.android.qualifiers.ActivityContext
import dev.msfjarvis.todo.urllauncher.UrlLauncher
import dev.msfjarvis.todo.urllauncher.UrlLauncherImpl
@InstallIn(ActivityComponent::class)
@Module
object UrlLauncherModule {
@Provides
fun provideUrlLauncher(@ActivityContext context: Context): UrlLauncher {
return UrlLauncherImpl(context)
}
}

View file

@ -0,0 +1,5 @@
package dev.msfjarvis.todo.urllauncher
interface UrlLauncher {
fun launch(url: String)
}

View file

@ -0,0 +1,14 @@
package dev.msfjarvis.todo.urllauncher
import android.content.Context
import android.content.Intent
import android.content.Intent.ACTION_VIEW
import android.net.Uri
import android.util.Patterns
class UrlLauncherImpl(private val context: Context) : UrlLauncher {
override fun launch(url: String) {
if (!Patterns.WEB_URL.matcher(url).matches()) return
context.startActivity(Intent(ACTION_VIEW).apply { data = Uri.parse(url) })
}
}