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
...
buttonStartChat.setOnClickListener { view ->
// get Action from genereted Directions class
val action = MessageListFragmentDirections.actionMessageListFragmentToMessageFragment(userRepository.getSignedInUser().userId)
// also you can set the value like this
action.userId = userRepository.getSignedInUser().userId
view.findNavController.navigate(action)
}
<navigation
...>
<fragment
...>
<action
android:id="@+id/actionStartChat"
app:destination="@id/messageFragment">
<argument
...
//navigate to Sign Up screen to create a new user
buttonSignUp.setOnClickListener { view ->
view.findNavController().navigate(R.id.action_signInFragment_to_signUpFragment)
}
...
...
//check is there a signed in user and navigate to specific destination
if (usersRepositoty.userExists())
Navigation.findNavController(view).navigate(R.id.action_splashFragment_to_messageListFragment)
} else {
Navigation.findNavController(view).navigate(R.id.action_splashFragment_to_signInFragment)
}
@robertlevonyan
robertlevonyan / root_activity.xml
Created June 14, 2019 18:44
An activity which contains a NavHostFragment for Android Navigation component
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragmentNavHost"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
fun chainWorks(filter1: Work, filter2: Work, compress: Work, upload: Work) {
WorkManager.getInstance()
// Run these works in parallel
.beginWith(listOf(filter1, filter2))
// Dependent work (only runs after all previous work in chain)
.then(compress)
.then(upload)
// Don't forget to enqueue()
.enqueue()
}
fun createConstraints() = Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED) // if connected to WIFI
// other values(NOT_REQUIRED, CONNECTED, NOT_ROAMING, METERED)
.setRequiresBatteryNotLow(true) // if the battery is not low
.setRequiresStorageNotLow(true) // if the storage is not low
.build()
fun createWorkRequest(data: Data) = PeriodicWorkRequestBuilder<LocationWorker>(12, TimeUnit.HOURS) // setting period to 12 hours
// set input data for the work
.setInputData(data)
class LocationWorker(context: Context, workerParams: WorkerParameters)
: Worker(context, workerParams) {
...
override fun doWork(): Result {
val latitude = inputData.getDouble(KEY_LATITUDE, 40.1903484)
val longitude = inputData.getDouble(KEY_LONGITUDE, 44.5148367)
override fun onCreate(savedInstanceState: Bundle?) {
// some smart stuff here
bottomAppBar.replaceMenu(R.menu.menu_wallpaper)
bottomAppBar.setNavigationOnClickListener {
// do something interesting on navigation click
}
}