Skip to content

Commit

Permalink
Refactor: Move UserSettingsController initialization
Browse files Browse the repository at this point in the history
Moved the initialization of UserSettingsController to the onCreate method of QuickLyricsSearchActivity due to problems with use of Context before initialization.

Signed-off-by: Gabriel Fontán <gabilessto@gmail.com>
  • Loading branch information
BobbyESP committed Nov 15, 2024
1 parent e978f54 commit e81ce85
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ import pl.lambada.songsync.ui.theme.SongSyncTheme
import pl.lambada.songsync.util.dataStore

class QuickLyricsSearchActivity : AppCompatActivity() {

val userSettingsController = UserSettingsController(dataStore)
private val lyricsProviderService = LyricsProviderService()

private val viewModel: QuickLyricsSearchViewModel by viewModels {
QuickLyricsSearchViewModelFactory(userSettingsController, lyricsProviderService)
}

@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val userSettingsController = UserSettingsController(dataStore)
val viewModel: QuickLyricsSearchViewModel by viewModels {
QuickLyricsSearchViewModelFactory(userSettingsController, lyricsProviderService)
}
activityImageLoader = ImageLoader.Builder(this)
.memoryCache {
MemoryCache.Builder(this)
Expand All @@ -57,7 +56,7 @@ class QuickLyricsSearchActivity : AppCompatActivity() {
.build()

enableEdgeToEdge()
handleShareIntent(intent)
handleShareIntent(intent, sendEvent = viewModel::onEvent)

setContent {
val sheetState = rememberModalBottomSheetState()
Expand Down Expand Up @@ -86,7 +85,10 @@ class QuickLyricsSearchActivity : AppCompatActivity() {
}


private fun handleShareIntent(intent: Intent) {
private fun handleShareIntent(
intent: Intent,
sendEvent: (QuickLyricsSearchViewModel.Event) -> Unit
) {
when (intent.action) {
Intent.ACTION_SEND -> {
val songName =
Expand All @@ -104,7 +106,7 @@ class QuickLyricsSearchActivity : AppCompatActivity() {
return
}

viewModel.onEvent(
sendEvent(
QuickLyricsSearchViewModel.Event.Fetch(
song = songName to artistName,
context = this
Expand All @@ -116,5 +118,6 @@ class QuickLyricsSearchActivity : AppCompatActivity() {

companion object {
lateinit var activityImageLoader: ImageLoader
lateinit var userSettingsController: UserSettingsController
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class QuickLyricsSearchViewModel(
}

if (lyricsCall.isSuccess) {

val syncedLyrics = lyricsCall.getOrNull()

if (syncedLyrics == null) updateLyricsState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,11 @@ class LyricsProviderService {
musixmatchSongInfo = it
} ?: throw NoTrackFoundException()
}
} catch (e: InternalErrorException) {
throw e
} catch (e: NoTrackFoundException) {
throw e
} catch (e: EmptyQueryException) {
throw e
} catch (e: Exception) {
throw InternalErrorException(Log.getStackTraceString(e))
when (e) {
is InternalErrorException, is NoTrackFoundException, is EmptyQueryException -> throw e
else -> throw InternalErrorException(Log.getStackTraceString(e))
}
}
}

Expand Down

0 comments on commit e81ce85

Please sign in to comment.