Skip to content

Instantly share code, notes, and snippets.

View douglasiacovelli's full-sized avatar

Douglas Iacovelli douglasiacovelli

  • @contratadome
  • Bertioga
View GitHub Profile
fun onClickLikeButton() {
if (viewModel.liked) {
//First we update the local model so the user feels the action is instantaneous
viewModel.liked = false
viewModel.count--
//Then we call the API to update it on the server
apiService.removeLike.enqueue({ response ->
fun onClickLikeButton() {
if (viewModel.liked) {
removeLike()
} else {
addLike()
}
}
private fun addLike() {
class ViewModel {
//... here comes the attributes and the model
//we are changing (irrelevant for the example)
fun addLikeLocally() {
liked = true
count++
}
fun removeLikeLocally() {
fun onClickLikeButton() {
if (viewModel.liked) {
removeLike()
} else {
addLike()
}
}
private fun showError(errorMessage: String) {
Toast.makeToast(activity, errorMessage, LENGTH_LONG).show()
dependencies {
...
}
//This step below fixes the issues
afterEvaluate {
android.applicationVariants.all {
def name = it.name.capitalize()
tasks["kapt${name}Kotlin"].dependsOn("transformDataBindingWithDataBindingMergeArtifactsFor${name}")
}
object BindingAdapters {
@BindingAdapter(values = ["selected", "options"], requireAll = false)
@JvmStatic
fun setOptionsAndSelected(view: MyCustomView, selected: Option?, options: List<Option>?) {
options?.let {
view.setOptions(it)
}
selected?.let {
class SectionPagerAdapter(
fragmentManager: FragmentManager,
private val sections: List<Section>
): FragmentStatePagerAdapter(fragmentManager) {
var currentFormSection: FormSection? = null
override fun getItem(position: Int): Fragment {
return when (sections[position].type) {
SectionType.MAP -> MapFragment.newInstance(sections[position])
class MainActivity : AppCompatActivity() {
private val viewPager by lazy { findViewById<ViewPager>(R.id.viewPager) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//getSections somehow from API/local storage
class FormFragment : Fragment() {
private val section by lazy { arguments!!.getParcelable<Section>(SECTION) }
private val fieldInputs = mutableListOf<FieldInput>()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.form_section_fragment, container, false)
return view
}
interface FieldInput {
var key: String
fun getValue(): String
fun setHint(hint: String)
}