| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,72 @@ |
| 1 |
+/* |
|
| 2 |
+ * Copyright (c) 2023 Samson Achiaga |
|
| 3 |
+ * |
|
| 4 |
+ * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 |
+ * you may not use this file except in compliance with the License. |
|
| 6 |
+ * You may obtain a copy of the License at |
|
| 7 |
+ * |
|
| 8 |
+ * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 |
+ * |
|
| 10 |
+ * Unless required by applicable law or agreed to in writing, software |
|
| 11 |
+ * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 |
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 |
+ * See the License for the specific language governing permissions and |
|
| 14 |
+ * limitations under the License. |
|
| 15 |
+ */ |
|
| 16 |
+ |
|
| 17 |
+package com.certified.audionote.utils |
|
| 18 |
+ |
|
| 19 |
+import android.app.Activity |
|
| 20 |
+import android.content.Context |
|
| 21 |
+import android.os.Bundle |
|
| 22 |
+import android.view.View |
|
| 23 |
+import android.view.WindowManager |
|
| 24 |
+import android.view.inputmethod.InputMethodManager |
|
| 25 |
+import android.widget.Toast |
|
| 26 |
+import androidx.annotation.IdRes |
|
| 27 |
+import androidx.core.content.res.ResourcesCompat |
|
| 28 |
+import androidx.datastore.preferences.preferencesDataStore |
|
| 29 |
+import androidx.fragment.app.Fragment |
|
| 30 |
+import androidx.navigation.NavController |
|
| 31 |
+import androidx.navigation.NavDirections |
|
| 32 |
+ |
|
| 33 |
+object Extensions {
|
|
| 34 |
+ fun Fragment.flags(color: Int) {
|
|
| 35 |
+ val window = requireActivity().window |
|
| 36 |
+ window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) |
|
| 37 |
+// window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) |
|
| 38 |
+ window.statusBarColor = ResourcesCompat.getColor(resources, color, null) |
|
| 39 |
+ } |
|
| 40 |
+ |
|
| 41 |
+ fun Fragment.showToast(message: String) {
|
|
| 42 |
+ Toast.makeText( |
|
| 43 |
+ requireContext(), |
|
| 44 |
+ message, |
|
| 45 |
+ Toast.LENGTH_LONG |
|
| 46 |
+ ).show() |
|
| 47 |
+ } |
|
| 48 |
+ |
|
| 49 |
+ val Context.dataStore by preferencesDataStore(name = "com.certified.audionotes.preferences") |
|
| 50 |
+ |
|
| 51 |
+ fun View.hideKeyboardFrom(context: Context) {
|
|
| 52 |
+ val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager |
|
| 53 |
+ imm.hideSoftInputFromWindow(windowToken, 0) |
|
| 54 |
+ } |
|
| 55 |
+ |
|
| 56 |
+ fun View.showKeyboardFor(context: Context) {
|
|
| 57 |
+ val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager |
|
| 58 |
+ imm.showSoftInput(this, 0) |
|
| 59 |
+ } |
|
| 60 |
+ |
|
| 61 |
+ fun NavController.safeNavigate(direction: NavDirections) {
|
|
| 62 |
+ currentDestination?.getAction(direction.actionId)?.run { navigate(direction) }
|
|
| 63 |
+ } |
|
| 64 |
+ |
|
| 65 |
+ fun NavController.safeNavigate( |
|
| 66 |
+ @IdRes currentDestinationId: Int, |
|
| 67 |
+ @IdRes id: Int, |
|
| 68 |
+ args: Bundle? = null |
|
| 69 |
+ ) {
|
|
| 70 |
+ if (currentDestinationId == currentDestination?.id) navigate(id, args) |
|
| 71 |
+ } |
|
| 72 |
+} |
|
| 0 | 73 |
\ No newline at end of file |