Skip to content

Instantly share code, notes, and snippets.

@kevivforever
kevivforever / DoubleLiveData.kt
Last active August 17, 2019 05:48
View Models
class DoubleLiveData<A, B>(a: LiveData<A>, b: LiveData<B>) : MediatorLiveData<Pair<A?, B?>>() {
init {
addSource(a) { value = it to b.value }
addSource(b) { value = a.value to it }
}
}
Creating observables
create() - Observable.create()
just() - Observable.just("first", "second", "third", "fourth", "fifth", "sixth",
"seventh", "eighth", "ninth", "tenth")
.subscribeOn(Schedulers.io()) // What thread to do the work on
.observeOn(AndroidSchedulers.mainThread())
range() - Observable.range(0,11)
repeat() - Observable.range(0,3)
.repeat(2)
@kevivforever
kevivforever / Anonymous Functions
Last active August 21, 2019 08:14
All Kotlin Basics explained with examples
lambda expressions can’t have a return statement. The return type is either inferred from the variable which stores
the lambda or from the last statement of the lambda body. Another missing thing is multiple return points you might
often need.
// 1
private fun isValidArtist(artist: String?): Boolean {
// 2
val artistValidator = fun(value: String?): Boolean {
// 3
if (value.isNullOrEmpty()) {
@kevivforever
kevivforever / async-await.txt
Created May 23, 2019 16:10
How to write async await
async function addInterestedCategories(uid, categories) {
await User.findOneAndUpdate({ uid: uid },
{ $addToSet: { interested_categories: categories } }, { new: true}).exec()
}
exports.getPost = async (req, res, next) => {
const postUpdate = await Post.findOneAndUpdate({ postID: req.params.postid
, viewedBy: { $not: { $elemMatch: { uid: req.query.uid } } }
},
{ $addToSet: { viewedBy: USER }, $inc: { totalViews: 1} }, { new: true })
@kevivforever
kevivforever / chip.txt
Created May 19, 2019 09:25
material chip
private fun setTags(tagList: List<Tag>) {
tagList.forEachIndexed { index, tag ->
val chipView = getChipView().apply {
configureChip(index, tag)
}
chipView.setOnCheckedChangeListener { chip, isChecked ->
//resId is basically a index of a tag in passed tagList
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/user_Profile_appbarlayout"
android:layout_width="match_parent"
@kevivforever
kevivforever / PagerAdapter.kt
Created May 5, 2019 10:27
Tablayout + Viewpager
class PagerAdapter(fragmentManager: FragmentManager) : FragmentStatePagerAdapter(fragmentManager) {
// Returns total number of pages
override fun getCount(): Int {
return NUM_ITEMS
}
// Returns the fragment to display for that page
override fun getItem(position: Int): Fragment? {
when (position) {
@kevivforever
kevivforever / Divider.java
Last active May 23, 2019 16:12
Recyclerview Helper
public class Divider extends RecyclerView.ItemDecoration {
private Drawable mDivider;
private int mOrientation;
public Divider(Context context, int orientation) {
mDivider = ContextCompat.getDrawable(context, R.drawable.divider);
if (orientation != LinearLayoutManager.VERTICAL) {
throw new IllegalArgumentException("This Item Decoration can be used only with a RecyclerView that uses a LinearLayoutManager with vertical orientation");
}
@kevivforever
kevivforever / bg_circle.xml
Created May 3, 2019 17:07
Different xml drawables backgrounds
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="@color/colorGreyIcon"/>
<size
android:width="60dp"
@kevivforever
kevivforever / Cloud function
Last active May 30, 2019 06:15
Firebase cloud Function to read data change on one child. Join data from two childs and write to third child
//Read data change on your ref
exports.functionname = functions.database.ref('/Items/{itemid}')
.onWrite(event => {
const item = event.data.val();
// read data once from second node
const locationRef = event.data.adminRef.root.child('location').child(event.params.itemid);
locationRef.once('value').then(snapshot =>{
const location = snapshot.val();
const city = location.city;
// data node where you want to write data