Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Deep Link support for the three screens #12

Merged
merged 2 commits into from
Nov 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ art collector displays a selection of paintings from the [Harvard Art Museum API
We need three screens:

- `/` showing a collection of paintings
- `/artist/{person_id}` showing the collection of paintings by the given artist
- `/artist/{person_id}/{object_id}` showing a single painting
- `/{person_id}` showing the collection of paintings by the given artist
- `/{person_id}/{object_id}` showing a single painting

## Why

Expand Down
13 changes: 13 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@

</activity-alias>

<activity android:name=".DeepLinkActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="art-collector.ataulm.com" />
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data tags inside an intent-filter are merged so all combinations are considered:

  • http + art-collector.ataulm.com
  • https + art-collector.ataulm.com

</intent-filter>
</activity>

</application>

</manifest>
32 changes: 32 additions & 0 deletions app/src/main/java/com/ataulm/artcollector/DeepLinkActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.ataulm.artcollector

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

private const val ARTIST = 0
private const val PAINTING = 1

/**
* Supports http/https:
*
* - art-collector.ataulm.com
* - art-collector.ataulm.com/{artist-id}
* - art-collector.ataulm.com/{artist-id}/{painting-id}
*/
class DeepLinkActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val uri = getIntentUri()
val segments = uri.pathSegments
val intent = when (uri.pathSegments.size) {
2 -> paintingIntent(segments[ARTIST], segments[PAINTING])
1 -> artistGalleryIntent(segments[ARTIST])
else -> galleryIntent()
}
startActivity(intent)
finish()
}

private fun getIntentUri() = intent.data!! // only way to open this activity
}
9 changes: 6 additions & 3 deletions app/src/main/java/com/ataulm/artcollector/Navigation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import android.net.Uri

private const val SCHEME = "https"
private const val AUTHORITY = "art-collector.ataulm.com"
private const val GALLERY = "${BuildConfig.APPLICATION_ID}.gallery.ui.GalleryActivity"
private const val ARTIST_GALLERY = "${BuildConfig.APPLICATION_ID}.artist.ui.ArtistActivity"
private const val PAINTING = "${BuildConfig.APPLICATION_ID}.painting.ui.PaintingActivity"

fun galleryIntent() = intent(GALLERY)

fun artistGalleryIntent(artistId: String): Intent {
val uri = Uri.Builder()
.scheme(SCHEME)
.authority(AUTHORITY)
.path(artistId)
.build()

return intent(uri, ARTIST_GALLERY)
return intent(ARTIST_GALLERY, uri)
}

fun paintingIntent(artistId: String, paintingId: String): Intent {
Expand All @@ -25,10 +28,10 @@ fun paintingIntent(artistId: String, paintingId: String): Intent {
.path("$artistId/$paintingId")
.build()

return intent(uri, PAINTING)
return intent(PAINTING, uri)
}

private fun intent(uri: Uri, componentName: String): Intent {
private fun intent(componentName: String, uri: Uri? = null): Intent {
return Intent(Intent.ACTION_VIEW)
.setClassName(BuildConfig.APPLICATION_ID, componentName)
.setData(uri)
Expand Down