Skip to content

Commit bb80676

Browse files
committed
Refactoring
1 parent e0b7173 commit bb80676

File tree

6 files changed

+155
-144
lines changed

6 files changed

+155
-144
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ apply plugin: 'com.android.library'
22
apply plugin: 'kotlin-android'
33
apply plugin: 'kotlin-android-extensions'
44
android {
5-
compileSdkVersion 30
6-
buildToolsVersion "30.0.2"
5+
compileSdkVersion 31
6+
buildToolsVersion "30.0.3"
77

88
defaultConfig {
99
minSdkVersion 19
10-
targetSdkVersion 30
10+
targetSdkVersion 31
1111
versionCode 1
1212
versionName "1.0"
1313

@@ -28,23 +28,17 @@ android {
2828

2929
dependencies {
3030
implementation fileTree(dir: 'libs', include: ['*.jar'])
31-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
3231

3332
//AndroidX
34-
implementation 'androidx.appcompat:appcompat:1.2.0'
35-
implementation 'com.google.android.material:material:1.2.1'
36-
implementation 'androidx.core:core-ktx:1.3.2'
33+
implementation 'androidx.appcompat:appcompat:1.4.1'
34+
implementation 'com.google.android.material:material:1.5.0'
35+
implementation 'androidx.core:core-ktx:1.7.0'
3736

3837
//Gson
39-
implementation 'com.google.code.gson:gson:2.8.6'
38+
api 'com.google.code.gson:gson:2.8.7'
4039
//Coroutines
41-
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
40+
api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
4241

4342
//OkHttp
44-
implementation 'com.squareup.okhttp3:okhttp:4.8.0'
45-
46-
//Test
47-
testImplementation 'junit:junit:4.13.1'
48-
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
49-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
43+
api 'com.squareup.okhttp3:okhttp:4.9.3'
5044
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.gapps.library">
3+
4+
<uses-permission android:name="android.permission.INTERNET" />
5+
6+
<application android:allowBackup="true">
7+
8+
</application>
9+
</manifest>
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.gapps.library.api
2+
3+
import android.content.Context
4+
import android.util.Log
5+
import com.gapps.library.api.models.api.base.VideoInfoModel
6+
import com.gapps.library.api.models.video.VideoPreviewModel
7+
import com.gapps.library.api.models.video.base.BaseVideoResponse
8+
import com.gapps.library.cache.getCachedVideoModel
9+
import com.gapps.library.cache.insertModel
10+
import com.gapps.library.utils.errors.ERROR_2
11+
import com.gapps.library.utils.errors.ERROR_3
12+
import com.google.gson.GsonBuilder
13+
import com.google.gson.JsonElement
14+
import com.google.gson.JsonParser.parseString
15+
import kotlinx.coroutines.*
16+
import okhttp3.OkHttpClient
17+
import okhttp3.Request
18+
import java.lang.reflect.Type
19+
import kotlin.coroutines.CoroutineContext
20+
21+
internal class VideoLoadHelper(
22+
private val context: Context?,
23+
private val client: OkHttpClient,
24+
private val isCacheEnabled: Boolean,
25+
private val isLogEnabled: Boolean,
26+
) : CoroutineScope {
27+
override val coroutineContext: CoroutineContext
28+
get() = SupervisorJob() + Dispatchers.Main
29+
30+
private val gson = GsonBuilder()
31+
.setLenient()
32+
.setPrettyPrinting()
33+
.create()
34+
35+
fun getVideoInfo(
36+
originalUrl: String?,
37+
videoInfoModel: VideoInfoModel<*>,
38+
onSuccess: (VideoPreviewModel) -> Unit,
39+
onError: (String, String) -> Unit,
40+
) {
41+
val finalUrl = videoInfoModel.getInfoUrl(originalUrl)
42+
val videoId = videoInfoModel.parseVideoId(originalUrl)
43+
44+
if (finalUrl == null || videoId == null) {
45+
onError.invoke(originalUrl ?: "null url", ERROR_3)
46+
47+
return
48+
}
49+
50+
val playLink = videoInfoModel.getPlayLink(videoId)
51+
52+
launch {
53+
if (isCacheEnabled) {
54+
if (context != null) {
55+
try {
56+
val model = getCachedVideoModel(context, playLink)
57+
58+
if (model != null) {
59+
onSuccess.invoke(model)
60+
return@launch
61+
}
62+
} catch (e: Exception) {
63+
e.printStackTrace()
64+
}
65+
}
66+
}
67+
try {
68+
val jsonBody = makeCallGetBody(client, finalUrl)
69+
70+
if (isLogEnabled) {
71+
Log.i(
72+
VideoService.TAG,
73+
"a response from $originalUrl:\n${gson.toJson(jsonBody)}"
74+
)
75+
}
76+
77+
if (jsonBody == null) {
78+
onSuccess.invoke(
79+
VideoPreviewModel.error(
80+
originalUrl,
81+
"$ERROR_2 \n---> Response is null"
82+
)
83+
)
84+
85+
return@launch
86+
}
87+
88+
val result = fromJson(jsonBody, videoInfoModel.type)
89+
.toPreview(
90+
url = originalUrl,
91+
linkToPlay = playLink,
92+
hostingName = videoInfoModel.hostingName,
93+
videoId = videoId
94+
)
95+
96+
onSuccess.invoke(result)
97+
98+
try {
99+
if (context != null && isCacheEnabled) {
100+
insertModel(context, result)
101+
}
102+
} catch (e: Exception) {
103+
onError.invoke(
104+
originalUrl
105+
?: "null url", "$ERROR_2\n---> ${e.localizedMessage}"
106+
)
107+
}
108+
} catch (e: Exception) {
109+
onError.invoke(
110+
originalUrl ?: "null url",
111+
"$ERROR_2 !!! \n---> ${e.localizedMessage}"
112+
)
113+
}
114+
}
115+
}
116+
117+
@Suppress("BlockingMethodInNonBlockingContext")
118+
private suspend fun makeCallGetBody(client: OkHttpClient, url: String) =
119+
withContext(Dispatchers.IO) {
120+
val response = client.newCall(Request.Builder().url(url).build()).execute()
121+
val stringBody = response.body?.string() ?: return@withContext null
122+
val jsonObject = parseString(stringBody)
123+
124+
return@withContext if (jsonObject.isJsonArray) {
125+
jsonObject.asJsonArray[0]
126+
} else {
127+
jsonObject
128+
}
129+
}
130+
131+
private fun fromJson(json: JsonElement?, type: Type?): BaseVideoResponse {
132+
return gson.fromJson(json, type)
133+
}
134+
}

library/src/main/AndroidManifest.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.

library/src/main/java/com/gapps/library/api/VideoLoadHelper.kt

Lines changed: 0 additions & 120 deletions
This file was deleted.

0 commit comments

Comments
 (0)