Untitled

 avatar
unknown
kotlin
3 years ago
3.6 kB
3
Indexable
// View Model
class UserViewModel : ViewModel() {

    val userList = MutableLiveData<List<User>>()
    private var userLiveData: MutableLiveData<List<User>>? = MutableLiveData()

    fun getUsers(): LiveData<List<User>>? {
        userLiveData = getUsersApiCall()
        return userLiveData
    }
    
    fun getUsersApiCall(): MutableLiveData<List<User>> {
        val call = RetrofitService.apiInterface.getUsers()
        call.enqueue(object : Callback<List<User>> {
            override fun onResponse(
                call: Call<List<User>>,
                response: Response<List<User>>
            ) {
                if (response.isSuccessful) {
                    userList.postValue(response.body())
                }
            }

            override fun onFailure(call: Call<List<User>>, t: Throwable) {
                Toast.makeText(Application.appContext, t.message, Toast.LENGTH_LONG).show()
            }
        })
        return userList
    }
}

// Activity (Untuk fragment juga hampir sama)
class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var userViewModel: UserViewModel
    private val listUserAdapter by lazy { ListUserAdapter(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        showLoading(true)
        setupRecyclerView()

        userViewModel = ViewModelProvider(this).get(UserViewModel::class.java)
        userViewModel.getUsers()?.observe(this, getListUserObserver)
    }

    private val getListUserObserver: Observer<List<User>> =
        Observer { users ->
            showNotFound(false)
            users?.let { listUserAdapter.setData(it) }
            showLoading(false)
        }

    private fun setupRecyclerView() {
        binding.rvUsers.layoutManager = LinearLayoutManager(this)
        binding.rvUsers.adapter = listUserAdapter
        binding.rvUsers.setHasFixedSize(true)

        listUserAdapter.setOnItemClickCallback(object : ListUserAdapter.OnItemClickCallback {
            override fun onItemClicked(data: User) {
                val intent = Intent(this@MainActivity, DetailActivity::class.java)
                intent.putExtra(DetailActivity.EXTRA_USERNAME, data.username)
                startActivity(intent)
            }
        })
    }

    private fun showLoading(state: Boolean) {
        if (state) {
            binding.listUserShimmerContainer.visibility = View.VISIBLE
        } else {
            binding.listUserShimmerContainer.visibility = View.GONE
        }
    }
}

// Model User
@Entity
@Parcelize
data class User(
    @PrimaryKey(autoGenerate = false)
    @SerializedName("id")
    val id: Int,

    @SerializedName("login")
    val username: String,

    @SerializedName("name")
    val name: String?,

    @SerializedName("avatar_url")
    val avatarUrl: String?,

    @SerializedName("company")
    val company: String?,

    @SerializedName("location")
    val location: String?,

    @SerializedName("public_repos")
    val repositoryCount: Int?,

    @SerializedName("followers")
    val followers: Int?,

    @SerializedName("following")
    val following: Int?,

    @SerializedName("html_url")
    val profileUrl: String?,

    @SerializedName("followers_url")
    val followersUrl: String?,

    @SerializedName("following_url")
    val followingUrl: String?,

    @SerializedName("created_at")
    val createdDate: String?,
) : Parcelable
Editor is loading...