-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from BobbyESP/master
feat: Added Quick Lyrics Search Activity
- Loading branch information
Showing
28 changed files
with
1,798 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
123 changes: 123 additions & 0 deletions
123
app/src/main/java/pl/lambada/songsync/activities/quicksearch/QuickLyricsSearchActivity.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package pl.lambada.songsync.activities.quicksearch | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.widget.Toast | ||
import androidx.activity.compose.setContent | ||
import androidx.activity.enableEdgeToEdge | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.ModalBottomSheet | ||
import androidx.compose.material3.ModalBottomSheetDefaults | ||
import androidx.compose.material3.rememberModalBottomSheetState | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import coil.ImageLoader | ||
import coil.disk.DiskCache | ||
import coil.memory.MemoryCache | ||
import kotlinx.coroutines.Dispatchers | ||
import pl.lambada.songsync.R | ||
import pl.lambada.songsync.activities.quicksearch.viewmodel.QuickLyricsSearchViewModel | ||
import pl.lambada.songsync.activities.quicksearch.viewmodel.QuickLyricsSearchViewModelFactory | ||
import pl.lambada.songsync.data.UserSettingsController | ||
import pl.lambada.songsync.data.remote.lyrics_providers.LyricsProviderService | ||
import pl.lambada.songsync.ui.theme.SongSyncTheme | ||
import pl.lambada.songsync.util.dataStore | ||
|
||
class QuickLyricsSearchActivity : AppCompatActivity() { | ||
private val lyricsProviderService = 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) | ||
.maxSizePercent(0.35) | ||
.build() | ||
} | ||
.diskCache { | ||
DiskCache.Builder() | ||
.directory(this.cacheDir.resolve("image_cache")) | ||
.maxSizeBytes(7 * 1024 * 1024) | ||
.build() | ||
} | ||
.respectCacheHeaders(false) | ||
.allowHardware(true) | ||
.crossfade(true) | ||
.bitmapFactoryMaxParallelism(12) | ||
.dispatcher(Dispatchers.IO) | ||
.build() | ||
|
||
enableEdgeToEdge() | ||
handleShareIntent(intent, sendEvent = viewModel::onEvent) | ||
|
||
setContent { | ||
val sheetState = rememberModalBottomSheetState() | ||
val viewModelState = viewModel.state.collectAsStateWithLifecycle() | ||
SongSyncTheme(pureBlack = userSettingsController.pureBlack) { | ||
ModalBottomSheet( | ||
sheetState = sheetState, | ||
properties = ModalBottomSheetDefaults.properties, | ||
onDismissRequest = { finish() } | ||
) { | ||
QuickLyricsSearchPage( | ||
state = viewModelState, | ||
onSendLyrics = { lyrics -> | ||
val resultIntent = Intent().apply { | ||
action = Intent.ACTION_SEND | ||
putExtra("lyrics", lyrics) | ||
type = "text/plain" | ||
} | ||
setResult(RESULT_OK, resultIntent) | ||
finish() | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
private fun handleShareIntent( | ||
intent: Intent, | ||
sendEvent: (QuickLyricsSearchViewModel.Event) -> Unit | ||
) { | ||
when (intent.action) { | ||
Intent.ACTION_SEND -> { | ||
val songName = | ||
intent.getStringExtra("songName") | ||
val artistName = intent.getStringExtra("artistName") | ||
?: "" // Artist name is optional. This may be misleading sometimes. | ||
|
||
if (songName.isNullOrBlank()) { | ||
Toast.makeText( | ||
this, | ||
this.getString(R.string.song_name_not_provided), | ||
Toast.LENGTH_SHORT | ||
).show() | ||
finish() | ||
return | ||
} | ||
|
||
sendEvent( | ||
QuickLyricsSearchViewModel.Event.Fetch( | ||
song = songName to artistName, | ||
context = this | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
lateinit var activityImageLoader: ImageLoader | ||
lateinit var userSettingsController: UserSettingsController | ||
} | ||
} |
Oops, something went wrong.