Skip to content

Commit 317e87f

Browse files
committed
refactoring instagram client
1 parent 63ef5dc commit 317e87f

File tree

10 files changed

+82
-52
lines changed

10 files changed

+82
-52
lines changed

app/src/main/java/com/ebr163/socialauth/MainActivity.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
public class MainActivity extends AppCompatActivity implements View.OnClickListener,
2222
VkClient.VkProfileLoadedListener, VkClient.VkLogOutListener,
2323
FacebookClient.FacebookProfileLoadedListener, FacebookClient.FacebookLogOutListener,
24-
GooglePlusClient.GooglePlusProfileLoadedListener, GooglePlusClient.GooglePlusLogOutListener {
24+
GooglePlusClient.GooglePlusProfileLoadedListener, GooglePlusClient.GooglePlusLogOutListener,
25+
InstagramClient.InstagramProfileLoadedListener, InstagramClient.InstagramLogOutListener {
2526

2627
GooglePlusClient googlePlusClient;
2728
InstagramClient instagramClient;
@@ -36,7 +37,9 @@ protected void onCreate(Bundle savedInstanceState) {
3637
googlePlusClient.setGooglePlusProfileLoadedListener(this);
3738
googlePlusClient.setGooglePlusLogOutListener(this);
3839

39-
instagramClient = new InstagramClient(this, getString(R.string.instagramRedirectUri), getString(R.string.instagramClientId));
40+
instagramClient = new InstagramClient(this);
41+
instagramClient.setInstagramProfileLoadedListener(this);
42+
instagramClient.setInstagramLogOutListener(this);
4043

4144
facebookClient = new FacebookClient(this);
4245
facebookClient.setFacebookProfileLoadedListener(this);
@@ -57,21 +60,10 @@ public void onClick(View view) {
5760
googlePlusClient.logOut();
5861
break;
5962
case R.id.instagram:
60-
instagramClient.getProfile(new InstagramClient.InstagramProfileLoadedListener() {
61-
@Override
62-
public void onProfileLoaded(InstagramProfile instagramProfile) {
63-
Toast.makeText(MainActivity.this, instagramProfile.toString(), Toast.LENGTH_SHORT).show();
64-
toNextScreen();
65-
}
66-
});
63+
instagramClient.getProfile();
6764
break;
6865
case R.id.instagram_logout:
69-
instagramClient.logOut(new InstagramClient.InstagramLogOutListener() {
70-
@Override
71-
public void onLogOut() {
72-
Toast.makeText(MainActivity.this, "instagram logout", Toast.LENGTH_SHORT).show();
73-
}
74-
});
66+
instagramClient.logOut();
7567
break;
7668
case R.id.facebook:
7769
facebookClient.getProfile();
@@ -100,8 +92,8 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
10092
}
10193

10294
private void toNextScreen() {
103-
// startActivity(new Intent(MainActivity.this, ProfileActivity.class));
104-
// finish();
95+
startActivity(new Intent(MainActivity.this, ProfileActivity.class));
96+
finish();
10597
}
10698

10799
@Override
@@ -112,6 +104,7 @@ public void onLogOutVk() {
112104
@Override
113105
public void onVkProfileLoaded(VkProfile vkProfile) {
114106
Toast.makeText(MainActivity.this, "vk", Toast.LENGTH_SHORT).show();
107+
toNextScreen();
115108
}
116109

117110
@Override
@@ -147,4 +140,20 @@ public void onGooglePlusProfileLoaded(GooglePlusProfile googlePlusProfile) {
147140
@Override
148141
public void onErrorGooglePlusProfileLoaded(Exception exeption) {
149142
}
143+
144+
@Override
145+
public void onLogOutInstagram() {
146+
Toast.makeText(MainActivity.this, "instagram logout", Toast.LENGTH_SHORT).show();
147+
}
148+
149+
@Override
150+
public void onInstagramProfileLoaded(InstagramProfile instagramProfile) {
151+
Toast.makeText(MainActivity.this, instagramProfile.toString(), Toast.LENGTH_SHORT).show();
152+
toNextScreen();
153+
}
154+
155+
@Override
156+
public void onErrorInstagramProfileLoaded(Exception exception) {
157+
158+
}
150159
}

app/src/main/java/com/ebr163/socialauth/ProfileActivity.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected void onCreate(Bundle savedInstanceState) {
2121
super.onCreate(savedInstanceState);
2222
setContentView(R.layout.activity_profile);
2323
googlePlusClient = new GooglePlusClient(this);
24-
instagramClient = new InstagramClient(this, getString(R.string.instagramRedirectUri), getString(R.string.instagramClientId));
24+
instagramClient = new InstagramClient(this);
2525
facebookClient = new FacebookClient(this);
2626
}
2727

@@ -31,12 +31,6 @@ public void onClick(View view) {
3131
case R.id.google_plus_logout:
3232
break;
3333
case R.id.instagram_logout:
34-
instagramClient.logOut(new InstagramClient.InstagramLogOutListener() {
35-
@Override
36-
public void onLogOut() {
37-
signOut();
38-
}
39-
});
4034
break;
4135
case R.id.facebook_logout:
4236
break;

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<string name="google_app_id">200921782501-vp97n0glaqr8la3odt4v5ahqfaggtp9t.apps.googleusercontent.com</string>
55

6-
<string name="instagramRedirectUri">https://oauth.vk.com/blank.html</string>
7-
<string name="instagramClientId">bc0cfc8d49664f9eb3f920acef8c73e1</string>
6+
<string name="instagram_redirect_uri">https://oauth.vk.com/blank.html</string>
7+
<string name="instagram_app_id">bc0cfc8d49664f9eb3f920acef8c73e1</string>
88
<string name="facebook_app_id">360833260945108</string>
99
</resources>

facebook/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,5 @@ dependencies {
3232
})
3333
testCompile 'junit:junit:4.12'
3434

35-
compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION"
3635
compile 'com.facebook.android:facebook-android-sdk:4.+'
3736
}

facebook/src/main/java/com/ebr163/socialauth/facebook/FacebookClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.ebr163.socialauth.facebook;
22

33
import android.app.Activity;
4+
import android.app.Fragment;
45
import android.content.Intent;
56
import android.os.Bundle;
6-
import android.support.v4.app.Fragment;
77

88
import com.ebr163.socialauth.facebook.model.FacebookProfile;
99
import com.ebr163.socialauth.facebook.model.Location;

google/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ dependencies {
2828
})
2929
testCompile 'junit:junit:4.12'
3030

31-
compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION"
3231
compile "com.google.android.gms:play-services-auth:$GOOGLE_PLAY_SERVICE_VERSION"
3332
compile "com.google.android.gms:play-services-plus:$GOOGLE_PLAY_SERVICE_VERSION"
3433
}

google/src/main/java/com/ebr163/socialauth/google/GooglePlusClient.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.ebr163.socialauth.google;
22

33
import android.app.Activity;
4+
import android.app.Fragment;
5+
import android.content.Context;
46
import android.content.Intent;
57
import android.support.annotation.NonNull;
6-
import android.support.v4.app.Fragment;
78
import android.support.v4.app.FragmentActivity;
89
import android.util.Log;
910

@@ -133,12 +134,18 @@ public void onResult(Status status) {
133134
}
134135
}
135136

