Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit c62e12e

Browse files
committed
modified databinding logics
1 parent 3bf656f commit c62e12e

15 files changed

+325
-243
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.skydoves.githubfollows.binding
2+
3+
import androidx.databinding.BindingAdapter
4+
import androidx.recyclerview.widget.RecyclerView
5+
import com.skydoves.githubfollows.extension.bindResource
6+
import com.skydoves.githubfollows.models.Follower
7+
import com.skydoves.githubfollows.models.GithubUser
8+
import com.skydoves.githubfollows.models.History
9+
import com.skydoves.githubfollows.models.Resource
10+
import com.skydoves.githubfollows.view.adapter.GithubUserAdapter
11+
import com.skydoves.githubfollows.view.adapter.HistoryAdapter
12+
import com.skydoves.githubfollows.view.ui.main.MainActivityViewModel
13+
14+
/**
15+
* Developed by skydoves on 2019-06-07.
16+
* Copyright (c) 2018 skydoves rights reserved.
17+
*/
18+
19+
@BindingAdapter("adapterGithubUser")
20+
fun bindAdapterGithubUser(view: RecyclerView, resource: Resource<GithubUser>?) {
21+
if (resource != null) {
22+
view.bindResource(resource) {
23+
val adapter = view.adapter as? GithubUserAdapter
24+
adapter?.updateHeader(resource)
25+
}
26+
}
27+
}
28+
29+
@BindingAdapter("adapterFollowers", "viewModel")
30+
fun bindAdapterFollowers(view: RecyclerView, resource: Resource<List<Follower>>?, viewModel: MainActivityViewModel) {
31+
if (resource != null) {
32+
viewModel.fetchStatus(resource)
33+
view.bindResource(resource) {
34+
val adapter = view.adapter as? GithubUserAdapter
35+
adapter?.addFollowList(resource.data!!)
36+
}
37+
}
38+
}
39+
40+
@BindingAdapter("adapterHistory")
41+
fun bindAdapterHistory(view: RecyclerView, resource: List<History>?) {
42+
if (resource != null) {
43+
val adapter = view.adapter as? HistoryAdapter
44+
adapter?.updateItemList(resource)
45+
}
46+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.skydoves.githubfollows.binding
2+
3+
import android.view.View
4+
import androidx.databinding.BindingConversion
5+
6+
/**
7+
* Developed by skydoves on 2018-02-18.
8+
* Copyright (c) 2018 skydoves rights reserved.
9+
*/
10+
11+
@BindingConversion
12+
fun bindingVisibility(visible: Boolean): Int {
13+
return if (visible) View.VISIBLE else View.GONE
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.skydoves.githubfollows.extension
2+
3+
import androidx.fragment.app.FragmentActivity
4+
import androidx.lifecycle.ViewModel
5+
import androidx.lifecycle.ViewModelProvider
6+
import androidx.lifecycle.ViewModelProviders
7+
import kotlin.reflect.KClass
8+
9+
/**
10+
* Developed by skydoves on 2019-06-07.
11+
* Copyright (c) 2018 skydoves rights reserved.
12+
*/
13+
14+
fun <T : ViewModel> FragmentActivity.vm(factory: ViewModelProvider.Factory, model: KClass<T>): T {
15+
return ViewModelProviders.of(this, factory).get(model.java)
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.skydoves.githubfollows.extension
2+
3+
import androidx.recyclerview.widget.RecyclerView
4+
import com.skydoves.githubfollows.models.Resource
5+
import com.skydoves.githubfollows.models.Status
6+
import org.jetbrains.anko.toast
7+
8+
/**
9+
* Developed by skydoves on 2019-06-07.
10+
* Copyright (c) 2018 skydoves rights reserved.
11+
*/
12+
13+
fun RecyclerView.bindResource(resource: Resource<Any>, onSuccess: () -> Unit) {
14+
when (resource.status) {
15+
Status.LOADING -> Unit
16+
Status.SUCCESS -> onSuccess()
17+
Status.ERROR -> this.context.toast(resource.message.toString())
18+
}
19+
}

app/src/main/java/com/skydoves/githubfollows/utils/BindingUtils.kt

Lines changed: 0 additions & 17 deletions
This file was deleted.

app/src/main/java/com/skydoves/githubfollows/view/ui/main/MainActivity.kt

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import android.content.Intent
44
import android.os.Bundle
55
import android.view.View
66
import androidx.appcompat.app.AppCompatActivity
7-
import androidx.lifecycle.ViewModelProviders
7+
import androidx.databinding.DataBindingUtil
88
import androidx.recyclerview.widget.LinearLayoutManager
99
import com.skydoves.baserecyclerviewadapter.RecyclerViewPaginator
1010
import com.skydoves.githubfollows.R
11-
import com.skydoves.githubfollows.extension.observeLiveData
11+
import com.skydoves.githubfollows.databinding.ActivityMainBinding
12+
import com.skydoves.githubfollows.extension.vm
1213
import com.skydoves.githubfollows.factory.AppViewModelFactory
1314
import com.skydoves.githubfollows.models.Follower
1415
import com.skydoves.githubfollows.models.GithubUser
15-
import com.skydoves.githubfollows.models.Resource
16-
import com.skydoves.githubfollows.models.Status
1716
import com.skydoves.githubfollows.utils.PowerMenuUtils
1817
import com.skydoves.githubfollows.view.adapter.GithubUserAdapter
1918
import com.skydoves.githubfollows.view.ui.detail.DetailActivity
@@ -28,24 +27,23 @@ import kotlinx.android.synthetic.main.activity_main.*
2827
import kotlinx.android.synthetic.main.toolbar_main.*
2928
import org.jetbrains.anko.startActivity
3029
import org.jetbrains.anko.startActivityForResult
31-
import org.jetbrains.anko.toast
3230
import javax.inject.Inject
3331

3432
/**
3533
* Developed by skydoves on 2018-01-19.
3634
* Copyright (c) 2018 skydoves rights reserved.
3735
*/
3836

37+
@Suppress("SpellCheckingInspection")
3938
class MainActivity : AppCompatActivity(),
4039
GithubUserHeaderViewHolder.Delegate,
4140
GithubUserViewHolder.Delegate {
4241

4342
@Inject
4443
lateinit var viewModelFactory: AppViewModelFactory
45-
46-
private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory).get(MainActivityViewModel::class.java) }
44+
private val viewModel by lazy { vm(viewModelFactory, MainActivityViewModel::class) }
45+
private lateinit var binding: ActivityMainBinding
4746
private val adapter by lazy { GithubUserAdapter(this, this) }
48-
4947
private lateinit var paginator: RecyclerViewPaginator
5048
private lateinit var powerMenu: PowerMenu
5149

@@ -61,7 +59,9 @@ class MainActivity : AppCompatActivity(),
6159
override fun onCreate(savedInstanceState: Bundle?) {
6260
AndroidInjection.inject(this)
6361
super.onCreate(savedInstanceState)
64-
setContentView(R.layout.activity_main)
62+
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
63+
binding.viewModel = viewModel
64+
binding.lifecycleOwner = this
6565

6666
main_recyclerView.adapter = adapter
6767
main_recyclerView.layoutManager = LinearLayoutManager(this)
@@ -73,7 +73,6 @@ class MainActivity : AppCompatActivity(),
7373
)
7474

7575
initializeUI()
76-
observeViewModel()
7776
}
7877

7978
private fun initializeUI() {
@@ -83,32 +82,10 @@ class MainActivity : AppCompatActivity(),
8382
toolbar_main_search.setOnClickListener { startActivityForResult<SearchActivity>(SearchActivity.intent_requestCode) }
8483
}
8584

86-
private fun observeViewModel() {
87-
observeLiveData(viewModel.githubUserLiveData) { updateGithubUser(it) }
88-
observeLiveData(viewModel.followersLiveData) { updateFollowerList(it) }
89-
}
90-
9185
private fun loadMore(page: Int) {
9286
viewModel.postPage(page)
9387
}
9488

95-
private fun updateGithubUser(resource: Resource<GithubUser>) {
96-
when (resource.status) {
97-
Status.SUCCESS -> adapter.updateHeader(resource)
98-
Status.ERROR -> toast(resource.message.toString())
99-
Status.LOADING -> Unit
100-
}
101-
}
102-
103-
private fun updateFollowerList(resource: Resource<List<Follower>>) {
104-
viewModel.fetchStatus(resource)
105-
when (resource.status) {
106-
Status.SUCCESS -> adapter.addFollowList(resource.data!!)
107-
Status.ERROR -> toast(resource.message.toString())
108-
Status.LOADING -> Unit
109-
}
110-
}
111-
11289
private fun restPagination(user: String) {
11390
adapter.clearAll()
11491
paginator.resetCurrentPage()

app/src/main/java/com/skydoves/githubfollows/view/ui/main/MainActivityViewModel.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ constructor(
3838

3939
login.postValue(getUserName())
4040
githubUserLiveData = login.switchMap {
41-
login.value?.let { user -> githubUserRepository.loadUser(user) }
41+
login.value?.let { user ->
42+
githubUserRepository.loadUser(user)
43+
}
4244
?: AbsentLiveData.create()
4345
}
4446

app/src/main/java/com/skydoves/githubfollows/view/ui/search/SearchActivity.kt

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import android.view.ViewTreeObserver
77
import android.view.inputmethod.EditorInfo
88
import android.widget.TextView
99
import androidx.appcompat.app.AppCompatActivity
10-
import androidx.lifecycle.ViewModelProviders
10+
import androidx.databinding.DataBindingUtil
1111
import androidx.recyclerview.widget.LinearLayoutManager
1212
import com.skydoves.githubfollows.R
13+
import com.skydoves.githubfollows.databinding.ActivitySearchBinding
1314
import com.skydoves.githubfollows.extension.checkIsMaterialVersion
1415
import com.skydoves.githubfollows.extension.circularRevealed
1516
import com.skydoves.githubfollows.extension.circularUnRevealed
1617
import com.skydoves.githubfollows.extension.inVisible
1718
import com.skydoves.githubfollows.extension.observeLiveData
19+
import com.skydoves.githubfollows.extension.vm
1820
import com.skydoves.githubfollows.factory.AppViewModelFactory
1921
import com.skydoves.githubfollows.models.GithubUser
2022
import com.skydoves.githubfollows.models.History
@@ -39,21 +41,19 @@ class SearchActivity : AppCompatActivity(),
3941

4042
@Inject
4143
lateinit var viewModelFactory: AppViewModelFactory
42-
43-
private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory).get(SearchActivityViewModel::class.java) }
44-
private val adapter by lazy { HistoryAdapter(this) }
44+
private val viewModel by lazy { vm(viewModelFactory, SearchActivityViewModel::class) }
45+
private lateinit var binding: ActivitySearchBinding
4546

4647
override fun onCreate(savedInstanceState: Bundle?) {
4748
AndroidInjection.inject(this)
4849
super.onCreate(savedInstanceState)
49-
setContentView(R.layout.activity_search)
50-
startCircularRevealed(savedInstanceState)
51-
52-
observeViewModel()
53-
initializeAdapter()
50+
binding = DataBindingUtil.setContentView(this, R.layout.activity_search)
51+
binding.viewModel = viewModel
52+
binding.lifecycleOwner = this
5453

55-
toolbar_search_home.setOnClickListener { onBackPressed() }
56-
toolbar_search_input.setOnEditorActionListener(this)
54+
initializeUI()
55+
startCircularRevealed(savedInstanceState)
56+
observeLiveData(viewModel.githubUserLiveData) { onChangeUser(it) }
5757
}
5858

5959
private fun startCircularRevealed(savedInstanceState: Bundle?) {
@@ -72,15 +72,11 @@ class SearchActivity : AppCompatActivity(),
7272
}
7373
}
7474

