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
import com.google.android.material.tabs.TabLayout | |
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener | |
import com.google.android.material.tabs.TabLayout.Tab | |
inline fun TabLayout.doOnTabReselected( | |
crossinline action: (tab: Tab?) -> Unit | |
) = addOnTabSelectedListener(onTabReselected = action) | |
inline fun TabLayout.doOnTabUnselected( | |
crossinline action: (tab: Tab?) -> Unit |
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
import android.os.Bundle | |
import android.util.Log | |
import android.util.Patterns | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.databinding.DataBindingUtil | |
import androidx.lifecycle.MediatorLiveData | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.ViewModelProviders | |
import works.wever.mvvm.R |
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
class MainViewModel : ViewModel() { | |
val api = NetworkConfiguration.getPlaceholderApi() | |
private val mutablePostData = MutableLiveData<List<Post>>() | |
val postData: LiveData<List<Post>> | |
get() = mutablePostData | |
fun fetchPosts() { | |
api.getPosts().enqueue(object : Callback<List<Post>> { |
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
@JsonClass(generateAdapter = true) | |
data class Post( | |
@Json(name = "userId") val userId: Int, | |
@Json(name = "id") val id: Int, | |
@Json(name = "title") val title: String, | |
@Json(name = "body") val body: String | |
) |
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
interface PlaceholderApi { | |
@GET("/posts") | |
fun getPosts(): Call<Response<List<Post>>> | |
} |
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
object NetworkConfiguration { | |
const val BASE_URL = "https://jsonplaceholder.typicode.com/" | |
val retrofit = Retrofit.Builder() | |
.addConverterFactory(MoshiConverterFactory.create()) | |
.client(OkHttpClient()) | |
.baseUrl(BASE_URL) | |
.build() |
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
class MainViewModelTest { | |
@get:Rule | |
val instantTaskRule = InstantTaskExecutorRule() | |
@RelaxedMockK | |
lateinit var mockObserver: Observer<Boolean> | |
lateinit var viewModel: MainViewModel |
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
class MainActivity : AppCompatActivity() { | |
lateinit var viewModel: MainViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java) |
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
class MainViewModel : ViewModel() { | |
private val _isFormValid = MutableLiveData<Boolean>() | |
val isFormValid: LiveData<Boolean> | |
get() = _isFormValid | |
var username = "" | |
set(value) { | |
field = value |
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
class MainActivity : AppCompatActivity() { | |
lateinit var viewModel: MainViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java) |
NewerOlder