Skip to content

Commit

Permalink
feat(inventory): remove header in specific tab
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Del Pino <[email protected]>
  • Loading branch information
Ivan Del Pino authored and rafaelje committed Oct 5, 2018
1 parent 3c86a58 commit 05e05ca
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,63 +42,20 @@ public class InventoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold

private ArrayList<HashMap<String, String>> data;

private static final int ITEM_TYPE_DATA = 0;
private static final int ITEM_TYPE_HEADER = 1;

public InventoryAdapter(ArrayList<HashMap<String, String>> data) {
this.data = data;
}

@Override
public int getItemViewType(int position) {
if((data.get(position)).get("type").equals("header")) {
return ITEM_TYPE_HEADER;
} else {
return ITEM_TYPE_DATA;
}
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

// Vista por defecto
if(viewType == ITEM_TYPE_DATA) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_inventory, viewGroup, false);
return new DataViewHolder(v);
}

// Vista alternativa
else if (viewType == ITEM_TYPE_HEADER) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_inventory_header, viewGroup, false);
return new HeaderViewHolder(v);
}

return null;
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_inventory, viewGroup, false);
return new DataViewHolder(v);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
HashMap<String, String> response = data.get(position);

final int itemType = getItemViewType(position);

if (itemType == ITEM_TYPE_DATA) {
((DataViewHolder) holder).bindData(response);
}

if (itemType == ITEM_TYPE_HEADER) {
((HeaderViewHolder) holder).bindData(response);
}
}

@Override
public int getItemCount() {
return data.size();
}

@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
((DataViewHolder) holder).bindData(response, position);
}

public class DataViewHolder extends RecyclerView.ViewHolder {
Expand All @@ -111,24 +68,25 @@ public class DataViewHolder extends RecyclerView.ViewHolder {
description = itemView.findViewById(R.id.description);
}

void bindData(HashMap<String, String> model) {
title.setText( Html.fromHtml( Helpers.splitCamelCase(model.get("title")) ));
description.setText( Html.fromHtml( model.get("description") ));
void bindData(HashMap<String, String> model, int position) {
if (position > 0) {
title.setText(Html.fromHtml(Helpers.splitCamelCase(model.get("title"))));
description.setText(Html.fromHtml(model.get("description")));
} else {
title.setVisibility(View.GONE);
description.setVisibility(View.GONE);
}
}
}

public class HeaderViewHolder extends RecyclerView.ViewHolder {
TextView title;

HeaderViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
}

void bindData(HashMap<String, String> model) {
title.setText( Html.fromHtml( model.get("title") ));
}
@Override
public int getItemCount() {
return data.size();
}

@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

package org.flyve.inventory.agent.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
Expand All @@ -34,6 +33,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import org.flyve.inventory.agent.R;
import org.flyve.inventory.agent.adapter.InventoryAdapter;
Expand All @@ -53,6 +53,12 @@ public class FragmentInventoryList extends Fragment implements Report.View {
private String data;
private String key;

private RecyclerViewReadyCallback recyclerViewReadyCallback;

public interface RecyclerViewReadyCallback {
void onLayoutReady();
}

public static FragmentInventoryList newInstance(String data, String key) {
FragmentInventoryList fragmentFirst = new FragmentInventoryList();
Bundle args = new Bundle();
Expand Down Expand Up @@ -84,17 +90,33 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat

Report.Presenter presenter = new ReportPresenter(this);

RecyclerView lst = view.findViewById(R.id.lst);
final RecyclerView lst = view.findViewById(R.id.lst);
lst.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (recyclerViewReadyCallback != null) {
recyclerViewReadyCallback.onLayoutReady();
}
lst.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
lst.setLayoutManager(new GridLayoutManager(getActivity(), 1));
InventoryAdapter mAdapter = new InventoryAdapter(load(requireActivity()));
InventoryAdapter mAdapter = new InventoryAdapter(load());
lst.setAdapter(mAdapter);
String message = requireActivity().getResources().getString(R.string.loading);
final ProgressDialog progressBar = ProgressDialog.show(requireActivity(), "Creating inventory", message);

recyclerViewReadyCallback = new RecyclerViewReadyCallback() {
@Override
public void onLayoutReady() {
progressBar.dismiss();
}
};

presenter.generateReport(getActivity());
}

private ArrayList<HashMap<String, String>> load(Activity activity) {
String message = activity.getResources().getString(R.string.loading);
ProgressDialog progressBar = ProgressDialog.show(activity, "Creating inventory", message);
private ArrayList<HashMap<String, String>> load() {
ArrayList<HashMap<String, String>> dataList = new ArrayList<>();

try {
Expand Down Expand Up @@ -140,10 +162,8 @@ private ArrayList<HashMap<String, String>> load(Activity activity) {
break;
}
}
progressBar.dismiss();
return dataList;
} catch (Exception ex) {
progressBar.dismiss();
FlyveLog.e(ex.getMessage());
}
return dataList;
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_inventory_new.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
app:tabGravity="fill"
app:tabIndicatorColor="@color/blue"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/white"
app:tabTextColor="@color/snackbar_action_good" />
app:tabTextColor="@color/white"
app:tabSelectedTextColor="@color/white"/>

<ProgressBar
android:id="@+id/pb"
Expand Down
20 changes: 0 additions & 20 deletions app/src/main/res/layout/list_item_inventory_header.xml

This file was deleted.

0 comments on commit 05e05ca

Please sign in to comment.