Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save serhiitereshchenko/3ebb63a2170f7ada1b20978170c29258 to your computer and use it in GitHub Desktop.
Save serhiitereshchenko/3ebb63a2170f7ada1b20978170c29258 to your computer and use it in GitHub Desktop.
public class MainActivity : Activity() {
private lateinit var userAvatarImageView: ImageView
private lateinit var firstNameTextView: TextView
private lateinit var lastNameTextView: TextView
private var apiInterface: APIInterface? = null
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
sInstance = this
setContentView(R.layout.activity_main)
initViews()
apiInterface = APIClient.getClient().create(APIInterface::class.java)
loadUserProfile()
}
private fun initViews() {
firstNameTextView = findViewById(R.id.user_name)
lastNameTextView = findViewById(R.id.user_name)
userAvatarImageView = findViewById(R.id.user_avatar)
}
private fun loadUserProfile() {
val userAuthToken = DnCustomerPreferences.getInstance(this).userAuthToken
val call = apiInterface.doGetUserProfile(userAuthToken)
call.enqueue(object : Callback<UserResponse> {
override fun onResponse(call: Call<UserResponse>, response: Response<UserResponse>) {
firstNameTextView.text = parseUserName(response.body()?.name)[0]
lastNameTextView.text = parseUserName(response.body()?.name)[1]
}
override fun onFailure(call: Call<UserResponse>, t: Throwable) {
}
})
userAvatarImageView.setImageBitmap(BitmapFactory.decodeStream(assets.open("user_avatar.png")))
}
private fun parseUserName(fullName: String): List<String> {
if (fullName.contains(".")) {
return fullName.split(".")
} else {
if (fullName.contains(",")) {
return fullName.split(",")
} else {
if (fullName.contains(" ")) {
return fullName.split(" ")
} else {
return mutableListOf(fullName)
}
}
}
}
public fun showToast(message: String) = Toast.makeText(this, message, Toast.LENGTH_SHORT)
companion object {
var sInstance: MainActivity? = null
}
}
data class UserResponse(val name: String?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment