-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented "android.arch.lifecycle" for EditSetFragment
- Loading branch information
Showing
14 changed files
with
235 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 47 additions & 21 deletions
68
src/main/java/de/avalax/fitbuddy/presentation/edit/set/EditSetFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,80 @@ | ||
package de.avalax.fitbuddy.presentation.edit.set; | ||
|
||
import android.arch.lifecycle.ViewModelProviders; | ||
import android.databinding.DataBindingUtil; | ||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.app.Fragment; | ||
import android.support.v4.app.FragmentManager; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
|
||
import javax.inject.Inject; | ||
|
||
import de.avalax.fitbuddy.R; | ||
import de.avalax.fitbuddy.databinding.EditSetBinding; | ||
import de.avalax.fitbuddy.domain.model.set.Set; | ||
import de.avalax.fitbuddy.presentation.FitbuddyApplication; | ||
import de.avalax.fitbuddy.presentation.dialog.EditRepsDialogFragment; | ||
import de.avalax.fitbuddy.presentation.dialog.EditWeightDialogFragment; | ||
import de.avalax.fitbuddy.presentation.edit.exercise.EditExerciseViewHelper; | ||
|
||
public class EditSetFragment extends Fragment { | ||
import static de.avalax.fitbuddy.presentation.FitbuddyApplication.EDIT_REPS; | ||
import static de.avalax.fitbuddy.presentation.FitbuddyApplication.EDIT_WEIGHT; | ||
|
||
private Set set; | ||
public class EditSetFragment extends Fragment implements | ||
EditWeightDialogFragment.DialogListener, | ||
EditRepsDialogFragment.DialogListener { | ||
|
||
private EditSetViewModel viewModel; | ||
|
||
@Inject | ||
protected EditExerciseViewHelper editExerciseViewHelper; | ||
|
||
@Override | ||
public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||
Bundle savedInstanceState) { | ||
View view = inflater.inflate(R.layout.fragment_edit_set, container, false); | ||
public void onActivityCreated(@Nullable Bundle savedInstanceState) { | ||
super.onActivityCreated(savedInstanceState); | ||
Set set = (Set) getArguments().getSerializable("set"); | ||
viewModel.init(editExerciseViewHelper, set); | ||
} | ||
|
||
@Override | ||
public View onCreateView(LayoutInflater inflater, | ||
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | ||
EditSetBinding binding = DataBindingUtil.inflate(inflater, R.layout.edit_set, container, false); | ||
((FitbuddyApplication) getActivity().getApplication()).getComponent().inject(this); | ||
viewModel = ViewModelProviders.of(this).get(EditSetViewModel.class); | ||
binding.setEditSetViewModel(viewModel); | ||
binding.setLifecycleOwner(this); | ||
|
||
view.findViewById(R.id.set_weight).setOnClickListener(onClickListener -> changeWeight()); | ||
view.findViewById(R.id.set_reps).setOnClickListener(onClickListener -> changeMaxReps()); | ||
set = (Set) getActivity().getIntent().getSerializableExtra("set"); | ||
TextView repsTextView = view.findViewById(R.id.set_reps_text_view); | ||
repsTextView.setText(editExerciseViewHelper.title(set)); | ||
TextView weightTextView = view.findViewById(R.id.set_weight_text_view); | ||
weightTextView.setText(editExerciseViewHelper.subtitle(set)); | ||
return view; | ||
binding.getRoot().findViewById(R.id.set_weight).setOnClickListener(onClickListener -> changeWeight()); | ||
binding.getRoot().findViewById(R.id.set_reps).setOnClickListener(onClickListener -> changeMaxReps()); | ||
return binding.getRoot(); | ||
} | ||
|
||
private void changeWeight() { | ||
double weight = set.getWeight(); | ||
FragmentManager fm = getActivity().getSupportFragmentManager(); | ||
EditWeightDialogFragment.newInstance(weight).show(fm, "fragment_edit_weight"); | ||
double weight = viewModel.getSet().getWeight(); | ||
EditWeightDialogFragment editWeightDialogFragment = EditWeightDialogFragment.newInstance(weight); | ||
editWeightDialogFragment.setTargetFragment(this, EDIT_WEIGHT); | ||
editWeightDialogFragment.show(getFragmentManager(), "fragment_edit_weight"); | ||
} | ||
|
||
private void changeMaxReps() { | ||
FragmentManager fm = getActivity().getSupportFragmentManager(); | ||
int reps = set.getMaxReps(); | ||
EditRepsDialogFragment.newInstance(reps).show(fm, "fragment_edit_max_reps"); | ||
int reps = viewModel.getSet().getMaxReps(); | ||
EditRepsDialogFragment editRepsDialogFragment = EditRepsDialogFragment.newInstance(reps); | ||
editRepsDialogFragment.setTargetFragment(this, EDIT_REPS); | ||
editRepsDialogFragment.show(getFragmentManager(), "fragment_edit_max_reps"); | ||
} | ||
|
||
@Override | ||
public void onDialogPositiveClick(EditRepsDialogFragment editRepsDialogFragment) { | ||
int reps = editRepsDialogFragment.getReps(); | ||
viewModel.setReps(reps); | ||
} | ||
|
||
@Override | ||
public void onDialogPositiveClick(EditWeightDialogFragment editWeightDialogFragment) { | ||
double weight = editWeightDialogFragment.getWeight(); | ||
viewModel.setWeight(weight); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/de/avalax/fitbuddy/presentation/edit/set/EditSetViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package de.avalax.fitbuddy.presentation.edit.set; | ||
|
||
import android.arch.lifecycle.LiveData; | ||
import android.arch.lifecycle.MutableLiveData; | ||
import android.arch.lifecycle.ViewModel; | ||
|
||
import de.avalax.fitbuddy.domain.model.set.Set; | ||
import de.avalax.fitbuddy.presentation.edit.exercise.EditExerciseViewHelper; | ||
|
||
public class EditSetViewModel extends ViewModel { | ||
private EditExerciseViewHelper editExerciseViewHelper; | ||
private Set set; | ||
private MutableLiveData<String> weight; | ||
private MutableLiveData<String> reps; | ||
|
||
public void init(EditExerciseViewHelper editExerciseViewHelper, Set set) { | ||
this.editExerciseViewHelper = editExerciseViewHelper; | ||
this.set = set; | ||
this.weight = new MutableLiveData<>(); | ||
this.reps = new MutableLiveData<>(); | ||
|
||
setWeightFromSet(); | ||
setRepsFromSet(); | ||
} | ||
|
||
private void setRepsFromSet() { | ||
this.reps.setValue(this.editExerciseViewHelper.repsFrom(this.set)); | ||
} | ||
|
||
private void setWeightFromSet() { | ||
this.weight.setValue(this.editExerciseViewHelper.weightFrom(this.set)); | ||
} | ||
|
||
public Set getSet() { | ||
return set; | ||
} | ||
|
||
public LiveData<String> getWeight() { | ||
return weight; | ||
} | ||
|
||
public LiveData<String> getReps() { | ||
return reps; | ||
} | ||
|
||
public void setReps(int reps) { | ||
set.setMaxReps(reps); | ||
setRepsFromSet(); | ||
} | ||
|
||
public void setWeight(double weight) { | ||
set.setWeight(weight); | ||
setWeightFromSet(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.