Add two features: refresh searching and searching in SearchActivity. Fixed a bug which is the listener object is null when it is called because the parse failed in ParsePost, ParseSubredditData and ParseUserData.

This commit is contained in:
Alex Ning 2019-02-26 09:27:51 +08:00
parent b1db59fda2
commit 0ea2b24c60
13 changed files with 223 additions and 96 deletions

12
.idea/misc.xml generated
View File

@ -2,10 +2,10 @@
<project version="4">
<component name="NullableNotNullManager">
<option name="myDefaultNullable" value="android.support.annotation.Nullable" />
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
<option name="myDefaultNotNull" value="androidx.annotation.NonNull" />
<option name="myNullables">
<value>
<list size="7">
<list size="10">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
<item index="2" class="java.lang.String" itemvalue="javax.annotation.CheckForNull" />
@ -13,18 +13,24 @@
<item index="4" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
<item index="5" class="java.lang.String" itemvalue="androidx.annotation.Nullable" />
<item index="6" class="java.lang.String" itemvalue="androidx.annotation.RecentlyNullable" />
<item index="7" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.qual.Nullable" />
<item index="8" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NullableDecl" />
<item index="9" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NullableType" />
</list>
</value>
</option>
<option name="myNotNulls">
<value>
<list size="6">
<list size="9">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
<item index="4" class="java.lang.String" itemvalue="androidx.annotation.NonNull" />
<item index="5" class="java.lang.String" itemvalue="androidx.annotation.RecentlyNonNull" />
<item index="6" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.qual.NonNull" />
<item index="7" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NonNullDecl" />
<item index="8" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NonNullType" />
</list>
</value>
</option>

View File

