Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Pinterest Auth with upload #2892

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ android {
vectorDrawables.useSupportLibrary = true
resConfigs "en"
ndk {
abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}
renderscriptTargetApi 19
renderscriptSupportModeEnabled true
Expand Down Expand Up @@ -134,6 +134,7 @@ spotless {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
Expand Down Expand Up @@ -236,9 +237,6 @@ dependencies {
//opencv
implementation project(':openCV')

//pdk
implementation 'com.github.forkerknights:android-pdk:1.2b'

//compressor
implementation 'id.zelory:compressor:2.1.0'

Expand Down
13 changes: 11 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<uses-library
android:name="org.apache.http.legacy"
android:required="false" />

<activity
android:name=".share.twitter.LoginActivity"
android:screenOrientation="portrait">
Expand Down Expand Up @@ -392,8 +397,12 @@
android:name="io.fabric.ApiKey"
android:value="78c6ec5c18fcd10ffbfd28ae038f3311f2763889" />

<activity android:name=".editor.CompressImageActivity"></activity>
<activity android:name=".trashbin.TrashBinActivity"></activity>
<activity android:name=".editor.CompressImageActivity" />
<activity android:name=".trashbin.TrashBinActivity" />
<activity android:name=".share.pinterest.PinterestAuthActivity"
android:screenOrientation="portrait" />
<activity android:name=".share.pinterest.PinterestShareActivity"
android:screenOrientation="portrait"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package org.fossasia.phimpme.accounts;

import static com.pinterest.android.pdk.PDKClient.setDebugMode;
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.PINTEREST_APP_ID;
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.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -31,13 +28,8 @@
import com.box.androidsdk.content.models.BoxSession;
import com.dropbox.core.android.Auth;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.pinterest.android.pdk.PDKCallback;
import com.pinterest.android.pdk.PDKClient;
import com.pinterest.android.pdk.PDKException;
import com.pinterest.android.pdk.PDKResponse;
import com.twitter.sdk.android.core.identity.TwitterAuthClient;
import io.realm.RealmQuery;
import java.util.ArrayList;
import org.fossasia.phimpme.R;
import org.fossasia.phimpme.base.PhimpmeProgressBarHandler;
import org.fossasia.phimpme.base.RecyclerItemClickListner;
Expand All @@ -51,6 +43,7 @@
import org.fossasia.phimpme.share.imgur.ImgurAuthActivity;
import org.fossasia.phimpme.share.nextcloud.NextCloudAuth;
import org.fossasia.phimpme.share.owncloud.OwnCloudActivity;
import org.fossasia.phimpme.share.pinterest.PinterestAuthActivity;
import org.fossasia.phimpme.share.twitter.LoginActivity;
import org.fossasia.phimpme.utilities.ActivitySwitchHelper;
import org.fossasia.phimpme.utilities.BasicCallBack;
Expand Down Expand Up @@ -82,7 +75,6 @@ public class AccountActivity extends ThemedActivity
private AccountViewModel accountViewModel;

private TwitterAuthClient client;
private PDKClient pdkClient;
private BoxSession sessionBox;

@Override
Expand All @@ -103,9 +95,6 @@ public void onCreate(Bundle savedInstanceState) {
phimpmeProgressBarHandler.show();
setUpRecyclerView();
client = new TwitterAuthClient();
pdkClient = PDKClient.configureInstance(this, PINTEREST_APP_ID);
pdkClient.onConnect(this);
setDebugMode(true);
configureBoxClient();
initObserver();
}
Expand Down Expand Up @@ -190,7 +179,7 @@ public void onItemClick(final View childView, final int position) {
break;

case PINTEREST:
signInPinterest();
startActivity(new Intent(this, PinterestAuthActivity.class));
break;

case FLICKR:
Expand Down Expand Up @@ -284,37 +273,6 @@ public void callBack(int status, Object data) {
startActivity(i);
}

private void signInPinterest() {
ArrayList<String> scopes = new ArrayList<>();
scopes.add(PDKClient.PDKCLIENT_PERMISSION_READ_PUBLIC);
scopes.add(PDKClient.PDKCLIENT_PERMISSION_WRITE_PUBLIC);
scopes.add(PDKClient.PDKCLIENT_PERMISSION_READ_RELATIONSHIPS);
scopes.add(PDKClient.PDKCLIENT_PERMISSION_WRITE_RELATIONSHIPS);

pdkClient.login(
this,
scopes,
new PDKCallback() {
@Override
public void onSuccess(PDKResponse response) {
Log.d(getClass().getName(), response.getData().toString());
accountViewModel.savePinterestToken(
response.getUser().getFirstName() + " " + response.getUser().getLastName());
finish();
startActivity(getIntent());
SnackBarHandler.create(coordinatorLayout, getString(R.string.account_logged_pinterest))
.show();
}

@Override
public void onFailure(PDKException exception) {
Log.e(getClass().getName(), exception.getDetailMessage());
SnackBarHandler.create(coordinatorLayout, getString(R.string.pinterest_signIn_fail))
.show();
}
});
}

@Override
public void onItemLongPress(View childView, int position) {
// No need to be implemented
Expand Down Expand Up @@ -362,7 +320,6 @@ private void boxAuthentication() {
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
client.onActivityResult(requestCode, resultCode, data);
pdkClient.onOauthResponse(requestCode, resultCode, data);
if ((requestCode == accountViewModel.OWNCLOUD_REQUEST_CODE
&& resultCode == accountViewModel.RESULT_OK)
|| (requestCode == accountViewModel.NEXTCLOUD_REQUEST_CODE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
package org.fossasia.phimpme.accounts;

import android.graphics.Bitmap;
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.PinterestApi;
import org.fossasia.phimpme.utilities.RetrofitClient;
import org.fossasia.phimpme.utilities.Utils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

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

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() {
Expand Down Expand Up @@ -41,4 +58,78 @@ void saveUsernameAndToken(
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);
}
}

// 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");
}
}

@Override
public void onFailure(Call<PinterestBoardsResp> call, Throwable t) {
callBack.callBack(Constants.FAIL, null);
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import androidx.lifecycle.ViewModel;
import io.realm.RealmQuery;
import org.fossasia.phimpme.data.local.AccountDatabase;
import org.fossasia.phimpme.share.pinterest.PinterestBoardsResp;
import org.fossasia.phimpme.share.pinterest.PinterestUploadImgResp;
import org.fossasia.phimpme.utilities.Constants;

/** Created by @codedsun on 09/Oct/2019 */
public class AccountViewModel extends ViewModel {
Expand All @@ -13,8 +16,12 @@ public class AccountViewModel extends ViewModel {
final int RESULT_OK = 1;

private AccountRepository accountRepository = new AccountRepository();
MutableLiveData<Boolean> error = new MutableLiveData<>();
public MutableLiveData<Boolean> error = new MutableLiveData<>();
MutableLiveData<RealmQuery<AccountDatabase>> accountDetails = new MutableLiveData<>();
public MutableLiveData<PinterestBoardsResp> boards = new MutableLiveData<>();
public MutableLiveData<PinterestUploadImgResp> pinterestUploadImageResponse =
new MutableLiveData<>();
public MutableLiveData<String> pinterestUploadImageError = new MutableLiveData<>();

public AccountViewModel() {}

Expand Down Expand Up @@ -69,4 +76,38 @@ void deleteAccountFromDatabase(String accountName) {
void saveImgurAccount(String username, String token) {
accountRepository.saveUsernameAndToken(AccountDatabase.AccountName.IMGUR, username, token);
}

public void savePinterestAccount(String username, String token) {
accountRepository.saveUsernameAndToken(AccountDatabase.AccountName.PINTEREST, username, token);
}

public void getUserPinterestBoards() {
accountRepository.getPinterestBoards(
(status, data) -> {
if (status == Constants.SUCCESS) {
PinterestBoardsResp resp = (PinterestBoardsResp) data;
boards.postValue(resp);
} else {
error.postValue(true);
}
});
}

public void uploadImageToBoards(String image, String note, String board) {
accountRepository.uploadImageToPinterest(
(status, data) -> {
if (status == Constants.SUCCESS) {
pinterestUploadImageResponse.postValue((PinterestUploadImgResp) data);
} else {
if (data == null) {
pinterestUploadImageError.postValue("No account logged in");
} else {
pinterestUploadImageError.postValue(((PinterestUploadImgResp) data).getMessage());
}
}
},
image,
note,
board);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ public void delete(ImageDescModel item) {
item.deleteFromRealm();
realm.commitTransaction();
}

public AccountDatabase getAccountByName(String accountName) {
return realm.where(AccountDatabase.class).contains("name", accountName).findFirst();
}
}
Loading