Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Update ProgressFragment to ViewBinding #609

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,35 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import org.openobservatory.ooniprobe.R;
import org.openobservatory.ooniprobe.activity.RunningActivity;
import org.openobservatory.ooniprobe.common.Application;
import org.openobservatory.ooniprobe.common.PreferenceManager;
import org.openobservatory.ooniprobe.common.TestProgressRepository;
import org.openobservatory.ooniprobe.common.service.RunTestService;
import org.openobservatory.ooniprobe.databinding.FragmentProgressBinding;
import org.openobservatory.ooniprobe.receiver.TestRunBroadRequestReceiver;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
* Monitors and displays progress of {@link RunTestService}.
*/
public class ProgressFragment extends Fragment {
private TestRunBroadRequestReceiver receiver;

private FragmentProgressBinding biding;

@Inject
PreferenceManager preferenceManager;
@Inject
TestProgressRepository testProgressRepository;
@BindView(R.id.progress_layout)
FrameLayout progress_layout;
@BindView(R.id.progress)
ProgressBar progress;
@BindView(R.id.running)
TextView running;
@BindView(R.id.name)
TextView name;

public ProgressFragment() {
// Required empty public constructor
Expand All @@ -62,24 +47,18 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_progress, container, false);
ButterKnife.bind(this, v);
biding = FragmentProgressBinding.inflate(inflater, container, false);
((Application) getActivity().getApplication()).getFragmentComponent().inject(this);
v.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
Intent intent = new Intent(getContext(), RunningActivity.class);
ActivityCompat.startActivity(getActivity(), intent, null);
}
return true;
}
biding.getRoot().setOnClickListener(v -> {
Intent intent = new Intent(getContext(), RunningActivity.class);
ActivityCompat.startActivity(getActivity(), intent, null);
});
testProgressRepository.getProgress().observe(getViewLifecycleOwner(),progressValue -> {
if (progressValue!=null) {
progress.setProgress(progressValue);
biding.progress.setProgress(progressValue);
}
});
return v;
return biding.getRoot();
}

@Override
Expand All @@ -97,10 +76,10 @@ public void bindTestService() {
if (activity!=null && ((Application)activity.getApplication()).isTestRunning()) {
Intent intent = new Intent(getActivity(), RunTestService.class);
getActivity().bindService(intent, receiver, Context.BIND_AUTO_CREATE);
progress_layout.setVisibility(View.VISIBLE);
biding.progressLayout.setVisibility(View.VISIBLE);
}
else
progress_layout.setVisibility(View.GONE);
biding.progressLayout.setVisibility(View.GONE);
}

private void updateUI(RunTestService service){
Expand All @@ -109,15 +88,15 @@ private void updateUI(RunTestService service){

Integer progressLevel = testProgressRepository.getProgress().getValue();
if (progressLevel != null) {
progress.setProgress(progressLevel);
biding.progress.setProgress(progressLevel);
} else {
progress.setIndeterminate(true);
biding.progress.setIndeterminate(true);
}
if (service != null && service.task != null){
if (service.task.currentSuite != null)
progress.setMax(service.task.getMax(preferenceManager));
biding.progress.setMax(service.task.getMax(preferenceManager));
if (service.task.currentTest != null)
name.setText(getString(service.task.currentTest.getLabelResId()));
biding.name.setText(getString(service.task.currentTest.getLabelResId()));
}
}
}
Expand Down Expand Up @@ -146,15 +125,15 @@ public void onStart(RunTestService service) {

@Override
public void onRun(String value) {
name.setText(value);
biding.name.setText(value);
}

@Override
public void onProgress(int state, double eta) {
if (progress.isIndeterminate())
if (biding.progress.isIndeterminate())
updateUI(receiver.service);
progress.setIndeterminate(false);
progress.setProgress(state);
biding.progress.setIndeterminate(false);
biding.progress.setProgress(state);
}

@Override
Expand All @@ -169,17 +148,17 @@ public void onError(String value) {

@Override
public void onUrl() {
progress.setIndeterminate(false);
biding.progress.setIndeterminate(false);
}

@Override
public void onInterrupt() {
running.setText(getString(R.string.Dashboard_Running_Stopping_Title));
biding.running.setText(getString(R.string.Dashboard_Running_Stopping_Title));
}

@Override
public void onEnd(Context context) {
progress_layout.setVisibility(View.GONE);
biding.progressLayout.setVisibility(View.GONE);
}
}
}