75-
private fun observeViewModel() {
76-
observeLiveData(viewModel.selectHistories()) { adapter.updateItemList(it) }
77-
observeLiveData(viewModel.githubUserLiveData) { onChangeUser(it) }
78-
}
79-
80-
private fun initializeAdapter() {
75+
private fun initializeUI() {
8176
search_recyclerView.layoutManager = LinearLayoutManager(this)
82-
search_recyclerView.adapter = adapter
83-
viewModel.selectHistories()
77+
search_recyclerView.adapter = HistoryAdapter(this)
78+
toolbar_search_home.setOnClickListener { onBackPressed() }
79+
toolbar_search_input.setOnEditorActionListener(this)
8480
}
8581

8682
override fun onEditorAction(p0: TextView?, actionId: Int, event: KeyEvent?): Boolean {

app/src/main/java/com/skydoves/githubfollows/view/ui/search/SearchActivityViewModel.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ constructor(
2626

2727
val login: MutableLiveData<String> = MutableLiveData()
2828
val githubUserLiveData: LiveData<Resource<GithubUser>>
29+
val historyLiveData: LiveData<List<History>>
2930

3031
init {
3132
Timber.d("Injection SearchActivityViewModel")
3233

34+
historyLiveData = historyRepository.selectHistories()
35+
3336
githubUserLiveData = login.switchMap { user ->
3437
login.value?.let { githubUserRepository.loadUser(user) }
3538
?: AbsentLiveData.create()
@@ -38,8 +41,6 @@ constructor(
3841

3942
fun insertHistory(search: String) = historyRepository.insertHistory(search)
4043

41-
fun selectHistories(): LiveData<List<History>> = historyRepository.selectHistories()
42-
4344
fun deleteHistory(history: History) = historyRepository.deleteHistory(history)
4445

4546
fun getPreferenceUserKeyName(): String = githubUserRepository.getUserKeyName()
Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<layout xmlns:android="http://schemas.android.com/apk/res/android">
33

4-
<data></data>
4+
<data>
55

6-
<LinearLayout
7-
android:layout_width="match_parent"
8-
android:layout_height="match_parent"
9-
android:background="@color/background"
10-
android:orientation="vertical">
6+
<variable
7+
name="viewModel"
8+
type="com.skydoves.githubfollows.view.ui.detail.DetailActivityViewModel" />
9+
</data>
1110

12-
<com.google.android.material.appbar.AppBarLayout
13-
android:id="@+id/appBarLayout"
14-
android:layout_width="match_parent"
15-
android:layout_height="wrap_content">
11+
<LinearLayout
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent"
14+
android:background="@color/background"
15+
android:orientation="vertical">
1616

17-
<include
18-
android:id="@+id/detail_toolbar"
19-
layout="@layout/toolbar_default" />
20-
</com.google.android.material.appbar.AppBarLayout>
17+
<com.google.android.material.appbar.AppBarLayout
18+
android:id="@+id/appBarLayout"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content">
2121

22-
<include
23-
android:id="@+id/detail_header"
24-
layout="@layout/layout_detail_header" />
22+
<include
23+
android:id="@+id/detail_toolbar"
24+
layout="@layout/toolbar_default" />
25+
</com.google.android.material.appbar.AppBarLayout>
2526

26-
<include layout="@layout/layout_detail_body" />
27+
<include
28+
android:id="@+id/detail_header"
29+
layout="@layout/layout_detail_header" />
2730

28-
</LinearLayout>
31+
<include layout="@layout/layout_detail_body" />
32+
33+
</LinearLayout>
2934
</layout>

0 commit comments

Comments
 (0)