Skip to content

Commit 188e039

Browse files
committed
refactor DetailActivity
1 parent cf7ee0b commit 188e039

File tree

2 files changed

+89
-100
lines changed

2 files changed

+89
-100
lines changed

app/src/main/java/com/wirelessalien/android/moviedb/activity/DetailActivity.kt

Lines changed: 31 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package com.wirelessalien.android.moviedb.activity
2121

22+
import android.annotation.SuppressLint
2223
import android.app.Activity
2324
import android.app.UiModeManager
2425
import android.content.ComponentName
@@ -219,6 +220,7 @@ class DetailActivity : BaseActivity(), ListTmdbBottomSheetFragment.OnListCreated
219220
private var mCastAndCrewLoaded = false
220221
private var mVideosLoaded = false
221222
private var videos: JSONArray? = null
223+
@SuppressLint("SetTextI18n")
222224
override fun onCreate(savedInstanceState: Bundle?) {
223225
super.onCreate(savedInstanceState)
224226
binding = ActivityDetailBinding.inflate(
@@ -457,12 +459,7 @@ class DetailActivity : BaseActivity(), ListTmdbBottomSheetFragment.OnListCreated
457459
databaseHelper.onCreate(database)
458460

459461
// Check if the show is already in the database.
460-
val cursor = database.rawQuery(
461-
"SELECT * FROM " +
462-
MovieDatabaseHelper.TABLE_MOVIES +
463-
" WHERE " + MovieDatabaseHelper.COLUMN_MOVIES_ID +
464-
"=" + movieId + " LIMIT 1", null
465-
)
462+
val cursor = databaseHelper.getMovieCursor(movieId)
466463
cursor.use { cursor1 ->
467464
if (cursor1.count > 0) {
468465
// A record has been found
@@ -729,12 +726,7 @@ class DetailActivity : BaseActivity(), ListTmdbBottomSheetFragment.OnListCreated
729726
var userRatingString = getString(R.string.rating_na)
730727
var reviewString = getString(R.string.rating_na)
731728
var timesWatchedString = getString(R.string.rating_na)
732-
val cursorr = database.rawQuery(
733-
"SELECT * FROM " +
734-
MovieDatabaseHelper.TABLE_MOVIES +
735-
" WHERE " + MovieDatabaseHelper.COLUMN_MOVIES_ID +
736-
"=" + movieId + " LIMIT 1", null
737-
)
729+
val cursorr = databaseHelper.getMovieCursor(movieId)
738730
cursorr.use {
739731
if (it.moveToFirst()) {
740732
val personalRating = it.getFloat(it.getColumnIndexOrThrow(MovieDatabaseHelper.COLUMN_PERSONAL_RATING))
@@ -833,16 +825,10 @@ class DetailActivity : BaseActivity(), ListTmdbBottomSheetFragment.OnListCreated
833825
if (added) {
834826

835827
// Remove the show from the database.
836-
database.delete(
837-
MovieDatabaseHelper.TABLE_MOVIES,
838-
MovieDatabaseHelper.COLUMN_MOVIES_ID + "=" + movieId, null
839-
)
828+
databaseHelper.deleteMovie(movieId)
840829

841830
// Remove all episodes related to the show from the database.
842-
database.delete(
843-
MovieDatabaseHelper.TABLE_EPISODES,
844-
MovieDatabaseHelper.COLUMN_MOVIES_ID + "=" + movieId, null
845-
)
831+
databaseHelper.deleteEpisodesForMovie(movieId)
846832

847833
added = false
848834
binding.fabSave.icon = ContextCompat.getDrawable(
@@ -2184,29 +2170,17 @@ class DetailActivity : BaseActivity(), ListTmdbBottomSheetFragment.OnListCreated
21842170
val seasonNumber = season.getInt("season_number")
21852171
val episodeCount = season.getInt("episode_count")
21862172
for (j in 1..episodeCount) {
2187-
val cursor = database.rawQuery(
2188-
"SELECT * FROM ${MovieDatabaseHelper.TABLE_EPISODES} WHERE " +
2189-
"${MovieDatabaseHelper.COLUMN_MOVIES_ID} = ? AND " +
2190-
"${MovieDatabaseHelper.COLUMN_SEASON_NUMBER} = ? AND " +
2191-
"${MovieDatabaseHelper.COLUMN_EPISODE_NUMBER} = ?",
2192-
arrayOf(movieId.toString(), seasonNumber.toString(), j.toString())
2193-
)
2194-
if (cursor.count == 0) {
2173+
if (!databaseHelper.isEpisodeInDatabase(movieId, seasonNumber, listOf(j))) {
21952174
val values = ContentValues().apply {
21962175
put(MovieDatabaseHelper.COLUMN_MOVIES_ID, movieId)
21972176
put(MovieDatabaseHelper.COLUMN_SEASON_NUMBER, seasonNumber)
21982177
put(MovieDatabaseHelper.COLUMN_EPISODE_NUMBER, j)
21992178
}
22002179
val newRowId = database.insert(MovieDatabaseHelper.TABLE_EPISODES, null, values)
22012180
if (newRowId == -1L) {
2202-
Toast.makeText(
2203-
this,
2204-
R.string.error_adding_episode_to_database,
2205-
Toast.LENGTH_SHORT
2206-
).show()
2181+
Toast.makeText(this, R.string.error_adding_episode_to_database, Toast.LENGTH_SHORT).show()
22072182
}
22082183
}
2209-
cursor.close()
22102184
}
22112185
}
22122186
} catch (e: JSONException) {
@@ -2370,12 +2344,7 @@ class DetailActivity : BaseActivity(), ListTmdbBottomSheetFragment.OnListCreated
23702344
databaseHelper.onCreate(database)
23712345

23722346
// Retrieve and present saved data of the show.
2373-
val cursor = database.rawQuery(
2374-
"SELECT * FROM " +
2375-
MovieDatabaseHelper.TABLE_MOVIES +
2376-
" WHERE " + MovieDatabaseHelper.COLUMN_MOVIES_ID +
2377-
"=" + movieId + " LIMIT 1", null
2378-
)
2347+
val cursor = databaseHelper.getMovieCursor(movieId)
23792348
if (cursor.count > 0) {
23802349
cursor.moveToFirst()
23812350
// Set the rating to the personal rating of the user.
@@ -2872,7 +2841,7 @@ class DetailActivity : BaseActivity(), ListTmdbBottomSheetFragment.OnListCreated
28722841
// Save the category to the database
28732842
val showValues = ContentValues()
28742843
database = databaseHelper.writableDatabase
2875-
val cursor = database.rawQuery("SELECT * FROM " + MovieDatabaseHelper.TABLE_MOVIES + " WHERE " + MovieDatabaseHelper.COLUMN_MOVIES_ID + "=" + movieId + " LIMIT 1", null)
2844+
val cursor = databaseHelper.getMovieCursor(movieId)
28762845

28772846
val currentDate = SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).format(Date())
28782847
val category = getCategoryNumber(position)
@@ -3240,7 +3209,7 @@ class DetailActivity : BaseActivity(), ListTmdbBottomSheetFragment.OnListCreated
32403209

32413210
private fun updateEditShowDetails() {
32423211
database = databaseHelper.writableDatabase
3243-
database.rawQuery("SELECT * FROM " + MovieDatabaseHelper.TABLE_MOVIES + " WHERE " + MovieDatabaseHelper.COLUMN_MOVIES_ID + "=" + movieId + " LIMIT 1", null).use { cursor ->
3212+
databaseHelper.getMovieCursor(movieId).use { cursor ->
32443213
if (cursor.count > 0) {
32453214
cursor.moveToFirst()
32463215
when (cursor.getInt(cursor.getColumnIndexOrThrow(MovieDatabaseHelper.COLUMN_CATEGORIES))) {
@@ -3380,26 +3349,26 @@ class DetailActivity : BaseActivity(), ListTmdbBottomSheetFragment.OnListCreated
33803349
}
33813350
}
33823351

3383-
private fun formatDateString(dateString: String): String {
3384-
return when {
3385-
dateString.startsWith("00-00-") -> {
3386-
val year = dateString.substring(6)
3387-
year
3388-
}
3389-
dateString.startsWith("00-") -> {
3390-
val monthYear = dateString.substring(3)
3391-
SimpleDateFormat("MM-yyyy", Locale.getDefault()).parse(monthYear)?.let {
3392-
SimpleDateFormat("MMMM yyyy", Locale.getDefault()).format(it)
3393-
} ?: dateString
3394-
}
3395-
else -> {
3396-
val dbDateFormat = SimpleDateFormat("dd-MM-yyyy", Locale.getDefault())
3397-
dbDateFormat.parse(dateString)?.let {
3398-
DateFormat.getDateInstance(DateFormat.DEFAULT).format(it)
3399-
} ?: dateString
3400-
}
3401-
}
3402-
}
3352+
// private fun formatDateString(dateString: String): String {
3353+
// return when {
3354+
// dateString.startsWith("00-00-") -> {
3355+
// val year = dateString.substring(6)
3356+
// year
3357+
// }
3358+
// dateString.startsWith("00-") -> {
3359+
// val monthYear = dateString.substring(3)
3360+
// SimpleDateFormat("MM-yyyy", Locale.getDefault()).parse(monthYear)?.let {
3361+
// SimpleDateFormat("MMMM yyyy", Locale.getDefault()).format(it)
3362+
// } ?: dateString
3363+
// }
3364+
// else -> {
3365+
// val dbDateFormat = SimpleDateFormat("dd-MM-yyyy", Locale.getDefault())
3366+
// dbDateFormat.parse(dateString)?.let {
3367+
// DateFormat.getDateInstance(DateFormat.DEFAULT).format(it)
3368+
// } ?: dateString
3369+
// }
3370+
// }
3371+
// }
34033372

34043373
private fun showYearMonthPickerDialog(context: Context, onYearMonthSelected: (Int, Int?) -> Unit) {
34053374
val binding = DialogYearMonthPickerBinding.inflate(LayoutInflater.from(context))

app/src/main/java/com/wirelessalien/android/moviedb/helper/MovieDatabaseHelper.kt

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import kotlinx.coroutines.Dispatchers
4646
import kotlinx.coroutines.launch
4747
import kotlinx.coroutines.withContext
4848
import org.json.JSONArray
49-
import org.json.JSONException
5049
import org.json.JSONObject
5150
import java.io.BufferedOutputStream
5251
import java.io.BufferedReader
@@ -65,43 +64,36 @@ import java.util.Locale
6564
*/
6665
class MovieDatabaseHelper (context: Context?) : SQLiteOpenHelper(context, databaseFileName, null, DATABASE_VERSION), AutoCloseable {
6766

68-
/**
69-
* Converts the show table in the database to a JSON string.
70-
*
71-
* @param database the database to get the data from.
72-
* @return a string in JSON format containing all the show data.
73-
*/
74-
75-
private fun getEpisodesForMovie(movieId: Int, database: SQLiteDatabase): JSONArray {
76-
val selectQuery =
77-
"SELECT * FROM $TABLE_EPISODES WHERE $COLUMN_MOVIES_ID = $movieId"
78-
val cursor = database.rawQuery(selectQuery, null)
79-
80-
// Convert the database to JSON
81-
val episodesSet = JSONArray()
82-
cursor.moveToFirst()
83-
while (!cursor.isAfterLast) {
84-
val totalColumn = cursor.columnCount
85-
val rowObject = JSONObject()
86-
for (i in 0 until totalColumn) {
87-
if (cursor.getColumnName(i) != null) {
88-
try {
89-
if (cursor.getString(i) != null) {
90-
rowObject.put(cursor.getColumnName(i), cursor.getString(i))
91-
} else {
92-
rowObject.put(cursor.getColumnName(i), "")
93-
}
94-
} catch (e: JSONException) {
95-
e.printStackTrace()
96-
}
97-
}
98-
}
99-
episodesSet.put(rowObject)
100-
cursor.moveToNext()
101-
}
102-
cursor.close()
103-
return episodesSet
104-
}
67+
// private fun getEpisodesForMovie(movieId: Int, database: SQLiteDatabase): JSONArray {
68+
// val selectQuery =
69+
// "SELECT * FROM $TABLE_EPISODES WHERE $COLUMN_MOVIES_ID = $movieId"
70+
// val cursor = database.rawQuery(selectQuery, null)
71+
//
72+
// // Convert the database to JSON
73+
// val episodesSet = JSONArray()
74+
// cursor.moveToFirst()
75+
// while (!cursor.isAfterLast) {
76+
// val totalColumn = cursor.columnCount
77+
// val rowObject = JSONObject()
78+
// for (i in 0 until totalColumn) {
79+
// if (cursor.getColumnName(i) != null) {
80+
// try {
81+
// if (cursor.getString(i) != null) {
82+
// rowObject.put(cursor.getColumnName(i), cursor.getString(i))
83+
// } else {
84+
// rowObject.put(cursor.getColumnName(i), "")
85+
// }
86+
// } catch (e: JSONException) {
87+
// e.printStackTrace()
88+
// }
89+
// }
90+
// }
91+
// episodesSet.put(rowObject)
92+
// cursor.moveToNext()
93+
// }
94+
// cursor.close()
95+
// return episodesSet
96+
// }
10597

10698
suspend fun getJSONExportString(database: SQLiteDatabase): String = withContext(Dispatchers.IO) {
10799
val moviesArray = JSONArray()
@@ -1136,4 +1128,32 @@ class MovieDatabaseHelper (context: Context?) : SQLiteOpenHelper(context, databa
11361128
cursor.close()
11371129
return mediaType
11381130
}
1131+
1132+
fun getMovieCursor(movieId: Int): Cursor {
1133+
val db = this.readableDatabase
1134+
return db.rawQuery(
1135+
"SELECT * FROM " +
1136+
TABLE_MOVIES +
1137+
" WHERE " + COLUMN_MOVIES_ID +
1138+
"=" + movieId + " LIMIT 1", null
1139+
)
1140+
}
1141+
1142+
fun deleteMovie(movieId: Int) {
1143+
val db = this.writableDatabase
1144+
db.delete(
1145+
TABLE_MOVIES,
1146+
"$COLUMN_MOVIES_ID = ?",
1147+
arrayOf(movieId.toString())
1148+
)
1149+
}
1150+
1151+
fun deleteEpisodesForMovie(movieId: Int) {
1152+
val db = this.writableDatabase
1153+
db.delete(
1154+
TABLE_EPISODES,
1155+
"$COLUMN_MOVIES_ID = ?",
1156+
arrayOf(movieId.toString())
1157+
)
1158+
}
11391159
}

0 commit comments

Comments
 (0)