137+
private Context getContext() {
138+
if (fragment != null) return fragment.getActivity();
139+
else return activity;
140+
}
141+
136142
private String getStringResByName(String aString) {
137-
int resId = activity.getResources().getIdentifier(aString, "string", activity.getPackageName());
143+
Context context = getContext();
144+
int resId = context.getResources().getIdentifier(aString, "string", context.getPackageName());
138145
try {
139-
return activity.getResources().getString(resId);
146+
return context.getResources().getString(resId);
140147
} catch (Exception e) {
141-
googlePlusProfileLoadedListener.onErrorGooglePlusProfileLoaded(new Exception("Not find google_app_id"));
148+
googlePlusProfileLoadedListener.onErrorGooglePlusProfileLoaded(new Exception("Not find " + aString));
142149
return null;
143150
}
144151
}

instagram/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ dependencies {
2727
exclude group: 'com.android.support', module: 'support-annotations'
2828
})
2929
testCompile 'junit:junit:4.12'
30-
compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION"
3130
compile 'com.squareup.retrofit2:retrofit:2.1.0'
3231
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
3332
}

instagram/src/main/java/com/ebr163/socialauth/instagram/InstagramClient.java

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.app.Fragment;
55
import android.content.Context;
66
import android.content.Intent;
7-
import android.util.Log;
87

