Home > Enterprise >  How should I go about implementing MVVM architecture pattern in my project?
How should I go about implementing MVVM architecture pattern in my project?

Time:11-03

I know this is a very documented topic, but I couldn't find a way to implement it in my project, even after spending hours trying to figure it out.

My root problem is that I have a RecyclerView with an Adapter whose content isn't updating as I'd like. I'm a beginner in Android, so I didn't implement any MVVM or such architecture, and my project only contains a repository, fetching data from Firebase Database, and passing it to a list of ShowModel, a copy of said list being used in my Adapter to display my shows (In order to filter/sort them without modifying the list with all shows).

However, when adding a show to the database from another Activity, my Adapter isn't displaying the newly added show (as detailed here)

I was told to use LiveData and ViewModel, but even though I started understanding how it works after spending time researching it, I don't fully get how I should use it in order to implement it in my project.

Currently I have the following classes:

The Adapter:


class ShowAdapter(private val context: MainActivity, private val layoutId: Int, private val textNoResult: TextView?) : RecyclerView.Adapter<ShowAdapter.ViewHolder>(), Filterable {
    var displayList = ArrayList(showList)

    class ViewHolder(view : View) : RecyclerView.ViewHolder(view){
        val showName: TextView = view.findViewById(R.id.show_name)
        val showMenuIcon: ImageView = view.findViewById(R.id.menu_icon)
    }

    @SuppressLint("NewApi")
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("NewApi", "WeekBasedYear")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val currentShow = displayList[position]
        val index = holder.adapterPosition

        holder.showName.text = currentShow.name

        holder.itemView.setOnClickListener{ // Display show content
            val intent = Intent(context, DetailsActivity::class.java)
            intent.putExtra("position", index)
            startActivity(context, intent, null)
        }

        holder.showMenuIcon.setOnClickListener{
            val popupMenu = PopupMenu(context, it)
            popupMenu.menuInflater.inflate(R.menu.show_management_menu, popupMenu.menu)
            popupMenu.show()
            popupMenu.setOnMenuItemClickListener {
                when(it.itemId){
                    R.id.edit -> { // Edit show
                        val intent = Intent(context, AddShowActivity::class.java)
                        intent.putExtra("position", index)
                        startActivity(context, intent, null)
                        return@setOnMenuItemClickListener true
                    }
                    R.id.delete -> { // Delete show
                            val repo = ShowRepository()
                            repo.deleteShow(currentShow)
                            displayList.remove(currentShow)
                            notifyItemRemoved(index)
                            return@setOnMenuItemClickListener true
                    }
                    else -> false
                }
            }
        }
    }

    override fun getItemCount(): Int = displayList.size

    // Sorting/Filtering methods
}

The fragment displaying the adapter:

class HomeFragment : Fragment() {
    private lateinit var context: MainActivity
    private lateinit var verticalRecyclerView: RecyclerView
    private lateinit var buttonAddShow: Button
    private lateinit var showsAdapter: ShowAdapter

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_home, container, false)

        context = getContext() as MainActivity

        buttonAddShow = view.findViewById(R.id.home_button_add_show)
        buttonAddShow.setOnClickListener{ // Starts activity to add a show
            startActivity(Intent(context, AddShowActivity::class.java))
        }

        verticalRecyclerView = view.findViewById(R.id.home_recycler_view)
        showsAdapter = ShowAdapter(context, R.layout.item_show, null)

        verticalRecyclerView.adapter = showsAdapter

        return view
    }
}

The MainActivity:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        loadFragment(HomeFragment())
    }

    private fun loadFragment(fragment: Fragment){
        val repo = ShowRepository()

        if(showsListener != null) databaseRef.removeEventListener(showsListener!!)

        repo.updateData{
            val transaction = supportFragmentManager.beginTransaction()
            transaction.replace(R.id.fragment_container, fragment)
            transaction.addToBackStack(null)
            if(supportFragmentManager.isStateSaved)transaction.commitAllowingStateLoss()
            else transaction.commit()
        }
    }
}

The repository:

class ShowRepository {

    object Singleton{
        val databaseRef = FirebaseDatabase.getInstance().getReference("shows")
        val showList = arrayListOf<ShowModel>()
        var showsListener: ValueEventListener? = null
    }

    fun updateData(callback: () -> Unit){
        showsListener = databaseRef.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {
                showList.clear()

                for(ds in snapshot.children){
                    val show = ds.getValue(ShowModel::class.java)
                    if(show != null) showList.add(show)
                }
                callback()
            }

            override fun onCancelled(p0: DatabaseError) { }
        })
    }

    fun insertShow(show: ShowModel){
        databaseRef.child(show.id).setValue(show)
    }

    fun deleteShow(show: ShowModel){
        databaseRef.child(show.id).removeValue()
    }
}

From what I understand of LiveData and ViewModel, what I should do is creating a ShowViewModel containing a MutableLiveData<List<ShowModel>> containing the shows, and then observe it in my HomeFragment and update the adapter depending on the changes happening. However, everytime I start something to implement it, I encounter a situation where I'm lost and don't know what I should do, which leads me back to square one once again. I've been trying this for more than a week without progressing even a little bit, and that's why I'm here, hoping for some insight.

Sorry for the silly question and the absurd amount of informations, and hoping someone will be able to help me understand what I do wrong/should do.

CodePudding user response:

(this ended up longer than I meant it to be - hope it's not too much! There's a lot to learn, but you don't have to make it super complicated at first)

Broadly, working backwards, it should go like this:

Adapter

  • displays whatever the Fragment tells it to (some kind of setData function that updates its internal list and refreshes)
  • passes events to the Fragment (deleteItem(item), showDetails(item) etc.) - don't have the Adapter doing things like starting Activites, that's not its responsibility

Fragment

  • grabs a reference to any ViewModels (only certain components like Fragments and Activities can actually "own" them)
  • observes any LiveData (or collects Flows if you're doing it that way) on the VM, and updates stuff in the UI in response
  • e.g. model.shows.observe(viewLifecycleOwner) { shows -> adapter.setData(shows) }
  • handles UI events and calls methods on the VM in response, e.g. click listeners, events from the Adapter

ViewModel

  • acts as a go-between for the UI (the Fragment) and the data layer (the repository)
  • exposes methods for handling events like deleting items, interacts with the data layer as required (e.g. calling the appropriate delete function)
  • exposes data state for the UI to observe, so it can react to changes/updates (e.g. a LiveData containing the current list of shows that the data layer has provided)

That's the basic setup - the VM exposes data which the UI layer observes and reacts to, by displaying it. The UI layer also produces events (usually down to user interaction) which are passed to the VM. You can read more about this general approach in this guide about app architecture - it's worth reading because not only is it recommended as a way to build apps, a lot of the components you use in modern Android are designed with this kind of approach in mind (like the reactive model of wiring stuff up).


You could handle the Adapter events like this:

// in your Adapter
var itemDeletedListener: ((Item) -> Unit)? = null

// when the delete event happens for an item
itemDeletedListener?.invoke(item)


// in your Fragment
adapter.itemDeletedListener = { viewModel.deleteItem(it) }

which is easier than implementing an interface, and lets you wire up your Adapter similar to doing setOnClickListener on a button. Notice we're passing the actual Item object here instead of a list index - generally this is easier to work with, you don't need to maintain multiple copies of a list just so you can look up an index given to you by something else. Passing a unique ID can make sense though, especially if you're working with a database! But usually the object itself is more useful and consistent


The data layer is the tricky bit - the ViewModel needs to communicate with that to get the current state. Say you delete an item - you then need to get the current, updated list of shows. You have three approaches:

Call the delete function, immediately after fetch the current data, and set it on the appropriate LiveData

This can work, but it's not very reactive - you're doing one option, then immediately doing another because you know your data is stale. It would be better if the new data just arrived automatically and you could react to that by pushing it out. The other issue is that calling the delete function might not have an immediate effect - if you fetch the current data, nothing might have changed yet. It's better if the data layer is responsible for announcing updates.

This is the simplest approach though, and probably a good start! You could run this task in a coroutine (viewModelScope.launch { // delete and fetch and update LiveData }) so any slowness doesn't block the current thread.

Have the data layer's functions return the current, updated data that results

Similar to above, you're just sort of pushing the fetching into the data layer. This requires all those functions to be written to return the current state, which could take a while! And depending on what data you want, this might be impossible - if you have an active query on some data, how does the function know what specific data to return?

Make the ViewModel observe the data it wants, so when the data layer updates, you get the results automatically

This is the recommended reactive approach - again it's that two-way idea. The VM calling a function on the data layer is completely separate from the VM receiving new data. One thing just happens as a natural consequence of the other, they don't need to be tied together. You just need to wire them up right!


How do you actually do that though? If you're working with something like Room, that's already baked in. Queries can return async data providers like LiveData or Flows - your VM just needs to observe those and expose the results, or just expose them directly. That way, when a table is updated, any queries (like the current shows) push a new value, and the observers receive it and do whatever they need to do, like telling the Adapter to display the data. It all Just Works once it's wired up.

Since you have your own repo, you need to expose your own data sources. You could have a currentShows LiveData or (probably preferably) the flow equivalent, StateFlow. When the repo initialises, and when any data is changed, it updates that currentShows data. Anything observing that (e.g. the VM, the Fragment through a LiveData/Flow that the VM exposes) will automatically get the new values. So broadly:

// Repo

// this setup is exactly the same as your typical LiveData, except you need an initial value
private val _currentShows = MutableStateFlow<List<Show>>(emptyList()) // or whatever default
val currentShows: StateFlow<List<Show>> = _currentShows

fun deleteItem(item: Item) {
    // do the deletion
    // get the updated show list
    _currentShows.value = updatedShowList
}


// ViewModel

// one way of doing things - you have a lot of options! This literally just exposes
// the state from the data layer, and turns it into a LiveData (if you want that)
val currentShows = repo.currentShows.asLiveData()


// Fragment

// wire things up so you handle new data as it arrives
viewModel.currentShows.observe(viewLifecycleOwner) { shows -> adapter.setData(shows) }

That's basically it. I've skimmed over a lot because honestly, there's a lot to learn with this - especially about Flows and coroutines if you're not already familiar with those. But hopefully that gives you an overview of the general idea, and don't be afraid to take shortcuts (like just updating your data in the ViewModel by setting its LiveData values) while you're learning and getting the hang of it. Definitely give that app architecture guide a read, and also the guides for ViewModels and LiveData. It'll start to click when you get the general idea!

  • Related