Makes EditMonsterFragment load the monster based with the id passed in and show it's name in the title.
This commit is contained in:
		| @@ -8,13 +8,20 @@ import android.widget.TextView; | ||||
|  | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.annotation.Nullable; | ||||
| import androidx.fragment.app.Fragment; | ||||
| import androidx.lifecycle.ViewModelProvider; | ||||
|  | ||||
| import com.majinnaibu.monstercards.R; | ||||
| import com.majinnaibu.monstercards.data.MonsterRepository; | ||||
| import com.majinnaibu.monstercards.models.Monster; | ||||
| import com.majinnaibu.monstercards.ui.MCFragment; | ||||
| import com.majinnaibu.monstercards.ui.monster.MonsterDetailFragmentArgs; | ||||
| import com.majinnaibu.monstercards.utils.Logger; | ||||
|  | ||||
| public class EditMonsterFragment extends Fragment { | ||||
| import java.util.UUID; | ||||
|  | ||||
| import io.reactivex.rxjava3.observers.DisposableSingleObserver; | ||||
|  | ||||
| public class EditMonsterFragment extends MCFragment { | ||||
|  | ||||
|     private EditMonsterViewModel mViewModel; | ||||
|     private ViewHolder mHolder; | ||||
| @@ -26,13 +33,35 @@ public class EditMonsterFragment extends Fragment { | ||||
|     @Override | ||||
|     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, | ||||
|                              @Nullable Bundle savedInstanceState) { | ||||
|         // TODO: Get our monster from the repo based on the id passed in. | ||||
|         // TODO: Show a loading spinner until we have the monster loaded. | ||||
|         // TODO: If there is an error loading the monster show the error. | ||||
|  | ||||
|         MonsterRepository repository = getMonsterRepository(); | ||||
|         Bundle arguments = getArguments(); | ||||
|         assert arguments != null; | ||||
|         UUID monsterId = UUID.fromString(MonsterDetailFragmentArgs.fromBundle(arguments).getMonsterId()); | ||||
|  | ||||
|         View root = inflater.inflate(R.layout.fragment_edit_monster, container, false); | ||||
|  | ||||
|         mHolder = new ViewHolder(root); | ||||
|         requireAppCompatActivity().getSupportActionBar().setTitle(getString(R.string.title_edit_monster, getString(R.string.default_monster_name))); | ||||
|  | ||||
|         // TODO: Show a loading spinner until we have the monster loaded. | ||||
|         repository.getMonster(monsterId).toObservable() | ||||
|                 .firstOrError() | ||||
|                 .subscribe(new DisposableSingleObserver<Monster>() { | ||||
|                     @Override | ||||
|                     public void onSuccess(@io.reactivex.rxjava3.annotations.NonNull Monster monster) { | ||||
|                         Logger.logDebug(String.format("Monster loaded: %s", monster.name)); | ||||
|                         mViewModel.copyFromMonster(monster); | ||||
|                         requireAppCompatActivity().getSupportActionBar().setTitle(getString(R.string.title_edit_monster, monster.name)); | ||||
|                         dispose(); | ||||
|                     } | ||||
|  | ||||
|                     @Override | ||||
|                     public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) { | ||||
|                         // TODO: Show an error state. | ||||
|                         Logger.logError(e); | ||||
|                         dispose(); | ||||
|                     } | ||||
|                 }); | ||||
|  | ||||
|         mHolder.basicInfoButton.setOnClickListener(v -> { | ||||
|             // TODO: Navigate to the EditBasicInfo fragment | ||||
|   | ||||
| @@ -1,7 +1,39 @@ | ||||
| package com.majinnaibu.monstercards.ui.editmonster; | ||||
|  | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.lifecycle.LiveData; | ||||
| import androidx.lifecycle.MutableLiveData; | ||||
| import androidx.lifecycle.ViewModel; | ||||
|  | ||||
| import com.majinnaibu.monstercards.models.Monster; | ||||
|  | ||||
| import java.util.UUID; | ||||
|  | ||||
| public class EditMonsterViewModel extends ViewModel { | ||||
|     // TODO: Implement the ViewModel | ||||
| } | ||||
|     private MutableLiveData<String> mName; | ||||
|     private MutableLiveData<UUID> mMonsterId; | ||||
|  | ||||
|     public EditMonsterViewModel() { | ||||
|  | ||||
|         mName = new MutableLiveData<>(); | ||||
|         mName.setValue(""); | ||||
|  | ||||
|         mMonsterId = new MutableLiveData<>(); | ||||
|         mMonsterId.setValue(UUID.randomUUID()); | ||||
|     } | ||||
|  | ||||
|     public void copyFromMonster(Monster monster) { | ||||
|         // TODO: copy from monster to other fields | ||||
|         mMonsterId.setValue(monster.id); | ||||
|         mName.setValue(monster.name); | ||||
|     } | ||||
|  | ||||
|     public LiveData<String> getName() { | ||||
|         return mName; | ||||
|     } | ||||
|  | ||||
|     public void setName(@NonNull String name) { | ||||
|         mName.setValue(name); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user