223 lines
6.7 KiB
Kotlin
223 lines
6.7 KiB
Kotlin
package xyz.mtfos.buywhat
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.content.Context
|
|
import android.os.Bundle
|
|
import android.os.Handler
|
|
import android.os.Message
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import android.widget.*
|
|
import org.json.JSONArray
|
|
import org.json.JSONObject
|
|
import xyz.mtfos.tools.apiObject
|
|
import xyz.mtfos.tools.objectTool.bind
|
|
import java.util.*
|
|
import kotlin.collections.ArrayList
|
|
|
|
/**
|
|
* Created by jay on 2017/7/6.
|
|
*/
|
|
class ItemRandom : SampleActivity() {
|
|
private val tvTitle: TextView by bind(R.id.tvTitle)
|
|
private val ivRefresh: ImageView by bind(R.id.ivRefresh)
|
|
private val lvCommodity: ListView by bind(R.id.lvCommodity)
|
|
private var store_id: Int? = null
|
|
private var itemList: ArrayList<JSONObject> = ArrayList()
|
|
private var adapter: lvAdapter? = null
|
|
private val rangeMin: EditText by bind(R.id.rangeMin)
|
|
private val rangeMax: EditText by bind(R.id.rangeMax)
|
|
private val btnRandom: Button by bind(R.id.btnRandom)
|
|
private val tvResult: TextView by bind(R.id.tvResult)
|
|
private val ivBack: ImageView by bind(R.id.ivBack)
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setViewSource(R.layout.activity_item_random)
|
|
|
|
tvTitle?.text = intent?.getStringExtra("name")
|
|
store_id = intent?.getIntExtra("id", 0)
|
|
|
|
adapter = lvAdapter(this@ItemRandom, 0)
|
|
|
|
lvCommodity.adapter = adapter
|
|
|
|
lvCommodity.setOnItemClickListener({parent, view, position, id ->
|
|
val data: JSONObject = itemList.get(position) as JSONObject
|
|
var sk:Boolean = data!!["skip"] as Boolean
|
|
data?.put("skip", !sk)
|
|
itemList.set(position, data)
|
|
adapter?.notifyDataSetChanged()
|
|
|
|
randomItem()
|
|
})
|
|
|
|
btnRandom.setOnClickListener{v->
|
|
val json : JSONObject? = randomItem()
|
|
val str : String = if(json != null) {
|
|
"Result : ${json!!["name"]} $${json!!["price"]}"
|
|
}else{
|
|
"Result : 目標區間無商品"
|
|
}
|
|
tvResult.text = str
|
|
}
|
|
|
|
ivRefresh.setOnClickListener({ v ->
|
|
getList()
|
|
})
|
|
|
|
ivBack.setOnClickListener({ v ->
|
|
finish()
|
|
})
|
|
|
|
getList()
|
|
}
|
|
|
|
fun getList() {
|
|
if(store_id == 0 || store_id == null) return
|
|
resetView()
|
|
toggleLoading(true)
|
|
api.getItems(store_id!!, object: apiObject.apicb{
|
|
override fun Callback(json: JSONObject?) {
|
|
uiHandler.post { toggleLoading(false) }
|
|
if(json!!["status"] == 1) {
|
|
val jarr :JSONArray = json!!["record"] as JSONArray
|
|
itemList.clear()
|
|
if(jarr.length() > 0) {
|
|
uiHandler.sendEmptyMessage(3)
|
|
for(i in 0..(jarr.length() - 1)){
|
|
val tmp: JSONObject =jarr!![i] as JSONObject
|
|
tmp.put("skip", false)
|
|
itemList.add(tmp)
|
|
}
|
|
}else{
|
|
uiHandler.sendEmptyMessage(2)
|
|
}
|
|
uiHandler.sendEmptyMessage(1)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
|
|
fun resetView () {
|
|
rangeMin.setText("0")
|
|
rangeMax.setText("0")
|
|
tvResult.text = ""
|
|
}
|
|
|
|
fun randomItem (): JSONObject? {
|
|
val min: String = rangeMin.text.toString()
|
|
val max: String = rangeMax.text.toString()
|
|
|
|
var imin = if(min.toIntOrNull() == null) 0 else min.toInt()
|
|
var imax = if(max.toIntOrNull() == null) 0 else max.toInt()
|
|
|
|
val tmp: ArrayList<JSONObject> = ArrayList()
|
|
|
|
for(i in 0..(itemList.size - 1)){
|
|
val t:JSONObject = itemList[i]
|
|
var flag:Boolean = false
|
|
|
|
val price:Int = t!!["price"] as Int
|
|
|
|
if(t!!["skip"] == false){
|
|
if(imin == 0 && imax == 0){
|
|
flag = true
|
|
}else if(imin > 0 && imax == 0){
|
|
if(price >= imin){
|
|
flag = true
|
|
}
|
|
}else if(imin == 0 && imax > 0) {
|
|
if(price <= imax) {
|
|
flag = true
|
|
}
|
|
}else{
|
|
if(price in imin..imax) {
|
|
flag = true
|
|
}
|
|
}
|
|
}
|
|
|
|
if(flag){
|
|
tmp.add(t)
|
|
}
|
|
}
|
|
|
|
var arr: Array<JSONObject> = tmp.toTypedArray()
|
|
|
|
if(arr.isNotEmpty()) {
|
|
arr = shuffleArray(arr) as Array<JSONObject>
|
|
}
|
|
|
|
return if(arr.isNotEmpty()) {
|
|
arr[0]
|
|
} else {
|
|
null
|
|
}
|
|
}
|
|
|
|
fun shuffleArray(arr: Array<JSONObject>) : Array<JSONObject>{
|
|
var tmp = arr
|
|
|
|
var rand: Random = Random()
|
|
|
|
var limit: Int = 0
|
|
while (limit < 2) {
|
|
for (i in 0..(tmp.size - 1)) {
|
|
val pos: Int = rand.nextInt(tmp.size)
|
|
val a = tmp[pos]
|
|
tmp[pos] = tmp[i]
|
|
tmp[i] = a
|
|
}
|
|
limit ++
|
|
}
|
|
|
|
return tmp
|
|
}
|
|
|
|
@SuppressLint("HandlerLeak")
|
|
private val uiHandler = object : Handler() {
|
|
override fun handleMessage(msg: Message?) {
|
|
super.handleMessage(msg)
|
|
when (msg!!.what) {
|
|
1 -> {
|
|
// update listview data
|
|
adapter!!.clear()
|
|
adapter!!.addAll(itemList)
|
|
}
|
|
2 -> {
|
|
btnRandom.isClickable = false
|
|
}
|
|
3 -> {
|
|
btnRandom.isClickable = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class lvAdapter(context: Context, resource: Int) : ArrayAdapter<JSONObject>(context, resource) {
|
|
|
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
|
|
val data = getItem(position) as JSONObject
|
|
var v: View = if (convertView == null) {
|
|
View.inflate(context, R.layout.listview_commodity_item, null)
|
|
} else {
|
|
convertView
|
|
}
|
|
|
|
val tvSkip: TextView = v.findViewById<TextView>(R.id.tvSkip)
|
|
tvSkip.text = if(data!!["skip"] == false){
|
|
context.getString(R.string.commodity_no_skip)
|
|
}else{
|
|
context.getString(R.string.commodity_skip)
|
|
}
|
|
|
|
val tv: TextView = v.findViewById(R.id.tvItem)
|
|
val name:String = data!!["name"] as String
|
|
val price:Int = data!!["price"] as Int
|
|
tv.text = "${name} $${price}"
|
|
return v
|
|
}
|
|
}
|
|
} |