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

Commit

Permalink
Merge pull request #2 from SanjayDevTech/migrate-kotlin
Browse files Browse the repository at this point in the history
Migrated to kotlin
Replaced threads with coroutines
  • Loading branch information
SanjayDevTech authored Jan 6, 2021
2 parents 8936a5e + a44f7ea commit afa9500
Show file tree
Hide file tree
Showing 21 changed files with 533 additions and 457 deletions.
11 changes: 11 additions & 0 deletions .idea/dictionaries/admin_pc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
apply plugin: 'com.android.application'

plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
Expand All @@ -20,10 +22,16 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.12'
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sanjaydevtech.instarepost">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
Expand All @@ -11,6 +11,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SecondActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
73 changes: 0 additions & 73 deletions app/src/main/java/com/sanjaydevtech/instarepost/MainActivity.java

This file was deleted.

74 changes: 74 additions & 0 deletions app/src/main/java/com/sanjaydevtech/instarepost/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.sanjaydevtech.instarepost

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.sanjaydevtech.instautils.InstaDownloader
import com.sanjaydevtech.instautils.InstaScraper
import com.sanjaydevtech.instautils.InstaTask
import java.util.regex.Pattern

class MainActivity : AppCompatActivity() {
private lateinit var downloader: InstaDownloader
private lateinit var img: ImageView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

downloader = InstaDownloader(this) { instaTask ->
onResponse(instaTask)
}

val urlTxt: EditText = findViewById(R.id.urlTxt)
val doneBtn: Button = findViewById(R.id.doneBtn)
val testBtn: Button = findViewById(R.id.test_btn)
img = findViewById(R.id.imageView)

testBtn.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}

doneBtn.setOnClickListener {
var pattern = Pattern.compile(URL_PATTERN)
var matcher = pattern.matcher(urlTxt.text.toString())
if (matcher.find()) {
downloader.get(urlTxt.text.toString()) // Request the post data
} else {
pattern = Pattern.compile(DP_URL_PATTERN)
matcher = pattern.matcher(urlTxt.text.toString())
if (matcher.find()) {
InstaScraper.getDP(this@MainActivity, urlTxt.text.toString()) { instaTask ->
onResponse(instaTask)
}
} else {
Toast.makeText(this@MainActivity, "Invalid insta url", Toast.LENGTH_SHORT).show()
}
}
}
}

private fun onResponse(instaTask: InstaTask) {
val instaPost = instaTask.instaPost
if (instaPost != null) {
Log.d(TAG, instaPost.url)
// Retrieve the post object after request
downloader.setImage(instaPost, img)
Log.d(TAG, "Type: " + instaPost.type)
} else {
instaTask.exception!!.printStackTrace()
}
}

companion object {
private val TAG = MainActivity::class.java.simpleName
private const val URL_PATTERN = "^https://www.instagram.com/p/.+"
private const val DP_URL_PATTERN = "^(https://(www\\.)?instagram\\.com/[^p][0-9a-zA-Z_.]+)"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.sanjaydevtech.instarepost;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.sanjaydevtech.instautils.InstaDownloader;
import com.sanjaydevtech.instautils.InstaPost;
import com.sanjaydevtech.instautils.InstaResponse;
import com.sanjaydevtech.instautils.InstaScraper;
import com.sanjaydevtech.instautils.InstaTask;

import org.jetbrains.annotations.NotNull;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SecondActivity extends AppCompatActivity {

private InstaDownloader downloader; // Declare the InstaDownloader
private static final String TAG = MainActivity.class.getSimpleName();
private static final String URL_PATTERN = "^https://www.instagram.com/p/.+";
private static final String DP_URL_PATTERN = "^(https://(www\\.)?instagram\\.com/[^p][0-9a-zA-Z_.]+)";
private ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

downloader = new InstaDownloader(this, instaTask ->
onResponseMethod(instaTask)
);

final EditText urlTxt = findViewById(R.id.urlTxt);
final Button doneBtn = findViewById(R.id.doneBtn);
img = findViewById(R.id.imageView);

doneBtn.setOnClickListener(view -> {
Pattern pattern = Pattern.compile(URL_PATTERN);
Matcher matcher = pattern.matcher(urlTxt.getText().toString());
if (matcher.find()) {
downloader.get(urlTxt.getText().toString()); // Request the post data
} else {
pattern = Pattern.compile(DP_URL_PATTERN);
matcher = pattern.matcher(urlTxt.getText().toString());
if (matcher.find()) {
InstaScraper.getDP(SecondActivity.this, urlTxt.getText().toString(), instaTask ->
onResponseMethod(instaTask)
);
} else {
Toast.makeText(SecondActivity.this, "Invalid insta url", Toast.LENGTH_SHORT).show();
}
}

});

}

private void onResponseMethod(@NotNull InstaTask instaTask) {
InstaPost instaPost = instaTask.getInstaPost();
if (instaPost != null) {
Log.d(TAG, instaPost.getUrl());
// Retrieve the post object after request
downloader.setImage(instaPost, img);
Log.d(TAG, "Type: " + instaPost.getType());
} else {
instaTask.getException().printStackTrace();
}
}

}
13 changes: 12 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,22 @@
android:layout_alignParentRight="true"
android:text="Done" />

<Button
android:id="@+id/test_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/doneBtn"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Test Java Activity" />

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/doneBtn"
android:layout_below="@id/test_btn"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/res/layout/activity_second.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">


<EditText
android:id="@+id/urlTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:ems="10"
android:hint="Instagram URL"
android:inputType="textUri" />

<Button
android:id="@+id/doneBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/urlTxt"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Done" />

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/doneBtn"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:scaleType="fitCenter"
tools:srcCompat="@tools:sample/avatars"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.21"
repositories {
google()
jcenter()
Expand All @@ -8,6 +9,7 @@ buildscript {
classpath 'com.android.tools.build:gradle:4.1.1'
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"
classpath "com.github.dcendents:android-maven-gradle-plugin:2.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -19,6 +21,7 @@ allprojects {
google()
jcenter()
}
tasks.withType(Javadoc).all { enabled = false }
}

task clean(type: Delete) {
Expand Down
Loading

0 comments on commit afa9500

Please sign in to comment.