Skip to content

Commit

Permalink
instagram linking
Browse files Browse the repository at this point in the history
  • Loading branch information
zerezhka committed Feb 28, 2021
1 parent 00ddaeb commit cd84f1e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
Expand Down Expand Up @@ -99,7 +98,7 @@ public View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bu
inviteButton = v.findViewById(R.id.invite_button);
invites = v.findViewById(R.id.num_of_invites);
invitePhoneNum = v.findViewById(R.id.invite_phone_num);
webView=v.findViewById(R.id.webView);
webView = v.findViewById(R.id.webView);

followBtn.setOnClickListener(this::onFollowClick);
instagram.setOnClickListener(this::onInstagramClick);
Expand All @@ -114,6 +113,7 @@ public View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bu
inviteButton.setOnClickListener(this::onInviteClick);
}

webView.setVisibility(View.GONE);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

Expand Down Expand Up @@ -313,42 +313,74 @@ private void onInstagramClick(View v){
headers.put("Authorization", "Token "+ClubhouseSession.userToken);
headers.put("CH-UserID", ClubhouseSession.userID);

webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Boolean redirect = checkRedirect(request.getUrl().toString());
view.loadUrl(request.getUrl().toString());
return redirect;
}
if (user.instagram == null) {
webView.setVisibility(View.VISIBLE);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Boolean redirect = checkRedirect(request.getUrl().toString());
view.loadUrl(request.getUrl().toString());
return redirect;
}

public boolean shouldOverrideUrlLoading(WebView view, String url){
Boolean redirect = checkRedirect(url);
view.loadUrl(url);
return redirect;
}
});
webView.loadUrl(
"https://www.instagram.com/oauth/authorize?client_id="+
BuildConfig.INSTAGRAM_APP_ID +
"&redirect_uri=" + UpdateInstagram.REDIRECT_INSTAGRAM_URL +
"&scope=user_profile" +
"&response_type=code",
headers
);
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Boolean redirect = checkRedirect(url);
view.loadUrl(url);
return redirect;
}
});
webView.loadUrl(
"https://www.instagram.com/oauth/authorize?client_id=" +
BuildConfig.INSTAGRAM_APP_ID +
"&redirect_uri=" + UpdateInstagram.REDIRECT_INSTAGRAM_URL +
"&scope=user_profile" +
"&response_type=code",
headers
);
} else {
new AlertDialog.Builder(getActivity())
.setMessage(getString(R.string.confirm_unlink_instagram_title))
.setMessage(getString(R.string.confirm_unlink_instagram))
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i){
new UpdateInstagram(null)
.wrapProgress(getActivity())
.setCallback(new Callback<BaseResponse>(){
@Override
public void onSuccess(BaseResponse result){
instagram.setText(R.string.add_instagram);
webView.setVisibility(View.GONE);
}

@Override
public void onError(ErrorResponse error){
error.showToast(getActivity());
}
})
.exec();
}
})
.setNegativeButton(R.string.no, null)
.show();
}
} else
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://instagram.com/"+user.instagram)));
}

private Boolean checkRedirect(String url){
if (url.startsWith(UpdateInstagram.REDIRECT_INSTAGRAM_URL)) {

new UpdateInstagram(url.substring((UpdateInstagram.REDIRECT_INSTAGRAM_URL+ "?code=").length()-1, url.length()-2)) // last2 chars is #_ by docs https://developers.facebook.com/docs/instagram-basic-display-api/getting-started
// last2 chars is #_ by docs https://developers.facebook.com/docs/instagram-basic-display-api/getting-started
String code = url.substring((UpdateInstagram.REDIRECT_INSTAGRAM_URL+ "?code=").length(), url.length()-2);

new UpdateInstagram(code)
.wrapProgress(getActivity())
.setCallback(new Callback<BaseResponse>(){
@Override
public void onSuccess(BaseResponse result){
user.notificationType=0;
instagram.setText("instagram linked");
instagram.setText(R.string.instagram_linked);
webView.setVisibility(View.GONE);
}

@Override
Expand Down
9 changes: 5 additions & 4 deletions Houseclub/src/main/res/layout/profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@
tools:text="instagram"/>

</LinearLayout>
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="240dp"/>

<LinearLayout
android:id="@+id/inviter_btn"
Expand Down Expand Up @@ -191,6 +187,11 @@
android:text="@string/invite_button_label" />

</LinearLayout>
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="540dp"/>

</LinearLayout>

Expand Down
3 changes: 3 additions & 0 deletions Houseclub/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,7 @@
<string name="log_in_to_activate">Please log in again to activate your account.</string>
<string name="ok">OK</string>
<string name="add_instagram">Link instagram</string>
<string name="confirm_unlink_instagram_title">Unlink instagram?</string>
<string name="confirm_unlink_instagram">Are you sure want to unlink instagram? It can be linked in future</string>
<string name="instagram_linked">Instagram linked, reopen page to see</string>
</resources>

0 comments on commit cd84f1e

Please sign in to comment.