Organization labels (#771)

List, Create, Edit & Delete OrgLabels

Co-authored-by: M M Arif <mmarif@swatian.com>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/771
Reviewed-by: 6543 <6543@noreply.codeberg.org>
This commit is contained in:
M M Arif 2020-11-30 23:20:14 +01:00 committed by 6543
parent b871a1fb22
commit bbbcb56b98
12 changed files with 395 additions and 94 deletions

View File

@ -23,6 +23,7 @@ import org.mian.gitnex.helpers.Toasty;
import org.mian.gitnex.models.CreateLabel;
import org.mian.gitnex.models.Labels;
import org.mian.gitnex.viewmodels.LabelsViewModel;
import org.mian.gitnex.viewmodels.OrganizationLabelsViewModel;
import java.util.Objects;
import retrofit2.Call;
import retrofit2.Callback;
@ -37,6 +38,7 @@ public class CreateLabelActivity extends BaseActivity {
private TextView colorPicker;
private EditText labelName;
private Button createLabelButton;
private TinyDB tinyDB;
@Override
protected int getLayoutResourceId(){
@ -50,20 +52,17 @@ public class CreateLabelActivity extends BaseActivity {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
final TinyDB tinyDb = TinyDB.getInstance(appCtx);
String repoFullName = tinyDb.getString("repoFullName");
tinyDB = TinyDB.getInstance(appCtx);
String repoFullName = tinyDB.getString("repoFullName");
String[] parts = repoFullName.split("/");
final String repoOwner = parts[0];
final String repoName = parts[1];
final String loginUid = tinyDb.getString("loginUid");
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
if(getIntent().getStringExtra("labelAction") != null && Objects.requireNonNull(getIntent().getStringExtra("labelAction")).equals("delete")) {
deleteLabel(instanceToken, repoOwner, repoName, Integer.parseInt(Objects.requireNonNull(getIntent().getStringExtra("labelId"))), loginUid);
deleteLabel(repoOwner, repoName, Integer.parseInt(Objects.requireNonNull(getIntent().getStringExtra("labelId"))));
finish();
return;
}
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
@ -87,7 +86,7 @@ public class CreateLabelActivity extends BaseActivity {
//Log.i("#Hex no alpha", String.format("#%06X", (0xFFFFFF & color)));
colorPicker.setBackgroundColor(color);
tinyDb.putString("labelColor", String.format("#%06X", (0xFFFFFF & color)));
tinyDB.putString("labelColor", String.format("#%06X", (0xFFFFFF & color)));
cp.dismiss();
});
@ -96,7 +95,7 @@ public class CreateLabelActivity extends BaseActivity {
labelName.setText(getIntent().getStringExtra("labelTitle"));
int labelColor_ = Color.parseColor("#" + getIntent().getStringExtra("labelColor"));
colorPicker.setBackgroundColor(labelColor_);
tinyDb.putString("labelColorDefault", "#" + getIntent().getStringExtra("labelColor"));
tinyDB.putString("labelColorDefault", "#" + getIntent().getStringExtra("labelColor"));
TextView toolbar_title = findViewById(R.id.toolbar_title);
toolbar_title.setText(getResources().getString(R.string.pageTitleLabelUpdate));
@ -123,26 +122,23 @@ public class CreateLabelActivity extends BaseActivity {
private void processUpdateLabel() {
final TinyDB tinyDb = TinyDB.getInstance(appCtx);
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
AppUtil appUtil = new AppUtil();
String repoFullName = tinyDb.getString("repoFullName");
String repoFullName = tinyDB.getString("repoFullName");
String[] parts = repoFullName.split("/");
final String repoOwner = parts[0];
final String repoName = parts[1];
final String loginUid = tinyDb.getString("loginUid");
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
String updateLabelName = labelName.getText().toString();
String updateLabelColor;
if(tinyDb.getString("labelColor").isEmpty()) {
if(tinyDB.getString("labelColor").isEmpty()) {
updateLabelColor = tinyDb.getString("labelColorDefault");
updateLabelColor = tinyDB.getString("labelColorDefault");
}
else {
updateLabelColor = tinyDb.getString("labelColor");
updateLabelColor = tinyDB.getString("labelColor");
}
if(!connToInternet) {
@ -164,8 +160,8 @@ public class CreateLabelActivity extends BaseActivity {
}
disableProcessButton();
patchLabel(instanceToken, repoOwner, repoName, updateLabelName, updateLabelColor, Integer.parseInt(
Objects.requireNonNull(getIntent().getStringExtra("labelId"))), loginUid);
patchLabel(repoOwner, repoName, updateLabelName, updateLabelColor, Integer.parseInt(
Objects.requireNonNull(getIntent().getStringExtra("labelId"))));
}
@ -173,24 +169,22 @@ public class CreateLabelActivity extends BaseActivity {
boolean connToInternet = AppUtil.hasNetworkConnection(appCtx);
AppUtil appUtil = new AppUtil();
TinyDB tinyDb = TinyDB.getInstance(appCtx);
String repoFullName = tinyDb.getString("repoFullName");
String repoFullName = tinyDB.getString("repoFullName");
String[] parts = repoFullName.split("/");
final String repoOwner = parts[0];
final String repoName = parts[1];
final String loginUid = tinyDb.getString("loginUid");
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
String newLabelName = labelName.getText().toString();
String newLabelColor;
if(tinyDb.getString("labelColor").isEmpty()) {
if(tinyDB.getString("labelColor").isEmpty()) {
newLabelColor = String.format("#%06X", (0xFFFFFF & ContextCompat.getColor(ctx, R.color.releasePre)));
}
else {
newLabelColor = tinyDb.getString("labelColor");
newLabelColor = tinyDB.getString("labelColor");
}
if(!connToInternet) {
@ -212,19 +206,23 @@ public class CreateLabelActivity extends BaseActivity {
}
disableProcessButton();
createNewLabel(instanceToken, repoOwner, repoName, newLabelName, newLabelColor, loginUid);
createNewLabel(repoOwner, repoName, newLabelName, newLabelColor);
}
private void createNewLabel(final String instanceToken, String repoOwner, String repoName, String newLabelName, String newLabelColor, String loginUid) {
private void createNewLabel(String repoOwner, String repoName, String newLabelName, String newLabelColor) {
CreateLabel createLabelFunc = new CreateLabel(newLabelName, newLabelColor);
final TinyDB tinyDb = TinyDB.getInstance(appCtx);
Call<CreateLabel> call;
call = RetrofitClient
.getApiInterface(ctx)
.createLabel(Authorization.get(ctx), repoOwner, repoName, createLabelFunc);
if(getIntent().getStringExtra("type") != null && Objects.requireNonNull(getIntent().getStringExtra("type")).equals("org")) {
call = RetrofitClient.getApiInterface(ctx).createOrganizationLabel(Authorization.get(ctx), getIntent().getStringExtra("orgName"), createLabelFunc);
}
else {
call = RetrofitClient.getApiInterface(ctx).createLabel(Authorization.get(ctx), repoOwner, repoName, createLabelFunc);
}
call.enqueue(new Callback<CreateLabel>() {
@ -234,8 +232,8 @@ public class CreateLabelActivity extends BaseActivity {
if(response.code() == 201) {
Toasty.success(ctx, getString(R.string.labelCreated));
tinyDb.putString("labelColor", "");
tinyDb.putBoolean("labelsRefresh", true);
tinyDB.putString("labelColor", "");
tinyDB.putBoolean("labelsRefresh", true);
finish();
}
else if(response.code() == 401) {
@ -249,7 +247,7 @@ public class CreateLabelActivity extends BaseActivity {
else {
enableProcessButton();
tinyDb.putString("labelColor", "");
tinyDB.putString("labelColor", "");
Toasty.error(ctx, getString(R.string.labelGeneralError));
}
}
@ -257,7 +255,7 @@ public class CreateLabelActivity extends BaseActivity {
@Override
public void onFailure(@NonNull Call<CreateLabel> call, @NonNull Throwable t) {
tinyDb.putString("labelColor", "");
tinyDB.putString("labelColor", "");
Log.e("onFailure", t.toString());
enableProcessButton();
}
@ -265,16 +263,20 @@ public class CreateLabelActivity extends BaseActivity {
}
private void patchLabel(final String instanceToken, String repoOwner, String repoName, String updateLabelName, String updateLabelColor, int labelId, String loginUid) {
private void patchLabel(String repoOwner, String repoName, String updateLabelName, String updateLabelColor, int labelId) {
CreateLabel createLabelFunc = new CreateLabel(updateLabelName, updateLabelColor);
final TinyDB tinyDb = TinyDB.getInstance(appCtx);
Call<CreateLabel> call;
call = RetrofitClient
.getApiInterface(appCtx)
.patchLabel(Authorization.get(ctx), repoOwner, repoName, labelId, createLabelFunc);
if(getIntent().getStringExtra("type") != null && Objects.requireNonNull(getIntent().getStringExtra("type")).equals("org")) {
call = RetrofitClient.getApiInterface(ctx).patchOrganizationLabel(Authorization.get(ctx), getIntent().getStringExtra("orgName"), labelId, createLabelFunc);
}
else {
call = RetrofitClient.getApiInterface(appCtx).patchLabel(Authorization.get(ctx), repoOwner, repoName, labelId, createLabelFunc);
}
call.enqueue(new Callback<CreateLabel>() {
@ -286,13 +288,14 @@ public class CreateLabelActivity extends BaseActivity {
if(response.code() == 200) {
Toasty.success(ctx, getString(R.string.labelUpdated));
tinyDb.putString("labelColor", "");
tinyDb.putBoolean("labelsRefresh", true);
tinyDb.putString("labelColorDefault", "");
tinyDB.putString("labelColor", "");
tinyDB.putBoolean("labelsRefresh", true);
tinyDB.putString("labelColorDefault", "");
getIntent().removeExtra("labelAction");
getIntent().removeExtra("labelId");
getIntent().removeExtra("labelTitle");
getIntent().removeExtra("labelColor");
getIntent().removeExtra("type");
finish();
}
}
@ -307,8 +310,8 @@ public class CreateLabelActivity extends BaseActivity {
else {
enableProcessButton();
tinyDb.putString("labelColor", "");
tinyDb.putString("labelColorDefault", "");
tinyDB.putString("labelColor", "");
tinyDB.putString("labelColorDefault", "");
Toasty.error(ctx, getString(R.string.labelGeneralError));
}
}
@ -316,8 +319,8 @@ public class CreateLabelActivity extends BaseActivity {
@Override
public void onFailure(@NonNull Call<CreateLabel> call, @NonNull Throwable t) {
tinyDb.putString("labelColor", "");
tinyDb.putString("labelColorDefault", "");
tinyDB.putString("labelColor", "");
tinyDB.putString("labelColorDefault", "");
Log.e("onFailure", t.toString());
enableProcessButton();
}
@ -333,17 +336,23 @@ public class CreateLabelActivity extends BaseActivity {
getIntent().removeExtra("labelId");
getIntent().removeExtra("labelTitle");
getIntent().removeExtra("labelColor");
getIntent().removeExtra("type");
finish();
};
}
private void deleteLabel(final String instanceToken, final String repoOwner, final String repoName, int labelId, String loginUid) {
private void deleteLabel(final String repoOwner, final String repoName, int labelId) {
Call<Labels> call;
call = RetrofitClient
.getApiInterface(appCtx)
.deleteLabel(Authorization.get(ctx), repoOwner, repoName, labelId);
if(getIntent().getStringExtra("type") != null && Objects.requireNonNull(getIntent().getStringExtra("type")).equals("org")) {
call = RetrofitClient.getApiInterface(appCtx).deleteOrganizationLabel(Authorization.get(ctx), getIntent().getStringExtra("orgName"), labelId);
}
else {
call = RetrofitClient.getApiInterface(appCtx).deleteLabel(Authorization.get(ctx), repoOwner, repoName, labelId);
}
call.enqueue(new Callback<Labels>() {
@ -355,9 +364,17 @@ public class CreateLabelActivity extends BaseActivity {
if(response.code() == 204) {
Toasty.success(ctx, getString(R.string.labelDeleteText));
LabelsViewModel.loadLabelsList(instanceToken, repoOwner, repoName, ctx);
if(getIntent().getStringExtra("type") != null && Objects.requireNonNull(getIntent().getStringExtra("type")).equals("org")) {
OrganizationLabelsViewModel.loadOrgLabelsList(Authorization.get(ctx), getIntent().getStringExtra("orgName"), ctx, null, null);
}
else {
LabelsViewModel.loadLabelsList(Authorization.get(ctx), repoOwner, repoName, ctx);
}
getIntent().removeExtra("labelAction");
getIntent().removeExtra("labelId");
getIntent().removeExtra("type");
}
}
else if(response.code() == 401) {

View File

@ -23,6 +23,7 @@ import org.mian.gitnex.R;
import org.mian.gitnex.fragments.BottomSheetOrganizationFragment;
import org.mian.gitnex.fragments.MembersByOrgFragment;
import org.mian.gitnex.fragments.OrganizationInfoFragment;
import org.mian.gitnex.fragments.OrganizationLabelsFragment;
import org.mian.gitnex.fragments.RepositoriesByOrgFragment;
import org.mian.gitnex.fragments.TeamsByOrgFragment;
import org.mian.gitnex.helpers.Toasty;
@ -145,6 +146,13 @@ public class OrganizationDetailActivity extends BaseActivity implements BottomSh
tinyDB.putBoolean("organizationAction", true);
startActivity(new Intent(OrganizationDetailActivity.this, CreateRepoActivity.class));
break;
case "label":
Intent intent = new Intent(ctx, CreateLabelActivity.class);
intent.putExtra("orgName", getIntent().getStringExtra("orgName"));
intent.putExtra("type", "org");
ctx.startActivity(intent);
break;
case "team":
startActivity(new Intent(OrganizationDetailActivity.this, CreateTeamByOrgActivity.class));
@ -191,11 +199,14 @@ public class OrganizationDetailActivity extends BaseActivity implements BottomSh
return OrganizationInfoFragment.newInstance(orgName);
case 1: // repos
return RepositoriesByOrgFragment.newInstance(orgName);
case 2: // teams
return RepositoriesByOrgFragment.newInstance(orgName);
case 2: // labels
return OrganizationLabelsFragment.newInstance(orgName);
case 3: // teams
return TeamsByOrgFragment.newInstance(orgName);
case 3: // members
case 4: // members
return MembersByOrgFragment.newInstance(orgName);
}

View File

@ -32,6 +32,8 @@ public class LabelsAdapter extends RecyclerView.Adapter<LabelsAdapter.LabelsView
private List<Labels> labelsList;
final private Context mCtx;
private ArrayList<Integer> labelsArray = new ArrayList<>();
private static String type;
private static String orgName;
static class LabelsViewHolder extends RecyclerView.ViewHolder {
@ -77,6 +79,8 @@ public class LabelsAdapter extends RecyclerView.Adapter<LabelsAdapter.LabelsView
intent.putExtra("labelTitle", labelTitle.getText());
intent.putExtra("labelColor", labelColor.getText());
intent.putExtra("labelAction", "edit");
intent.putExtra("type", type);
intent.putExtra("orgName", orgName);
context.startActivity(intent);
dialog.dismiss();
@ -87,8 +91,9 @@ public class LabelsAdapter extends RecyclerView.Adapter<LabelsAdapter.LabelsView
AlertDialogs.labelDeleteDialog(context, labelTitle.getText().toString(), labelId.getText().toString(),
context.getResources().getString(R.string.labelDeleteTitle),
context.getResources().getString(R.string.labelDeleteMessage),
context.getResources().getString(R.string.labelDeletePositiveButton),
context.getResources().getString(R.string.labelDeleteNegativeButton));
context.getResources().getString(R.string.labelDeleteTitle),
context.getResources().getString(R.string.labelDeleteNegativeButton),
type, orgName);
dialog.dismiss();
});
@ -98,9 +103,12 @@ public class LabelsAdapter extends RecyclerView.Adapter<LabelsAdapter.LabelsView
}
}
public LabelsAdapter(Context mCtx, List<Labels> labelsMain) {
public LabelsAdapter(Context mCtx, List<Labels> labelsMain, String type, String orgName) {
this.mCtx = mCtx;
this.labelsList = labelsMain;
LabelsAdapter.type = type;
LabelsAdapter.orgName = orgName;
}
@NonNull

View File

@ -45,27 +45,23 @@ public class OrganizationsListAdapter extends RecyclerView.Adapter<Organizations
image = itemView.findViewById(R.id.imageAvatar);
organizationId = itemView.findViewById(R.id.organizationId);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemView.setOnClickListener(v -> {
Context context = v.getContext();
Intent intent = new Intent(context, OrganizationDetailActivity.class);
intent.putExtra("orgName", mTextView1.getText().toString());
Context context = v.getContext();
Intent intent = new Intent(context, OrganizationDetailActivity.class);
intent.putExtra("orgName", mTextView1.getText().toString());
TinyDB tinyDb = TinyDB.getInstance(context);
tinyDb.putString("orgName", mTextView1.getText().toString());
tinyDb.putString("organizationId", organizationId.getText().toString());
tinyDb.putBoolean("organizationAction", true);
context.startActivity(intent);
}
TinyDB tinyDb = TinyDB.getInstance(context);
tinyDb.putString("orgName", mTextView1.getText().toString());
tinyDb.putString("organizationId", organizationId.getText().toString());
tinyDb.putBoolean("organizationAction", true);
context.startActivity(intent);
});
}
}
public OrganizationsListAdapter(Context mCtx, List<UserOrganizations> orgsListMain) {
this.mCtx = mCtx;
this.orgList = orgsListMain;
orgListFull = new ArrayList<>(orgList);
@ -74,6 +70,7 @@ public class OrganizationsListAdapter extends RecyclerView.Adapter<Organizations
@NonNull
@Override
public OrganizationsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_organizations, parent, false);
return new OrganizationsViewHolder(v);
}
@ -88,11 +85,12 @@ public class OrganizationsListAdapter extends RecyclerView.Adapter<Organizations
PicassoService.getInstance(mCtx).get().load(currentItem.getAvatar_url()).placeholder(R.drawable.loader_animated).transform(new RoundedTransformation(8, 0)).resize(120, 120).centerCrop().into(holder.image);
holder.mTextView1.setText(currentItem.getUsername());
if (!currentItem.getDescription().equals("")) {
holder.mTextView2.setVisibility(View.VISIBLE);
holder.mTextView2.setText(currentItem.getDescription());
}
}
@Override
@ -105,14 +103,19 @@ public class OrganizationsListAdapter extends RecyclerView.Adapter<Organizations
return orgFilter;
}
private Filter orgFilter = new Filter() {
private final Filter orgFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<UserOrganizations> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(orgListFull);
} else {
}
else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (UserOrganizations item : orgListFull) {
@ -130,6 +133,7 @@ public class OrganizationsListAdapter extends RecyclerView.Adapter<Organizations
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
orgList.clear();
orgList.addAll((List) results.values);
notifyDataSetChanged();

View File

@ -28,6 +28,7 @@ public class BottomSheetOrganizationFragment extends BottomSheetDialogFragment {
TextView createTeam = v.findViewById(R.id.createTeam);
TextView createRepository = v.findViewById(R.id.createRepository);
TextView copyOrgUrl = v.findViewById(R.id.copyOrgUrl);
TextView createLabel = v.findViewById(R.id.createLabel);
createTeam.setOnClickListener(v1 -> {
@ -35,6 +36,12 @@ public class BottomSheetOrganizationFragment extends BottomSheetDialogFragment {
dismiss();
});
createLabel.setOnClickListener(v1 -> {
bmListener.onButtonClicked("label");
dismiss();
});
createRepository.setOnClickListener(v12 -> {
bmListener.onButtonClicked("repository");

View File

@ -10,9 +10,7 @@ import android.view.ViewGroup;
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;
@ -22,9 +20,7 @@ import org.mian.gitnex.R;
import org.mian.gitnex.adapters.LabelsAdapter;
import org.mian.gitnex.helpers.Authorization;
import org.mian.gitnex.helpers.TinyDB;
import org.mian.gitnex.models.Labels;
import org.mian.gitnex.viewmodels.LabelsViewModel;
import java.util.List;
/**
* Author M M Arif
@ -38,6 +34,7 @@ public class LabelsFragment extends Fragment {
private TextView noData;
private static String repoNameF = "param2";
private static String repoOwnerF = "param1";
private final String type = "repo";
private String repoName;
private String repoOwner;
@ -48,6 +45,7 @@ public class LabelsFragment extends Fragment {
}
public static LabelsFragment newInstance(String param1, String param2) {
LabelsFragment fragment = new LabelsFragment();
Bundle args = new Bundle();
args.putString(repoOwnerF, param1);
@ -58,8 +56,11 @@ public class LabelsFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
repoName = getArguments().getString(repoNameF);
repoOwner = getArguments().getString(repoOwnerF);
}
@ -88,17 +89,16 @@ public class LabelsFragment extends Fragment {
swipeRefresh.setRefreshing(false);
LabelsViewModel.loadLabelsList(Authorization.get(getContext()), repoOwner, repoName, getContext());
}, 200));
fetchDataAsync(Authorization.get(getContext()), repoOwner, repoName);
return v;
}
@Override
public void onResume() {
super.onResume();
final TinyDB tinyDb = TinyDB.getInstance(getContext());
@ -108,24 +108,29 @@ public class LabelsFragment extends Fragment {
final String repoName = parts[1];
if(tinyDb.getBoolean("labelsRefresh")) {
LabelsViewModel.loadLabelsList(Authorization.get(getContext()), repoOwner, repoName, getContext());
tinyDb.putBoolean("labelsRefresh", 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);
}
@ -133,21 +138,23 @@ public class LabelsFragment extends Fragment {
LabelsViewModel labelsModel = new ViewModelProvider(this).get(LabelsViewModel.class);
labelsModel.getLabelsList(instanceToken, owner, repo, getContext()).observe(getViewLifecycleOwner(), new Observer<List<Labels>>() {
@Override
public void onChanged(@Nullable List<Labels> labelsListMain) {
adapter = new LabelsAdapter(getContext(), labelsListMain);
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);
labelsModel.getLabelsList(instanceToken, owner, repo, getContext()).observe(getViewLifecycleOwner(), labelsListMain -> {
adapter = new LabelsAdapter(getContext(), labelsListMain, type, owner);
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);
});
}

View File

@ -0,0 +1,151 @@
package org.mian.gitnex.fragments;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
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.adapters.LabelsAdapter;
import org.mian.gitnex.helpers.Authorization;
import org.mian.gitnex.helpers.TinyDB;
import org.mian.gitnex.viewmodels.OrganizationLabelsViewModel;
/**
* Author M M Arif
*/
public class OrganizationLabelsFragment extends Fragment {
private ProgressBar mProgressBar;
private RecyclerView mRecyclerView;
private LabelsAdapter adapter;
private TextView noData;
private static final String repoOwnerF = "param1";
private final String type = "org";
private String repoOwner;
private OnFragmentInteractionListener mListener;
public static OrganizationLabelsFragment newInstance(String param1) {
OrganizationLabelsFragment fragment = new OrganizationLabelsFragment();
Bundle args = new Bundle();
args.putString(repoOwnerF, param1);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
repoOwner = getArguments().getString(repoOwnerF);
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_labels, container, false);
setHasOptionsMenu(true);
final SwipeRefreshLayout swipeRefresh = v.findViewById(R.id.pullToRefresh);
noData = v.findViewById(R.id.noData);
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);
mProgressBar = v.findViewById(R.id.progress_bar);
swipeRefresh.setOnRefreshListener(() -> new Handler(Looper.getMainLooper()).postDelayed(() -> {
swipeRefresh.setRefreshing(false);
OrganizationLabelsViewModel.loadOrgLabelsList(Authorization.get(getContext()), repoOwner, getContext(), mProgressBar, noData);
}, 200));
fetchDataAsync(Authorization.get(getContext()), repoOwner);
return v;
}
@Override
public void onResume() {
super.onResume();
final TinyDB tinyDb = TinyDB.getInstance(getContext());
if(tinyDb.getBoolean("labelsRefresh")) {
OrganizationLabelsViewModel.loadOrgLabelsList(Authorization.get(getContext()), repoOwner, getContext(), mProgressBar, noData);
tinyDb.putBoolean("labelsRefresh", 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);
}
private void fetchDataAsync(String instanceToken, String owner) {
OrganizationLabelsViewModel organizationLabelsViewModel = new ViewModelProvider(this).get(OrganizationLabelsViewModel.class);
organizationLabelsViewModel.getOrgLabelsList(instanceToken, owner, getContext(), mProgressBar, noData).observe(getViewLifecycleOwner(), labelsListMain -> {
adapter = new LabelsAdapter(getContext(), labelsListMain, type, owner);
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);
});
}
}

View File

@ -62,7 +62,7 @@ public class AlertDialogs {
alertDialogBuilder.create().show();
}
public static void labelDeleteDialog(final Context context, final String labelTitle, final String labelId, String title, String message, String positiveButton, String negativeButton) {
public static void labelDeleteDialog(final Context context, final String labelTitle, final String labelId, String title, String message, String positiveButton, String negativeButton, String type, String orgName) {
new AlertDialog.Builder(context)
.setTitle(title + labelTitle)
@ -73,6 +73,8 @@ public class AlertDialogs {
Intent intent = new Intent(context, CreateLabelActivity.class);
intent.putExtra("labelId", labelId);
intent.putExtra("labelAction", "delete");
intent.putExtra("type", type);
intent.putExtra("orgName", orgName);
context.startActivity(intent);
})

View File

@ -229,6 +229,15 @@ public interface ApiInterface {
@GET("orgs/{owner}/labels") // get org labels list
Call<List<Labels>> getOrganizationLabels(@Header("Authorization") String token, @Path("owner") String ownerName);
@POST("orgs/{owner}/labels") // create org label
Call<CreateLabel> createOrganizationLabel(@Header("Authorization") String token, @Path("owner") String ownerName, @Body CreateLabel jsonStr);
@PATCH("orgs/{owner}/labels/{id}") // update / patch org label
Call<CreateLabel> patchOrganizationLabel(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("id") int labelID, @Body CreateLabel jsonStr);
@DELETE("orgs/{owner}/labels/{id}") // delete org label
Call<Labels> deleteOrganizationLabel(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("id") int labelID);
@GET("users/{username}/repos") // get current logged in user repositories
Call<List<UserRepositories>> getCurrentUserRepositories(@Header("Authorization") String token, @Path("username") String username, @Query("page") int page, @Query("limit") int limit);

View File

@ -0,0 +1,67 @@
package org.mian.gitnex.viewmodels;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import org.jetbrains.annotations.NotNull;
import org.mian.gitnex.clients.RetrofitClient;
import org.mian.gitnex.models.Labels;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Author M M Arif
*/
public class OrganizationLabelsViewModel extends ViewModel {
private static MutableLiveData<List<Labels>> orgLabelsList;
public LiveData<List<Labels>> getOrgLabelsList(String token, String owner, Context ctx, ProgressBar progressBar, TextView noData) {
orgLabelsList = new MutableLiveData<>();
loadOrgLabelsList(token, owner, ctx, progressBar = null, noData = null);
return orgLabelsList;
}
public static void loadOrgLabelsList(String token, String owner, Context ctx, ProgressBar progressBar, TextView noData) {
Call<List<Labels>> call = RetrofitClient
.getApiInterface(ctx)
.getOrganizationLabels(token, owner);
call.enqueue(new Callback<List<Labels>>() {
@Override
public void onResponse(@NonNull Call<List<Labels>> call, @NonNull Response<List<Labels>> response) {
if(response.isSuccessful()) {
orgLabelsList.postValue(response.body());
}
else {
progressBar.setVisibility(View.GONE);
noData.setVisibility(View.VISIBLE);
Log.i("onResponse-org-labels", String.valueOf(response.code()));
}
}
@Override
public void onFailure(@NonNull Call<List<Labels>> call, @NotNull Throwable t) {
Log.i("onFailure", t.toString());
}
});
}
}

View File

@ -56,6 +56,12 @@
android:layout_height="wrap_content"
android:text="@string/orgTabRepos" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tabItem_labels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_labels" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tabItem_teams"
android:layout_width="wrap_content"

View File

@ -30,6 +30,18 @@
android:padding="12dp"
app:drawableStartCompat="@drawable/ic_repo" />
<TextView
android:id="@+id/createLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/createLabel"
android:drawablePadding="24dp"
android:textColor="?attr/primaryTextColor"
android:textSize="16sp"
android:padding="12dp"
app:drawableStartCompat="@drawable/ic_label" />
<TextView
android:id="@+id/createTeam"
android:layout_width="match_parent"