Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit 0057edb

Browse files
authored
Merge pull request #110 from amardeshbd/add-technical-resource-type
[REFACTOR] Added support for blog URL by removing hard coded logic for youtube preview.
2 parents c4a9a7e + e25ea84 commit 0057edb

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

app/src/main/java/com/hossainkhan/android/demo/data/ResourceInfo.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
package com.hossainkhan.android.demo.data
1818

19+
import android.net.Uri
1920
import com.google.firebase.Timestamp
20-
import java.util.*
21+
import java.util.Date
2122

2223

2324
/**
@@ -50,4 +51,13 @@ data class ResourceInfo(
5051
val date = publish_date?.toDate() ?: Date()
5152
return FirestoreDateFormatter.date(date)
5253
}
54+
55+
val isYouTubeUrl: Boolean = url.contains("youtube.com/watch?v=", ignoreCase = true)
56+
57+
/**
58+
* Provides [Uri] for directly invoking youtube app via Android Intent.
59+
* @throws IllegalStateException if the URL is not for youtube. Check [isYouTubeUrl] first.
60+
*/
61+
val youtubeIntentUri: String
62+
get() = if (isYouTubeUrl) ("vnd.youtube:" + url.split("=")[1]) else throw IllegalStateException("Not a YouTube URL.")
5363
}

app/src/main/java/com/hossainkhan/android/demo/ui/resource/LearningResourceActivity.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,18 @@ class LearningResourceActivity : AppCompatActivity() {
8585
}
8686

8787
private fun openContent(resourceInfo: ResourceInfo) {
88-
// TODO: Update logic later when the URL is not only youtube.
89-
val intentApp = Intent(Intent.ACTION_VIEW,
90-
Uri.parse("vnd.youtube:" + resourceInfo.url.split("=")[1]))
88+
val urlPreviewUri = Uri.parse(resourceInfo.url) // Generic URL preview using any supported app.
89+
90+
val previewContentIntent = if (resourceInfo.isYouTubeUrl) {
91+
Intent(Intent.ACTION_VIEW, Uri.parse(resourceInfo.youtubeIntentUri))
92+
} else {
93+
Intent(Intent.ACTION_VIEW, urlPreviewUri)
94+
}
9195

92-
val intentBrowser = Intent(Intent.ACTION_VIEW, Uri.parse(resourceInfo.url))
9396
try {
94-
startActivity(intentApp)
97+
startActivity(previewContentIntent)
9598
} catch (ex: ActivityNotFoundException) {
96-
startActivity(intentBrowser)
99+
startActivity(Intent(Intent.ACTION_VIEW, urlPreviewUri))
97100
}
98101
}
99102
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2019 Hossain Khan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hossainkhan.android.demo.data
18+
19+
import org.junit.Assert.assertEquals
20+
import org.junit.Assert.assertFalse
21+
import org.junit.Assert.assertTrue
22+
import org.junit.Test
23+
24+
/**
25+
* Tests [ResourceInfo].
26+
*/
27+
class ResourceInfoTest {
28+
29+
@Test
30+
fun isYouTubeUrl_givenValidUrl_returnsTrue() {
31+
val resourceInfoYoutube = ResourceInfo(url = "https://www.youtube.com/watch?v=29gLA90m6Gk")
32+
33+
assertTrue(resourceInfoYoutube.isYouTubeUrl)
34+
}
35+
36+
@Test
37+
fun isYouTubeUrl_givenInvalidUrl_returnsFalse() {
38+
val resourceInfoYoutube = ResourceInfo(url = "https://hossainkhan.com/watch?v=29gLA90m6Gk")
39+
40+
assertFalse(resourceInfoYoutube.isYouTubeUrl)
41+
}
42+
43+
@Test
44+
fun getYoutubeIntentUri_givenValidUrl_providesYoutubeIntentUri() {
45+
val resourceInfoYoutube = ResourceInfo(url = "https://www.youtube.com/watch?v=29gLA90m6Gk")
46+
47+
assertEquals("vnd.youtube:29gLA90m6Gk", resourceInfoYoutube.youtubeIntentUri)
48+
}
49+
50+
@Test(expected = IllegalStateException::class)
51+
fun getYoutubeIntentUri_givenInvalidUrl_throwsException() {
52+
val resourceInfoYoutube = ResourceInfo(url = "https://hossainkhan.com")
53+
54+
resourceInfoYoutube.youtubeIntentUri
55+
}
56+
}

0 commit comments

Comments
 (0)