mirror of
https://github.com/rebelonion/Dantotsu.git
synced 2026-01-29 11:01:01 +00:00
* small changes * Changes * new nest button for settings * full language name in language selector * tv banner * hide lang selector if there is one language only * and some small changes * import fix * alter dialog * wont refresh if nothing is changed
104 lines
3.9 KiB
Kotlin
104 lines
3.9 KiB
Kotlin
package ani.dantotsu.others.imagesearch
|
|
|
|
import android.content.Intent
|
|
import android.net.Uri
|
|
import android.os.Bundle
|
|
import android.view.View
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
import androidx.activity.viewModels
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import androidx.lifecycle.lifecycleScope
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
import ani.dantotsu.App.Companion.context
|
|
import ani.dantotsu.R
|
|
import ani.dantotsu.connections.anilist.Anilist
|
|
import ani.dantotsu.databinding.ActivityImageSearchBinding
|
|
import ani.dantotsu.initActivity
|
|
import ani.dantotsu.media.MediaDetailsActivity
|
|
import ani.dantotsu.others.LangSet
|
|
import ani.dantotsu.themes.ThemeManager
|
|
import ani.dantotsu.toast
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.launch
|
|
import kotlinx.coroutines.withContext
|
|
|
|
class ImageSearchActivity : AppCompatActivity() {
|
|
private lateinit var binding: ActivityImageSearchBinding
|
|
private val viewModel: ImageSearchViewModel by viewModels()
|
|
|
|
private val imageSelectionLauncher =
|
|
registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
|
|
uri?.let { imageUri ->
|
|
val contentResolver = applicationContext.contentResolver
|
|
lifecycleScope.launch(Dispatchers.IO) {
|
|
withContext(Dispatchers.Main) {
|
|
binding.progressBar.visibility = View.VISIBLE
|
|
}
|
|
val inputStream = contentResolver.openInputStream(imageUri)
|
|
|
|
if (inputStream != null) viewModel.analyzeImage(inputStream)
|
|
else toast(getString(R.string.error_loading_image))
|
|
|
|
withContext(Dispatchers.Main) {
|
|
binding.progressBar.visibility = View.GONE
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
LangSet.setLocale(this)
|
|
initActivity(this)
|
|
ThemeManager(this).applyTheme()
|
|
binding = ActivityImageSearchBinding.inflate(layoutInflater)
|
|
setContentView(binding.root)
|
|
|
|
binding.uploadImage.setOnClickListener {
|
|
viewModel.clearResults()
|
|
imageSelectionLauncher.launch("image/*")
|
|
}
|
|
binding.imageSearchTitle.setOnClickListener {
|
|
onBackPressedDispatcher.onBackPressed()
|
|
}
|
|
|
|
viewModel.searchResultLiveData.observe(this) { result ->
|
|
result?.let { displayResult(it) }
|
|
}
|
|
|
|
}
|
|
|
|
private fun displayResult(result: ImageSearchViewModel.SearchResult) {
|
|
val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
|
|
val searchResults: List<ImageSearchViewModel.ImageResult> = result.result.orEmpty()
|
|
val adapter = ImageSearchResultAdapter(searchResults)
|
|
|
|
adapter.setOnItemClickListener(object : ImageSearchResultAdapter.OnItemClickListener {
|
|
override fun onItemClick(searchResult: ImageSearchViewModel.ImageResult) {
|
|
lifecycleScope.launch(Dispatchers.IO) {
|
|
val id = searchResult.anilist?.id?.toInt()
|
|
if (id == null) {
|
|
toast(getString(R.string.no_anilist_id_found))
|
|
return@launch
|
|
}
|
|
val media = Anilist.query.getMedia(id, false)
|
|
|
|
withContext(Dispatchers.Main) {
|
|
media?.let {
|
|
startActivity(
|
|
Intent(this@ImageSearchActivity, MediaDetailsActivity::class.java)
|
|
.putExtra("media", it)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
recyclerView.post {
|
|
recyclerView.adapter = adapter
|
|
recyclerView.layoutManager = LinearLayoutManager(context)
|
|
}
|
|
}
|
|
} |