Skip to content

Instantly share code, notes, and snippets.

View weverb2's full-sized avatar

Brandon Wever weverb2

View GitHub Profile
@weverb2
weverb2 / TabLayoutExtensions.kt
Created March 30, 2020 16:04
Tab Selected Inline Functions
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
@weverb2
weverb2 / FormValidationActivity.kt
Last active March 19, 2023 01:30
Data Binding + LIve Data Form Validation
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
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>> {
@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
)
@weverb2
weverb2 / api.kt
Last active August 6, 2020 14:49
interface PlaceholderApi {
@GET("/posts")
fun getPosts(): Call<Response<List<Post>>>
}
object NetworkConfiguration {
const val BASE_URL = "https://jsonplaceholder.typicode.com/"
val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create())
.client(OkHttpClient())
.baseUrl(BASE_URL)
.build()
class MainViewModelTest {
@get:Rule
val instantTaskRule = InstantTaskExecutorRule()
@RelaxedMockK
lateinit var mockObserver: Observer<Boolean>
lateinit var viewModel: MainViewModel
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)
class MainViewModel : ViewModel() {
private val _isFormValid = MutableLiveData<Boolean>()
val isFormValid: LiveData<Boolean>
get() = _isFormValid
var username = ""
set(value) {
field = value
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)