Skip to content

Instantly share code, notes, and snippets.

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

Emre Hamurcu hamurcuabi

🏠
Working from home
View GitHub Profile
@hamurcuabi
hamurcuabi / SharedViewModel.kt
Created January 7, 2024 12:01
SharedViewModel
class SharedViewModel() : ViewModel() {
var data: String = "SharedViewModel Data"
private set
}
@hamurcuabi
hamurcuabi / FragmentA.kt
Created January 19, 2022 18:26
Start Fragment For Result With Navigation Component
class FragmentA : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Observe the result to be set by Fragment B in the stateHandle of the currentBackStackEntry
val currentBackStackEntry = findNavController().currentBackStackEntry
val savedStateHandle = currentBackStackEntry?.savedStateHandle
savedStateHandle?.getLiveData<String>(RESULT_FROM_FRAGMENT_B)
?.observe(currentBackStackEntry, Observer { result ->
print(result)
private fun getPermissionState(activity: Activity, result: Map<String, Boolean>): PermissionState {
val deniedPermissions = result.filterValues { !it }.keys
return when {
deniedPermissions.isEmpty() -> PermissionState.Granted
deniedPermissions.any { permission ->
!shouldShowRequestPermissionRationale(
/* activity = */
activity,
/* permission = */
import android.os.Bundle
import android.widget.ProgressBar
import android.widget.TextView
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
class MainActivity : AppCompatActivity() {
private val mainViewModel: MainViewModel by viewModels()
val user = User()
val userValidator = Validator(user).apply {
addRule("Name Cannot be empty") { it?.name.isNullOrEmpty() }
addRule("Username lenght min 5 character") { it?.name?.length < 5 }
addRule("Email cannot be empty") { it?.email.isNullOrEmpty() }
addRule("Email format is wrong") { it?.email.isValidEmail() }
addRule("Age min 18") { it?.age < 18 }
}
package com.hamurcuabi.myapplication
import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.core.view.isVisible
import com.hamurcuabi.myapplication.databinding.ViewCustomToolbarBinding
class OnBackPressedDelegationImpl : OnBackPressedDelegation, DefaultLifecycleObserver {
private val onBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
onBackPressed.invoke()
}
}
private var fragmentActivity: FragmentActivity? = null
class HomeFragment : Fragment(),
OnBackPressedDelegation by OnBackPressedDelegationImpl() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
registerOnBackPressedDelegation(activity, this.lifecycle) {
// OnBackPressed fired!!!
}
}
}
interface OnBackPressedDelegation {
fun registerOnBackPressedDelegation(
fragmentActivity: FragmentActivity?,
lifecycle: Lifecycle,
onBackPressed: () -> Unit
)
}
@hamurcuabi
hamurcuabi / HomeFragment.kt
Created October 29, 2022 13:18
Without Delegation
class HomeFragment : Fragment() {
private val onBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
// On back pressed
}
}
override fun onResume() {
super.onResume()