Skip to content

Commit

Permalink
Merge pull request #602 from ooni/chore/dashboard-fragment-upgrade-to…
Browse files Browse the repository at this point in the history
…-view-binding

Chore: Update `DashboardFragment` to `ViewBinding`
  • Loading branch information
aanorbel authored Sep 11, 2023
2 parents 26f1a94 + 0182e61 commit 4fd3e60
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,75 +1,66 @@
package org.openobservatory.ooniprobe.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.openobservatory.ooniprobe.R;
import org.openobservatory.ooniprobe.activity.AbstractActivity;
import org.openobservatory.ooniprobe.activity.MainActivity;
import org.openobservatory.ooniprobe.activity.OverviewActivity;
import org.openobservatory.ooniprobe.activity.RunningActivity;
import org.openobservatory.ooniprobe.common.Application;
import org.openobservatory.ooniprobe.common.PreferenceManager;
import org.openobservatory.ooniprobe.common.ReachabilityManager;
import org.openobservatory.ooniprobe.common.ThirdPartyServices;
import org.openobservatory.ooniprobe.databinding.FragmentDashboardBinding;
import org.openobservatory.ooniprobe.item.SeperatorItem;
import org.openobservatory.ooniprobe.item.TestsuiteItem;
import org.openobservatory.ooniprobe.model.database.Result;
import org.openobservatory.ooniprobe.test.TestAsyncTask;
import org.openobservatory.ooniprobe.test.suite.AbstractSuite;

import java.util.ArrayList;
import java.util.Objects;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import localhost.toolkit.widget.recyclerview.HeterogeneousRecyclerAdapter;
import localhost.toolkit.widget.recyclerview.HeterogeneousRecyclerItem;

public class DashboardFragment extends Fragment implements View.OnClickListener {
@BindView(R.id.recycler) RecyclerView recycler;
@BindView(R.id.toolbar) Toolbar toolbar;
@BindView(R.id.last_tested) TextView lastTested;
@BindView(R.id.run_all) TextView runAll;
@BindView(R.id.vpn) TextView vpn;

@Inject
PreferenceManager preferenceManager;

private ArrayList<HeterogeneousRecyclerItem> items;

private ArrayList<AbstractSuite> testSuites;

private HeterogeneousRecyclerAdapter<HeterogeneousRecyclerItem> adapter;

private FragmentDashboardBinding binding;

@Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dashboard, container, false);
ButterKnife.bind(this, v);
binding = FragmentDashboardBinding.inflate(inflater,container,false);
((Application) getActivity().getApplication()).getFragmentComponent().inject(this);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(binding.toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(null);
items = new ArrayList<>();
testSuites = new ArrayList<>();
adapter = new HeterogeneousRecyclerAdapter<>(getActivity(), items);
recycler.setAdapter(adapter);
recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
runAll.setOnClickListener(v1 -> runAll());
vpn.setOnClickListener(view -> ((Application) getActivity().getApplication()).openVPNSettings());
return v;
binding.recycler.setAdapter(adapter);
binding.recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
binding.runAll.setOnClickListener(v1 -> runAll());
binding.vpn.setOnClickListener(view -> ((Application) getActivity().getApplication()).openVPNSettings());
return binding.getRoot();
}

@Override public void onResume() {
Expand Down Expand Up @@ -100,19 +91,19 @@ public class DashboardFragment extends Fragment implements View.OnClickListener
adapter.notifyTypesChanged();
if (ReachabilityManager.isVPNinUse(this.getContext())
&& preferenceManager.isWarnVPNInUse())
vpn.setVisibility(View.VISIBLE);
binding.vpn.setVisibility(View.VISIBLE);
else
vpn.setVisibility(View.GONE);
binding.vpn.setVisibility(View.GONE);
}

private void setLastTest() {
Result lastResult = Result.getLastResult();
if (lastResult == null)
lastTested.setText(getString(R.string.Dashboard_Overview_LatestTest)
binding.lastTested.setText(getString(R.string.Dashboard_Overview_LatestTest)
+ " " +
getString(R.string.Dashboard_Overview_LastRun_Never));
else
lastTested.setText(getString(R.string.Dashboard_Overview_LatestTest)
binding.lastTested.setText(getString(R.string.Dashboard_Overview_LatestTest)
+ " " +
DateUtils.getRelativeTimeSpanString(lastResult.start_time.getTime()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
package org.openobservatory.ooniprobe.item;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import localhost.toolkit.widget.recyclerview.HeterogeneousRecyclerItem;
import org.openobservatory.ooniprobe.R;
import org.openobservatory.ooniprobe.common.PreferenceManager;
import org.openobservatory.ooniprobe.databinding.ItemTestsuiteBinding;
import org.openobservatory.ooniprobe.test.suite.AbstractSuite;

import butterknife.BindView;
import butterknife.ButterKnife;
import localhost.toolkit.widget.recyclerview.HeterogeneousRecyclerItem;

public class TestsuiteItem extends HeterogeneousRecyclerItem<AbstractSuite, TestsuiteItem.ViewHolderImpl> {
private final View.OnClickListener onClickListener;
private final PreferenceManager preferenceManager;
Expand All @@ -33,36 +24,34 @@ public TestsuiteItem(AbstractSuite extra, View.OnClickListener onClickListener,
}

@Override public ViewHolderImpl onCreateViewHolder(LayoutInflater layoutInflater, ViewGroup viewGroup) {
return new ViewHolderImpl(layoutInflater.inflate(R.layout.item_testsuite, viewGroup, false));
return new ViewHolderImpl(ItemTestsuiteBinding.inflate(layoutInflater, viewGroup, false));
}

@Override public void onBindViewHolder(ViewHolderImpl holder) {
holder.title.setText(extra.getTitle());
holder.desc.setText(extra.getCardDesc());
holder.icon.setImageResource(extra.getIconGradient());
holder.binding.title.setText(extra.getTitle());
holder.binding.desc.setText(extra.getCardDesc());
holder.binding.icon.setImageResource(extra.getIconGradient());
holder.itemView.setTag(extra);
if(extra.isTestEmpty(preferenceManager)) {
((CardView)holder.itemView).setElevation(0);
Resources resources = holder.itemView.getContext().getResources();
((CardView)holder.itemView).setCardBackgroundColor(resources.getColor(R.color.disabled_test_background));
holder.title.setTextColor(resources.getColor(R.color.disabled_test_text));
holder.desc.setTextColor(resources.getColor(R.color.disabled_test_text));
holder.icon.setColorFilter(resources.getColor(R.color.disabled_test_text), PorterDuff.Mode.SRC_IN);
holder.binding.title.setTextColor(resources.getColor(R.color.disabled_test_text));
holder.binding.desc.setTextColor(resources.getColor(R.color.disabled_test_text));
holder.binding.icon.setColorFilter(resources.getColor(R.color.disabled_test_text), PorterDuff.Mode.SRC_IN);
holder.setIsRecyclable(false);
holder.itemView.setClickable(false);
} else {
holder.itemView.setOnClickListener(onClickListener);
}
}

class ViewHolderImpl extends RecyclerView.ViewHolder {
@BindView(R.id.title) TextView title;
@BindView(R.id.desc) TextView desc;
@BindView(R.id.icon) ImageView icon;
static class ViewHolderImpl extends RecyclerView.ViewHolder {
ItemTestsuiteBinding binding;

ViewHolderImpl(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
ViewHolderImpl(ItemTestsuiteBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}

0 comments on commit 4fd3e60

Please sign in to comment.