Skip to content

Commit

Permalink
Merge branch 'development' into pintrest
Browse files Browse the repository at this point in the history
  • Loading branch information
Suneet Srivastava committed Nov 4, 2019
2 parents 0fcf64f + 9d671ac commit e0aa001
Show file tree
Hide file tree
Showing 26 changed files with 856 additions and 860 deletions.
6 changes: 4 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ android {

configurations {
implementation.exclude group: 'org.jetbrains', module: 'annotations'
all*.exclude group: 'com.android.volley'
}

lintOptions {
Expand Down Expand Up @@ -146,7 +145,10 @@ dependencies {
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'

implementation 'com.mcxiaoke.volley:library:1.0.19'
//retrofit
implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofitVersion"
implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofitVersion"
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.0'

//google and support
implementation "androidx.appcompat:appcompat:$rootProject.supportLibraryVersion"
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/org/fossasia/phimpme/MyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.util.Log;
import androidx.multidex.MultiDex;

import com.android.volley.VolleyLog;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.twitter.sdk.android.core.DefaultLogger;
Expand Down Expand Up @@ -56,7 +55,6 @@ public void onCreate() {
.debug(true)
.build();
Twitter.initialize(config);
VolleyLog.DEBUG = true;
/** Realm initialization */
Realm.init(this);
RealmConfiguration realmConfiguration =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
package org.fossasia.phimpme.accounts;

import static org.fossasia.phimpme.R.string.no_account_signed_in;
import static org.fossasia.phimpme.data.local.AccountDatabase.AccountName.BOX;
import static org.fossasia.phimpme.utilities.Constants.BOX_CLIENT_ID;
import static org.fossasia.phimpme.utilities.Constants.BOX_CLIENT_SECRET;
import static org.fossasia.phimpme.utilities.Constants.SUCCESS;
import static org.fossasia.phimpme.utilities.Utils.checkNetwork;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.SwitchCompat;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.box.androidsdk.content.BoxConfig;
import com.box.androidsdk.content.auth.BoxAuthentication;
import com.box.androidsdk.content.models.BoxSession;
import com.dropbox.core.android.Auth;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.twitter.sdk.android.core.identity.TwitterAuthClient;

import io.realm.RealmQuery;
import org.fossasia.phimpme.R;
import org.fossasia.phimpme.base.PhimpmeProgressBarHandler;
import org.fossasia.phimpme.base.RecyclerItemClickListner;
Expand All @@ -44,17 +51,6 @@
import org.fossasia.phimpme.utilities.SnackBarHandler;
import org.jetbrains.annotations.NotNull;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.realm.RealmQuery;

import static org.fossasia.phimpme.R.string.no_account_signed_in;
import static org.fossasia.phimpme.data.local.AccountDatabase.AccountName.BOX;
import static org.fossasia.phimpme.utilities.Constants.BOX_CLIENT_ID;
import static org.fossasia.phimpme.utilities.Constants.BOX_CLIENT_SECRET;
import static org.fossasia.phimpme.utilities.Constants.SUCCESS;
import static org.fossasia.phimpme.utilities.Utils.checkNetwork;

/** Created by pa1pal on 13/6/17. */
public class AccountActivity extends ThemedActivity
implements RecyclerItemClickListner.OnItemClickListener {
Expand Down
283 changes: 116 additions & 167 deletions app/src/main/java/org/fossasia/phimpme/accounts/AccountRepository.java
Original file line number Diff line number Diff line change
@@ -1,186 +1,135 @@
package org.fossasia.phimpme.accounts;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Base64;
import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;

import io.realm.Realm;
import io.realm.RealmQuery;

import java.io.ByteArrayOutputStream;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import org.fossasia.phimpme.data.local.AccountDatabase;
import org.fossasia.phimpme.data.local.DatabaseHelper;
import org.fossasia.phimpme.share.pinterest.PinterestBoardsResp;
import org.fossasia.phimpme.share.pinterest.PinterestUploadImgResp;
import org.fossasia.phimpme.utilities.BasicCallBack;
import org.fossasia.phimpme.utilities.Constants;
import org.fossasia.phimpme.utilities.MultipartRequest;
import org.fossasia.phimpme.utilities.PinterestApi;
import org.fossasia.phimpme.utilities.RetrofitClient;
import org.fossasia.phimpme.utilities.Utils;
import org.fossasia.phimpme.utilities.VolleyClient;
import org.json.JSONException;
import org.json.JSONObject;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* Created by @codedsun on 09/Oct/2019
*/
/** Created by @codedsun on 09/Oct/2019 */
class AccountRepository {

private Realm realm = Realm.getDefaultInstance();
private DatabaseHelper databaseHelper = new DatabaseHelper(realm);

// Fetches the details of all accounts
RealmQuery<AccountDatabase> fetchAllAccounts() {
return databaseHelper.fetchAccountDetails();
}

// saves username, password and serverUrl for an account in database
void saveUsernamePasswordServerUrlForAccount(
AccountDatabase.AccountName accountName, String serverUrl, String username, String password) {
realm.beginTransaction();
AccountDatabase account = realm.createObject(AccountDatabase.class, accountName.toString());
account.setServerUrl(serverUrl);
account.setUsername(username);
account.setPassword(password);
realm.commitTransaction();
}

// saves username and token for an account in database
void saveUsernameAndToken(
AccountDatabase.AccountName accountName, String username, String token) {
realm.beginTransaction();
AccountDatabase account = realm.createObject(AccountDatabase.class, accountName.toString());
account.setUsername(username);
account.setToken(token);
realm.commitTransaction();
}

// deletes an account from database
void deleteAccount(String accountName) {
databaseHelper.deleteSignedOutAccount(accountName);
}

// void uploadImageToPinterest(BasicCallBack callBack, Context context, String imagePath, String note, String board) {
// AccountDatabase pinterestAccount = databaseHelper.getAccountByName(AccountDatabase.AccountName.PINTEREST.name());
// if (pinterestAccount != null && pinterestAccount.getToken() != null) {
// JSONObject params = new JSONObject();
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Bitmap bitmap = Utils.getBitmapFromPath(imagePath);
// int numPixels = bitmap.getHeight() * bitmap.getWidth();
// if(numPixels > 3150000) {
// Log.e("sUneet - photo error","Image too large");
// return;
// }
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
// String b64Str = Base64.encodeToString(baos.toByteArray(), Base64.NO_WRAP);
// try {
// params.put("image_base64", b64Str);
// params.put("note", note);
// params.put("board", "suneetbond/vrindavan");
// } catch (JSONException e) {
// e.printStackTrace();
// }
// JsonObjectRequest uploadImageRequest = new JsonObjectRequest(Request.Method.POST,Constants.PINTEREST_POST_CREATE_PIN,params, response -> {
// VolleyLog.e("Suneet - reponse",response.toString());
// },
// error -> {
// VolleyLog.e("Suneet - error",error.toString());
// });
//
// uploadImageRequest.setRetryPolicy(new DefaultRetryPolicy( 50000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// VolleyClient.getInstance(context).getRequestQueue().add(uploadImageRequest);
//
// }else{
// //No account found for pinterest
// callBack.callBack(Constants.FAIL, null);
// }
//
//
// }

void uploadImageToPinterest(BasicCallBack callBack, Context context, String imagePath, String note, String board) {
AccountDatabase pinterestAccount = databaseHelper.getAccountByName(AccountDatabase.AccountName.PINTEREST.name());
if (pinterestAccount != null && pinterestAccount.getToken() != null) {
byte[] byteFile = new byte[]{};
try {
byteFile = Files.readAllBytes(Paths.get(imagePath));
} catch (IOException e) {
e.printStackTrace();
}
MultipartRequest multipartRequest = new MultipartRequest();
MultipartRequest.MultipartRequestBuild uploadRequst = multipartRequest.sendMultipartRequest(Request.Method.POST,
Constants.PINTEREST_POST_CREATE_PIN, byteFile, null, "Suneetsro", new Response.Listener<NetworkResponse>() {
@Override
public void onResponse(NetworkResponse response) {
VolleyLog.e("suneet - repsonse");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("suneet - error");
}
});
uploadRequst.setRetryPolicy(new DefaultRetryPolicy(50000 , 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleyClient.getInstance(context).getRequestQueue().add(uploadRequst);

}else{
//No account found for pinterest
callBack.callBack(Constants.FAIL, null);
}


}

//get pinterest boards of user
void getPinterestBoards(BasicCallBack callBack, Context context) {
AccountDatabase pinterestAccount = databaseHelper.getAccountByName(AccountDatabase.AccountName.PINTEREST.name());
if (pinterestAccount != null && pinterestAccount.getToken() != null) {
JsonObjectRequest pinterestBoardsRequest = new JsonObjectRequest(Request.Method.GET, Constants.PINTEREST_GET_USER_BOARDS + pinterestAccount.getToken(),
response -> {
callBack.callBack(Constants.SUCCESS, response);
}, error -> {
callBack.callBack(Constants.FAIL, error);
});
VolleyClient.getInstance(context).getRequestQueue().add(pinterestBoardsRequest);
} else {
//No account found for pinterest
callBack.callBack(Constants.FAIL, null);
}
private Realm realm = Realm.getDefaultInstance();
private DatabaseHelper databaseHelper = new DatabaseHelper(realm);
private PinterestApi pinterestApi =
RetrofitClient.getRetrofitClient(Constants.PINTEREST_BASE_URL).create(PinterestApi.class);

// Fetches the details of all accounts
RealmQuery<AccountDatabase> fetchAllAccounts() {
return databaseHelper.fetchAccountDetails();
}

// saves username, password and serverUrl for an account in database
void saveUsernamePasswordServerUrlForAccount(
AccountDatabase.AccountName accountName, String serverUrl, String username, String password) {
realm.beginTransaction();
AccountDatabase account = realm.createObject(AccountDatabase.class, accountName.toString());
account.setServerUrl(serverUrl);
account.setUsername(username);
account.setPassword(password);
realm.commitTransaction();
}

// saves username and token for an account in database
void saveUsernameAndToken(
AccountDatabase.AccountName accountName, String username, String token) {
realm.beginTransaction();
AccountDatabase account = realm.createObject(AccountDatabase.class, accountName.toString());
account.setUsername(username);
account.setToken(token);
realm.commitTransaction();
}

// deletes an account from database
void deleteAccount(String accountName) {
databaseHelper.deleteSignedOutAccount(accountName);
}

void uploadImageToPinterest(BasicCallBack callBack, String imagePath, String note, String board) {
AccountDatabase pinterestAccount =
databaseHelper.getAccountByName(AccountDatabase.AccountName.PINTEREST.name());
if (pinterestAccount != null && pinterestAccount.getToken() != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = Utils.getBitmapFromPath(imagePath);
int numPixels = bitmap.getHeight() * bitmap.getWidth();
if (numPixels > 3150000) {
PinterestUploadImgResp resp = new PinterestUploadImgResp();
resp.setMessage("Image Size too large");
callBack.callBack(Constants.FAIL, resp);
return;
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
RequestBody reqFile =
RequestBody.create(baos.toByteArray(), MediaType.parse("multipart/form-data"));
MultipartBody.Part multipartBody =
MultipartBody.Part.createFormData("image", imagePath, reqFile);
pinterestApi
.uploadImageToPinterest(pinterestAccount.getToken(), note, board, multipartBody)
.enqueue(
new Callback<PinterestUploadImgResp>() {
@Override
public void onResponse(
Call<PinterestUploadImgResp> call, Response<PinterestUploadImgResp> response) {
if (response.isSuccessful()) {
callBack.callBack(Constants.SUCCESS, response.body());
} else {
PinterestUploadImgResp resp = new PinterestUploadImgResp();
resp.setMessage(response.message());
callBack.callBack(Constants.FAIL, resp);
}
}

@Override
public void onFailure(Call<PinterestUploadImgResp> call, Throwable t) {
PinterestUploadImgResp resp = new PinterestUploadImgResp();
resp.setMessage(t.toString());
callBack.callBack(Constants.FAIL, resp);
}
});
} else {
// No account found for pinterest
callBack.callBack(Constants.FAIL, null);
}

//Creates Pinterest Board
void createBoard(String name, Context context, BasicCallBack basicCallBack) {
JSONObject params = new JSONObject();
try {
params.put("name", name);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest createBoardRequest = new JsonObjectRequest(Request.Method.POST, Constants.PINTEREST_POST_CREATE_BOARD, params,
response -> {

},
error -> {
}

// get pinterest boards of user
void getPinterestBoards(BasicCallBack callBack) {
AccountDatabase pinterestAccount =
databaseHelper.getAccountByName(AccountDatabase.AccountName.PINTEREST.name());
if (pinterestAccount != null && pinterestAccount.getToken() != null) {
pinterestApi
.getUserBoards(pinterestAccount.getToken())
.enqueue(
new Callback<PinterestBoardsResp>() {
@Override
public void onResponse(
Call<PinterestBoardsResp> call, Response<PinterestBoardsResp> response) {
if (response.body() != null && response.isSuccessful()) {
callBack.callBack(Constants.SUCCESS, response.body());
} else {
callBack.callBack(Constants.FAIL, "Unable to get Boards");
}
}

);

VolleyClient.getInstance(context).getRequestQueue().add(createBoardRequest);
@Override
public void onFailure(Call<PinterestBoardsResp> call, Throwable t) {
callBack.callBack(Constants.FAIL, null);
}
});
}

}
}
Loading

0 comments on commit e0aa001

Please sign in to comment.