Created
June 1, 2020 12:16
-
-
Save lomza/58fbab69b3bb9900cbcb8729f51591bf to your computer and use it in GitHub Desktop.
MoviesListFragment.kt
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
class MoviesListFragment : Fragment() { | |
private lateinit var moviesListAdapter: MoviesListAdapter | |
private lateinit var moviesViewModel: MoviesViewModel | |
private lateinit var moviesList: List<Movie> | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
initData() | |
} | |
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | |
val view = inflater.inflate(R.layout.fragment_movies, container, false) | |
initView(view) | |
return view | |
} | |
private fun initData() { | |
moviesViewModel = ViewModelProvider(this).get(MoviesViewModel::class.java) | |
moviesViewModel.moviesList.observe(this, | |
Observer { movies: List<Movie> -> | |
moviesList = movies | |
moviesListAdapter.setMovieList(movies) | |
} | |
) | |
moviesViewModel.directorsList.observe(this, | |
Observer { _ -> | |
// we need to refresh the movies list in case when director's name changed | |
moviesViewModel.moviesList.value?.let { | |
moviesList = it | |
moviesListAdapter.setMovieList(it) | |
} | |
} | |
) | |
} | |
private suspend fun getDirectorFullName(movie: Movie): String? { | |
return MoviesDatabase.getDatabase(requireContext()).directorDao().findDirectorById(movie.directorId)?.fullName | |
} | |
private fun initView(view: View) { | |
val recyclerView: RecyclerView = view.findViewById(R.id.recyclerview_movies) | |
moviesListAdapter = MoviesListAdapter(this) | |
recyclerView.adapter = moviesListAdapter | |
recyclerView.addItemDecoration(DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)) | |
recyclerView.layoutManager = LinearLayoutManager(requireContext()) | |
} | |
fun removeData() { | |
GlobalScope.launch(Dispatchers.IO) { moviesViewModel.deleteAll() } | |
} | |
fun exportMoviesWithDirectorsToCSVFile(csvFile: File) { | |
csvWriter().open(csvFile, append = false) { | |
// Header | |
writeRow(listOf("[id]", "[${Movie.TABLE_NAME}]", "[${Director.TABLE_NAME}]")) | |
moviesList.forEachIndexed { index, movie -> | |
val directorName: String = moviesViewModel.directorsList.value?.find { it.id == movie.directorId }?.fullName ?: "" | |
writeRow(listOf(index, movie.title, directorName)) | |
} | |
} | |
} | |
companion object { | |
fun newInstance(): MoviesListFragment = MoviesListFragment() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment