boorujelly/app/src/main/java/com/example/testes/MainActivity.kt

90 lines
2.6 KiB
Kotlin

package com.example.testes
import android.app.SearchManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
import com.example.testes.databinding.ActivityMainBinding
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
private fun imageListFromJson(json: JSONObject) {
try {
val array: JSONArray = json.getJSONArray("post")
val list = BooruImage.listFromJsonArray(array)
loadGrid(list)
//Log.i("Image", list.toString())
}
catch (e: JSONException) {
e.printStackTrace()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val url = "https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&limit=18"
search(assemblePostsUrl("izumi_konata"))
binding.search.setOnQueryTextListener(object :
android.widget.SearchView.OnQueryTextListener {
override fun onQueryTextChange(newText: String): Boolean {
return false
}
override fun onQueryTextSubmit(query: String): Boolean {
search(assemblePostsUrl(query))
// task HERE
return false
}
}
)
}
private fun search(url: String) {
Request.getRequest(
this,
url,
::imageListFromJson
)
Log.i("ocko", url)
}
private fun assemblePostsUrl(query: String): String{
return "https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&limit=18&tags=$query"
}
private fun loadGrid(images: List<BooruImage>) {
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.imageGridRecV, ImageGridFragment(images))
fragmentTransaction.commit()
}
inner class SearchListener(
var callback: (String) -> Unit
) : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query : String): Boolean {
callback(query)
return true
}
override fun onQueryTextChange(query : String): Boolean {
//callback(query)
return true
}
}
}