98
import com.ebr163.socialauth.instagram.model.InstagramProfile;
109
import com.ebr163.socialauth.instagram.model.InstagramResponse;
@@ -21,18 +20,24 @@
2120
public class InstagramClient {
2221
private static final int INSTAGRAM_AUTH_CODE = 300;
2322
private static final int INSTAGRAM_LOG_OUT_CODE = 301;
23+
private static final String INSTAGRAM_APP_ID = "instagram_app_id";
24+
private static final String INSTAGRAM_REDIRECT_URI = "instagram_redirect_uri";
2425

2526
private Fragment fragment;
2627
private Activity activity;
2728
private AuthorizationListener authListener;
2829
private LogOutListener logOutListener;
30+
private InstagramLogOutListener instagramLogOutListener;
31+
private InstagramProfileLoadedListener instagramProfileLoadedListener;
2932

3033
public interface InstagramProfileLoadedListener {
31-
void onProfileLoaded(InstagramProfile instagramProfile);
34+
void onInstagramProfileLoaded(InstagramProfile instagramProfile);
35+
36+
void onErrorInstagramProfileLoaded(Exception exception);
3237
}
3338

3439
public interface InstagramLogOutListener {
35-
void onLogOut();
40+
void onLogOutInstagram();
3641
}
3742

3843
private interface AuthorizationListener {
@@ -43,35 +48,43 @@ private interface LogOutListener {
4348
void onLogOut();
4449
}
4550

46-
private InstagramClient(String redirectUri, String clientId) {
47-
InstagramConfig.getInstance().setClientId(clientId);
48-
InstagramConfig.getInstance().setRedirectUri(redirectUri);
49-
}
50-
51-
public InstagramClient(Fragment fragment, String redirectUri, String clientId) {
52-
this(redirectUri, clientId);
51+
public InstagramClient(Fragment fragment) {
5352
this.fragment = fragment;
53+
init();
5454
}
5555

56-
public InstagramClient(Activity activity, String redirectUri, String clientId) {
57-
this(redirectUri, clientId);
56+
public InstagramClient(Activity activity) {
5857
this.activity = activity;
58+
init();
5959
}
6060

61-
public void getProfile(final InstagramProfileLoadedListener listener) {
61+
private void init() {
62+
InstagramConfig.getInstance().setClientId(getStringResByName(INSTAGRAM_APP_ID));
63+
InstagramConfig.getInstance().setRedirectUri(getStringResByName(INSTAGRAM_REDIRECT_URI));
64+
}
65+
66+
public void setInstagramLogOutListener(InstagramLogOutListener instagramLogOutListener) {
67+
this.instagramLogOutListener = instagramLogOutListener;
68+
}
69+
70+
public void setInstagramProfileLoadedListener(InstagramProfileLoadedListener instagramProfileLoadedListener) {
71+
this.instagramProfileLoadedListener = instagramProfileLoadedListener;
72+
}
73+
74+
public void getProfile() {
6275
authorize(new AuthorizationListener() {
6376
@Override
6477
public void onAuthorized() {
6578
new InstagramRestClient().getService(InstagramService.class)
6679
.getProfile(getAccessToken()).enqueue(new Callback<InstagramResponse<InstagramProfile>>() {
6780
@Override
6881
public void onResponse(Call<InstagramResponse<InstagramProfile>> call, Response<InstagramResponse<InstagramProfile>> response) {
69-
listener.onProfileLoaded(response.body().getData());
82+
instagramProfileLoadedListener.onInstagramProfileLoaded(response.body().getData());
7083
}
7184

7285
@Override
7386
public void onFailure(Call<InstagramResponse<InstagramProfile>> call, Throwable t) {
74-
Log.i("TAG", "onFailure: " + t.getMessage());
87+
instagramProfileLoadedListener.onErrorInstagramProfileLoaded(new Exception(t));
7588
}
7689
});
7790
}
@@ -108,11 +121,22 @@ private Context getContext() {
108121
else return activity;
109122
}
110123

111-
public void logOut(final InstagramLogOutListener instagramLogOutListener) {
124+
private String getStringResByName(String aString) {
125+
Context context = getContext();
126+
int resId = context.getResources().getIdentifier(aString, "string", context.getPackageName());
127+
try {
128+
return context.getResources().getString(resId);
129+
} catch (Exception e) {
130+
instagramProfileLoadedListener.onErrorInstagramProfileLoaded(new Exception("Not find " + aString));
131+
return null;
132+
}
133+
}
134+
135+
public void logOut() {
112136
logOutListener = new LogOutListener() {
113137
@Override
114138
public void onLogOut() {
115-
instagramLogOutListener.onLogOut();
139+
instagramLogOutListener.onLogOutInstagram();
116140
}
117141
};
118142

vk/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@ dependencies {
2828
})
2929
testCompile 'junit:junit:4.12'
3030

31-
compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION"
3231
compile "com.vk:androidsdk:$VK_SDK_VERSION"
3332
}

0 commit comments

Comments
 (0)