@ -75,8 +75,6 @@ public class MainActivity extends AppCompatActivity {
private boolean mFetchUserInfoSuccess;
private boolean mInsertSuccess;
private FragmentCommunicator mFragmentCommunicator;
private SubscribedSubredditViewModel mSubscribedSubredditViewModel;
private SubscribedUserViewModel mSubscribedUserViewModel;

View File

@ -37,19 +37,24 @@ class ParsePost {
ParsePostDataAsyncTask(String response, Locale locale,
ParsePostListener parsePostListener) {
this.parsePostListener = parsePostListener;
try {
jsonResponse = new JSONObject(response);
this.locale = locale;
this.parsePostListener = parsePostListener;
newPosts = new ArrayList<>();
parseFailed = false;
} catch (JSONException e) {
e.printStackTrace();
parseFailed = true;
}
}
@Override
protected Void doInBackground(Void... voids) {
if(parseFailed) {
return null;
}
try {
JSONArray allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);

View File

@ -116,7 +116,6 @@ class ParseSubredditData {
} catch (JSONException e) {
Log.i("subreddit json error", e.getMessage());
parseFailed = true;
parseSubredditListingDataListener.onParseSubredditListingDataFail();
}
}

View File

@ -52,24 +52,6 @@ public class ParseUserData {
protected Void doInBackground(Void... voids) {
try {
userData = parseUserDataBase(jsonResponse);
/*jsonResponse = jsonResponse.getJSONObject(JSONUtils.DATA_KEY);
String userName = jsonResponse.getString(JSONUtils.NAME_KEY);
String iconImageUrl = jsonResponse.getString(JSONUtils.ICON_IMG_KEY);
String bannerImageUrl = "";
boolean canBeFollowed;
if(jsonResponse.has(JSONUtils.SUBREDDIT_KEY) && !jsonResponse.isNull(JSONUtils.SUBREDDIT_KEY)) {
bannerImageUrl = jsonResponse.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.BANNER_IMG_KEY);
canBeFollowed = true;
} else {
canBeFollowed = false;
}
int linkKarma = jsonResponse.getInt(JSONUtils.LINK_KARMA_KEY);
int commentKarma = jsonResponse.getInt(JSONUtils.COMMENT_KARMA_KEY);
int karma = linkKarma + commentKarma;
boolean isGold = jsonResponse.getBoolean(JSONUtils.IS_GOLD_KEY);
boolean isFriend = jsonResponse.getBoolean(JSONUtils.IS_FRIEND_KEY);
userData = new UserData(userName, iconImageUrl, bannerImageUrl, karma, isGold, isFriend, canBeFollowed);*/
} catch (JSONException e) {
parseFailed = true;
Log.i("parse user data error", e.getMessage());
@ -88,6 +70,7 @@ public class ParseUserData {
}
private static class ParseUserListingDataAsyncTask extends AsyncTask<Void, Void, Void> {
private String response;
private JSONObject jsonResponse;
private ParseUserListingDataListener parseUserListingDataListener;
private String after;
@ -96,24 +79,27 @@ public class ParseUserData {
private ArrayList<UserData> userDataArrayList;
ParseUserListingDataAsyncTask(String response, ParseUserListingDataListener parseUserListingDataListener){
this.parseUserListingDataListener = parseUserListingDataListener;
this.response = response;
try {
jsonResponse = new JSONObject(response);
this.parseUserListingDataListener = parseUserListingDataListener;
parseFailed = false;
userDataArrayList = new ArrayList<>();
} catch (JSONException e) {
Log.i("userdata json error", e.getMessage());
this.parseUserListingDataListener.onParseUserListingDataFailed();
parseFailed = true;
}
}
@Override
protected Void doInBackground(Void... voids) {
try {
after = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
for(int i = 0; i < children.length(); i++) {
userDataArrayList.add(parseUserDataBase(children.getJSONObject(i)));
if(!parseFailed) {
after = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
for(int i = 0; i < children.length(); i++) {
userDataArrayList.add(parseUserDataBase(children.getJSONObject(i)));
}
}
} catch (JSONException e) {
parseFailed = true;
@ -127,7 +113,11 @@ public class ParseUserData {
if(!parseFailed) {
parseUserListingDataListener.onParseUserListingDataSuccess(userDataArrayList, after);
} else {
parseUserListingDataListener.onParseUserListingDataFailed();
if(response.equals("\"{}\"")) {
parseUserListingDataListener.onParseUserListingDataSuccess(new ArrayList<>(), null);
} else {
parseUserListingDataListener.onParseUserListingDataFailed();
}
}
}
}

View File

@ -2,37 +2,97 @@ package ml.docilealligator.infinityforreddit;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.ferfalk.simplesearchview.SimpleSearchView;
import com.google.android.material.tabs.TabLayout;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
public class SearchActivity extends AppCompatActivity {
static final String QUERY_KEY = "QK";
private String mQuery;
@BindView(R.id.toolbar_search_activity) Toolbar toolbar;
@BindView(R.id.search_view_search_activity) SimpleSearchView simpleSearchView;
@BindView(R.id.tab_layout_search_activity) TabLayout tabLayout;
@BindView(R.id.transparent_overlay_search_activity) View transparentOverlay;
@BindView(R.id.view_pager_search_activity) ViewPager viewPager;
private SectionsPagerAdapter sectionsPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
ButterKnife.bind(this);
Toolbar toolbar = findViewById(R.id.toolbar_search_activity);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ViewPager viewPager = findViewById(R.id.view_pager_search_activity);
PagerAdapter pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = findViewById(R.id.tab_layout_search_activity);
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOffscreenPageLimit(2);
tabLayout.setupWithViewPager(viewPager);
transparentOverlay.setOnClickListener(view -> simpleSearchView.onBackPressed());
simpleSearchView.setOnQueryTextListener(new SimpleSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Intent intent = getIntent();
intent.putExtra(SearchActivity.QUERY_KEY, query);
finish();
startActivity(intent);
overridePendingTransition(0, 0);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
@Override
public boolean onQueryTextCleared() {
return false;
}
});
simpleSearchView.setOnSearchViewListener(new SimpleSearchView.SearchViewListener() {
@Override
public void onSearchViewShown() {
transparentOverlay.setVisibility(View.VISIBLE);
}
@Override
public void onSearchViewClosed() {
transparentOverlay.setVisibility(View.GONE);
}
@Override
public void onSearchViewShownAnimation() {
}
@Override
public void onSearchViewClosedAnimation() {
}
});
// Get the intent, verify the action and get the query
Intent intent = getIntent();
String query = intent.getExtras().getString(QUERY_KEY);
@ -42,22 +102,55 @@ public class SearchActivity extends AppCompatActivity {
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (simpleSearchView.onActivityResult(requestCode, resultCode, data)) {
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity, menu);
simpleSearchView.setMenuItem(menu.findItem(R.id.action_search_main_activity));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.action_refresh_main_activity:
sectionsPagerAdapter.refresh();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (simpleSearchView.onBackPressed()) {
return;
}
super.onBackPressed();
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private PostFragment postFragment;
private SubredditListingFragment subredditListingFragment;
private UserListingFragment userListingFragment;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position) {
@ -104,5 +197,41 @@ public class SearchActivity extends AppCompatActivity {
}
return null;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
switch (position) {
case 0:
postFragment = (PostFragment) fragment;
break;
case 1:
subredditListingFragment = (SubredditListingFragment) fragment;
break;
case 2:
userListingFragment = (UserListingFragment) fragment;
break;
}
return fragment;
}
public void refresh() {
if(postFragment != null) {
((FragmentCommunicator) postFragment).refresh();
}
if(subredditListingFragment != null) {
((FragmentCommunicator) subredditListingFragment).refresh();
}
if (userListingFragment != null) {
((FragmentCommunicator) userListingFragment).refresh();
}
}
public void newSearch() {
getItem(0);
getItem(1);
getItem(2);
}
}
}

View File

@ -24,7 +24,7 @@ public class SubredditListingDataSourceFactory extends DataSource.Factory {
@NonNull
@Override
public DataSource create() {
SubredditListingDataSource subredditListingDataSource = new SubredditListingDataSource(retrofit,
subredditListingDataSource = new SubredditListingDataSource(retrofit,
query, onSubredditListingDataFetchedCallback);
subredditListingDataSourceMutableLiveData.postValue(subredditListingDataSource);
return subredditListingDataSource;

View File

@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
@ -31,7 +30,7 @@ import retrofit2.Retrofit;
/**
* A simple {@link Fragment} subclass.
*/
public class SubredditListingFragment extends Fragment {
public class SubredditListingFragment extends Fragment implements FragmentCommunicator {
static final String QUERY_KEY = "QK";
@ -79,9 +78,6 @@ public class SubredditListingFragment extends Fragment {
mQuery = getArguments().getString(QUERY_KEY);
String accessToken = getActivity().getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE)
.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
SubredditListingViewModel.Factory factory = new SubredditListingViewModel.Factory(mRetrofit, mQuery,
new SubredditListingDataSource.OnSubredditListingDataFetchedCallback() {
@Override
@ -116,7 +112,7 @@ public class SubredditListingFragment extends Fragment {
mProgressBar.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mFetchSubredditListingInfoLinearLayout.setOnClickListener(view -> mSubredditListingViewModel.retry());
showErrorView(R.string.load_posts_error);
showErrorView(R.string.search_subreddits_error);
} else {
mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
@ -138,4 +134,9 @@ public class SubredditListingFragment extends Fragment {
Glide.with(this).load(R.drawable.load_post_error_indicator).into(mFetchSubredditListingInfoImageView);
}
}
@Override
public void refresh() {
mSubredditListingViewModel.refresh();
}
}

View File

@ -10,31 +10,31 @@ public class UserListingDataSourceFactory extends DataSource.Factory {
private String query;
private UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback;
private UserListingDataSource UserListingDataSource;
private MutableLiveData<UserListingDataSource> UserListingDataSourceMutableLiveData;
private UserListingDataSource userListingDataSource;
private MutableLiveData<UserListingDataSource> userListingDataSourceMutableLiveData;
UserListingDataSourceFactory(Retrofit retrofit, String query,
UserListingDataSource.OnUserListingDataFetchedCallback onUserListingDataFetchedCallback) {
this.retrofit = retrofit;
this.query = query;
this.onUserListingDataFetchedCallback = onUserListingDataFetchedCallback;
UserListingDataSourceMutableLiveData = new MutableLiveData<>();
userListingDataSourceMutableLiveData = new MutableLiveData<>();
}
@NonNull
@Override
public DataSource create() {
UserListingDataSource UserListingDataSource = new UserListingDataSource(retrofit,
userListingDataSource = new UserListingDataSource(retrofit,
query, onUserListingDataFetchedCallback);
UserListingDataSourceMutableLiveData.postValue(UserListingDataSource);
return UserListingDataSource;
userListingDataSourceMutableLiveData.postValue(userListingDataSource);
return userListingDataSource;
}
public MutableLiveData<UserListingDataSource> getUserListingDataSourceMutableLiveData() {
return UserListingDataSourceMutableLiveData;
return userListingDataSourceMutableLiveData;
}
UserListingDataSource getUserListingDataSource() {
return UserListingDataSource;
return userListingDataSource;
}
}

View File

@ -31,7 +31,7 @@ import retrofit2.Retrofit;
/**
* A simple {@link Fragment} subclass.
*/
public class UserListingFragment extends Fragment {
public class UserListingFragment extends Fragment implements FragmentCommunicator {
static final String QUERY_KEY = "QK";
@BindView(R.id.coordinator_layout_user_listing_fragment)
@ -122,7 +122,7 @@ public class UserListingFragment extends Fragment {
mProgressBar.setVisibility(View.GONE);
} else if(networkState.getStatus().equals(NetworkState.Status.FAILED)) {
mFetchUserListingInfoLinearLayout.setOnClickListener(view -> mUserListingViewModel.retry());
showErrorView(R.string.load_posts_error);
showErrorView(R.string.search_users_error);
} else {
mFetchUserListingInfoLinearLayout.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
@ -144,4 +144,9 @@ public class UserListingFragment extends Fragment {
Glide.with(this).load(R.drawable.load_post_error_indicator).into(mFetchUserListingInfoImageView);
}
}
@Override
public void refresh() {
mUserListingViewModel.refresh();
}
}

View File

@ -293,40 +293,4 @@ public class ViewUserDetailActivity extends AppCompatActivity {
return null;
}
}
/*private static class CheckIsFollowingUserAsyncTask extends AsyncTask<Void, Void, Void> {
private SubscribedUserDao subscribedUserDao;
private String userName;
private SubscribedUserData subscribedUserData;
private CheckIsFollowingUserListener checkIsFollowingUserListener;
interface CheckIsFollowingUserListener {
void isSubscribed();
void isNotSubscribed();
}
CheckIsFollowingUserAsyncTask(SubscribedUserDao subscribedUserDao, String userName,
CheckIsFollowingUserListener checkIsFollowingUserListener) {
this.subscribedUserDao = subscribedUserDao;
this.userName = userName;
this.checkIsFollowingUserListener = checkIsFollowingUserListener;
}
@Override
protected Void doInBackground(Void... voids) {
subscribedUserData = subscribedUserDao.getSubscribedUser(userName);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(subscribedUserData != null) {
checkIsFollowingUserListener.isSubscribed();
} else {
checkIsFollowingUserListener.isNotSubscribed();
}
}
}*/
}

View File

@ -11,12 +11,32 @@
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_search_activity"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_search_activity"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<com.ferfalk.simplesearchview.SimpleSearchView
android:id="@+id/search_view_search_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@android:color/white"
app:voiceSearch="true"
app:searchBackground="@color/colorPrimary"
app:cursorColor="@android:color/white"
app:hintColor="#E0E0E0"
app:iconsTint="@android:color/white"
app:backIconTint="@android:color/white"
app:iconsAlpha="1"
app:backIconAlpha="1" />
</FrameLayout>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout_search_activity"
@ -34,6 +54,14 @@
</com.google.android.material.appbar.AppBarLayout>
<View
android:id="@+id/transparent_overlay_search_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#40000000"
android:elevation="1dp"
android:visibility="gone"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager_search_activity"
android:layout_width="match_parent"

View File

@ -13,6 +13,8 @@
<string name="tap_to_retry">Error loading image. Tap to retry.</string>
<string name="load_posts_error">Error loading posts.\nTap to retry.</string>
<string name="search_subreddits_error">Error searching subreddits.\nTap to retry.</string>
<string name="search_users_error">Error searching users.\nTap to retry.</string>
<string name="no_posts">No posts found.</string>
<string name="no_subreddits">No subreddits found.</string>
<string name="no_users">No users found.</string>