diff options
Diffstat (limited to '')
3 files changed, 175 insertions, 17 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index bbd6e60c..3aec9917 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -1,19 +1,11 @@ package ml.docilealligator.infinityforreddit; -import androidx.lifecycle.ViewModelProviders; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.Color; import android.os.AsyncTask; import android.os.Bundle; -import androidx.fragment.app.Fragment; -import androidx.core.view.GravityCompat; -import androidx.drawerlayout.widget.DrawerLayout; -import androidx.appcompat.app.ActionBarDrawerToggle; -import androidx.appcompat.app.AppCompatActivity; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; -import androidx.appcompat.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -23,6 +15,7 @@ import android.widget.TextView; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; +import com.ferfalk.simplesearchview.SimpleSearchView; import java.util.ArrayList; import java.util.List; @@ -41,6 +34,15 @@ import SubscribedUserDatabase.SubscribedUserDao; import SubscribedUserDatabase.SubscribedUserData; import SubscribedUserDatabase.SubscribedUserRoomDatabase; import SubscribedUserDatabase.SubscribedUserViewModel; +import androidx.appcompat.app.ActionBarDrawerToggle; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.core.view.GravityCompat; +import androidx.drawerlayout.widget.DrawerLayout; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProviders; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import butterknife.BindView; import butterknife.ButterKnife; import de.hdodenhof.circleimageview.CircleImageView; @@ -56,6 +58,8 @@ public class MainActivity extends AppCompatActivity { private static final String FETCH_USER_INFO_STATE = "FUIS"; private static final String INSERT_SUBSCRIBED_SUBREDDIT_STATE = "ISSS"; + @BindView(R.id.search_view_main_activity) SimpleSearchView simpleSearchView; + @BindView(R.id.transparent_overlay_main_activity) View transparentOverlay; @BindView(R.id.subscribed_subreddit_recycler_view_main_activity) RecyclerView subscribedSubredditRecyclerView; @BindView(R.id.subscriptions_label_main_activity) TextView subscriptionsLabelTextView; @BindView(R.id.subscribed_user_recycler_view_main_activity) RecyclerView subscribedUserRecyclerView; @@ -111,6 +115,52 @@ public class MainActivity extends AppCompatActivity { drawer.addDrawerListener(toggle); toggle.syncState(); + transparentOverlay.setOnClickListener(view -> simpleSearchView.onBackPressed()); + + simpleSearchView.setOnQueryTextListener(new SimpleSearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + Intent intent = new Intent(MainActivity.this, SearchActivity.class); + intent.putExtra(SearchActivity.QUERY_KEY, query); + startActivity(intent); + 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); + getWindow().setStatusBarColor(Color.GRAY); + } + + @Override + public void onSearchViewClosed() { + transparentOverlay.setVisibility(View.GONE); + getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark)); + } + + @Override + public void onSearchViewShownAnimation() { + + } + + @Override + public void onSearchViewClosedAnimation() { + + } + }); + String accessToken = getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); if (accessToken.equals("")) { Intent loginIntent = new Intent(this, LoginActivity.class); @@ -266,8 +316,20 @@ public class MainActivity 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; } @@ -292,6 +354,10 @@ public class MainActivity extends AppCompatActivity { if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { + if (simpleSearchView.onBackPressed()) { + return; + } + super.onBackPressed(); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 39344b73..95d0f173 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -3,13 +3,11 @@ package ml.docilealligator.infinityforreddit; import java.util.Map; import retrofit2.Call; -import retrofit2.http.DELETE; import retrofit2.http.FieldMap; import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; import retrofit2.http.HeaderMap; import retrofit2.http.POST; -import retrofit2.http.PUT; import retrofit2.http.Path; import retrofit2.http.Query; @@ -53,15 +51,21 @@ public interface RedditAPI { @POST("api/subscribe") Call<String> subredditSubscription(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); - @PUT("api/v1/me/friends/username") - Call<String> subscribeUser(@HeaderMap Map<String, String> headers); - - @DELETE("api/v1/me/friends/username") - Call<String> unsubscribeUser(@HeaderMap Map<String, String> headers); - @GET("api/morechildren?api_type=json&raw_json=1") Call<String> getMoreChildren(@Query("link_id") String linkId, @Query("children") String children); @GET("{subredditNamePrefixed}/api/info.json?raw_json=1") Call<String> getInfo(@Path("subredditNamePrefixed") String subredditNamePrefixed, @Query("id") String id); + + @GET("subreddits/search.json?raw_json=1&include_over_18=on") + Call<String> searchSubreddits(@Query("q") String subredditName, @Query("after") String after); + + @GET("/profiles/search.json?raw_json=1") + Call<String> searchProfiles(@Query("q") String profileName, @Query("after") String after); + + @GET("/search.json?raw_json=1") + Call<String> searchPosts(@Query("q") String query, @Query("after") String after); + + @GET("/r/{subredditName}/search.json?raw_json=1&restrict_sr=true") + Call<String> searchPostsInSpecificSubreddit(@Query("q") String query, @Query("after") String after); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java new file mode 100644 index 00000000..51099a9a --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java @@ -0,0 +1,88 @@ +package ml.docilealligator.infinityforreddit; + +import android.content.Intent; +import android.os.Bundle; +import android.view.MenuItem; + +import com.google.android.material.tabs.TabLayout; + +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; + +public class SearchActivity extends AppCompatActivity { + static final String QUERY_KEY = "QK"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_search); + + 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); + tabLayout.setupWithViewPager(viewPager); + + // Get the intent, verify the action and get the query + Intent intent = getIntent(); + String query = intent.getExtras().getString(QUERY_KEY); + if(query != null) { + setTitle(query); + } + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + onBackPressed(); + return true; + } + return super.onOptionsItemSelected(item); + } + + public class SectionsPagerAdapter extends FragmentPagerAdapter { + + public SectionsPagerAdapter(FragmentManager fm) { + super(fm); + } + + @Override + public Fragment getItem(int position) { + // getItem is called to instantiate the fragment for the given page. + // Return a PlaceholderFragment (defined as a static inner class below). + PostFragment mFragment = new PostFragment(); + Bundle bundle = new Bundle(); + bundle.putInt(PostFragment.POST_TYPE_KEY, PostDataSource.TYPE_FRONT_PAGE); + mFragment.setArguments(bundle); + return mFragment; + } + + @Override + public int getCount() { + return 3; + } + + @Override + public CharSequence getPageTitle(int position) { + switch (position) { + case 0: + return "Posts"; + case 1: + return "Subreddits"; + case 2: + return "Users"; + } + return null; + } + } +} |