Skip to content

Instantly share code, notes, and snippets.

@grishko188
grishko188 / build.gradle
Created February 5, 2023 16:27
testInstrumentationRunner
android {
compileSdk 33
defaultConfig {
applicationId "com.grishko188.pointofinterest"
...
testInstrumentationRunner "com.grishko188.pointofinterest.MyAppTestRunner"
}
}
@grishko188
grishko188 / MyAppTestRunner.kt
Created February 5, 2023 16:25
Test runner for Hilt-testing
class MyAppTestRunner : AndroidJUnitRunner() {
override fun newApplication(cl: ClassLoader?, name: String?, context: Context?): Application {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}
@grishko188
grishko188 / build.gradle
Created February 5, 2023 16:24
Hilt instrumented test deps.
dependencies {
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.44'
// ...with Kotlin.
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.44'
// ...with Java.
androidTestAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.44'
}
@grishko188
grishko188 / SearchViewModelTest.kt
Created February 5, 2023 16:02
Manual dependencies
@OptIn(ExperimentalCoroutinesApi::class)
class SearchViewModelTest {
@Inject
lateinit var searchUseCase: SearchPoiUseCase
lateinit var SUT: SearchVm
@Before
fun setup() {
@grishko188
grishko188 / SharedContent.kt
Created January 25, 2023 17:23
Fetching shared data from intent
fun Intent.parseSharedContent(): SharedContent {
if (action != Intent.ACTION_SEND) return SharedContent(null, ContentType.EMPTY)
return if (isTextMimeType()) {
val textContent = getStringExtra(Intent.EXTRA_TEXT) ?: ""
if (Patterns.WEB_URL.matcher(textContent).matches()) {
SharedContent(textContent, ContentType.URL)
} else {
SharedContent(textContent, ContentType.PLAIN_TEXT)
@grishko188
grishko188 / ShareTargetScreen.kt
Created January 25, 2023 17:09
View model injection into composable
@Composable
fun ShareTargetScreen(
...
viewModel: SharingTargetViewModel = hiltViewModel()
) {
...
}
@grishko188
grishko188 / SharingTargetViewModel.kt
Last active January 25, 2023 16:53
Access shared data in ViewModel
@HiltViewModel
class SharingTargetViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
...
) : ViewModel() {
private val sharedContentState = savedStateHandle.getStateFlow(NavController.KEY_DEEP_LINK_INTENT, Intent())
.map { intent -> intent.parseSharedContent() }
.map {
// map to ui state
@grishko188
grishko188 / NavigationGraph.kt
Last active January 25, 2023 16:29
Navigation graph
@Composable
fun Navigation(appState: PoiAppState, paddingValues: PaddingValues) {
NavHost(appState.navController, startDestination = Screen.Home.route, Modifier.padding(paddingValues)) {
...
composable(
route = "share_target_route"
deepLinks = listOf(
navDeepLink {
action = Intent.ACTION_SEND
mimeType = "text/*"
@grishko188
grishko188 / Manifest.xml
Last active February 1, 2023 09:42
Share target activity with intent filter
<activity
android:name=".features.main.MainActivity"
android:theme="@style/Theme.AppSplash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />