2021-08-17 12:17:43 +02:00
|
|
|
package be.kuleuven.howlongtobeat
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2021-08-19 09:46:03 +02:00
|
|
|
import androidx.core.os.bundleOf
|
2021-08-17 12:17:43 +02:00
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import androidx.navigation.fragment.findNavController
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
import be.kuleuven.howlongtobeat.databinding.FragmentGamelistBinding
|
|
|
|
import be.kuleuven.howlongtobeat.model.Game
|
|
|
|
import be.kuleuven.howlongtobeat.model.GameRepository
|
|
|
|
|
|
|
|
class GameListFragment : Fragment(R.layout.fragment_gamelist) {
|
|
|
|
|
|
|
|
private val gameList = arrayListOf<Game>()
|
|
|
|
|
|
|
|
private lateinit var binding: FragmentGamelistBinding
|
|
|
|
private lateinit var main: MainActivity
|
|
|
|
private lateinit var adapter: GameListAdapter
|
|
|
|
private lateinit var gameRepository: GameRepository
|
|
|
|
|
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View? {
|
|
|
|
binding = FragmentGamelistBinding.inflate(layoutInflater)
|
|
|
|
main = activity as MainActivity
|
2021-08-17 20:44:27 +02:00
|
|
|
gameRepository = GameRepository.defaultImpl(main.applicationContext)
|
2021-08-17 12:17:43 +02:00
|
|
|
loadGames()
|
|
|
|
|
|
|
|
adapter = GameListAdapter(gameList)
|
|
|
|
binding.rvGameList.adapter = adapter
|
|
|
|
binding.rvGameList.layoutManager = LinearLayoutManager(this.context)
|
|
|
|
|
2021-08-17 15:58:19 +02:00
|
|
|
binding.btnAddTodo.setOnClickListener {
|
|
|
|
findNavController().navigate(R.id.action_gameListFragment_to_loadingFragment)
|
|
|
|
}
|
2021-08-17 12:17:43 +02:00
|
|
|
return binding.root
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun loadGames() {
|
2021-08-19 09:46:03 +02:00
|
|
|
gameList.clear()
|
2021-08-17 12:17:43 +02:00
|
|
|
gameList.addAll(gameRepository.load())
|
|
|
|
if(!gameList.any()) {
|
|
|
|
gameList.add(Game.NONE_YET)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 09:46:03 +02:00
|
|
|
fun selectGame(game: Game) {
|
|
|
|
findNavController().navigate(R.id.action_gameListFragment_to_gameDetailFragment, bundleOf(Game.GAME_ID to game.id.toString()))
|
2021-08-17 12:17:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun clearAllItems() {
|
|
|
|
gameList.clear()
|
2021-08-19 09:46:03 +02:00
|
|
|
gameList.add(Game.NONE_YET)
|
2021-08-17 12:17:43 +02:00
|
|
|
|
2021-08-19 09:46:03 +02:00
|
|
|
adapter.notifyDataSetChanged()
|
2021-08-17 12:17:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|