From 9a7bd4f95619c78d21c987c2331c3755734d184d Mon Sep 17 00:00:00 2001 From: Norbel Ambanumben Date: Fri, 18 Aug 2023 10:12:45 +0100 Subject: [PATCH 1/2] Chore: Update `DashboardFragment` to `ViewBinding` --- .../ooniprobe/fragment/DashboardFragment.java | 41 ++++++++----------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/openobservatory/ooniprobe/fragment/DashboardFragment.java b/app/src/main/java/org/openobservatory/ooniprobe/fragment/DashboardFragment.java index 0ebeb4d8d..7eee1ca50 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/fragment/DashboardFragment.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/fragment/DashboardFragment.java @@ -1,31 +1,27 @@ 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; @@ -33,43 +29,38 @@ 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 items; + private ArrayList testSuites; + private HeterogeneousRecyclerAdapter 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() { @@ -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())); } From 0182e6117bca1639a5e1c85595862afb5c797a22 Mon Sep 17 00:00:00 2001 From: Norbel Ambanumben Date: Sat, 26 Aug 2023 16:40:13 +0100 Subject: [PATCH 2/2] Replace `Butterknife` in `TestsuiteItem` --- .../ooniprobe/item/TestsuiteItem.java | 39 +++++++------------ 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/openobservatory/ooniprobe/item/TestsuiteItem.java b/app/src/main/java/org/openobservatory/ooniprobe/item/TestsuiteItem.java index 98fb0f70c..9eb8bf60b 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/item/TestsuiteItem.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/item/TestsuiteItem.java @@ -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 { private final View.OnClickListener onClickListener; private final PreferenceManager preferenceManager; @@ -33,21 +24,21 @@ 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 { @@ -55,14 +46,12 @@ public TestsuiteItem(AbstractSuite extra, View.OnClickListener 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; } } }