This file contains hidden or 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
@Database(entities = [RoomMovie::class], version = 1) // an array with entites and the current db version | |
@TypeConverters(value = [DateTypeConverter::class]) // an array of all type converters | |
abstract class AppDatabase : RoomDatabase() { | |
abstract fun movieDao(): MovieDao | |
companion object { | |
@Volatile | |
private var instance: AppDatabase? = null | |
fun getInstance(context: Context): AppDatabase = |
This file contains hidden or 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
@Dao | |
interface MovieDao { | |
@Insert(onConflict = OnConflictStrategy.REPLACE) | |
suspend fun saveMovies(vararg movies: RoomMovie) | |
@Query("SELECT * FROM RoomMovie") | |
fun getMovies(): Flow<List<RoomMovie>> | |
} |
This file contains hidden or 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
// 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 | |
} | |
) |
This file contains hidden or 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 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 | |
} | |
} |
This file contains hidden or 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
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 | |
} |
This file contains hidden or 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
<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" |
This file contains hidden or 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 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) | |
} | |
} | |
} |
This file contains hidden or 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
@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() { |
This file contains hidden or 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
... | |
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) | |
) |
This file contains hidden or 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
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 | |
} |