Skip to content
This repository was archived by the owner on Oct 27, 2023. It is now read-only.

Commit a44f7ea

Browse files
committed
Migrated to Kotlin
* Upgraded java to 1.8 * Added support for Fragments * Made InstaImage.kt as functional Interface
1 parent a0e6045 commit a44f7ea

File tree

8 files changed

+99
-66
lines changed

8 files changed

+99
-66
lines changed

app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ android {
2222
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
2323
}
2424
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
2529
}
2630

2731
dependencies {

app/src/main/java/com/sanjaydevtech/instarepost/MainActivity.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import android.widget.ImageView
99
import android.widget.Toast
1010
import androidx.appcompat.app.AppCompatActivity
1111
import com.sanjaydevtech.instautils.InstaDownloader
12-
import com.sanjaydevtech.instautils.InstaResponse
1312
import com.sanjaydevtech.instautils.InstaScraper
1413
import com.sanjaydevtech.instautils.InstaTask
1514
import java.util.regex.Pattern

app/src/main/java/com/sanjaydevtech/instarepost/SecondActivity.java

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package com.sanjaydevtech.instarepost;
22

3-
import androidx.appcompat.app.AppCompatActivity;
4-
53
import android.os.Bundle;
64
import android.util.Log;
7-
import android.view.View;
85
import android.widget.Button;
96
import android.widget.EditText;
107
import android.widget.ImageView;
118
import android.widget.Toast;
129

10+
import androidx.appcompat.app.AppCompatActivity;
11+
1312
import com.sanjaydevtech.instautils.InstaDownloader;
1413
import com.sanjaydevtech.instautils.InstaPost;
1514
import com.sanjaydevtech.instautils.InstaResponse;
@@ -34,39 +33,31 @@ protected void onCreate(Bundle savedInstanceState) {
3433
super.onCreate(savedInstanceState);
3534
setContentView(R.layout.activity_second);
3635

37-
downloader = new InstaDownloader(this, new InstaResponse() {
38-
@Override
39-
public void onResponse(@NotNull InstaTask instaTask) {
40-
onResponseMethod(instaTask);
41-
}
42-
});
36+
downloader = new InstaDownloader(this, instaTask ->
37+
onResponseMethod(instaTask)
38+
);
4339

4440
final EditText urlTxt = findViewById(R.id.urlTxt);
4541
final Button doneBtn = findViewById(R.id.doneBtn);
4642
img = findViewById(R.id.imageView);
4743

48-
doneBtn.setOnClickListener(new View.OnClickListener() {
49-
@Override
50-
public void onClick(View view) {
51-
Pattern pattern = Pattern.compile(URL_PATTERN);
52-
Matcher matcher = pattern.matcher(urlTxt.getText().toString());
44+
doneBtn.setOnClickListener(view -> {
45+
Pattern pattern = Pattern.compile(URL_PATTERN);
46+
Matcher matcher = pattern.matcher(urlTxt.getText().toString());
47+
if (matcher.find()) {
48+
downloader.get(urlTxt.getText().toString()); // Request the post data
49+
} else {
50+
pattern = Pattern.compile(DP_URL_PATTERN);
51+
matcher = pattern.matcher(urlTxt.getText().toString());
5352
if (matcher.find()) {
54-
downloader.get(urlTxt.getText().toString()); // Request the post data
53+
InstaScraper.getDP(SecondActivity.this, urlTxt.getText().toString(), instaTask ->
54+
onResponseMethod(instaTask)
55+
);
5556
} else {
56-
pattern = Pattern.compile(DP_URL_PATTERN);
57-
matcher = pattern.matcher(urlTxt.getText().toString());
58-
if(matcher.find()) {
59-
InstaScraper.getDP(SecondActivity.this, urlTxt.getText().toString(), new InstaResponse() {
60-
@Override
61-
public void onResponse(@NotNull InstaTask instaTask) {
62-
onResponseMethod(instaTask);
63-
}
64-
});
65-
} else {
66-
Toast.makeText(SecondActivity.this, "Invalid insta url", Toast.LENGTH_SHORT).show();
67-
}
57+
Toast.makeText(SecondActivity.this, "Invalid insta url", Toast.LENGTH_SHORT).show();
6858
}
6959
}
60+
7061
});
7162

7263
}

instautils/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ android {
5353
}
5454

5555
dependencies {
56-
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
56+
api "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
5757
implementation 'androidx.core:core-ktx:1.3.2'
5858
def coroutines_version = "1.4.2"
59-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
60-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
59+
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
60+
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
6161
def lifecycle_version = "2.2.0"
62-
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
62+
api "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
6363
implementation fileTree(dir: "libs", include: ["*.jar"])
6464
implementation 'androidx.appcompat:appcompat:1.2.0'
6565
testImplementation 'junit:junit:4.12'

instautils/src/main/java/com/sanjaydevtech/instautils/InstaDownloader.kt

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.sanjaydevtech.instautils
22

3+
import android.content.Context
34
import android.graphics.Bitmap
45
import android.graphics.drawable.Drawable
6+
import android.util.Log
57
import android.widget.ImageView
8+
import androidx.fragment.app.Fragment
69
import androidx.fragment.app.FragmentActivity
710
import androidx.lifecycle.lifecycleScope
811
import com.bumptech.glide.Glide
912
import com.bumptech.glide.request.target.CustomTarget
1013
import com.bumptech.glide.request.transition.Transition
14+
import kotlinx.coroutines.CoroutineScope
1115
import kotlinx.coroutines.Dispatchers
1216
import kotlinx.coroutines.launch
1317
import kotlinx.coroutines.withContext
@@ -18,23 +22,20 @@ import java.io.IOException
1822
* Downloader Class to download insta posts
1923
*
2024
* @author Sanjay Developer
21-
* @version 1.0.2
25+
* @version 1.1.0
2226
*/
23-
class InstaDownloader(private val activity: FragmentActivity, private val response: InstaResponse) {
27+
class InstaDownloader(private val context: Context?, private val scope: CoroutineScope, private val response: InstaResponse) {
2428

25-
constructor(activity: FragmentActivity, response: (InstaTask) -> Unit) : this(activity, object : InstaResponse {
26-
override fun onResponse(instaTask: InstaTask) {
27-
response(instaTask)
28-
}
29-
})
29+
constructor(activity: FragmentActivity, response: InstaResponse) : this(activity, activity.lifecycleScope, response)
30+
constructor(fragment: Fragment, response: InstaResponse) : this(fragment.context, fragment.viewLifecycleOwner.lifecycleScope, response)
3031

3132
/**
3233
* Get the url of the post
3334
*
3435
* @param url URL of that post
3536
*/
3637
fun get(url: String) {
37-
activity.lifecycleScope.launch(Dispatchers.IO) {
38+
scope.launch(Dispatchers.IO) {
3839
try {
3940
val document = Jsoup.connect(url).userAgent("Mozilla/5.0").get()
4041
val metas = document.getElementsByTag("meta")
@@ -78,6 +79,15 @@ class InstaDownloader(private val activity: FragmentActivity, private val respon
7879
}
7980
}
8081

82+
/**
83+
* A Kotlin Extension function to set image
84+
*
85+
* @param post InstaPost instance
86+
*/
87+
fun ImageView.setImage(post: InstaPost) {
88+
setImage(post, this)
89+
}
90+
8191
/**
8292
* Retrieve the bitmap of the image post or thumbnail of video post
8393
*
@@ -86,16 +96,20 @@ class InstaDownloader(private val activity: FragmentActivity, private val respon
8696
*/
8797
fun getBitmap(post: InstaPost, instaImage: InstaImage) {
8898
val imgUrl = post.thumbnailUrl
89-
Glide.with(activity)
90-
.asBitmap()
91-
.load(imgUrl)
92-
.into(object : CustomTarget<Bitmap?>() {
93-
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap?>?) {
94-
instaImage.onBitmapLoaded(resource)
95-
}
99+
context?.let {
100+
Glide.with(it)
101+
.asBitmap()
102+
.load(imgUrl)
103+
.into(object : CustomTarget<Bitmap?>() {
104+
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap?>?) {
105+
instaImage.onBitmapLoaded(resource)
106+
}
96107

97-
override fun onLoadCleared(placeholder: Drawable?) {}
98-
})
108+
override fun onLoadCleared(placeholder: Drawable?) {}
109+
})
110+
} ?: run {
111+
Log.e(TAG, "method: getBitmap can't set image.. [REASON] Context is null")
112+
}
99113
}
100114

101115
/**
@@ -106,12 +120,16 @@ class InstaDownloader(private val activity: FragmentActivity, private val respon
106120
*/
107121
fun setImage(post: InstaPost, imageView: ImageView) {
108122
val imgUrl = post.thumbnailUrl
109-
Glide.with(activity)
110-
.load(imgUrl)
111-
.into(imageView)
123+
context?.let {
124+
Glide.with(it)
125+
.load(imgUrl)
126+
.into(imageView)
127+
} ?: run {
128+
Log.e(TAG, "method: setImage can't set image.. [REASON] Context is null")
129+
}
112130
}
113131

114132
companion object {
115-
private val TAG = InstaDownloader::class.java.simpleName
133+
private val TAG = InstaDownloader::class.simpleName
116134
}
117135
}

instautils/src/main/java/com/sanjaydevtech/instautils/InstaImage.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package com.sanjaydevtech.instautils
22

33
import android.graphics.Bitmap
44

5-
/**
6-
* InstaImage
5+
/** InstaImage
76
*/
8-
interface InstaImage {
7+
fun interface InstaImage {
98
/**
109
* OnBitmap loaded
1110
*

instautils/src/main/java/com/sanjaydevtech/instautils/InstaPost.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@ package com.sanjaydevtech.instautils
22

33
/**
44
* InstaPost Object
5+
*
6+
* [url] holds the actual image or video url
7+
*
8+
* [type] may be constants [INSTA_IMAGE] or [INSTA_VIDEO] or [INSTA_PROFILE]
9+
*
10+
* [thumbnailUrl] is the same as url for type [INSTA_IMAGE],
11+
* but a low res image for [INSTA_VIDEO] and [INSTA_PROFILE]
512
*/
613
data class InstaPost internal constructor(
714
val url: String,
815
val type: Int,
916
val thumbnailUrl: String) {
1017

1118
companion object {
19+
/** InstaPost object is from a Image */
1220
const val INSTA_IMAGE = 0
21+
22+
/** InstaPost object is from a Video */
1323
const val INSTA_VIDEO = 1
24+
25+
/** InstaPost object is from a Instagram Profile */
1426
const val INSTA_PROFILE = 2
1527
}
1628
}

instautils/src/main/java/com/sanjaydevtech/instautils/InstaScraper.kt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.sanjaydevtech.instautils
22

3+
import androidx.fragment.app.Fragment
34
import androidx.fragment.app.FragmentActivity
45
import androidx.lifecycle.lifecycleScope
6+
import kotlinx.coroutines.CoroutineScope
57
import kotlinx.coroutines.Dispatchers
68
import kotlinx.coroutines.launch
79
import kotlinx.coroutines.withContext
@@ -18,26 +20,34 @@ object InstaScraper {
1820
private const val PROFILE_HD_PATTERN = "\"profile_pic_url_hd\":\"([^\"]*)\""
1921
private const val PROFILE_PATTERN = "\"profile_pic_url\":\"([^\"]*)\""
2022

23+
/**
24+
* To retrieve Instagram profile
25+
*
26+
* @param activity Activity
27+
* @param url Url of the instagram profile
28+
* @param response InstaResponse instance
29+
*/
2130
@JvmStatic
22-
fun getDP(activity: FragmentActivity, url: String, response: (InstaTask) -> Unit) {
23-
getDP(activity, url, object : InstaResponse {
24-
override fun onResponse(instaTask: InstaTask) {
25-
response(instaTask)
26-
}
27-
})
31+
fun getDP(activity: FragmentActivity, url: String, response: InstaResponse) {
32+
getDP(activity.lifecycleScope, url, response)
2833
}
2934

3035
/**
3136
* To retrieve Instagram profile
3237
*
33-
* @param activity Current activity
38+
* @param fragment Fragment
3439
* @param url Url of the instagram profile
3540
* @param response InstaResponse instance
36-
* @throws IllegalArgumentException Throws if no InstaResponse is attached
3741
*/
3842
@JvmStatic
39-
fun getDP(activity: FragmentActivity, url: String, response: InstaResponse) {
40-
activity.lifecycleScope.launch(Dispatchers.IO) {
43+
fun getDP(fragment: Fragment, url: String, response: InstaResponse) {
44+
getDP(fragment.viewLifecycleOwner.lifecycleScope, url, response)
45+
}
46+
47+
48+
@JvmStatic
49+
private fun getDP(scope: CoroutineScope, url: String, response: InstaResponse) {
50+
scope.launch(Dispatchers.IO) {
4151
try {
4252
val document = Jsoup.connect(url).userAgent("Mozilla/5.0").get()
4353
val scripts = document.getElementsByTag("script")

0 commit comments

Comments
 (0)