aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/utils/FragmentExtensions.kt
blob: 01103d1996aa402dceb6d4117c5eb8363450c2cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.zeapo.pwdstore.utils

import android.content.pm.PackageManager
import android.view.View
import androidx.annotation.IdRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.commit
import com.zeapo.pwdstore.R

fun Fragment.isPermissionGranted(permission: String): Boolean {
    return ContextCompat.checkSelfPermission(requireActivity(), permission) == PackageManager.PERMISSION_GRANTED
}

fun Fragment.finish() = requireActivity().finish()

fun FragmentManager.performTransaction(destinationFragment: Fragment, @IdRes containerViewId: Int = android.R.id.content) {
    this.commit {
        beginTransaction()
        setCustomAnimations(
            R.animator.slide_in_left,
            R.animator.slide_out_left,
            R.animator.slide_in_right,
            R.animator.slide_out_right)
        replace(containerViewId, destinationFragment)
    }
}

fun FragmentManager.performTransactionWithBackStack(destinationFragment: Fragment, @IdRes containerViewId: Int = android.R.id.content) {
    this.commit {
        beginTransaction()
        addToBackStack(destinationFragment.tag)
        setCustomAnimations(
            R.animator.slide_in_left,
            R.animator.slide_out_left,
            R.animator.slide_in_right,
            R.animator.slide_out_right)
        replace(containerViewId, destinationFragment)
    }
}

fun FragmentManager.performSharedElementTransaction(destinationFragment: Fragment, views: List<View>, @IdRes containerViewId: Int = android.R.id.content) {
    this.commit {
        beginTransaction()
        for (view in views) {
            addSharedElement(view, view.transitionName)
        }
        addToBackStack(destinationFragment.tag)
        replace(containerViewId, destinationFragment)
    }
}