package com.zjgsu.jianshu.Activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import cn.bmob.v3.Bmob
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import com.zjgsu.jianshu.*
import com.zjgsu.jianshu.Bmob.Perception_bmob
import kotlinx.android.synthetic.main.activity_my_post.*

class MyPostActivity : AppCompatActivity() {
    private val PerceptionList = ArrayList<Perception>()
    lateinit var userId:String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my_post)
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
        userId = getSharedPreferences("userinf", Context.MODE_PRIVATE).getString("user_id", "").toString()
        inits()

        val layoutManager=LinearLayoutManager(this)
        myPost_recyclerView.layoutManager = layoutManager
        val adapter =PerceptionAdapter(PerceptionList)

        myPost_recyclerView.adapter = adapter

        Log.d("myLog",myPost_recyclerView.adapter.toString())
        mypost_back.setOnClickListener {
            val intent = Intent(this, MyActivity::class.java)
            startActivity(intent)
            finish()
        }
    }
    private fun inits() {
        val bmobQuery = BmobQuery<Perception_bmob>()
        bmobQuery.findObjects(object : FindListener<Perception_bmob>() {
            override fun done(list: List<Perception_bmob>, e: BmobException?) {
                if (e == null) {
                    for (i in list){
                        if(i.userid== userId) {
                            PerceptionList.add(Perception(i.objectId))
                            val adapter = PerceptionAdapter(PerceptionList)
                            myPost_recyclerView.adapter = adapter
                        }
                    }
                }
                else {
                    Log.d("error","error")}
            }
        })
    }
}