Untitled
unknown
java
2 years ago
10 kB
6
Indexable
package com.likefor.follows.ui;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import cn.pedant.SweetAlert.SweetAlertDialog;
import com.github.instagram4j.instagram4j.IGClient;
import com.github.instagram4j.instagram4j.actions.users.UserAction;
import com.github.instagram4j.instagram4j.actions.users.UsersAction;
import com.github.instagram4j.instagram4j.models.media.timeline.TimelineMedia;
import com.github.instagram4j.instagram4j.models.user.Profile;
import com.github.instagram4j.instagram4j.requests.feed.FeedTimelineRequest;
import com.github.instagram4j.instagram4j.requests.feed.FeedUserRequest;
import com.github.instagram4j.instagram4j.responses.accounts.AccountsUserResponse;
import com.github.instagram4j.instagram4j.responses.accounts.LoginResponse;
import com.github.instagram4j.instagram4j.responses.feed.FeedTimelineResponse;
import com.github.instagram4j.instagram4j.responses.feed.FeedUserResponse;
import com.github.instagram4j.instagram4j.responses.friendships.FriendshipStatusResponse;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.ValueEventListener;
import com.likefor.follows.Application;
import com.likefor.follows.R;
import com.likefor.follows.helpers.InstagramCallback;
import com.likefor.follows.models.User;
import com.likefor.follows.utils.Constants;
import com.likefor.follows.utils.FirebaseUtils;
import com.xcoder.easyinsta.Instagram;
import com.xcoder.easyinsta.exceptions.IGLoginException;
import com.xcoder.tasks.AsyncTask;
import es.dmoral.toasty.Toasty;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;
public class InstagramLoginActivity extends AppCompatActivity {
private EditText emailEditText,passwordEdittext;
private ProgressBar progressBar;
private static final String TAG = "InstagramLoginActivity";
private SweetAlertDialog dialog;
private final User modelUser = new User();
@Override
protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_instagram_login);
emailEditText = findViewById(R.id.email);
passwordEdittext = findViewById(R.id.psswd);
progressBar = findViewById(R.id.progress);
}
public void login(String username,String password){
showProgress(true);
dialog = new SweetAlertDialog(this,SweetAlertDialog.PROGRESS_TYPE);
dialog.setContentText("Loading...");
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
try {
File file = new File(getCacheDir(),Constants.COOKIE_NAME);
File client = new File(getCacheDir(),Constants.STATE_NAME);
Instagram instagram = Instagram.loginOrCache(new File(getCacheDir().toURI()),username,password);
instagram.profile().getUserInfo(username).setOnSuccessCallback(result -> {
Log.e(TAG, "login: 81" );
try {
Log.e(TAG, "login: 83" );
IGClient igClient = IGClient.builder().username(username).password(password).login();
igClient.serialize(file,client);
UsersAction userAction = new UsersAction(igClient);
Log.e(TAG, "login: 87" );
userAction.findByUsername(username).thenAccept(new Consumer<UserAction>() {
@Override
public void accept(UserAction userAction) {
Log.e(TAG, "login: 93" );
FeedUserRequest request = new FeedUserRequest(userAction.getUser().getPk());
igClient.sendRequest(request).thenAccept(new Consumer<FeedUserResponse>() {
@Override
public void accept(FeedUserResponse feedUserResponse) {
Log.e(TAG, "login: 98" );
List<TimelineMedia> userPosts = feedUserResponse.getItems();
modelUser.setFeeds(userPosts);
Log.d(TAG, "accept: "+userPosts);
runOnUiThread(() -> {
Log.e(TAG, "login: 102" );
modelUser.setIdUser(userAction.getUser().getPk());
modelUser.setFollowingCount(result.following);
modelUser.setFollowerCount(result.followers);
modelUser.setFullName(result.fullName);
modelUser.setUserName(result.username);
modelUser.setUrlImage(result.profilePicUrl);
FirebaseUtils.getRootUsers().child(String.valueOf(modelUser.getIdUser()))
.setValue(modelUser)
.addOnCompleteListener(task -> {
Log.e(TAG, "login: 113" );
if(task.isSuccessful()){
Log.e(TAG, "login: 115" );
startActivity(new Intent(InstagramLoginActivity.this,InstagramHomeActivity.class)
.putExtra(
Constants.USER_GSON,Application.objectToGson(modelUser)
));
}
Log.e(TAG, "login: 121" );
dialog.dismissWithAnimation();
});
});
}
}).join();
}
}).join();
} catch (IOException e) {
Log.e(TAG, "login: 131"+e.getMessage() );
dialog.dismissWithAnimation();
Toasty.error(InstagramLoginActivity.this, e.getMessage()+"Try to login instagram App.and back here!", Toasty.LENGTH_LONG).show();
}
});
dialog.dismissWithAnimation();
} catch (Exception e) {
dialog.dismissWithAnimation();
Log.e(TAG, "login: ",e );
}
}
private void getUserData(Instagram instagram) {
User modelUser = new User();
/*
client.actions().users().findByUsername(modelUser.getUserName()).thenAccept(userAction -> {
long pk = userAction.getUser().getPk();
FeedUserRequest request = new FeedUserRequest(pk);
client.sendRequest(request).thenAccept(feedUserResponse -> {
List<TimelineMedia> list = new ArrayList<>(feedUserResponse.getItems());
// FirebaseUtils.getRootPosts().setValue(list);*/
//modelUser.setFeeds(list);
FirebaseUtils.getRootUsers().child(modelUser.getUserName()).setValue(modelUser)
.addOnCompleteListener(task -> {
if(task.isSuccessful()){
runOnUiThread(() -> {
dialog.dismissWithAnimation();
startActivity(new Intent(InstagramLoginActivity.this,InstagramHomeActivity.class)
.putExtra(
Constants.USER_GSON,Application.objectToGson(modelUser)
));
});
}
});
}
public void clickLogin(View view){
if(checkEmailAndPassword(emailEditText.getText().toString(),passwordEdittext.getText().toString())){
login(emailEditText.getText().toString(),passwordEdittext.getText().toString());
}
}
public boolean checkEmailAndPassword(String email, String password) {
boolean isValid = true;
if (TextUtils.isEmpty(email)) {
emailEditText.setError("Invalid email address");
isValid = false;
showProgress(false);
dialog.dismissWithAnimation();
} else {
emailEditText.setError(null);
}
if (TextUtils.isEmpty(password) || password.length() < 6) {
passwordEdittext.setError("Password should be at least 6 characters long");
isValid = false;
} else {
passwordEdittext.setError(null);
}
return isValid;
}
private void showProgress(boolean show) {
if (show) {
progressBar.setVisibility(View.VISIBLE);
findViewById(R.id.login).setClickable(false);
} else {
progressBar.setVisibility(View.GONE);
findViewById(R.id.login).setClickable(true);
}
}
}
Editor is loading...