Skip to content

Instantly share code, notes, and snippets.

View peterpazmandi's full-sized avatar
🤖
Working on android stuff

Peter Pazmandi peterpazmandi

🤖
Working on android stuff
  • Zalaegerszeg, Hungary
  • 07:36 (UTC +02:00)
View GitHub Profile
@peterpazmandi
peterpazmandi / NetworkUtils.kt
Created July 16, 2020 05:22
Network Utility to detect availability or unavailability of Internet connection
import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Build
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
/**
fun observeSet(id: String) = resultLiveData(
databaseQuery = { dao.getLegoSet(id) },
networkCall = { legoSetRemoteDataSource.fetchSet(id) },
saveCallResult = { dao.insert(it) })
/**
* Creates a new [LiveData] object does not emit a value until the source `this` LiveData value
* has been changed. The value is considered changed if `equals()` yields `false`.
*/
.distinctUntilChanged()
@peterpazmandi
peterpazmandi / Converters.kt
Created July 14, 2020 14:08
Type converters to allow Room to reference complex data types.
class Converters
{
@TypeConverter fun calendarToDatestamp(calendar: Calendar): Long = calendar.timeInMillis
@TypeConverter fun datestampToCalendar(value: Long): Calendar =
Calendar.getInstance().apply { timeInMillis = value }
}
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
object ConnectivityUtil
{
fun isConnected(context: Context): Boolean
{
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork: NetworkInfo? = cm.activeNetworkInfo
btn_customToast.setOnClickListener {_button ->
Toast(this).showCustomToast (
et_message.text.toString().trim(),
this
)
}
fun Toast.showCustomToast(message: String, activity: Activity)
{
val layout = activity.layoutInflater.inflate (
R.layout.custom_toast,
activity.findViewById(R.id.cl_customToastContainer)
)
val textView = layout.findViewById<TextView>(R.id.tv_message)
textView.text = message
btn_customToast.setOnClickListener {
showCustomToast(et_message.text.toString().trim())
}
private fun showCustomToast(message: String)
{
val layout = layoutInflater.inflate (
R.layout.custom_toast,
findViewById(R.id.cl_customToastContainer)
)
val textView = layout.findViewById(R.id.tv_message)
textView.text = message
<LinearLayout
android:id="@+id/cl_customToastContainer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:orientation="horizontal"
android:gravity="center"
android:background="@drawable/shape_roundedcorners">
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/light_green"/>
<corners
android:radius="18dp"/>
<stroke
android:color="@color/green"