Created
April 19, 2021 11:59
-
-
Save serhiitereshchenko/fa9c67d6cab62e42d7dace164e244e85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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