2020-04-09 22:36:59 +02:00
|
|
|
package com.h.pixeldroid
|
|
|
|
|
2020-05-01 11:26:18 +02:00
|
|
|
import android.content.Intent
|
|
|
|
import android.graphics.Typeface
|
|
|
|
import android.net.Uri
|
2020-04-09 22:36:59 +02:00
|
|
|
import android.os.Bundle
|
2020-05-01 11:26:18 +02:00
|
|
|
import android.util.Log
|
|
|
|
import android.view.View
|
|
|
|
import android.widget.Button
|
|
|
|
import android.widget.ImageView
|
|
|
|
import android.widget.TextView
|
|
|
|
import android.widget.Toast
|
2020-04-09 22:36:59 +02:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2020-05-01 11:26:18 +02:00
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2020-05-08 10:53:56 +02:00
|
|
|
import com.h.pixeldroid.adapters.ProfilePostsRecyclerViewAdapter
|
2020-05-19 09:49:34 +02:00
|
|
|
import com.h.pixeldroid.api.PixelfedAPI
|
2020-04-09 22:36:59 +02:00
|
|
|
import com.h.pixeldroid.objects.Account
|
|
|
|
import com.h.pixeldroid.objects.Account.Companion.ACCOUNT_TAG
|
2020-05-01 11:26:18 +02:00
|
|
|
import com.h.pixeldroid.objects.Relationship
|
|
|
|
import com.h.pixeldroid.objects.Status
|
2020-05-19 09:49:34 +02:00
|
|
|
import com.h.pixeldroid.utils.DBUtils
|
2020-05-01 11:26:18 +02:00
|
|
|
import com.h.pixeldroid.utils.ImageConverter
|
|
|
|
import retrofit2.Call
|
|
|
|
import retrofit2.Callback
|
|
|
|
import retrofit2.Response
|
2020-04-09 22:36:59 +02:00
|
|
|
|
|
|
|
class ProfileActivity : AppCompatActivity() {
|
2020-05-01 11:26:18 +02:00
|
|
|
private lateinit var pixelfedAPI : PixelfedAPI
|
|
|
|
private lateinit var adapter : ProfilePostsRecyclerViewAdapter
|
|
|
|
private lateinit var recycler : RecyclerView
|
2020-05-19 09:49:34 +02:00
|
|
|
private lateinit var accessToken : String
|
|
|
|
private lateinit var domain : String
|
2020-05-01 11:26:18 +02:00
|
|
|
private var account: Account? = null
|
2020-04-09 22:36:59 +02:00
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.activity_profile)
|
|
|
|
|
2020-05-19 09:49:34 +02:00
|
|
|
val db = DBUtils.initDB(applicationContext)
|
|
|
|
|
|
|
|
val user = db.userDao().getActiveUser()
|
|
|
|
|
|
|
|
domain = user?.instance_uri.orEmpty()
|
|
|
|
pixelfedAPI = PixelfedAPI.create(domain)
|
|
|
|
accessToken = user?.accessToken.orEmpty()
|
|
|
|
db.close()
|
2020-05-01 11:26:18 +02:00
|
|
|
|
|
|
|
// Set posts RecyclerView as a grid with 3 columns
|
|
|
|
recycler = findViewById(R.id.profilePostsRecyclerView)
|
|
|
|
recycler.layoutManager = GridLayoutManager(applicationContext, 3)
|
2020-05-03 14:30:01 +02:00
|
|
|
adapter = ProfilePostsRecyclerViewAdapter()
|
2020-05-01 11:26:18 +02:00
|
|
|
recycler.adapter = adapter
|
|
|
|
|
|
|
|
setContent()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setContent() {
|
2020-04-09 22:36:59 +02:00
|
|
|
// Set profile according to given account
|
2020-05-01 11:26:18 +02:00
|
|
|
account = intent.getSerializableExtra(ACCOUNT_TAG) as Account?
|
|
|
|
|
|
|
|
account?.let {
|
|
|
|
setViews()
|
|
|
|
activateFollow()
|
|
|
|
setPosts()
|
|
|
|
} ?: run {
|
|
|
|
pixelfedAPI.verifyCredentials("Bearer $accessToken")
|
|
|
|
.enqueue(object : Callback<Account> {
|
|
|
|
override fun onResponse(call: Call<Account>, response: Response<Account>) {
|
|
|
|
if (response.code() == 200) {
|
|
|
|
account = response.body()!!
|
|
|
|
|
|
|
|
setViews()
|
|
|
|
// Populate profile page with user's posts
|
|
|
|
setPosts()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onFailure(call: Call<Account>, t: Throwable) {
|
|
|
|
Log.e("ProfileActivity:", t.toString())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Edit button redirects to Pixelfed's "edit account" page
|
|
|
|
val editButton = findViewById<Button>(R.id.editButton)
|
|
|
|
editButton.visibility = View.VISIBLE
|
|
|
|
editButton.setOnClickListener{ onClickEditButton() }
|
|
|
|
}
|
|
|
|
|
|
|
|
// On click open followers list
|
|
|
|
findViewById<TextView>(R.id.nbFollowersTextView).setOnClickListener{ onClickFollowers() }
|
|
|
|
// On click open followers list
|
|
|
|
findViewById<TextView>(R.id.nbFollowingTextView).setOnClickListener{ onClickFollowing() }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate myProfile page with user's data
|
|
|
|
*/
|
|
|
|
private fun setViews() {
|
|
|
|
val profilePicture = findViewById<ImageView>(R.id.profilePictureImageView)
|
|
|
|
ImageConverter.setRoundImageFromURL(View(applicationContext), account!!.avatar, profilePicture)
|
|
|
|
|
|
|
|
val description = findViewById<TextView>(R.id.descriptionTextView)
|
|
|
|
description.text = account!!.note
|
|
|
|
|
|
|
|
val accountName = findViewById<TextView>(R.id.accountNameTextView)
|
|
|
|
accountName.text = account!!.display_name
|
|
|
|
accountName.setTypeface(null, Typeface.BOLD)
|
|
|
|
|
|
|
|
val nbPosts = findViewById<TextView>(R.id.nbPostsTextView)
|
|
|
|
nbPosts.text = "${account!!.statuses_count}\nPosts"
|
|
|
|
nbPosts.setTypeface(null, Typeface.BOLD)
|
|
|
|
|
|
|
|
val nbFollowers = findViewById<TextView>(R.id.nbFollowersTextView)
|
|
|
|
nbFollowers.text = "${account!!.followers_count}\nFollowers"
|
|
|
|
nbFollowers.setTypeface(null, Typeface.BOLD)
|
|
|
|
|
|
|
|
val nbFollowing = findViewById<TextView>(R.id.nbFollowingTextView)
|
|
|
|
nbFollowing.text = "${account!!.following_count}\nFollowing"
|
|
|
|
nbFollowing.setTypeface(null, Typeface.BOLD)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate profile page with user's posts
|
|
|
|
*/
|
|
|
|
private fun setPosts() {
|
|
|
|
pixelfedAPI.accountPosts("Bearer $accessToken", account_id = account!!.id)
|
|
|
|
.enqueue(object : Callback<List<Status>> {
|
|
|
|
|
|
|
|
override fun onFailure(call: Call<List<Status>>, t: Throwable) {
|
|
|
|
Log.e("ProfileActivity.Posts:", t.toString())
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResponse(call: Call<List<Status>>, response: Response<List<Status>>) {
|
|
|
|
if(response.code() == 200) {
|
|
|
|
val statuses = response.body()!!
|
2020-05-03 14:30:01 +02:00
|
|
|
adapter.addPosts(statuses)
|
2020-05-01 11:26:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onClickEditButton() {
|
2020-05-19 09:49:34 +02:00
|
|
|
val url = "$domain/settings/home"
|
2020-05-01 11:26:18 +02:00
|
|
|
|
|
|
|
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
|
|
|
|
if(browserIntent.resolveActivity(packageManager) != null) {
|
|
|
|
startActivity(browserIntent)
|
|
|
|
} else {
|
|
|
|
val text = "Cannot open this link"
|
|
|
|
Log.e("ProfileActivity", text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onClickFollowers() {
|
|
|
|
val intent = Intent(this, FollowsActivity::class.java)
|
|
|
|
intent.putExtra(Account.FOLLOWING_TAG, true)
|
|
|
|
intent.putExtra(Account.ACCOUNT_ID_TAG, account?.id)
|
|
|
|
|
|
|
|
ContextCompat.startActivity(this, intent, null)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onClickFollowing() {
|
|
|
|
val intent = Intent(this, FollowsActivity::class.java)
|
|
|
|
intent.putExtra(Account.FOLLOWING_TAG, false)
|
|
|
|
intent.putExtra(Account.ACCOUNT_ID_TAG, account?.id)
|
|
|
|
|
|
|
|
ContextCompat.startActivity(this, intent, null)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up follow button
|
|
|
|
*/
|
|
|
|
private fun activateFollow() {
|
|
|
|
// Get relationship between the two users (credential and this) and set followButton accordingly
|
|
|
|
pixelfedAPI.checkRelationships("Bearer $accessToken", listOf(account!!.id))
|
|
|
|
.enqueue(object : Callback<List<Relationship>> {
|
|
|
|
|
|
|
|
override fun onFailure(call: Call<List<Relationship>>, t: Throwable) {
|
|
|
|
Log.e("FOLLOW ERROR", t.toString())
|
|
|
|
Toast.makeText(applicationContext,"Could not get follow status", Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResponse(call: Call<List<Relationship>>, response: Response<List<Relationship>>) {
|
|
|
|
if(response.code() == 200) {
|
|
|
|
if(response.body()!!.isNotEmpty()) {
|
|
|
|
val followButton = findViewById<Button>(R.id.followButton)
|
|
|
|
|
|
|
|
if (response.body()!![0].following) {
|
|
|
|
followButton.text = "Unfollow"
|
|
|
|
setOnClickUnfollow()
|
|
|
|
} else {
|
|
|
|
followButton.text = "Follow"
|
|
|
|
setOnClickFollow()
|
|
|
|
}
|
|
|
|
followButton.visibility = View.VISIBLE
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Toast.makeText(applicationContext, "Could not display follow button", Toast.LENGTH_SHORT)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setOnClickFollow() {
|
|
|
|
val followButton = findViewById<Button>(R.id.followButton)
|
|
|
|
|
|
|
|
followButton.setOnClickListener {
|
|
|
|
pixelfedAPI.follow(account!!.id, "Bearer $accessToken")
|
|
|
|
.enqueue(object : Callback<Relationship> {
|
|
|
|
|
|
|
|
override fun onFailure(call: Call<Relationship>, t: Throwable) {
|
|
|
|
Log.e("FOLLOW ERROR", t.toString())
|
|
|
|
Toast.makeText(applicationContext, "Could not follow", Toast.LENGTH_SHORT)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResponse(
|
|
|
|
call: Call<Relationship>,
|
|
|
|
response: Response<Relationship>
|
|
|
|
) {
|
|
|
|
if (response.code() == 200) {
|
|
|
|
followButton.text = "Unfollow"
|
|
|
|
setOnClickUnfollow()
|
|
|
|
} else if (response.code() == 403) {
|
|
|
|
Toast.makeText(applicationContext, "This action is not allowed", Toast.LENGTH_SHORT)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setOnClickUnfollow() {
|
|
|
|
val followButton = findViewById<Button>(R.id.followButton)
|
|
|
|
|
|
|
|
followButton.setOnClickListener {
|
|
|
|
pixelfedAPI.unfollow(account!!.id, "Bearer $accessToken")
|
|
|
|
.enqueue(object : Callback<Relationship> {
|
2020-04-09 22:36:59 +02:00
|
|
|
|
2020-05-01 11:26:18 +02:00
|
|
|
override fun onFailure(call: Call<Relationship>, t: Throwable) {
|
|
|
|
Log.e("UNFOLLOW ERROR", t.toString())
|
|
|
|
Toast.makeText(applicationContext, "Could not unfollow", Toast.LENGTH_SHORT)
|
|
|
|
.show()
|
|
|
|
}
|
2020-04-09 22:36:59 +02:00
|
|
|
|
2020-05-01 11:26:18 +02:00
|
|
|
override fun onResponse(call: Call<Relationship>, response: Response<Relationship>) {
|
|
|
|
if (response.code() == 200) {
|
|
|
|
followButton.text = "Follow"
|
|
|
|
setOnClickFollow()
|
|
|
|
} else if (response.code() == 401) {
|
|
|
|
Toast.makeText(applicationContext, "The access token is invalid", Toast.LENGTH_SHORT)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-04-09 22:36:59 +02:00
|
|
|
}
|
2020-04-10 12:55:02 +02:00
|
|
|
}
|