This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.lomza.moviesroom"> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" |
This file contains hidden or 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
fun exportDirectorsToCSVFile(csvFile: File) { | |
csvWriter().open(csvFile, append = false) { | |
// Header | |
writeRow(listOf("[id]", "[${Director.TABLE_NAME}]")) | |
directorsList.forEachIndexed { index, director -> | |
writeRow(listOf(index, director.fullName)) | |
} | |
} | |
} |
This file contains hidden or 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() | |
} |
This file contains hidden or 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
private fun getCSVFileName() : String = | |
if (MOVIES_SHOWN) "MoviesRoomExample.csv" else "DirectorsRoomExample.csv" | |
private fun exportDatabaseToCSVFile() { | |
val csvFile = generateFile(this, getCSVFileName()) | |
if (csvFile != null) { | |
if (MOVIES_SHOWN) { | |
(shownFragment as MoviesListFragment).exportMoviesWithDirectorsToCSVFile(csvFile) | |
} else { | |
(shownFragment as DirectorsListFragment).exportDirectorsToCSVFile(csvFile) |
This file contains hidden or 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
fun generateFile(context: Context, fileName: String): File? { | |
val csvFile = File(context.filesDir, fileName) | |
csvFile.createNewFile() | |
return if (csvFile.exists()) { | |
csvFile | |
} else { | |
null | |
} | |
} |
This file contains hidden or 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
{ | |
"formatVersion": 1, | |
"database": { | |
"version": 1, | |
"identityHash": "627455825d506754f171b32a983cfc02", | |
"entities": [ | |
{ | |
"tableName": "movie", | |
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`mid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `title` TEXT NOT NULL, `directorId` INTEGER NOT NULL, FOREIGN KEY(`directorId`) REFERENCES `director`(`did`) ON UPDATE NO ACTION ON DELETE CASCADE )", | |
"fields": [ |
This file contains hidden or 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
private suspend fun saveMovie(movieTitle: String, movieDirectorFullName: String) { | |
if (TextUtils.isEmpty(movieTitle) || TextUtils.isEmpty(movieDirectorFullName)) { | |
return | |
} | |
val directorDao = MoviesDatabase.getDatabase(requireContext()).directorDao() | |
val movieDao = MoviesDatabase.getDatabase(requireContext()).movieDao() | |
var directorId: Long = -1L | |
if (movieDirectorFullNameExtra != null) { | |
// clicked on item row -> update | |
val directorToUpdate = directorDao.findDirectorByName(movieDirectorFullNameExtra) |
This file contains hidden or 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
private suspend fun saveDirector(fullName: String) { | |
if (TextUtils.isEmpty(fullName)) { | |
return | |
} | |
val directorDao = MoviesDatabase.getDatabase(requireContext()).directorDao() | |
if (directorFullNameExtra != null) { | |
// clicked on item row -> update | |
val directorToUpdate = directorDao.findDirectorByName(directorFullNameExtra) | |
if (directorToUpdate != null) { | |
if (directorToUpdate.fullName != fullName) { |
This file contains hidden or 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 DirectorsListFragment : Fragment() { | |
private lateinit var directorsListAdapter: DirectorsListAdapter | |
private lateinit var directorsViewModel: DirectorsViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
initData() | |
} |
This file contains hidden or 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 DirectorsViewModel(application: Application) : AndroidViewModel(application) { | |
private val directorDao: DirectorDao = MoviesDatabase.getDatabase(application).directorDao() | |
val directorList: LiveData<List<Director>> | |
init { | |
directorList = directorDao.allDirectors | |
} | |
suspend fun insert(vararg directors: Director) { |