Skip to content

Commit e0aa001

Browse files
author
Suneet Srivastava
committed
Merge branch 'development' into pintrest
2 parents 0fcf64f + 9d671ac commit e0aa001

26 files changed

+856
-860
lines changed

app/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ android {
9292

9393
configurations {
9494
implementation.exclude group: 'org.jetbrains', module: 'annotations'
95-
all*.exclude group: 'com.android.volley'
9695
}
9796

9897
lintOptions {
@@ -146,7 +145,10 @@ dependencies {
146145
androidTestImplementation 'androidx.test:runner:1.2.0'
147146
androidTestImplementation 'androidx.test:rules:1.2.0'
148147

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

151153
//google and support
152154
implementation "androidx.appcompat:appcompat:$rootProject.supportLibraryVersion"

app/src/main/java/org/fossasia/phimpme/MyApplication.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import android.util.Log;
77
import androidx.multidex.MultiDex;
88

9-
import com.android.volley.VolleyLog;
109
import com.squareup.leakcanary.LeakCanary;
1110
import com.squareup.leakcanary.RefWatcher;
1211
import com.twitter.sdk.android.core.DefaultLogger;
@@ -56,7 +55,6 @@ public void onCreate() {
5655
.debug(true)
5756
.build();
5857
Twitter.initialize(config);
59-
VolleyLog.DEBUG = true;
6058
/** Realm initialization */
6159
Realm.init(this);
6260
RealmConfiguration realmConfiguration =

app/src/main/java/org/fossasia/phimpme/accounts/AccountActivity.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
package org.fossasia.phimpme.accounts;
22

3+
import static org.fossasia.phimpme.R.string.no_account_signed_in;
4+
import static org.fossasia.phimpme.data.local.AccountDatabase.AccountName.BOX;
5+
import static org.fossasia.phimpme.utilities.Constants.BOX_CLIENT_ID;
6+
import static org.fossasia.phimpme.utilities.Constants.BOX_CLIENT_SECRET;
7+
import static org.fossasia.phimpme.utilities.Constants.SUCCESS;
8+
import static org.fossasia.phimpme.utilities.Utils.checkNetwork;
9+
310
import android.content.DialogInterface;
411
import android.content.Intent;
512
import android.os.Bundle;
613
import android.view.Menu;
714
import android.view.MenuItem;
815
import android.view.View;
916
import android.widget.RelativeLayout;
10-
1117
import androidx.appcompat.app.AlertDialog;
1218
import androidx.appcompat.widget.SwitchCompat;
1319
import androidx.appcompat.widget.Toolbar;
1420
import androidx.coordinatorlayout.widget.CoordinatorLayout;
1521
import androidx.lifecycle.ViewModelProviders;
1622
import androidx.recyclerview.widget.LinearLayoutManager;
1723
import androidx.recyclerview.widget.RecyclerView;
18-
24+
import butterknife.BindView;
25+
import butterknife.ButterKnife;
1926
import com.box.androidsdk.content.BoxConfig;
2027
import com.box.androidsdk.content.auth.BoxAuthentication;
2128
import com.box.androidsdk.content.models.BoxSession;
2229
import com.dropbox.core.android.Auth;
2330
import com.google.android.material.bottomnavigation.BottomNavigationView;
2431
import com.twitter.sdk.android.core.identity.TwitterAuthClient;
25-
32+
import io.realm.RealmQuery;
2633
import org.fossasia.phimpme.R;
2734
import org.fossasia.phimpme.base.PhimpmeProgressBarHandler;
2835
import org.fossasia.phimpme.base.RecyclerItemClickListner;
@@ -44,17 +51,6 @@
4451
import org.fossasia.phimpme.utilities.SnackBarHandler;
4552
import org.jetbrains.annotations.NotNull;
4653

47-
import butterknife.BindView;
48-
import butterknife.ButterKnife;
49-
import io.realm.RealmQuery;
50-
51-
import static org.fossasia.phimpme.R.string.no_account_signed_in;
52-
import static org.fossasia.phimpme.data.local.AccountDatabase.AccountName.BOX;
53-
import static org.fossasia.phimpme.utilities.Constants.BOX_CLIENT_ID;
54-
import static org.fossasia.phimpme.utilities.Constants.BOX_CLIENT_SECRET;
55-
import static org.fossasia.phimpme.utilities.Constants.SUCCESS;
56-
import static org.fossasia.phimpme.utilities.Utils.checkNetwork;
57-
5854
/** Created by pa1pal on 13/6/17. */
5955
public class AccountActivity extends ThemedActivity
6056
implements RecyclerItemClickListner.OnItemClickListener {
Lines changed: 116 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,135 @@
11
package org.fossasia.phimpme.accounts;
22

3-
import android.content.Context;
43
import android.graphics.Bitmap;
5-
import android.util.Base64;
6-
import android.util.Log;
7-
8-
import com.android.volley.DefaultRetryPolicy;
9-
import com.android.volley.NetworkResponse;
10-
import com.android.volley.Request;
11-
import com.android.volley.Response;
12-
import com.android.volley.VolleyError;
13-
import com.android.volley.VolleyLog;
14-
import com.android.volley.toolbox.JsonObjectRequest;
15-
164
import io.realm.Realm;
175
import io.realm.RealmQuery;
18-
6+
import java.io.ByteArrayOutputStream;
7+
import okhttp3.MediaType;
8+
import okhttp3.MultipartBody;
9+
import okhttp3.RequestBody;
1910
import org.fossasia.phimpme.data.local.AccountDatabase;
2011
import org.fossasia.phimpme.data.local.DatabaseHelper;
12+
import org.fossasia.phimpme.share.pinterest.PinterestBoardsResp;
13+
import org.fossasia.phimpme.share.pinterest.PinterestUploadImgResp;
2114
import org.fossasia.phimpme.utilities.BasicCallBack;
2215
import org.fossasia.phimpme.utilities.Constants;
23-
import org.fossasia.phimpme.utilities.MultipartRequest;
16+
import org.fossasia.phimpme.utilities.PinterestApi;
17+
import org.fossasia.phimpme.utilities.RetrofitClient;
2418
import org.fossasia.phimpme.utilities.Utils;
25-
import org.fossasia.phimpme.utilities.VolleyClient;
26-
import org.json.JSONException;
27-
import org.json.JSONObject;
19+
import retrofit2.Call;
20+
import retrofit2.Callback;
21+
import retrofit2.Response;
2822

29-
import java.io.ByteArrayOutputStream;
30-
import java.io.File;
31-
import java.io.IOException;
32-
import java.nio.file.Files;
33-
import java.nio.file.Path;
34-
import java.nio.file.Paths;
35-
import java.util.LinkedHashMap;
36-
import java.util.Map;
37-
38-
/**
39-
* Created by @codedsun on 09/Oct/2019
40-
*/
23+
/** Created by @codedsun on 09/Oct/2019 */
4124
class AccountRepository {
4225

43-
private Realm realm = Realm.getDefaultInstance();
44-
private DatabaseHelper databaseHelper = new DatabaseHelper(realm);
45-
46-
// Fetches the details of all accounts
47-
RealmQuery<AccountDatabase> fetchAllAccounts() {
48-
return databaseHelper.fetchAccountDetails();
49-
}
50-
51-
// saves username, password and serverUrl for an account in database
52-
void saveUsernamePasswordServerUrlForAccount(
53-
AccountDatabase.AccountName accountName, String serverUrl, String username, String password) {
54-
realm.beginTransaction();
55-
AccountDatabase account = realm.createObject(AccountDatabase.class, accountName.toString());
56-
account.setServerUrl(serverUrl);
57-
account.setUsername(username);
58-
account.setPassword(password);
59-
realm.commitTransaction();
60-
}
61-
62-
// saves username and token for an account in database
63-
void saveUsernameAndToken(
64-
AccountDatabase.AccountName accountName, String username, String token) {
65-
realm.beginTransaction();
66-
AccountDatabase account = realm.createObject(AccountDatabase.class, accountName.toString());
67-
account.setUsername(username);
68-
account.setToken(token);
69-
realm.commitTransaction();
70-
}
71-
72-
// deletes an account from database
73-
void deleteAccount(String accountName) {
74-
databaseHelper.deleteSignedOutAccount(accountName);
75-
}
76-
77-
// void uploadImageToPinterest(BasicCallBack callBack, Context context, String imagePath, String note, String board) {
78-
// AccountDatabase pinterestAccount = databaseHelper.getAccountByName(AccountDatabase.AccountName.PINTEREST.name());
79-
// if (pinterestAccount != null && pinterestAccount.getToken() != null) {
80-
// JSONObject params = new JSONObject();
81-
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
82-
// Bitmap bitmap = Utils.getBitmapFromPath(imagePath);
83-
// int numPixels = bitmap.getHeight() * bitmap.getWidth();
84-
// if(numPixels > 3150000) {
85-
// Log.e("sUneet - photo error","Image too large");
86-
// return;
87-
// }
88-
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
89-
// String b64Str = Base64.encodeToString(baos.toByteArray(), Base64.NO_WRAP);
90-
// try {
91-
// params.put("image_base64", b64Str);
92-
// params.put("note", note);
93-
// params.put("board", "suneetbond/vrindavan");
94-
// } catch (JSONException e) {
95-
// e.printStackTrace();
96-
// }
97-
// JsonObjectRequest uploadImageRequest = new JsonObjectRequest(Request.Method.POST,Constants.PINTEREST_POST_CREATE_PIN,params, response -> {
98-
// VolleyLog.e("Suneet - reponse",response.toString());
99-
// },
100-
// error -> {
101-
// VolleyLog.e("Suneet - error",error.toString());
102-
// });
103-
//
104-
// uploadImageRequest.setRetryPolicy(new DefaultRetryPolicy( 50000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
105-
// VolleyClient.getInstance(context).getRequestQueue().add(uploadImageRequest);
106-
//
107-
// }else{
108-
// //No account found for pinterest
109-
// callBack.callBack(Constants.FAIL, null);
110-
// }
111-
//
112-
//
113-
// }
114-
115-
void uploadImageToPinterest(BasicCallBack callBack, Context context, String imagePath, String note, String board) {
116-
AccountDatabase pinterestAccount = databaseHelper.getAccountByName(AccountDatabase.AccountName.PINTEREST.name());
117-
if (pinterestAccount != null && pinterestAccount.getToken() != null) {
118-
byte[] byteFile = new byte[]{};
119-
try {
120-
byteFile = Files.readAllBytes(Paths.get(imagePath));
121-
} catch (IOException e) {
122-
e.printStackTrace();
123-
}
124-
MultipartRequest multipartRequest = new MultipartRequest();
125-
MultipartRequest.MultipartRequestBuild uploadRequst = multipartRequest.sendMultipartRequest(Request.Method.POST,
126-
Constants.PINTEREST_POST_CREATE_PIN, byteFile, null, "Suneetsro", new Response.Listener<NetworkResponse>() {
127-
@Override
128-
public void onResponse(NetworkResponse response) {
129-
VolleyLog.e("suneet - repsonse");
130-
}
131-
}, new Response.ErrorListener() {
132-
@Override
133-
public void onErrorResponse(VolleyError error) {
134-
VolleyLog.e("suneet - error");
135-
}
136-
});
137-
uploadRequst.setRetryPolicy(new DefaultRetryPolicy(50000 , 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
138-
VolleyClient.getInstance(context).getRequestQueue().add(uploadRequst);
139-
140-
}else{
141-
//No account found for pinterest
142-
callBack.callBack(Constants.FAIL, null);
143-
}
144-
145-
146-
}
147-
148-
//get pinterest boards of user
149-
void getPinterestBoards(BasicCallBack callBack, Context context) {
150-
AccountDatabase pinterestAccount = databaseHelper.getAccountByName(AccountDatabase.AccountName.PINTEREST.name());
151-
if (pinterestAccount != null && pinterestAccount.getToken() != null) {
152-
JsonObjectRequest pinterestBoardsRequest = new JsonObjectRequest(Request.Method.GET, Constants.PINTEREST_GET_USER_BOARDS + pinterestAccount.getToken(),
153-
response -> {
154-
callBack.callBack(Constants.SUCCESS, response);
155-
}, error -> {
156-
callBack.callBack(Constants.FAIL, error);
157-
});
158-
VolleyClient.getInstance(context).getRequestQueue().add(pinterestBoardsRequest);
159-
} else {
160-
//No account found for pinterest
161-
callBack.callBack(Constants.FAIL, null);
162-
}
26+
private Realm realm = Realm.getDefaultInstance();
27+
private DatabaseHelper databaseHelper = new DatabaseHelper(realm);
28+
private PinterestApi pinterestApi =
29+
RetrofitClient.getRetrofitClient(Constants.PINTEREST_BASE_URL).create(PinterestApi.class);
30+
31+
// Fetches the details of all accounts
32+
RealmQuery<AccountDatabase> fetchAllAccounts() {
33+
return databaseHelper.fetchAccountDetails();
34+
}
35+
36+
// saves username, password and serverUrl for an account in database
37+
void saveUsernamePasswordServerUrlForAccount(
38+
AccountDatabase.AccountName accountName, String serverUrl, String username, String password) {
39+
realm.beginTransaction();
40+
AccountDatabase account = realm.createObject(AccountDatabase.class, accountName.toString());
41+
account.setServerUrl(serverUrl);
42+
account.setUsername(username);
43+
account.setPassword(password);
44+
realm.commitTransaction();
45+
}
46+
47+
// saves username and token for an account in database
48+
void saveUsernameAndToken(
49+
AccountDatabase.AccountName accountName, String username, String token) {
50+
realm.beginTransaction();
51+
AccountDatabase account = realm.createObject(AccountDatabase.class, accountName.toString());
52+
account.setUsername(username);
53+
account.setToken(token);
54+
realm.commitTransaction();
55+
}
56+
57+
// deletes an account from database
58+
void deleteAccount(String accountName) {
59+
databaseHelper.deleteSignedOutAccount(accountName);
60+
}
61+
62+
void uploadImageToPinterest(BasicCallBack callBack, String imagePath, String note, String board) {
63+
AccountDatabase pinterestAccount =
64+
databaseHelper.getAccountByName(AccountDatabase.AccountName.PINTEREST.name());
65+
if (pinterestAccount != null && pinterestAccount.getToken() != null) {
66+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
67+
Bitmap bitmap = Utils.getBitmapFromPath(imagePath);
68+
int numPixels = bitmap.getHeight() * bitmap.getWidth();
69+
if (numPixels > 3150000) {
70+
PinterestUploadImgResp resp = new PinterestUploadImgResp();
71+
resp.setMessage("Image Size too large");
72+
callBack.callBack(Constants.FAIL, resp);
73+
return;
74+
}
75+
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
76+
RequestBody reqFile =
77+
RequestBody.create(baos.toByteArray(), MediaType.parse("multipart/form-data"));
78+
MultipartBody.Part multipartBody =
79+
MultipartBody.Part.createFormData("image", imagePath, reqFile);
80+
pinterestApi
81+
.uploadImageToPinterest(pinterestAccount.getToken(), note, board, multipartBody)
82+
.enqueue(
83+
new Callback<PinterestUploadImgResp>() {
84+
@Override
85+
public void onResponse(
86+
Call<PinterestUploadImgResp> call, Response<PinterestUploadImgResp> response) {
87+
if (response.isSuccessful()) {
88+
callBack.callBack(Constants.SUCCESS, response.body());
89+
} else {
90+
PinterestUploadImgResp resp = new PinterestUploadImgResp();
91+
resp.setMessage(response.message());
92+
callBack.callBack(Constants.FAIL, resp);
93+
}
94+
}
16395

96+
@Override
97+
public void onFailure(Call<PinterestUploadImgResp> call, Throwable t) {
98+
PinterestUploadImgResp resp = new PinterestUploadImgResp();
99+
resp.setMessage(t.toString());
100+
callBack.callBack(Constants.FAIL, resp);
101+
}
102+
});
103+
} else {
104+
// No account found for pinterest
105+
callBack.callBack(Constants.FAIL, null);
164106
}
165-
166-
//Creates Pinterest Board
167-
void createBoard(String name, Context context, BasicCallBack basicCallBack) {
168-
JSONObject params = new JSONObject();
169-
try {
170-
params.put("name", name);
171-
} catch (JSONException e) {
172-
e.printStackTrace();
173-
}
174-
JsonObjectRequest createBoardRequest = new JsonObjectRequest(Request.Method.POST, Constants.PINTEREST_POST_CREATE_BOARD, params,
175-
response -> {
176-
177-
},
178-
error -> {
107+
}
108+
109+
// get pinterest boards of user
110+
void getPinterestBoards(BasicCallBack callBack) {
111+
AccountDatabase pinterestAccount =
112+
databaseHelper.getAccountByName(AccountDatabase.AccountName.PINTEREST.name());
113+
if (pinterestAccount != null && pinterestAccount.getToken() != null) {
114+
pinterestApi
115+
.getUserBoards(pinterestAccount.getToken())
116+
.enqueue(
117+
new Callback<PinterestBoardsResp>() {
118+
@Override
119+
public void onResponse(
120+
Call<PinterestBoardsResp> call, Response<PinterestBoardsResp> response) {
121+
if (response.body() != null && response.isSuccessful()) {
122+
callBack.callBack(Constants.SUCCESS, response.body());
123+
} else {
124+
callBack.callBack(Constants.FAIL, "Unable to get Boards");
125+
}
179126
}
180127

181-
);
182-
183-
VolleyClient.getInstance(context).getRequestQueue().add(createBoardRequest);
128+
@Override
129+
public void onFailure(Call<PinterestBoardsResp> call, Throwable t) {
130+
callBack.callBack(Constants.FAIL, null);
131+
}
132+
});
184133
}
185-
134+
}
186135
}

0 commit comments

Comments
 (0)