Skip to content

Instantly share code, notes, and snippets.

View robertlevonyan's full-sized avatar
🏠
Working from home

Robert Levonyan robertlevonyan

🏠
Working from home
View GitHub Profile
@Dao
interface MovieDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveMovies(vararg movies: RoomMovie)
@Query("SELECT * FROM RoomMovie")
fun getMovies(): Flow<List<RoomMovie>>
}
@robertlevonyan
robertlevonyan / SetDropListener.kt
Created February 9, 2021 12:20
setting the drop listener
// binding is a viewBinding instance
binding.rvSentence.setOnDragListener(
DropListener {
wordsAdapter.removeItem(selectedWord) // remove dropped word from word box
sentenceAdapter.addItem(selectedWord) // add dropped word to sentence
}
)
@robertlevonyan
robertlevonyan / DropListener.kt
Created February 9, 2021 12:17
drop item into recyclerview
class DropListener(private val onDrop: () -> Unit) : View.OnDragListener {
override fun onDrag(view: View, dragEvent: DragEvent): Boolean {
when (dragEvent.action) {
// when item has been dropped, notify about it
DragEvent.ACTION_DROP -> onDrop()
}
return true
}
}
@robertlevonyan
robertlevonyan / DragViewHolder.kt
Created February 9, 2021 12:01
start drag on long click
itemView.setOnLongClickListener { view ->
val data = ClipData.newPlainText("", word)
val shadowBuilder = View.DragShadowBuilder(view)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
view.startDragAndDrop(data, shadowBuilder, view, 0)
} else {
view.startDrag(data, shadowBuilder, view, 0)
}
true
}
@robertlevonyan
robertlevonyan / activity_main.xml
Created February 9, 2021 11:42
The UI representation in xml
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:background="@drawable/bg_gradient"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:id="@+id/rvSentenceCard"
class HeadsetConnectionReceiver(private val actionPlugged: (Boolean) -> Unit) : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
intent?.run {
if (Intent.ACTION_HEADSET_PLUG == action) {
when (getIntExtra("state", -1)) {
0 -> actionPlugged(false)
1 -> actionPlugged(true)
}
}
}
@robertlevonyan
robertlevonyan / NetworkFlow.kt
Created December 18, 2019 12:44
Network Listener Flow
@ExperimentalCoroutinesApi
fun notifyNetwork() = channelFlow<Boolean> {
val networkRequest = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.build()
val connectivityManager: ConnectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.registerNetworkCallback(networkRequest, object : ConnectivityManager.NetworkCallback() {
...
override fun onBindViewHolder(vh: ViewHolder, position: Int) {
val itemData = getData(position)
vh.textView.setTextFuture(PrecomputedTextCompat.getTextFuture(
itemData.text,
TextViewCompat.getTextMetricsParams(textView),
/*optional custom executor*/ null)
)
fun setTextAsync(textView: TextView, longString: String) {
val params = textView.textMetricsParams
val ref = WeakReference(textView)
CoroutineScope.launch(Dispatchers.Default) {
val pText = PrecomputedText.create(longString, params)
withContext(Dispatchers.Main) {
ref.get()?.let { textView ->
textView.text = pText
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
arguments?.let {
val args = MessageListFragmentArgs.fromBundle(it)
val userId = args.userId
}
...