From 2fe175223cee79e60a5549a96988c709b174021c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 5 Oct 2019 01:10:40 +0200 Subject: [PATCH 1/4] combine commits * add Explore Function * define API * Add Fragment + Code + String * add to main activity (by @mmarif) --- .../mian/gitnex/activities/MainActivity.java | 6 + .../ExploreRepositoriesFragment.java | 222 ++++++++++++++++++ .../mian/gitnex/interfaces/ApiInterface.java | 5 +- .../viewmodels/ExploreRepoListViewModel.java | 74 ++++++ .../main/res/layout/fragment_explore_repo.xml | 44 ++++ app/src/main/res/menu/drawer_menu.xml | 3 + app/src/main/res/values/strings.xml | 2 + 7 files changed, 355 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/org/mian/gitnex/fragments/ExploreRepositoriesFragment.java create mode 100644 app/src/main/java/org/mian/gitnex/viewmodels/ExploreRepoListViewModel.java create mode 100644 app/src/main/res/layout/fragment_explore_repo.xml diff --git a/app/src/main/java/org/mian/gitnex/activities/MainActivity.java b/app/src/main/java/org/mian/gitnex/activities/MainActivity.java index 0d0b877a..8a113645 100644 --- a/app/src/main/java/org/mian/gitnex/activities/MainActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/MainActivity.java @@ -21,6 +21,7 @@ import com.squareup.picasso.Picasso; import org.mian.gitnex.R; import org.mian.gitnex.clients.RetrofitClient; import org.mian.gitnex.fragments.AboutFragment; +import org.mian.gitnex.fragments.ExploreRepositoriesFragment; import org.mian.gitnex.fragments.MyRepositoriesFragment; import org.mian.gitnex.fragments.NavSubMenuBottomSheetFragment; import org.mian.gitnex.fragments.OrganizationsFragment; @@ -278,6 +279,11 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new StarredRepositoriesFragment()).commit(); break; + case R.id.nav_explore: + getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, + new ExploreRepositoriesFragment()).commit(); + break; + } drawer.closeDrawer(GravityCompat.START); diff --git a/app/src/main/java/org/mian/gitnex/fragments/ExploreRepositoriesFragment.java b/app/src/main/java/org/mian/gitnex/fragments/ExploreRepositoriesFragment.java new file mode 100644 index 00000000..13f06215 --- /dev/null +++ b/app/src/main/java/org/mian/gitnex/fragments/ExploreRepositoriesFragment.java @@ -0,0 +1,222 @@ +package org.mian.gitnex.fragments; + +import android.net.Uri; +import android.os.Bundle; +import android.os.Handler; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.view.inputmethod.EditorInfo; +import android.widget.ProgressBar; +import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.Observer; +import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.DividerItemDecoration; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; +import org.mian.gitnex.R; +import org.mian.gitnex.activities.MainActivity; +import org.mian.gitnex.adapters.MyReposListAdapter; +import org.mian.gitnex.helpers.Authorization; +import org.mian.gitnex.models.UserRepositories; +import org.mian.gitnex.util.AppUtil; +import org.mian.gitnex.util.TinyDB; +import org.mian.gitnex.viewmodels.ExploreRepoListViewModel; +import java.util.List; +import java.util.Objects; + +/** ++ * Template Author M M Arif ++ * Author 6543 ++ */ + +public class ExploreRepositoriesFragment extends Fragment { + + private static final String ARG_PARAM1 = "param1"; + private static final String ARG_PARAM2 = "param2"; + private ProgressBar mProgressBar; + private RecyclerView mRecyclerView; + private MyReposListAdapter adapter; + private TextView noData; + private String searchKeyword = "test"; //test value + + private String mParam1; + private String mParam2; + + private OnFragmentInteractionListener mListener; + + public ExploreRepositoriesFragment() { + } + + public static ExploreRepositoriesFragment newInstance(String param1, String param2) { + ExploreRepositoriesFragment fragment = new ExploreRepositoriesFragment(); + Bundle args = new Bundle(); + args.putString(ARG_PARAM1, param1); + args.putString(ARG_PARAM2, param2); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mParam1 = getArguments().getString(ARG_PARAM1); + mParam2 = getArguments().getString(ARG_PARAM2); + } + } + + @Override + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + + boolean connToInternet = AppUtil.haveNetworkConnection(Objects.requireNonNull(getContext())); + + final View v = inflater.inflate(R.layout.fragment_explore_repo, container, false); + setHasOptionsMenu(true); + ((MainActivity) Objects.requireNonNull(getActivity())).setActionBarTitle(getResources().getString(R.string.pageTitleExplore)); + + TinyDB tinyDb = new TinyDB(getContext()); + final String instanceUrl = tinyDb.getString("instanceUrl"); + final String loginUid = tinyDb.getString("loginUid"); + final String instanceToken = "token " + tinyDb.getString(loginUid + "-token"); + + final SwipeRefreshLayout swipeRefresh = v.findViewById(R.id.pullToRefresh); + + noData = v.findViewById(R.id.noData); + mProgressBar = v.findViewById(R.id.progress_bar); + mRecyclerView = v.findViewById(R.id.recyclerView); + mRecyclerView.setHasFixedSize(true); + mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + + DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(), + DividerItemDecoration.VERTICAL); + mRecyclerView.addItemDecoration(dividerItemDecoration); + + mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { + super.onScrollStateChanged(recyclerView, newState); + } + }); + + if(connToInternet) { + + swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + swipeRefresh.setRefreshing(false); + ExploreRepoListViewModel.loadReposList(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), searchKeyword); + } + }, 50); + } + }); + + fetchDataAsync(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), searchKeyword); + + } + else { + mProgressBar.setVisibility(View.GONE); + } + + return v; + + } + + @Override + public void onResume() { + super.onResume(); + TinyDB tinyDb = new TinyDB(getContext()); + final String instanceUrl = tinyDb.getString("instanceUrl"); + final String loginUid = tinyDb.getString("loginUid"); + final String instanceToken = "token " + tinyDb.getString(loginUid + "-token"); + + ExploreRepoListViewModel.loadReposList(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), searchKeyword); + + } + + private void fetchDataAsync(String instanceUrl, String instanceToken, String searchKeyword) { + + searchKeyword = this.searchKeyword; //test + + ExploreRepoListViewModel RepoModel = new ViewModelProvider(this).get(ExploreRepoListViewModel.class); + + RepoModel.getUserRepositories(instanceUrl, instanceToken, searchKeyword).observe(this, new Observer>() { + @Override + public void onChanged(@Nullable List myReposListMain) { + adapter = new MyReposListAdapter(getContext(), myReposListMain); + if(adapter.getItemCount() > 0) { + mRecyclerView.setAdapter(adapter); + noData.setVisibility(View.GONE); + } + else { + adapter.notifyDataSetChanged(); + mRecyclerView.setAdapter(adapter); + noData.setVisibility(View.VISIBLE); + } + mProgressBar.setVisibility(View.GONE); + } + }); + + } + + @Override + public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { + + boolean connToInternet = AppUtil.haveNetworkConnection(Objects.requireNonNull(getContext())); + + inflater.inflate(R.menu.search_menu, menu); + super.onCreateOptionsMenu(menu, inflater); + + MenuItem searchItem = menu.findItem(R.id.action_search); + androidx.appcompat.widget.SearchView searchView = (androidx.appcompat.widget.SearchView) searchItem.getActionView(); + searchView.setImeOptions(EditorInfo.IME_ACTION_DONE); + searchView.setQueryHint(getContext().getString(R.string.strFilter)); + + if(!connToInternet) { + return; + } + + searchView.setOnQueryTextListener(new androidx.appcompat.widget.SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + return false; + } + + @Override + public boolean onQueryTextChange(String newText) { + if(mRecyclerView.getAdapter() != null) { + adapter.getFilter().filter(newText); + } + return false; + } + }); + + } + + public void onButtonPressed(Uri uri) { + if (mListener != null) { + mListener.onFragmentInteraction(uri); + } + } + + @Override + public void onDetach() { + super.onDetach(); + mListener = null; + } + + public interface OnFragmentInteractionListener { + void onFragmentInteraction(Uri uri); + } +} diff --git a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java index ff37c741..0fe77a5d 100644 --- a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java +++ b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java @@ -213,6 +213,9 @@ public interface ApiInterface { @GET("repos/{owner}/{repo}/subscribers") // get all repo watchers Call> getRepoWatchers(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName); + @GET("repos/search") // get all repos who match query string + Call> queryRepos(@Header("Authorization") String token, @Query("q") String searchKeyword, @Query("limit") int limit, @Query("mode") String mode, @Query("sort") String sort, @Query("order") String order); + @POST("repos/{owner}/{repo}/contents/{file}") // create new file Call createNewFile(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("file") String fileName, @Body NewFile jsonStr); @@ -224,4 +227,4 @@ public interface ApiInterface { @GET("repos/{owner}/{repo}/contents/{fileDir}") // get all the sub files and dirs of a repository Call> getDirFiles(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("fileDir") String fileDir); -} \ No newline at end of file +} diff --git a/app/src/main/java/org/mian/gitnex/viewmodels/ExploreRepoListViewModel.java b/app/src/main/java/org/mian/gitnex/viewmodels/ExploreRepoListViewModel.java new file mode 100644 index 00000000..dfde6c4d --- /dev/null +++ b/app/src/main/java/org/mian/gitnex/viewmodels/ExploreRepoListViewModel.java @@ -0,0 +1,74 @@ +package org.mian.gitnex.viewmodels; + +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; + +import org.mian.gitnex.clients.RetrofitClient; +import org.mian.gitnex.models.UserRepositories; + +import java.util.List; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + +/** + * Template Author M M Arif + * Author 6543 + */ + +public class ExploreRepoListViewModel extends ViewModel { + + private static MutableLiveData> reposList; + + public LiveData> getUserRepositories(String instanceUrl, String token, String searchKeyword) { + + //if (reposList == null) { + reposList = new MutableLiveData<>(); + loadReposList(instanceUrl, token, searchKeyword); + //} + + return reposList; + } + + + public static void loadReposList(String instanceUrl, String token, String searchKeyword) { + + int limit = 10; //page size of results, maximum page size is 50 + String mode = ""; //type of repository to search for. Supported values are "fork", "source", “mirror” and “collaborative” + String sort = "alpha"; //sort repos by attribute. Supported values are "alpha", "created", "updated", "size", and "id". Default is “alpha” + String order = "asc"; //sort order, either “asc” (ascending) or “desc” (descending). Default is "asc", ignored if “sort” is not specified. + + Call> call = RetrofitClient + .getInstance(instanceUrl) + .getApiInterface() + .queryRepos(token, searchKeyword, limit, mode, sort, order); + + call.enqueue(new Callback>() { + + @Override + public void onResponse(@NonNull Call> call, @NonNull Response> response) { + + if(response.isSuccessful()) { + if(response.code() == 200) { + reposList.postValue(response.body()); + + } + } + + } + + @Override + public void onFailure(@NonNull Call> call, Throwable t) { + Log.i("onFailure", t.toString()); + } + + }); + + } + +} diff --git a/app/src/main/res/layout/fragment_explore_repo.xml b/app/src/main/res/layout/fragment_explore_repo.xml new file mode 100644 index 00000000..2d248d44 --- /dev/null +++ b/app/src/main/res/layout/fragment_explore_repo.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/drawer_menu.xml b/app/src/main/res/menu/drawer_menu.xml index 94116fa0..c49a0d3e 100644 --- a/app/src/main/res/menu/drawer_menu.xml +++ b/app/src/main/res/menu/drawer_menu.xml @@ -17,6 +17,9 @@ + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9e5ea1c3..d75b498d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -30,6 +30,7 @@ About Rate GitNex Logout + Explore @@ -53,6 +54,7 @@ New Team Add Email Address New File + Explore Version\u0020:\u0020 From d5cffddc72999fb784553f8c21544ed55ba8c3df Mon Sep 17 00:00:00 2001 From: M M Arif Date: Thu, 10 Oct 2019 18:40:43 +0500 Subject: [PATCH 2/4] Implemented search ui and screen for exploring repositories. --- .../adapters/ExploreRepositoriesAdapter.java | 214 ++++++++++++++++++ .../ExploreRepositoriesFragment.java | 165 ++++++-------- .../mian/gitnex/interfaces/ApiInterface.java | 5 +- .../gitnex/models/ExploreRepositories.java | 22 ++ .../viewmodels/ExploreRepoListViewModel.java | 74 ------ .../main/res/layout/fragment_explore_repo.xml | 55 +++-- app/src/main/res/values/strings.xml | 1 + 7 files changed, 340 insertions(+), 196 deletions(-) create mode 100644 app/src/main/java/org/mian/gitnex/adapters/ExploreRepositoriesAdapter.java create mode 100644 app/src/main/java/org/mian/gitnex/models/ExploreRepositories.java delete mode 100644 app/src/main/java/org/mian/gitnex/viewmodels/ExploreRepoListViewModel.java diff --git a/app/src/main/java/org/mian/gitnex/adapters/ExploreRepositoriesAdapter.java b/app/src/main/java/org/mian/gitnex/adapters/ExploreRepositoriesAdapter.java new file mode 100644 index 00000000..b75c2da1 --- /dev/null +++ b/app/src/main/java/org/mian/gitnex/adapters/ExploreRepositoriesAdapter.java @@ -0,0 +1,214 @@ +package org.mian.gitnex.adapters; + +import android.content.Context; +import android.content.Intent; +import android.graphics.Typeface; +import android.view.LayoutInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.appcompat.view.ContextThemeWrapper; +import androidx.appcompat.widget.PopupMenu; +import androidx.recyclerview.widget.RecyclerView; +import com.amulyakhare.textdrawable.TextDrawable; +import com.amulyakhare.textdrawable.util.ColorGenerator; +import com.squareup.picasso.Picasso; +import org.mian.gitnex.R; +import org.mian.gitnex.activities.OpenRepoInBrowserActivity; +import org.mian.gitnex.activities.RepoStargazersActivity; +import org.mian.gitnex.activities.RepoWatchersActivity; +import org.mian.gitnex.helpers.RoundedTransformation; +import org.mian.gitnex.models.UserRepositories; +import java.lang.reflect.Field; +import java.util.List; + +/** + * Author M M Arif + */ + +public class ExploreRepositoriesAdapter extends RecyclerView.Adapter { + + + private List searchedReposList; + private Context mCtx; + + public ExploreRepositoriesAdapter(List dataList, Context mCtx) { + this.mCtx = mCtx; + this.searchedReposList = dataList; + } + + static class ReposSearchViewHolder extends RecyclerView.ViewHolder { + + private ImageView image; + private TextView mTextView1; + private TextView mTextView2; + private TextView fullName; + private ImageView repoPrivatePublic; + private TextView repoStars; + private TextView repoForks; + private TextView repoOpenIssuesCount; + + private ReposSearchViewHolder(View itemView) { + super(itemView); + + mTextView1 = itemView.findViewById(R.id.repoName); + mTextView2 = itemView.findViewById(R.id.repoDescription); + image = itemView.findViewById(R.id.imageAvatar); + fullName = itemView.findViewById(R.id.repoFullName); + repoPrivatePublic = itemView.findViewById(R.id.imageRepoType); + repoStars = itemView.findViewById(R.id.repoStars); + repoForks = itemView.findViewById(R.id.repoForks); + repoOpenIssuesCount = itemView.findViewById(R.id.repoOpenIssuesCount); + ImageView reposDropdownMenu = itemView.findViewById(R.id.reposDropdownMenu); + + /*itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + Context context = v.getContext(); + TextView repoFullName = v.findViewById(R.id.repoFullName); + + Intent intent = new Intent(context, RepoDetailActivity.class); + intent.putExtra("repoFullName", repoFullName.getText().toString()); + + TinyDB tinyDb = new TinyDB(context); + tinyDb.putString("repoFullName", repoFullName.getText().toString()); + tinyDb.putBoolean("resumeIssues", true); + context.startActivity(intent); + + } + });*/ + + reposDropdownMenu.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + final Context context = v.getContext(); + Context context_ = new ContextThemeWrapper(context, R.style.popupMenuStyle); + + PopupMenu popupMenu = new PopupMenu(context_, v); + popupMenu.inflate(R.menu.repo_dotted_list_menu); + + Object menuHelper; + Class[] argTypes; + try { + + Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup"); + fMenuHelper.setAccessible(true); + menuHelper = fMenuHelper.get(popupMenu); + argTypes = new Class[] { boolean.class }; + menuHelper.getClass().getDeclaredMethod("setForceShowIcon", + argTypes).invoke(menuHelper, true); + + } catch (Exception e) { + + popupMenu.show(); + return; + + } + + popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + switch (item.getItemId()) { + case R.id.repoStargazers: + + Intent intent = new Intent(context, RepoStargazersActivity.class); + intent.putExtra("repoFullNameForStars", fullName.getText()); + context.startActivity(intent); + break; + + case R.id.repoWatchers: + + Intent intentW = new Intent(context, RepoWatchersActivity.class); + intentW.putExtra("repoFullNameForWatchers", fullName.getText()); + context.startActivity(intentW); + break; + + case R.id.repoOpenInBrowser: + + Intent intentOpenInBrowser = new Intent(context, OpenRepoInBrowserActivity.class); + intentOpenInBrowser.putExtra("repoFullNameBrowser", fullName.getText()); + context.startActivity(intentOpenInBrowser); + break; + + } + return false; + } + }); + + popupMenu.show(); + + } + }); + + } + + } + + @NonNull + @Override + public ExploreRepositoriesAdapter.ReposSearchViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.repos_list, parent, false); + return new ExploreRepositoriesAdapter.ReposSearchViewHolder(v); + } + + @Override + public void onBindViewHolder(@NonNull final ExploreRepositoriesAdapter.ReposSearchViewHolder holder, int position) { + + final UserRepositories currentItem = searchedReposList.get(position); + + + holder.mTextView2.setVisibility(View.GONE); + + ColorGenerator generator = ColorGenerator.MATERIAL; + int color = generator.getColor(currentItem.getName()); + String firstCharacter = String.valueOf(currentItem.getName().charAt(0)); + + TextDrawable drawable = TextDrawable.builder() + .beginConfig() + .useFont(Typeface.DEFAULT) + .fontSize(18) + .toUpperCase() + .width(28) + .height(28) + .endConfig() + .buildRoundRect(firstCharacter, color, 3); + + if (currentItem.getAvatar_url() != null) { + if (!currentItem.getAvatar_url().equals("")) { + Picasso.get().load(currentItem.getAvatar_url()).transform(new RoundedTransformation(8, 0)).resize(120, 120).centerCrop().into(holder.image); + } else { + holder.image.setImageDrawable(drawable); + } + } + else { + holder.image.setImageDrawable(drawable); + } + + holder.mTextView1.setText(currentItem.getName()); + if (!currentItem.getDescription().equals("")) { + holder.mTextView2.setVisibility(View.VISIBLE); + holder.mTextView2.setText(currentItem.getDescription()); + } + holder.fullName.setText(currentItem.getFullname()); + if(currentItem.getPrivateFlag()) { + holder.repoPrivatePublic.setImageResource(R.drawable.ic_lock_bold); + } + else { + holder.repoPrivatePublic.setImageResource(R.drawable.ic_public); + } + holder.repoStars.setText(currentItem.getStars_count()); + holder.repoForks.setText(currentItem.getForks_count()); + holder.repoOpenIssuesCount.setText(currentItem.getOpen_issues_count()); + + } + + @Override + public int getItemCount() { + return searchedReposList.size(); + } +} diff --git a/app/src/main/java/org/mian/gitnex/fragments/ExploreRepositoriesFragment.java b/app/src/main/java/org/mian/gitnex/fragments/ExploreRepositoriesFragment.java index 13f06215..bf5c853d 100644 --- a/app/src/main/java/org/mian/gitnex/fragments/ExploreRepositoriesFragment.java +++ b/app/src/main/java/org/mian/gitnex/fragments/ExploreRepositoriesFragment.java @@ -1,36 +1,35 @@ package org.mian.gitnex.fragments; +import android.content.Context; import android.net.Uri; import android.os.Bundle; -import android.os.Handler; +import android.util.Log; +import android.view.KeyEvent; import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.EditorInfo; import android.widget.ProgressBar; import android.widget.TextView; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; -import androidx.lifecycle.Observer; -import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import org.mian.gitnex.R; import org.mian.gitnex.activities.MainActivity; -import org.mian.gitnex.adapters.MyReposListAdapter; +import org.mian.gitnex.adapters.ExploreRepositoriesAdapter; +import org.mian.gitnex.clients.RetrofitClient; import org.mian.gitnex.helpers.Authorization; +import org.mian.gitnex.models.ExploreRepositories; import org.mian.gitnex.models.UserRepositories; import org.mian.gitnex.util.AppUtil; import org.mian.gitnex.util.TinyDB; -import org.mian.gitnex.viewmodels.ExploreRepoListViewModel; import java.util.List; import java.util.Objects; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; /** + * Template Author M M Arif @@ -39,16 +38,15 @@ import java.util.Objects; public class ExploreRepositoriesFragment extends Fragment { - private static final String ARG_PARAM1 = "param1"; - private static final String ARG_PARAM2 = "param2"; + private static String repoNameF = "param2"; + private static String repoOwnerF = "param1"; private ProgressBar mProgressBar; private RecyclerView mRecyclerView; - private MyReposListAdapter adapter; private TextView noData; - private String searchKeyword = "test"; //test value - - private String mParam1; - private String mParam2; + private TextView searchKeyword; + private Boolean repoTypeInclude = true; + private String sort = "updated"; + private String order = "asc"; private OnFragmentInteractionListener mListener; @@ -58,8 +56,8 @@ public class ExploreRepositoriesFragment extends Fragment { public static ExploreRepositoriesFragment newInstance(String param1, String param2) { ExploreRepositoriesFragment fragment = new ExploreRepositoriesFragment(); Bundle args = new Bundle(); - args.putString(ARG_PARAM1, param1); - args.putString(ARG_PARAM2, param2); + args.putString(repoOwnerF, param1); + args.putString(repoNameF, param2); fragment.setArguments(args); return fragment; } @@ -68,8 +66,8 @@ public class ExploreRepositoriesFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { - mParam1 = getArguments().getString(ARG_PARAM1); - mParam2 = getArguments().getString(ARG_PARAM2); + String repoName = getArguments().getString(repoNameF); + String repoOwner = getArguments().getString(repoOwnerF); } } @@ -80,7 +78,7 @@ public class ExploreRepositoriesFragment extends Fragment { boolean connToInternet = AppUtil.haveNetworkConnection(Objects.requireNonNull(getContext())); final View v = inflater.inflate(R.layout.fragment_explore_repo, container, false); - setHasOptionsMenu(true); + //setHasOptionsMenu(true); ((MainActivity) Objects.requireNonNull(getActivity())).setActionBarTitle(getResources().getString(R.string.pageTitleExplore)); TinyDB tinyDb = new TinyDB(getContext()); @@ -88,42 +86,26 @@ public class ExploreRepositoriesFragment extends Fragment { final String loginUid = tinyDb.getString("loginUid"); final String instanceToken = "token " + tinyDb.getString(loginUid + "-token"); - final SwipeRefreshLayout swipeRefresh = v.findViewById(R.id.pullToRefresh); - + searchKeyword = v.findViewById(R.id.searchKeyword); noData = v.findViewById(R.id.noData); mProgressBar = v.findViewById(R.id.progress_bar); - mRecyclerView = v.findViewById(R.id.recyclerView); - mRecyclerView.setHasFixedSize(true); - mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); - - DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(), - DividerItemDecoration.VERTICAL); - mRecyclerView.addItemDecoration(dividerItemDecoration); - - mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { - super.onScrollStateChanged(recyclerView, newState); - } - }); + mRecyclerView = v.findViewById(R.id.recyclerViewReposSearch); if(connToInternet) { - swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + searchKeyword.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override - public void onRefresh() { - new Handler().postDelayed(new Runnable() { - @Override - public void run() { - swipeRefresh.setRefreshing(false); - ExploreRepoListViewModel.loadReposList(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), searchKeyword); + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { + if (actionId == EditorInfo.IME_ACTION_SEND) { + if(!searchKeyword.getText().toString().equals("")) { + mProgressBar.setVisibility(View.VISIBLE); + loadSearchReposList(instanceUrl, instanceToken, loginUid, searchKeyword.getText().toString(), repoTypeInclude, sort, order, getContext()); } - }, 50); + } + return false; } }); - fetchDataAsync(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), searchKeyword); - } else { mProgressBar.setVisibility(View.GONE); @@ -133,74 +115,59 @@ public class ExploreRepositoriesFragment extends Fragment { } - @Override - public void onResume() { - super.onResume(); - TinyDB tinyDb = new TinyDB(getContext()); - final String instanceUrl = tinyDb.getString("instanceUrl"); - final String loginUid = tinyDb.getString("loginUid"); - final String instanceToken = "token " + tinyDb.getString(loginUid + "-token"); + private void loadSearchReposList(String instanceUrl, String instanceToken, String loginUid, String searchKeyword, Boolean repoTypeInclude, String sort, String order, final Context context) { - ExploreRepoListViewModel.loadReposList(instanceUrl, Authorization.returnAuthentication(getContext(), loginUid, instanceToken), searchKeyword); + Call call = RetrofitClient + .getInstance(instanceUrl) + .getApiInterface() + .queryRepos(Authorization.returnAuthentication(getContext(), loginUid, instanceToken), searchKeyword, repoTypeInclude, sort, order); - } + call.enqueue(new Callback() { - private void fetchDataAsync(String instanceUrl, String instanceToken, String searchKeyword) { - - searchKeyword = this.searchKeyword; //test - - ExploreRepoListViewModel RepoModel = new ViewModelProvider(this).get(ExploreRepoListViewModel.class); - - RepoModel.getUserRepositories(instanceUrl, instanceToken, searchKeyword).observe(this, new Observer>() { @Override - public void onChanged(@Nullable List myReposListMain) { - adapter = new MyReposListAdapter(getContext(), myReposListMain); - if(adapter.getItemCount() > 0) { - mRecyclerView.setAdapter(adapter); - noData.setVisibility(View.GONE); + public void onResponse(@NonNull Call call, @NonNull Response response) { + + if (response.isSuccessful()) { + assert response.body() != null; + getReposList(response.body().getSearchedData(), context); + } else { + Log.i("onResponse", String.valueOf(response.code())); } - else { - adapter.notifyDataSetChanged(); - mRecyclerView.setAdapter(adapter); - noData.setVisibility(View.VISIBLE); - } - mProgressBar.setVisibility(View.GONE); + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + Log.i("onFailure", t.getMessage()); + } + }); } - @Override - public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { + private void getReposList(List dataList, Context context) { - boolean connToInternet = AppUtil.haveNetworkConnection(Objects.requireNonNull(getContext())); + ExploreRepositoriesAdapter adapter = new ExploreRepositoriesAdapter(dataList, context); - inflater.inflate(R.menu.search_menu, menu); - super.onCreateOptionsMenu(menu, inflater); + mRecyclerView.setHasFixedSize(true); + mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(), + DividerItemDecoration.VERTICAL); + mRecyclerView.addItemDecoration(dividerItemDecoration); - MenuItem searchItem = menu.findItem(R.id.action_search); - androidx.appcompat.widget.SearchView searchView = (androidx.appcompat.widget.SearchView) searchItem.getActionView(); - searchView.setImeOptions(EditorInfo.IME_ACTION_DONE); - searchView.setQueryHint(getContext().getString(R.string.strFilter)); + if(adapter.getItemCount() > 0) { + + mRecyclerView.setAdapter(adapter); + noData.setVisibility(View.GONE); + mProgressBar.setVisibility(View.GONE); - if(!connToInternet) { - return; } + else { - searchView.setOnQueryTextListener(new androidx.appcompat.widget.SearchView.OnQueryTextListener() { - @Override - public boolean onQueryTextSubmit(String query) { - return false; - } + noData.setVisibility(View.VISIBLE); + mProgressBar.setVisibility(View.GONE); - @Override - public boolean onQueryTextChange(String newText) { - if(mRecyclerView.getAdapter() != null) { - adapter.getFilter().filter(newText); - } - return false; - } - }); + } } diff --git a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java index 0fe77a5d..9c488dea 100644 --- a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java +++ b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java @@ -3,6 +3,7 @@ package org.mian.gitnex.interfaces; import com.google.gson.JsonElement; import org.mian.gitnex.models.AddEmail; import org.mian.gitnex.models.Branches; +import org.mian.gitnex.models.ExploreRepositories; import org.mian.gitnex.models.Files; import org.mian.gitnex.models.NewFile; import org.mian.gitnex.models.UpdateIssueAssignee; @@ -213,8 +214,8 @@ public interface ApiInterface { @GET("repos/{owner}/{repo}/subscribers") // get all repo watchers Call> getRepoWatchers(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName); - @GET("repos/search") // get all repos who match query string - Call> queryRepos(@Header("Authorization") String token, @Query("q") String searchKeyword, @Query("limit") int limit, @Query("mode") String mode, @Query("sort") String sort, @Query("order") String order); + @GET("repos/search") // get all the repos which match the query string + Call queryRepos(@Header("Authorization") String token, @Query("q") String searchKeyword, @Query("private") Boolean repoTypeInclude, @Query("sort") String sort, @Query("order") String order); @POST("repos/{owner}/{repo}/contents/{file}") // create new file Call createNewFile(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("file") String fileName, @Body NewFile jsonStr); diff --git a/app/src/main/java/org/mian/gitnex/models/ExploreRepositories.java b/app/src/main/java/org/mian/gitnex/models/ExploreRepositories.java new file mode 100644 index 00000000..07aab8dc --- /dev/null +++ b/app/src/main/java/org/mian/gitnex/models/ExploreRepositories.java @@ -0,0 +1,22 @@ +package org.mian.gitnex.models; + +import java.util.ArrayList; + +/** + * Author M M Arif + */ + +public class ExploreRepositories { + + private ArrayList data; + private Boolean ok; + + public ArrayList getSearchedData() { + return data; + } + + public Boolean getOk() { + return ok; + } + +} diff --git a/app/src/main/java/org/mian/gitnex/viewmodels/ExploreRepoListViewModel.java b/app/src/main/java/org/mian/gitnex/viewmodels/ExploreRepoListViewModel.java deleted file mode 100644 index dfde6c4d..00000000 --- a/app/src/main/java/org/mian/gitnex/viewmodels/ExploreRepoListViewModel.java +++ /dev/null @@ -1,74 +0,0 @@ -package org.mian.gitnex.viewmodels; - -import android.util.Log; - -import androidx.annotation.NonNull; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.ViewModel; - -import org.mian.gitnex.clients.RetrofitClient; -import org.mian.gitnex.models.UserRepositories; - -import java.util.List; - -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; - -/** - * Template Author M M Arif - * Author 6543 - */ - -public class ExploreRepoListViewModel extends ViewModel { - - private static MutableLiveData> reposList; - - public LiveData> getUserRepositories(String instanceUrl, String token, String searchKeyword) { - - //if (reposList == null) { - reposList = new MutableLiveData<>(); - loadReposList(instanceUrl, token, searchKeyword); - //} - - return reposList; - } - - - public static void loadReposList(String instanceUrl, String token, String searchKeyword) { - - int limit = 10; //page size of results, maximum page size is 50 - String mode = ""; //type of repository to search for. Supported values are "fork", "source", “mirror” and “collaborative” - String sort = "alpha"; //sort repos by attribute. Supported values are "alpha", "created", "updated", "size", and "id". Default is “alpha” - String order = "asc"; //sort order, either “asc” (ascending) or “desc” (descending). Default is "asc", ignored if “sort” is not specified. - - Call> call = RetrofitClient - .getInstance(instanceUrl) - .getApiInterface() - .queryRepos(token, searchKeyword, limit, mode, sort, order); - - call.enqueue(new Callback>() { - - @Override - public void onResponse(@NonNull Call> call, @NonNull Response> response) { - - if(response.isSuccessful()) { - if(response.code() == 200) { - reposList.postValue(response.body()); - - } - } - - } - - @Override - public void onFailure(@NonNull Call> call, Throwable t) { - Log.i("onFailure", t.toString()); - } - - }); - - } - -} diff --git a/app/src/main/res/layout/fragment_explore_repo.xml b/app/src/main/res/layout/fragment_explore_repo.xml index 2d248d44..de000a22 100644 --- a/app/src/main/res/layout/fragment_explore_repo.xml +++ b/app/src/main/res/layout/fragment_explore_repo.xml @@ -1,25 +1,28 @@ - + android:background="@color/colorPrimary" + android:orientation="vertical"> - - - - - + android:layout_height="wrap_content" + android:padding="10dp" + android:textSize="14sp" + android:layout_marginStart="10dp" + android:layout_marginEnd="10dp" + android:layout_marginTop="20dp" + android:layout_marginBottom="20dp" + android:inputType="text" + android:background="@drawable/shape_inputs" + android:textColor="@color/white" + android:textColorHint="@color/colorAccent" + android:hint="@string/exploreTextBoxHint" + android:textColorHighlight="@color/white" + android:imeOptions="actionSend" /> @@ -37,8 +40,18 @@ style="@style/Base.Widget.AppCompat.ProgressBar" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_centerInParent="true" android:indeterminate="true" - android:visibility="visible" /> + android:visibility="gone" /> - \ No newline at end of file + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d75b498d..6dea7bec 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -503,5 +503,6 @@ Translate GitNex with Crowdin + Explore repositories From d285115090620a9d668f3505fee37090dc671e60 Mon Sep 17 00:00:00 2001 From: M M Arif Date: Thu, 10 Oct 2019 19:01:15 +0500 Subject: [PATCH 3/4] reset the adapter on new query --- .../mian/gitnex/fragments/ExploreRepositoriesFragment.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/mian/gitnex/fragments/ExploreRepositoriesFragment.java b/app/src/main/java/org/mian/gitnex/fragments/ExploreRepositoriesFragment.java index bf5c853d..cfe44e82 100644 --- a/app/src/main/java/org/mian/gitnex/fragments/ExploreRepositoriesFragment.java +++ b/app/src/main/java/org/mian/gitnex/fragments/ExploreRepositoriesFragment.java @@ -48,6 +48,8 @@ public class ExploreRepositoriesFragment extends Fragment { private String sort = "updated"; private String order = "asc"; + private ExploreRepositoriesAdapter adapter; + private OnFragmentInteractionListener mListener; public ExploreRepositoriesFragment() { @@ -99,6 +101,7 @@ public class ExploreRepositoriesFragment extends Fragment { if (actionId == EditorInfo.IME_ACTION_SEND) { if(!searchKeyword.getText().toString().equals("")) { mProgressBar.setVisibility(View.VISIBLE); + mRecyclerView.setVisibility(View.GONE); loadSearchReposList(instanceUrl, instanceToken, loginUid, searchKeyword.getText().toString(), repoTypeInclude, sort, order, getContext()); } } @@ -147,7 +150,9 @@ public class ExploreRepositoriesFragment extends Fragment { private void getReposList(List dataList, Context context) { - ExploreRepositoriesAdapter adapter = new ExploreRepositoriesAdapter(dataList, context); + adapter = new ExploreRepositoriesAdapter(dataList, context); + + mRecyclerView.setVisibility(View.VISIBLE); mRecyclerView.setHasFixedSize(true); mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); From 2f55e7f5d1943bb5e3fc8c3a9d6b591e9a97f3ed Mon Sep 17 00:00:00 2001 From: M M Arif Date: Thu, 10 Oct 2019 20:49:12 +0500 Subject: [PATCH 4/4] enable to browse repository --- .../mian/gitnex/adapters/ExploreRepositoriesAdapter.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/mian/gitnex/adapters/ExploreRepositoriesAdapter.java b/app/src/main/java/org/mian/gitnex/adapters/ExploreRepositoriesAdapter.java index b75c2da1..391cbfd2 100644 --- a/app/src/main/java/org/mian/gitnex/adapters/ExploreRepositoriesAdapter.java +++ b/app/src/main/java/org/mian/gitnex/adapters/ExploreRepositoriesAdapter.java @@ -18,10 +18,12 @@ import com.amulyakhare.textdrawable.util.ColorGenerator; import com.squareup.picasso.Picasso; import org.mian.gitnex.R; import org.mian.gitnex.activities.OpenRepoInBrowserActivity; +import org.mian.gitnex.activities.RepoDetailActivity; import org.mian.gitnex.activities.RepoStargazersActivity; import org.mian.gitnex.activities.RepoWatchersActivity; import org.mian.gitnex.helpers.RoundedTransformation; import org.mian.gitnex.models.UserRepositories; +import org.mian.gitnex.util.TinyDB; import java.lang.reflect.Field; import java.util.List; @@ -64,7 +66,7 @@ public class ExploreRepositoriesAdapter extends RecyclerView.Adapter