main

square/leakcanary

Last updated at: 29/12/2023 09:39

Sharer.kt

TLDR

The provided file is a Kotlin file in the org.leakcanary.util package that contains the Sharer interface, ActivitySharer class, and a SharerModule Dagger module. The Sharer interface defines the share method, while the ActivitySharer class implements the Sharer interface to enable sharing content via an intent. The SharerModule is responsible for binding the ActivitySharer implementation to the Sharer interface using Dagger.

package org.leakcanary.util

import android.content.Intent
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityRetainedComponent
import javax.inject.Inject

interface Sharer {
  fun share(content: String)
}

class ActivitySharer @Inject constructor(
  private val activityProvider: CurrentActivityProvider
) : Sharer {
  override fun share(content: String) {
    val intent = Intent(Intent.ACTION_SEND).apply {
      type = "text/plain"
      putExtra(Intent.EXTRA_TEXT, content)
    }

    activityProvider.withActivity {
      startActivity(
        Intent.createChooser(intent, "Share with…")
      )
    }
  }
}

@Module
@InstallIn(ActivityRetainedComponent::class)
interface SharerModule {
  @Binds fun bindSharer(sharer: ActivitySharer): Sharer
}