-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75d0f56
commit 277f140
Showing
9 changed files
with
337 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.aurelioklv.githubuser.ui | ||
|
||
fun formatCount(count: Long): String { | ||
return when { | ||
count < 1000L -> count.toString() | ||
count < 100000L -> String.format("%.1fk", count / 1000.0) | ||
count < 1000000L -> String.format("%dk", count / 1000L) | ||
else -> String.format("%.1fM", count / 1000000L) | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
app/src/main/java/com/aurelioklv/githubuser/ui/details/DetailsActivity.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.aurelioklv.githubuser.ui.details | ||
|
||
import android.os.Bundle | ||
import android.util.Log | ||
import android.view.View | ||
import androidx.activity.enableEdgeToEdge | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.constraintlayout.widget.ConstraintLayout | ||
import androidx.core.view.ViewCompat | ||
import androidx.core.view.WindowInsetsCompat | ||
import com.aurelioklv.githubuser.R | ||
import com.aurelioklv.githubuser.data.response.UserResponse | ||
import com.aurelioklv.githubuser.databinding.ActivityDetailsBinding | ||
import com.aurelioklv.githubuser.ui.formatCount | ||
import com.bumptech.glide.Glide | ||
|
||
class DetailsActivity : AppCompatActivity() { | ||
private lateinit var binding: ActivityDetailsBinding | ||
private val viewModel: DetailsViewModel by viewModels() | ||
|
||
private var username: String? = null | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
enableEdgeToEdge() | ||
|
||
binding = ActivityDetailsBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> | ||
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) | ||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) | ||
insets | ||
} | ||
|
||
if (intent != null && intent.hasExtra(EXTRA_USERNAME)) { | ||
username = intent.getStringExtra(EXTRA_USERNAME) | ||
viewModel.getUserDetails(username!!) | ||
} | ||
|
||
viewModel.user.observe(this) { | ||
setUserDetails(it) | ||
} | ||
|
||
viewModel.isLoading.observe(this) { | ||
showLoading(it) | ||
} | ||
} | ||
|
||
private fun setUserDetails(userResponse: UserResponse) { | ||
with(userResponse) { | ||
Glide.with(this@DetailsActivity) | ||
.load(avatarUrl) | ||
.into(binding.ivUserAvatar) | ||
Log.i(TAG, "name: $name\nusername: $login") | ||
if (name.isNullOrEmpty() || name.isBlank()) { | ||
binding.tvName.visibility = View.INVISIBLE | ||
|
||
val usernameLayoutParams = | ||
binding.tvUsername.layoutParams as ConstraintLayout.LayoutParams | ||
usernameLayoutParams.topToTop = binding.ivUserAvatar.id | ||
usernameLayoutParams.bottomToBottom = binding.ivUserAvatar.id | ||
} else { | ||
binding.tvName.text = name | ||
} | ||
binding.tvUsername.text = userResponse.login | ||
|
||
if (bio.isNullOrEmpty() || bio.isBlank()) { | ||
binding.tvBio.visibility = View.INVISIBLE | ||
|
||
val followersLayoutParams = | ||
binding.tvFollowers.layoutParams as ConstraintLayout.LayoutParams | ||
followersLayoutParams.topToBottom = binding.ivUserAvatar.id | ||
} else { | ||
binding.tvBio.text = bio | ||
} | ||
|
||
Log.i(TAG, "followers: $followers\nfollowing: $following") | ||
binding.tvFollowers.text = | ||
resources.getQuantityString( | ||
R.plurals.followers, | ||
followers, | ||
formatCount(followers.toLong()) | ||
) | ||
binding.tvFollowing.text = getString(R.string.following, following) | ||
} | ||
} | ||
|
||
private fun showLoading(isLoading: Boolean) { | ||
binding.progressBar.visibility = if (isLoading) View.VISIBLE else View.GONE | ||
} | ||
|
||
companion object { | ||
const val TAG = "DetailsActivity" | ||
const val EXTRA_USERNAME = "username" | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/aurelioklv/githubuser/ui/details/DetailsViewModel.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.aurelioklv.githubuser.ui.details | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import com.aurelioklv.githubuser.data.api.ApiConfig | ||
import com.aurelioklv.githubuser.data.response.UserResponse | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
|
||
class DetailsViewModel : ViewModel() { | ||
private val _user = MutableLiveData<UserResponse>() | ||
val user: LiveData<UserResponse> = _user | ||
|
||
private val _isLoading = MutableLiveData<Boolean>() | ||
val isLoading: LiveData<Boolean> = _isLoading | ||
|
||
fun getUserDetails(username: String) { | ||
_isLoading.value = true | ||
val client = ApiConfig.getApiService().getUserDetails(username) | ||
client.enqueue(object : Callback<UserResponse> { | ||
override fun onResponse(call: Call<UserResponse>, response: Response<UserResponse>) { | ||
_isLoading.value = false | ||
if (response.isSuccessful) { | ||
val responseBody = response.body() | ||
if (responseBody != null) { | ||
_user.value = responseBody | ||
} | ||
} else { | ||
Log.e(TAG, "onResponse !isSuccessFul: $response") | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<UserResponse>, t: Throwable) { | ||
Log.e(TAG, "onFailure: ${t.message}") | ||
} | ||
}) | ||
} | ||
|
||
companion object { | ||
const val TAG = "DetailsViewModel" | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.