mList) {
188 | mListener.onFragmentMessage(mList);
189 | }
190 |
191 | /**
192 | * This interface must be implemented by activities that contain this
193 | * fragment to allow an interaction in this fragment to be communicated
194 | * to the activity and potentially other fragments contained in that
195 | * activity.
196 | *
197 | * See the Android Training lesson Communicating with Other Fragments for more information.
200 | */
201 | public interface OnFragmentInteractionListener {
202 | // TODO: Update argument type and name
203 | public void onFragmentMessage(ArrayList mList);
204 | }
205 | }
206 |
--------------------------------------------------------------------------------
/app/src/main/java/com/jain/rakshit/instagramtrackerfinal/Network/InstagramApp.java:
--------------------------------------------------------------------------------
1 | package com.jain.rakshit.instagramtrackerfinal.Network;
2 |
3 | import android.app.ProgressDialog;
4 | import android.content.Context;
5 | import android.os.Handler;
6 | import android.os.Message;
7 | import android.util.Log;
8 |
9 | import com.jain.rakshit.instagramtrackerfinal.Model.InstagramSession;
10 | import com.jain.rakshit.instagramtrackerfinal.View.InstagramDialog;
11 | import com.jain.rakshit.instagramtrackerfinal.View.InstagramDialog.OAuthDialogListener;
12 |
13 | import org.json.JSONObject;
14 | import org.json.JSONTokener;
15 |
16 | import java.io.BufferedReader;
17 | import java.io.IOException;
18 | import java.io.InputStream;
19 | import java.io.InputStreamReader;
20 | import java.io.OutputStreamWriter;
21 | import java.net.HttpURLConnection;
22 | import java.net.URL;
23 |
24 | /**
25 | * Created by Rakshit on 6/20/2016.
26 | */
27 |
28 | public class InstagramApp {
29 | private static final String AUTH_URL = "https://api.instagram.com/oauth/authorize/";
30 | private static final String TOKEN_URL = "https://api.instagram.com/oauth/access_token";
31 | private static final String API_URL = "https://api.instagram.com/v1";
32 | private static final String TAG = "InstagramAPI";
33 | public static String mCallbackUrl = "instagram://connect";
34 | private static int WHAT_FINALIZE = 0;
35 | private static int WHAT_ERROR = 1;
36 | private static int WHAT_FETCH_INFO = 2;
37 | private InstagramSession mSession;
38 | private InstagramDialog mDialog;
39 | private OAuthAuthenticationListener mListener;
40 | private ProgressDialog mProgress;
41 | private String mAuthUrl;
42 | private String mTokenUrl;
43 | private String mAccessToken;
44 | private Context mCtx;
45 | private String mClientId;
46 | private String mClientSecret;
47 | private Context mContext;
48 | private Handler mHandler = new Handler() {
49 | @Override
50 | public void handleMessage(Message msg) {
51 | if (msg.what == WHAT_ERROR) {
52 | mProgress.dismiss();
53 | if (msg.arg1 == 1) {
54 | mListener.onFail("Failed to get access token");
55 | } else if (msg.arg1 == 2) {
56 | mListener.onFail("Failed to get user information");
57 | }
58 | } else if (msg.what == WHAT_FETCH_INFO) {
59 | fetchUserName();
60 | } else {
61 | mProgress.dismiss();
62 | mListener.onSuccess();
63 | }
64 | }
65 | };
66 |
67 | public InstagramApp(Context mcontext) {
68 |
69 | }
70 |
71 | public InstagramApp(Context mApplication_Context, Context context, String clientId, String clientSecret,
72 | String callbackUrl) {
73 |
74 | mClientId = clientId;
75 | mClientSecret = clientSecret;
76 | mCtx = context;
77 | mSession = new InstagramSession(mApplication_Context);
78 | mAccessToken = mSession.getAccessToken();
79 |
80 | mCallbackUrl = callbackUrl;
81 | mTokenUrl = TOKEN_URL + "?client_id=" + clientId + "&client_secret="
82 | + clientSecret + "&redirect_uri=" + mCallbackUrl + "&grant_type=authorization_code";
83 | mAuthUrl = AUTH_URL + "?client_id=" + clientId + "&redirect_uri="
84 | + mCallbackUrl + "&response_type=code&display=touch&scope=relationships+follower_list+public_content+basic";
85 |
86 | OAuthDialogListener listener = new OAuthDialogListener() {
87 | @Override
88 | public void onComplete(String code) {
89 | getAccessToken(code);
90 | }
91 |
92 | @Override
93 | public void onError(String error) {
94 | mListener.onFail("Authorization failed");
95 | }
96 | };
97 |
98 | mDialog = new InstagramDialog(mCtx, mAuthUrl, listener);
99 | mProgress = new ProgressDialog(context);
100 | mProgress.setCancelable(false);
101 |
102 | }
103 |
104 | private void getAccessToken(final String code) {
105 | mProgress.setMessage("Getting access token ...");
106 | mProgress.show();
107 |
108 | new Thread() {
109 | @Override
110 | public void run() {
111 | Log.i(TAG, "Getting access token");
112 | int what = WHAT_FETCH_INFO;
113 | try {
114 | URL url = new URL(TOKEN_URL);
115 | //URL url = new URL(mTokenUrl + "&code=" + code);
116 | Log.i(TAG, "Opening Token URL " + url.toString());
117 | HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
118 | urlConnection.setRequestMethod("POST");
119 | urlConnection.setDoInput(true);
120 | urlConnection.setDoOutput(true);
121 | //urlConnection.connect();
122 | OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getOutputStream());
123 | writer.write("client_id=" + mClientId +
124 | "&client_secret=" + mClientSecret +
125 | "&grant_type=authorization_code" +
126 | "&redirect_uri=" + mCallbackUrl +
127 | "&code=" + code);
128 | writer.flush();
129 | String response = streamToString(urlConnection.getInputStream());
130 | Log.i(TAG, "response " + response);
131 | JSONObject jsonObj = (JSONObject) new JSONTokener(response).nextValue();
132 |
133 | mAccessToken = jsonObj.getString("access_token");
134 | Log.i(TAG, "Got access token: " + mAccessToken);
135 |
136 | String id = jsonObj.getJSONObject("user").getString("id");
137 | String user = jsonObj.getJSONObject("user").getString("username");
138 | String name = jsonObj.getJSONObject("user").getString("full_name");
139 |
140 | mSession.storeAccessToken(mAccessToken, id, user, name);
141 |
142 |
143 | } catch (Exception ex) {
144 | what = WHAT_ERROR;
145 | ex.printStackTrace();
146 | }
147 |
148 | mHandler.sendMessage(mHandler.obtainMessage(what, 1, 0));
149 | }
150 | }.start();
151 | }
152 |
153 | private void fetchUserName() {
154 | mProgress.setMessage("Finalizing ...");
155 |
156 | new Thread() {
157 | @Override
158 | public void run() {
159 | Log.i(TAG, "Fetching user info");
160 | int what = WHAT_FINALIZE;
161 | try {
162 | URL url = new URL(API_URL + "/users/" + mSession.getId() + "/?access_token=" + mAccessToken);
163 |
164 | Log.d(TAG, "Opening URL " + url.toString());
165 | HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
166 | urlConnection.setRequestMethod("GET");
167 | urlConnection.setDoInput(true);
168 | urlConnection.connect();
169 | String response = streamToString(urlConnection.getInputStream());
170 | System.out.println(response);
171 | JSONObject jsonObj = (JSONObject) new JSONTokener(response).nextValue();
172 | String name = jsonObj.getJSONObject("data").getString("full_name");
173 | String bio = jsonObj.getJSONObject("data").getString("bio");
174 | Log.i(TAG, "Got name: " + name + ", bio [" + bio + "]");
175 | } catch (Exception ex) {
176 | what = WHAT_ERROR;
177 | ex.printStackTrace();
178 | }
179 |
180 | mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0));
181 | }
182 | }.start();
183 |
184 | }
185 |
186 | public boolean hasAccessToken() {
187 | return (mAccessToken == null) ? false : true;
188 | }
189 |
190 | public void setListener(OAuthAuthenticationListener listener) {
191 | mListener = listener;
192 | }
193 |
194 | public String getUserName() {
195 | return mSession.getUsername();
196 | }
197 |
198 | public String getId() {
199 | return mSession.getId();
200 | }
201 |
202 | public String getName() {
203 | return mSession.getName();
204 | }
205 |
206 | public void authorize() {
207 | //Intent webAuthIntent = new Intent(Intent.ACTION_VIEW);
208 | //webAuthIntent.setData(Uri.parse(AUTH_URL));
209 | //mCtx.startActivity(webAuthIntent);
210 | mDialog.show();
211 | }
212 |
213 | private String streamToString(InputStream is) throws IOException {
214 | String str = "";
215 |
216 | if (is != null) {
217 | StringBuilder sb = new StringBuilder();
218 | String line;
219 |
220 | try {
221 | BufferedReader reader = new BufferedReader(
222 | new InputStreamReader(is));
223 |
224 | while ((line = reader.readLine()) != null) {
225 | sb.append(line);
226 | }
227 |
228 | reader.close();
229 | } finally {
230 | is.close();
231 | }
232 |
233 | str = sb.toString();
234 | }
235 |
236 | return str;
237 | }
238 |
239 | public void resetAccessToken() {
240 | if (mAccessToken != null) {
241 | mSession.resetAccessToken();
242 | mAccessToken = null;
243 | }
244 | }
245 |
246 | public String getAccessToken() {
247 |
248 | return mSession.getAccessToken();
249 |
250 | }
251 |
252 |
253 | public interface OAuthAuthenticationListener {
254 | public abstract void onSuccess();
255 |
256 | public abstract void onFail(String error);
257 | }
258 | }
259 |
--------------------------------------------------------------------------------
/app/src/main/java/com/jain/rakshit/instagramtrackerfinal/Fragments/NotFollowingBack.java:
--------------------------------------------------------------------------------
1 | package com.jain.rakshit.instagramtrackerfinal.Fragments;
2 |
3 | import android.content.Context;
4 | import android.net.Uri;
5 | import android.os.AsyncTask;
6 | import android.os.Bundle;
7 | import android.support.annotation.Nullable;
8 | import android.support.v4.app.Fragment;
9 | import android.util.Log;
10 | import android.view.LayoutInflater;
11 | import android.view.View;
12 | import android.view.ViewGroup;
13 | import android.widget.ProgressBar;
14 |
15 | import com.jain.rakshit.instagramtrackerfinal.Activites.MainActivity;
16 | import com.jain.rakshit.instagramtrackerfinal.Adapters.DataAdapter;
17 | import com.jain.rakshit.instagramtrackerfinal.Adapters.LostFollowersRecyclerViewAdapter;
18 | import com.jain.rakshit.instagramtrackerfinal.EndPoints.RelationshipService;
19 | import com.jain.rakshit.instagramtrackerfinal.Model.InstagramSession;
20 | import com.jain.rakshit.instagramtrackerfinal.R;
21 | import com.jain.rakshit.instagramtrackerfinal.Rest.Model.Datum;
22 | import com.jain.rakshit.instagramtrackerfinal.Rest.Model.RelationshipData;
23 | import com.jain.rakshit.instagramtrackerfinal.Rest.Model.RelationshipData1;
24 | import com.jain.rakshit.instagramtrackerfinal.Rest.RestClient;
25 |
26 | import java.io.IOException;
27 | import java.util.ArrayList;
28 |
29 | import co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView;
30 | import io.realm.Realm;
31 | import io.realm.RealmChangeListener;
32 | import io.realm.RealmConfiguration;
33 | import io.realm.RealmResults;
34 | import retrofit2.Call;
35 |
36 | /**
37 | * A simple {@link Fragment} subclass.
38 | * Activities that contain this fragment must implement the
39 | * {@link LostFollowers.OnFragmentInteractionListener} interface
40 | * to handle interaction events.
41 | * Use the {@link LostFollowers#newInstance} factory method to
42 | * create an instance of this fragment.
43 | */
44 | public class NotFollowingBack extends Fragment {
45 | // TODO: Rename parameter arguments, choose names that match
46 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
47 | private static final String ARG_PARAM1 = "param1";
48 |
49 |
50 | private static final String TAG = "LostFollowers";
51 | public ArrayList mList;
52 | private View view;
53 | private InstagramSession mInstagramSession;
54 |
55 | private RealmResults mRealm;
56 |
57 | private RestClient mRestClient;
58 | private Realm realm1;
59 |
60 | private String outgoingStatus;
61 | private String oldIncommingStatus;
62 | private String newIncommingStatus;
63 | private OnFragmentInteractionListener mListener;
64 | private RelationshipService apiService;
65 | private String user_id;
66 | private String full_name;
67 | private String profile_picture;
68 | private DataAdapter adapter;
69 | private RealmConfiguration realmConfiguration;
70 | private RealmRecyclerView realmRecyclerView;
71 | private LostFollowersRecyclerViewAdapter lostFollowersRecyclerViewAdapter;
72 | private ProgressBar spinner;
73 |
74 |
75 | private ArrayList realm11;
76 |
77 | public NotFollowingBack() {
78 | // Required empty public constructor
79 | }
80 |
81 | /**
82 | * Use this factory method to create a new instance of
83 | * this fragment using the provided parameters.
84 | *
85 | * @param mList Parameter 1.
86 | * @return A new instance of fragment Following.
87 | */
88 | // TODO: Rename and change types and number of parameters
89 | public static NotFollowingBack newInstance(ArrayList mList) {
90 | NotFollowingBack fragment = new NotFollowingBack();
91 | Bundle args = new Bundle();
92 | args.putSerializable(ARG_PARAM1, mList);
93 | fragment.setArguments(args);
94 | return fragment;
95 | }
96 |
97 | @Override
98 | public void onCreate(Bundle savedInstanceState) {
99 | super.onCreate(savedInstanceState);
100 | if (getArguments() != null) {
101 |
102 | mList = (ArrayList) getArguments().getSerializable(ARG_PARAM1);
103 | }
104 |
105 |
106 | }
107 |
108 | @Override
109 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
110 | Bundle savedInstanceState) {
111 | // Inflate the layout for this fragment
112 | view = inflater.inflate(R.layout.lost_followers, container, false);
113 | return view;
114 | }
115 |
116 | // TODO: Rename method, update argument and hook method into UI event
117 | public void onButtonPressed(Uri uri) {
118 | if (mListener != null) {
119 | mListener.onFragmentInteraction(uri);
120 | }
121 | }
122 |
123 | @Override
124 | public void onAttach(Context context) {
125 | super.onAttach(context);
126 |
127 | }
128 |
129 | @Override
130 | public void onStart() {
131 | super.onStart();
132 | MainActivity mainActivity = new MainActivity();
133 | realm11 = new ArrayList();
134 | }
135 |
136 |
137 | @Override
138 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
139 | super.onActivityCreated(savedInstanceState);
140 |
141 | realmConfiguration = new RealmConfiguration.Builder(getContext())
142 | .deleteRealmIfMigrationNeeded()
143 | .build();
144 |
145 | Realm.setDefaultConfiguration(realmConfiguration);
146 |
147 | realm1 = Realm.getDefaultInstance();
148 |
149 |
150 | spinner = (ProgressBar) view.findViewById(R.id.progressBar1);
151 | spinner.setVisibility(View.VISIBLE);
152 |
153 | realmRecyclerView = (RealmRecyclerView) view.findViewById(R.id.realm_recycler_view);
154 | mInstagramSession = new InstagramSession(getActivity().getApplicationContext());
155 |
156 | mRestClient = new RestClient();
157 | apiService = mRestClient.getRelationshipService();
158 |
159 | Log.i(TAG, mInstagramSession.getAccessToken());
160 | Log.i(TAG, mInstagramSession.getId());
161 |
162 |
163 | final RealmResults ss = realm1.where(Datum.class).findAll();
164 | ss.addChangeListener(new RealmChangeListener>() {
165 | @Override
166 | public void onChange(RealmResults element) {
167 | Log.i(TAG, ss.toString() + "");
168 | }
169 | });
170 |
171 |
172 | new DownloadFilesTask().execute(mList, null, null);
173 |
174 |
175 | }
176 |
177 | public void recycleView() {
178 |
179 | realm1 = Realm.getDefaultInstance();
180 | mRealm = realm1.where(Datum.class).findAll();
181 | Log.i(TAG, mRealm + " OnPostExecute");
182 | lostFollowersRecyclerViewAdapter = new LostFollowersRecyclerViewAdapter(getActivity().getApplicationContext(), mRealm, true, true);
183 |
184 | realmRecyclerView.setAdapter(lostFollowersRecyclerViewAdapter);
185 | }
186 |
187 | @Override
188 | public void onDestroy() {
189 | super.onDestroy();
190 | // Remove the listener.
191 |
192 | // Close the realm instance.
193 | realm1.close();
194 | }
195 |
196 | @Override
197 | public void onDetach() {
198 | super.onDetach();
199 | mListener = null;
200 | }
201 |
202 | /**
203 | * This interface must be implemented by activities that contain this
204 | * fragment to allow an interaction in this fragment to be communicated
205 | * to the activity and potentially other fragments contained in that
206 | * activity.
207 | *
208 | * See the Android Training lesson Communicating with Other Fragments for more information.
211 | */
212 | public interface OnFragmentInteractionListener {
213 | // TODO: Update argument type and name
214 | void onFragmentInteraction(Uri uri);
215 | }
216 |
217 | private class DownloadFilesTask extends AsyncTask, Integer, Long> {
218 | @Override
219 | protected Long doInBackground(ArrayList... params) {
220 |
221 | realm1 = Realm.getDefaultInstance();
222 | for (int i = 0; i < mList.size(); i++) {
223 | user_id = mList.get(i).getId();
224 | profile_picture = mList.get(i).getProfilePicture();
225 | full_name = mList.get(i).getUsername();
226 | Call call1 = apiService.getRelation(user_id, mInstagramSession.getAccessToken());
227 |
228 | Log.i(TAG, "SETTING ID BEFORE CALLING " + user_id);
229 |
230 | try {
231 | RelationshipData mRelationshipData = call1.execute().body();
232 |
233 | Log.i(TAG, "SETTING ID " + user_id);
234 |
235 | RelationshipData1 users1 = mRelationshipData.getData();
236 |
237 | users1.setId(user_id);
238 |
239 | realm1.beginTransaction();
240 |
241 | if (!mInstagramSession.getFirst_time_variable1().equals("true")) {
242 |
243 | Log.i(TAG, mInstagramSession.getFirst_time_variable1() + "First time initialization");
244 |
245 | RealmResults query = realm1.where(RelationshipData1.class)
246 | .equalTo("id", user_id).findAll();
247 |
248 | oldIncommingStatus = query.get(0).getIncomingStatus();
249 |
250 | Log.i(TAG, "QUERY INITIATED " + oldIncommingStatus);
251 |
252 | outgoingStatus = query.get(0).getOutgoingStatus();
253 |
254 | }
255 | realm1.copyToRealmOrUpdate(users1);
256 |
257 | realm1.commitTransaction();
258 |
259 | RealmResults query1 = realm1.where(RelationshipData1.class)
260 | .equalTo("id", user_id).findAll();
261 |
262 |
263 | newIncommingStatus = query1.get(0).getIncomingStatus();
264 | Log.i(TAG, "2nd QUERY " + newIncommingStatus);
265 |
266 | Log.i(TAG, users1.getOutgoingStatus() + "");
267 | Log.i(TAG, users1.getIncomingStatus() + "");
268 | Log.i(TAG, users1.getTargetUserIsPrivate() + "");
269 |
270 | if (!mInstagramSession.getFirst_time_variable1().equals("true")) {
271 | Log.i(TAG, mInstagramSession.getFirst_time_variable() + "First time initialization");
272 |
273 | if ((outgoingStatus.equals("follows")) && (!newIncommingStatus.equals("followed_by"))) {
274 | Log.i(TAG, "Not Following Back");
275 | final Datum datum = new Datum();
276 | datum.setId(user_id);
277 | datum.setProfilePicture(profile_picture);
278 | datum.setFullName(full_name);
279 | realm1.executeTransaction(new Realm.Transaction() {
280 |
281 | @Override
282 | public void execute(Realm realm) {
283 |
284 | realm1.copyToRealmOrUpdate(datum);
285 |
286 | }
287 | });
288 |
289 |
290 | } else if (outgoingStatus.equals("follows") && (!newIncommingStatus.equals("followed_by"))) {
291 | Log.i(TAG, "Followed Back");
292 | realm1.beginTransaction();
293 | realm1.where(Datum.class).beginsWith("id", user_id).findAll().deleteFirstFromRealm();
294 | realm1.commitTransaction();
295 |
296 | }
297 |
298 | }
299 |
300 | } catch (IOException e) {
301 | e.printStackTrace();
302 | }
303 |
304 | }
305 | return null;
306 | }
307 |
308 | protected void onProgressUpdate(Integer... progress) {
309 |
310 | }
311 |
312 |
313 | protected void onPostExecute(Long result) {
314 | if (mInstagramSession.getFirst_time_variable1().equals("true")) {
315 | mInstagramSession.setFirst_time_variable1();
316 | }
317 | spinner.setVisibility(View.GONE);
318 | recycleView();
319 | }
320 | }
321 | }
322 |
--------------------------------------------------------------------------------
/app/src/main/java/com/jain/rakshit/instagramtrackerfinal/Fragments/LostFollowers.java:
--------------------------------------------------------------------------------
1 | package com.jain.rakshit.instagramtrackerfinal.Fragments;
2 |
3 | import android.content.Context;
4 | import android.net.Uri;
5 | import android.os.AsyncTask;
6 | import android.os.Bundle;
7 | import android.support.annotation.Nullable;
8 | import android.support.v4.app.Fragment;
9 | import android.util.Log;
10 | import android.view.LayoutInflater;
11 | import android.view.View;
12 | import android.view.ViewGroup;
13 | import android.widget.ProgressBar;
14 |
15 | import com.jain.rakshit.instagramtrackerfinal.Activites.MainActivity;
16 | import com.jain.rakshit.instagramtrackerfinal.Adapters.DataAdapter;
17 | import com.jain.rakshit.instagramtrackerfinal.Adapters.LostFollowersRecyclerViewAdapter;
18 | import com.jain.rakshit.instagramtrackerfinal.EndPoints.RelationshipService;
19 | import com.jain.rakshit.instagramtrackerfinal.Model.InstagramSession;
20 | import com.jain.rakshit.instagramtrackerfinal.R;
21 | import com.jain.rakshit.instagramtrackerfinal.Rest.Model.Datum;
22 | import com.jain.rakshit.instagramtrackerfinal.Rest.Model.RelationshipData;
23 | import com.jain.rakshit.instagramtrackerfinal.Rest.Model.RelationshipData1;
24 | import com.jain.rakshit.instagramtrackerfinal.Rest.RestClient;
25 |
26 | import java.io.IOException;
27 | import java.util.ArrayList;
28 |
29 | import co.moonmonkeylabs.realmrecyclerview.RealmRecyclerView;
30 | import io.realm.Realm;
31 | import io.realm.RealmChangeListener;
32 | import io.realm.RealmConfiguration;
33 | import io.realm.RealmResults;
34 | import retrofit2.Call;
35 |
36 | /**
37 | * A simple {@link Fragment} subclass.
38 | * Activities that contain this fragment must implement the
39 | * {@link LostFollowers.OnFragmentInteractionListener} interface
40 | * to handle interaction events.
41 | * Use the {@link LostFollowers#newInstance} factory method to
42 | * create an instance of this fragment.
43 | */
44 | public class LostFollowers extends Fragment {
45 | // TODO: Rename parameter arguments, choose names that match
46 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
47 | private static final String ARG_PARAM1 = "param1";
48 |
49 |
50 | private static final String TAG = "LostFollowers";
51 | public ArrayList mList;
52 | private View view;
53 | private InstagramSession mInstagramSession;
54 |
55 | private RealmResults mRealm;
56 |
57 | private RestClient mRestClient;
58 | private Realm realm;
59 |
60 | private String outgoingStatus;
61 | private String oldIncommingStatus;
62 | private String newIncommingStatus;
63 | private OnFragmentInteractionListener mListener;
64 | private RelationshipService apiService;
65 | private String user_id;
66 | private String full_name;
67 | private String profile_picture;
68 | private DataAdapter adapter;
69 | private RealmConfiguration realmConfiguration;
70 | private RealmRecyclerView realmRecyclerView;
71 | private LostFollowersRecyclerViewAdapter lostFollowersRecyclerViewAdapter;
72 | private ProgressBar spinner;
73 |
74 | private ArrayList realm11;
75 |
76 | public LostFollowers() {
77 | // Required empty public constructor
78 | }
79 |
80 | /**
81 | * Use this factory method to create a new instance of
82 | * this fragment using the provided parameters.
83 | *
84 | * @param mList Parameter 1.
85 | * @return A new instance of fragment Following.
86 | */
87 | // TODO: Rename and change types and number of parameters
88 | public static LostFollowers newInstance(ArrayList mList) {
89 | LostFollowers fragment = new LostFollowers();
90 | Bundle args = new Bundle();
91 | args.putSerializable(ARG_PARAM1, mList);
92 | fragment.setArguments(args);
93 | return fragment;
94 | }
95 |
96 | @Override
97 | public void onCreate(Bundle savedInstanceState) {
98 | super.onCreate(savedInstanceState);
99 |
100 | if (getArguments() != null) {
101 |
102 | mList = (ArrayList) getArguments().getSerializable(ARG_PARAM1);
103 | }
104 |
105 |
106 | }
107 |
108 | @Override
109 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
110 | Bundle savedInstanceState) {
111 | // Inflate the layout for this fragment
112 | view = inflater.inflate(R.layout.lost_followers, container, false);
113 | return view;
114 | }
115 |
116 | // TODO: Rename method, update argument and hook method into UI event
117 | public void onButtonPressed(Uri uri) {
118 | if (mListener != null) {
119 | mListener.onFragmentInteraction(uri);
120 | }
121 | }
122 |
123 | @Override
124 | public void onAttach(Context context) {
125 | super.onAttach(context);
126 |
127 | }
128 |
129 | @Override
130 | public void onStart() {
131 | super.onStart();
132 | MainActivity mainActivity = new MainActivity();
133 | realm11 = new ArrayList();
134 | }
135 |
136 |
137 | @Override
138 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
139 | super.onActivityCreated(savedInstanceState);
140 |
141 | realmConfiguration = new RealmConfiguration.Builder(getContext())
142 | .deleteRealmIfMigrationNeeded()
143 | .build();
144 |
145 | Realm.setDefaultConfiguration(realmConfiguration);
146 |
147 | realm = Realm.getDefaultInstance();
148 |
149 | spinner = (ProgressBar) view.findViewById(R.id.progressBar1);
150 |
151 | spinner.setVisibility(View.VISIBLE);
152 |
153 | realmRecyclerView = (RealmRecyclerView) view.findViewById(R.id.realm_recycler_view);
154 | mInstagramSession = new InstagramSession(getActivity().getApplicationContext());
155 |
156 | mRestClient = new RestClient();
157 | apiService = mRestClient.getRelationshipService();
158 |
159 | Log.i(TAG, mInstagramSession.getAccessToken());
160 | Log.i(TAG, mInstagramSession.getId());
161 |
162 |
163 | final RealmResults ss = realm.where(Datum.class).findAll();
164 | ss.addChangeListener(new RealmChangeListener>() {
165 | @Override
166 | public void onChange(RealmResults element) {
167 | Log.i(TAG, ss.toString() + "");
168 | }
169 | });
170 |
171 |
172 | new DownloadFilesTask().execute(mList, null, null);
173 |
174 |
175 | }
176 |
177 | public void recycleView() {
178 |
179 | realm = Realm.getDefaultInstance();
180 | mRealm = realm.where(Datum.class).findAll();
181 | Log.i(TAG, mRealm + " OnPostExecute");
182 | lostFollowersRecyclerViewAdapter = new LostFollowersRecyclerViewAdapter(getActivity().getApplicationContext(), mRealm, true, true);
183 |
184 | realmRecyclerView.setAdapter(lostFollowersRecyclerViewAdapter);
185 | }
186 |
187 | @Override
188 | public void onDestroy() {
189 | super.onDestroy();
190 | // Remove the listener.
191 |
192 | // Close the realm instance.
193 | realm.close();
194 | }
195 |
196 | @Override
197 | public void onDetach() {
198 | super.onDetach();
199 | mListener = null;
200 | }
201 |
202 | /**
203 | * This interface must be implemented by activities that contain this
204 | * fragment to allow an interaction in this fragment to be communicated
205 | * to the activity and potentially other fragments contained in that
206 | * activity.
207 | *
208 | * See the Android Training lesson Communicating with Other Fragments for more information.
211 | */
212 | public interface OnFragmentInteractionListener {
213 | // TODO: Update argument type and name
214 | void onFragmentInteraction(Uri uri);
215 | }
216 |
217 | private class DownloadFilesTask extends AsyncTask, Integer, Long> {
218 | @Override
219 | protected Long doInBackground(ArrayList... params) {
220 |
221 | realm = Realm.getDefaultInstance();
222 | for (int i = 0; i < mList.size(); i++) {
223 | user_id = mList.get(i).getId();
224 | profile_picture = mList.get(i).getProfilePicture();
225 | full_name = mList.get(i).getUsername();
226 | Call call1 = apiService.getRelation(user_id, mInstagramSession.getAccessToken());
227 |
228 | Log.i(TAG, "SETTING ID BEFORE CALLING " + user_id);
229 |
230 | try {
231 | RelationshipData mRelationshipData = call1.execute().body();
232 |
233 | Log.i(TAG, "SETTING ID " + user_id);
234 |
235 | RelationshipData1 users1 = mRelationshipData.getData();
236 |
237 | users1.setId(user_id);
238 |
239 | realm.beginTransaction();
240 |
241 | if (!mInstagramSession.getFirst_time_variable().equals("true")) {
242 |
243 | Log.i(TAG, mInstagramSession.getFirst_time_variable() + "First time initialization");
244 |
245 | RealmResults query = realm.where(RelationshipData1.class)
246 | .equalTo("id", user_id).findAll();
247 |
248 | oldIncommingStatus = query.get(0).getIncomingStatus();
249 |
250 | Log.i(TAG, "QUERY INITIATED " + oldIncommingStatus);
251 |
252 | outgoingStatus = query.get(0).getOutgoingStatus();
253 |
254 | }
255 | realm.copyToRealmOrUpdate(users1);
256 |
257 | realm.commitTransaction();
258 |
259 | RealmResults query1 = realm.where(RelationshipData1.class)
260 | .equalTo("id", user_id).findAll();
261 |
262 |
263 | newIncommingStatus = query1.get(0).getIncomingStatus();
264 | Log.i(TAG, "2nd QUERY " + newIncommingStatus);
265 |
266 | Log.i(TAG, users1.getOutgoingStatus() + "");
267 | Log.i(TAG, users1.getIncomingStatus() + "");
268 | Log.i(TAG, users1.getTargetUserIsPrivate() + "");
269 |
270 | if (!mInstagramSession.getFirst_time_variable().equals("true")) {
271 | Log.i(TAG, mInstagramSession.getFirst_time_variable() + "First time initialization");
272 |
273 | if ((outgoingStatus.equals("follows")) && (!oldIncommingStatus.equals(newIncommingStatus))
274 | && (oldIncommingStatus.equals("followed_by"))) {
275 | Log.i(TAG, "Un Follow");
276 | final Datum datum = new Datum();
277 | datum.setId(user_id);
278 | datum.setProfilePicture(profile_picture);
279 | datum.setFullName(full_name);
280 | realm.executeTransaction(new Realm.Transaction() {
281 |
282 | @Override
283 | public void execute(Realm realm) {
284 |
285 | realm.copyToRealmOrUpdate(datum);
286 |
287 | }
288 | });
289 |
290 |
291 | } else if (outgoingStatus.equals("follows") && (!oldIncommingStatus.equals(newIncommingStatus))
292 | && newIncommingStatus.equals("followed_by")) {
293 | Log.i(TAG, "Followed Back");
294 | realm.beginTransaction();
295 | realm.where(Datum.class).beginsWith("id", user_id).findAll().deleteFirstFromRealm();
296 | realm.commitTransaction();
297 |
298 | }
299 |
300 | }
301 |
302 | } catch (IOException e) {
303 | e.printStackTrace();
304 | }
305 |
306 | }
307 | return null;
308 | }
309 |
310 | protected void onProgressUpdate(Integer... progress) {
311 |
312 | }
313 |
314 |
315 | protected void onPostExecute(Long result) {
316 | if (mInstagramSession.getFirst_time_variable().equals("true")) {
317 | mInstagramSession.setFirst_time_variable();
318 | }
319 | spinner.setVisibility(View.GONE);
320 | recycleView();
321 | }
322 | }
323 | }
324 |
--------------------------------------------------------------------------------