|
1 | 1 | package org.fossasia.phimpme.accounts; |
2 | 2 |
|
3 | | -import android.content.Context; |
4 | 3 | 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 | | - |
16 | 4 | import io.realm.Realm; |
17 | 5 | import io.realm.RealmQuery; |
18 | | - |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import okhttp3.MediaType; |
| 8 | +import okhttp3.MultipartBody; |
| 9 | +import okhttp3.RequestBody; |
19 | 10 | import org.fossasia.phimpme.data.local.AccountDatabase; |
20 | 11 | import org.fossasia.phimpme.data.local.DatabaseHelper; |
| 12 | +import org.fossasia.phimpme.share.pinterest.PinterestBoardsResp; |
| 13 | +import org.fossasia.phimpme.share.pinterest.PinterestUploadImgResp; |
21 | 14 | import org.fossasia.phimpme.utilities.BasicCallBack; |
22 | 15 | 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; |
24 | 18 | 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; |
28 | 22 |
|
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 */ |
41 | 24 | class AccountRepository { |
42 | 25 |
|
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 | + } |
163 | 95 |
|
| 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); |
164 | 106 | } |
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 | + } |
179 | 126 | } |
180 | 127 |
|
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 | + }); |
184 | 133 | } |
185 | | - |
| 134 | + } |
186 | 135 | } |
0 commit comments