forked from Spikeysanju/Expenso
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from maifeeulasad/feature/biometric
Feature/biometric
- Loading branch information
Showing
12 changed files
with
350 additions
and
107 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
app/src/main/java/dev/spikeysanju/expensetracker/data/local/datastore/SettingsDataStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package dev.spikeysanju.expensetracker.data.local.datastore | ||
|
||
import android.content.Context | ||
import androidx.datastore.preferences.core.booleanPreferencesKey | ||
import androidx.datastore.preferences.core.edit | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Singleton | ||
|
||
|
||
class SettingsDataStore(context: Context) : | ||
PrefsDataStore( | ||
context, | ||
PREF_FILE_SETTINGS | ||
), | ||
SettingsImpl { | ||
|
||
// used to get the data from datastore | ||
override val biometric: Flow<Boolean> | ||
get() = dataStore.data.map { preferences -> | ||
val biometric = preferences[BIOMETRIC_KEY] ?: false | ||
biometric | ||
} | ||
|
||
// used to save the bio-metric preference to datastore | ||
override suspend fun saveToDataStore(isBiometricEnabled: Boolean) { | ||
dataStore.edit { preferences -> | ||
preferences[BIOMETRIC_KEY] = isBiometricEnabled | ||
} | ||
} | ||
|
||
companion object { | ||
private const val PREF_FILE_SETTINGS = "settings_preference" | ||
private val BIOMETRIC_KEY = booleanPreferencesKey("biometric_mode") | ||
} | ||
} | ||
|
||
@Singleton | ||
interface SettingsImpl { | ||
val biometric: Flow<Boolean> | ||
suspend fun saveToDataStore(isBiometricEnabled: Boolean) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
app/src/main/java/dev/spikeysanju/expensetracker/view/settings/SettingsFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package dev.spikeysanju.expensetracker.view.settings | ||
|
||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.lifecycleScope | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import dev.spikeysanju.expensetracker.databinding.FragmentSettingsBinding | ||
import dev.spikeysanju.expensetracker.view.base.BaseFragment | ||
import kotlinx.coroutines.flow.first | ||
|
||
@AndroidEntryPoint | ||
class SettingsFragment : BaseFragment<FragmentSettingsBinding, SettingsViewModel>() { | ||
override val viewModel: SettingsViewModel by viewModels() | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
// Set the item state | ||
lifecycleScope.launchWhenStarted { | ||
binding.biometric.isChecked = viewModel.bioMetricPreference.first() | ||
} | ||
|
||
initViews() | ||
} | ||
|
||
private fun initViews() = with(binding) { | ||
biometric.setOnCheckedChangeListener { _, biometricEnabled -> | ||
viewModel.setBioMetricLock(biometricEnabled) | ||
} | ||
} | ||
|
||
override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?) = | ||
FragmentSettingsBinding.inflate(inflater, container, false) | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/dev/spikeysanju/expensetracker/view/settings/SettingsViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package dev.spikeysanju.expensetracker.view.settings | ||
|
||
|
||
import android.app.Application | ||
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import dev.spikeysanju.expensetracker.data.local.datastore.SettingsDataStore | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class SettingsViewModel @Inject constructor( | ||
application: Application | ||
) : | ||
AndroidViewModel(application) { | ||
|
||
// init datastore | ||
private val settingsDataStore = SettingsDataStore(application) | ||
|
||
// get bio-metric preference | ||
val bioMetricPreference = settingsDataStore.biometric | ||
|
||
fun setBioMetricLock(bioMetricLock: Boolean) { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
settingsDataStore.saveToDataStore(bioMetricLock) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:fillViewport="true" | ||
android:scrollbars="none"> | ||
|
||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
tools:context=".view.settings.SettingsFragment"> | ||
|
||
<ImageView | ||
android:id="@+id/logo" | ||
android:layout_width="140dp" | ||
android:layout_height="140dp" | ||
android:layout_marginTop="@dimen/dimen_64" | ||
android:contentDescription="@string/app_name" | ||
android:src="@drawable/ic_logo" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<LinearLayout | ||
android:layout_marginStart="@dimen/dimen_16" | ||
android:layout_marginTop="@dimen/dimen_48" | ||
app:layout_constraintTop_toBottomOf="@id/logo" | ||
android:orientation="horizontal" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
<TextView | ||
android:id="@+id/biometricTitle" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:fontFamily="@font/open_sans_bold" | ||
android:text="@string/text_settings_biometric" | ||
android:textAppearance="@style/TextAppearance.MaterialComponents.Overline" | ||
app:layout_constraintStart_toStartOf="parent"/> | ||
|
||
|
||
<androidx.appcompat.widget.SwitchCompat | ||
android:layout_weight="1" | ||
android:id="@+id/biometric" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/biometricTitle" /> | ||
|
||
</LinearLayout> | ||
|
||
|
||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
|
||
</ScrollView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.