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.NonNull; | ||||||
| import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||||
| import androidx.fragment.app.Fragment; |  | ||||||
| import androidx.lifecycle.ViewModelProvider; | import androidx.lifecycle.ViewModelProvider; | ||||||
|  |  | ||||||
| import com.majinnaibu.monstercards.R; | 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; | 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 EditMonsterViewModel mViewModel; | ||||||
|     private ViewHolder mHolder; |     private ViewHolder mHolder; | ||||||
| @@ -26,13 +33,35 @@ public class EditMonsterFragment extends Fragment { | |||||||
|     @Override |     @Override | ||||||
|     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, |     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, | ||||||
|                              @Nullable Bundle savedInstanceState) { |                              @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. |         MonsterRepository repository = getMonsterRepository(); | ||||||
|         // TODO: If there is an error loading the monster show the error. |         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); |         View root = inflater.inflate(R.layout.fragment_edit_monster, container, false); | ||||||
|  |  | ||||||
|         mHolder = new ViewHolder(root); |         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 -> { |         mHolder.basicInfoButton.setOnClickListener(v -> { | ||||||
|             // TODO: Navigate to the EditBasicInfo fragment |             // TODO: Navigate to the EditBasicInfo fragment | ||||||
|   | |||||||
| @@ -1,7 +1,39 @@ | |||||||
| package com.majinnaibu.monstercards.ui.editmonster; | package com.majinnaibu.monstercards.ui.editmonster; | ||||||
|  |  | ||||||
|  | import androidx.annotation.NonNull; | ||||||
|  | import androidx.lifecycle.LiveData; | ||||||
|  | import androidx.lifecycle.MutableLiveData; | ||||||
| import androidx.lifecycle.ViewModel; | import androidx.lifecycle.ViewModel; | ||||||
|  |  | ||||||
|  | import com.majinnaibu.monstercards.models.Monster; | ||||||
|  |  | ||||||
|  | import java.util.UUID; | ||||||
|  |  | ||||||
| public class EditMonsterViewModel extends ViewModel { | 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); | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
| @@ -34,6 +34,7 @@ | |||||||
|     <string name="label_legendary_actions">Legendary Actions</string> |     <string name="label_legendary_actions">Legendary Actions</string> | ||||||
|     <string name="label_lair_actions">Lair Actions</string> |     <string name="label_lair_actions">Lair Actions</string> | ||||||
|     <string name="label_regional_actions">Regional Actions</string> |     <string name="label_regional_actions">Regional Actions</string> | ||||||
|  |     <string name="title_edit_monster">Edit %1$s</string> | ||||||
|     <string name="default_monster_name">Unnamed Monster</string> |     <string name="default_monster_name">Unnamed Monster</string> | ||||||
|     <string name="snackbar_failed_to_create_monster">Failed to create monster</string> |     <string name="snackbar_failed_to_create_monster">Failed to create monster</string> | ||||||
|     <string name="snackbar_monster_created">%1$s created</string> |     <string name="snackbar_monster_created">%1$s created</string> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user