Implement Deep Link to open reddit links directly in this app.

This commit is contained in:
Alex Ning 2019-08-01 13:23:47 +08:00
parent 47515b87d0
commit a2f301f75e
5 changed files with 104 additions and 9 deletions

View File

@ -73,7 +73,6 @@
android:theme="@style/AppTheme.NoActionBarWithTransparentStatusBar"> android:theme="@style/AppTheme.NoActionBarWithTransparentStatusBar">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
@ -92,15 +91,42 @@
<activity <activity
android:name=".ViewPostDetailActivity" android:name=".ViewPostDetailActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar" /> android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"
android:host="www.reddit.com"
android:pathPattern="/r/..*/comments/..*"/>
</intent-filter>
</activity>
<activity <activity
android:name=".ViewSubredditDetailActivity" android:name=".ViewSubredditDetailActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow" /> android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"
android:host="www.reddit.com"
android:pathPattern="/r/..*"/>
</intent-filter>
</activity>
<activity <activity
android:name=".ViewUserDetailActivity" android:name=".ViewUserDetailActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow" /> android:theme="@style/AppTheme.NoActionBarWithTranslucentWindow">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"
android:host="www.reddit.com"
android:pathPattern="/user/..*"/>
</intent-filter>
</activity>
<provider <provider
android:name="androidx.core.content.FileProvider" android:name="androidx.core.content.FileProvider"

View File

@ -44,6 +44,8 @@ import retrofit2.Retrofit;
public class MainActivity extends AppCompatActivity implements SortTypeBottomSheetFragment.SortTypeSelectionCallback, public class MainActivity extends AppCompatActivity implements SortTypeBottomSheetFragment.SortTypeSelectionCallback,
PostTypeBottomSheetFragment.PostTypeSelectionCallback { PostTypeBottomSheetFragment.PostTypeSelectionCallback {
static final String EXTRA_POST_TYPE = "EPT";
private static final String FETCH_USER_INFO_STATE = "FUIS"; private static final String FETCH_USER_INFO_STATE = "FUIS";
private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS";
@ -138,6 +140,14 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
if (savedInstanceState != null) { if (savedInstanceState != null) {
mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE); mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE);
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
} else {
if(getIntent().hasExtra(EXTRA_POST_TYPE)) {
if(getIntent().getExtras().getString(EXTRA_POST_TYPE).equals("popular")) {
viewPager.setCurrentItem(1);
} else {
viewPager.setCurrentItem(2);
}
}
} }
glide = Glide.with(this); glide = Glide.with(this);

View File

@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
@ -32,6 +33,7 @@ import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import javax.inject.Inject; import javax.inject.Inject;
@ -62,6 +64,8 @@ public class ViewPostDetailActivity extends AppCompatActivity {
@State @State
Post mPost; Post mPost;
@State @State
Uri postUri;
@State
boolean isLoadingMoreChildren = false; boolean isLoadingMoreChildren = false;
@State @State
boolean isRefreshing = false; boolean isRefreshing = false;
@ -122,13 +126,17 @@ public class ViewPostDetailActivity extends AppCompatActivity {
orientation = getResources().getConfiguration().orientation; orientation = getResources().getConfiguration().orientation;
if(mPost == null) { if(mPost == null) {
mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA); if(getIntent().getData() != null) {
postUri = getIntent().getData();
} else {
mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA);
}
} }
if(mPost == null) { if(postUri == null && mPost == null) {
mProgressBar.setVisibility(View.VISIBLE); mProgressBar.setVisibility(View.VISIBLE);
fetchPostAndCommentsById(getIntent().getExtras().getString(EXTRA_POST_ID)); fetchPostAndCommentsById(getIntent().getExtras().getString(EXTRA_POST_ID));
} else { } else if(mPost != null) {
mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit,
mOauthRetrofit, mGlide, mSharedPreferences, mPost, mOauthRetrofit, mGlide, mSharedPreferences, mPost,
mPost.getSubredditNamePrefixed(), mLocale, mLoadSubredditIconAsyncTask, mPost.getSubredditNamePrefixed(), mLocale, mLoadSubredditIconAsyncTask,
@ -162,6 +170,17 @@ public class ViewPostDetailActivity extends AppCompatActivity {
} }
} }
} }
} else {
mProgressBar.setVisibility(View.VISIBLE);
List<String> segments = postUri.getPathSegments();
int commentIndex = segments.indexOf("comments");
if(commentIndex >= 0 || commentIndex != segments.size() - 1) {
mProgressBar.setVisibility(View.VISIBLE);
fetchPostAndCommentsById(segments.get(commentIndex + 1));
} else {
//Deep link error handling
finish();
}
} }
if(getIntent().hasExtra(EXTRA_POST_LIST_POSITION)) { if(getIntent().hasExtra(EXTRA_POST_LIST_POSITION)) {

View File

@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
@ -26,6 +27,8 @@ import com.google.android.material.chip.Chip;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -63,6 +66,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
@BindView(R.id.fab_view_subreddit_detail_activity) FloatingActionButton fab; @BindView(R.id.fab_view_subreddit_detail_activity) FloatingActionButton fab;
private String subredditName; private String subredditName;
private Uri subredditUri;
private boolean subscriptionReady = false; private boolean subscriptionReady = false;
private boolean isInLazyMode = false; private boolean isInLazyMode = false;
@ -113,7 +117,26 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
statusBarHeight = getResources().getDimensionPixelSize(resourceId); statusBarHeight = getResources().getDimensionPixelSize(resourceId);
} }
subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY); if(getIntent().getData() != null) {
subredditUri = getIntent().getData();
List<String> segments = subredditUri.getPathSegments();
int rIndex = segments.indexOf("r");
if(rIndex >= 0 && rIndex < segments.size() - 1) {
subredditName = segments.get(rIndex + 1);
if(subredditName.equals("popular") || subredditName.equals("all")) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_POST_TYPE, subredditName);
startActivity(intent);
finish();
}
} else {
//Deep link error handling
finish();
}
} else {
subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY);
}
String title = "r/" + subredditName; String title = "r/" + subredditName;
subredditNameTextView.setText(title); subredditNameTextView.setText(title);

View File

@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
@ -29,6 +30,8 @@ import com.google.android.material.chip.Chip;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -71,6 +74,7 @@ public class ViewUserDetailActivity extends AppCompatActivity {
private AppBarLayout.LayoutParams params; private AppBarLayout.LayoutParams params;
private String userName; private String userName;
private Uri userUri;
private boolean subscriptionReady = false; private boolean subscriptionReady = false;
private boolean isInLazyMode = false; private boolean isInLazyMode = false;
private int colorPrimary; private int colorPrimary;
@ -106,7 +110,20 @@ public class ViewUserDetailActivity extends AppCompatActivity {
statusBarHeight = getResources().getDimensionPixelSize(resourceId); statusBarHeight = getResources().getDimensionPixelSize(resourceId);
} }
userName = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY); if(getIntent().getData() != null) {
userUri = getIntent().getData();
List<String> segments = userUri.getPathSegments();
int userIndex = segments.indexOf("user");
if(userIndex >= 0 && userIndex < segments.size() - 1) {
userName = segments.get(userIndex + 1);
} else {
//Deep link error handling
finish();
}
} else {
userName = getIntent().getExtras().getString(EXTRA_USER_NAME_KEY);
}
String title = "u/" + userName; String title = "u/" + userName;
userNameTextView.setText(title); userNameTextView.setText(title);