Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.jeeldobariya.passcodes.flags

import android.content.Context
import androidx.core.content.edit

class FeatureFlagManager private constructor(context: Context) {
private val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)

var latestFeaturesEnabled: Boolean
get() = prefs.getBoolean(KEY_LATEST_FEATURES, false)
set(value) { prefs.edit { putBoolean(KEY_LATEST_FEATURES, value) } }

companion object {
private const val PREFS_NAME = "feature_flags"
private const val KEY_LATEST_FEATURES = "latest_features_enabled"

@Volatile private var INSTANCE: FeatureFlagManager? = null

fun get(context: Context): FeatureFlagManager {
return INSTANCE ?: synchronized(this) {
val instance = FeatureFlagManager(context.applicationContext)
INSTANCE = instance
instance
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import android.view.LayoutInflater

import com.jeeldobariya.passcodes.R
import com.jeeldobariya.passcodes.databinding.ActivitySettingsBinding
import com.jeeldobariya.passcodes.flags.FeatureFlagManager
import androidx.core.content.edit

class SettingsActivity : AppCompatActivity() {

Expand Down Expand Up @@ -44,6 +46,8 @@ class SettingsActivity : AppCompatActivity() {

setInitialLangSelection()

binding.switchLatestFeatures.isChecked = FeatureFlagManager.get(this).latestFeaturesEnabled

// Add event onclick listener
addOnClickListenerOnButton()

Expand Down Expand Up @@ -90,10 +94,15 @@ class SettingsActivity : AppCompatActivity() {
val newThemeStyle = THEMES[nextIndex]

// Save the new theme and restart the application to apply it
sharedPrefs.edit().putInt(THEME_KEY, newThemeStyle).apply()
sharedPrefs.edit { putInt(THEME_KEY, newThemeStyle) }
recreate()

Toast.makeText(this@SettingsActivity, getString(R.string.restart_app_require), Toast.LENGTH_SHORT).show()
}

binding.switchLatestFeatures.setOnCheckedChangeListener { _, isChecked ->
FeatureFlagManager.get(this).latestFeaturesEnabled = isChecked
Toast.makeText(this@SettingsActivity, getString(R.string.future_feat_clause) + isChecked.toString(), Toast.LENGTH_SHORT).show()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import androidx.lifecycle.lifecycleScope
import com.jeeldobariya.passcodes.R
import com.jeeldobariya.passcodes.database.Password
import com.jeeldobariya.passcodes.databinding.ActivityViewPasswordBinding
import com.jeeldobariya.passcodes.flags.FeatureFlagManager
import com.jeeldobariya.passcodes.utils.Controller
import com.jeeldobariya.passcodes.utils.DatabaseOperationException
import com.jeeldobariya.passcodes.utils.DateTimeUtils
import com.jeeldobariya.passcodes.utils.PasswordNotFoundException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -50,6 +52,8 @@ class ViewPasswordActivity : AppCompatActivity() {

controller = Controller(this)

binding.copyPasswordBtn.isEnabled = FeatureFlagManager.get(this).latestFeaturesEnabled

// Add event onclick listener
addOnClickListenerOnButton()

Expand All @@ -62,11 +66,13 @@ class ViewPasswordActivity : AppCompatActivity() {
try {
passwordEntity = controller.getPasswordById(passwordEntityId)
withContext(Dispatchers.Main) {
val lastUpdatedAt = DateTimeUtils.getRelativeDays(passwordEntity.updatedAt.orEmpty(), "yyyy-MM-dd HH:mm:ss")

binding.tvDomain.text = "${getString(R.string.domain_prefix)} ${passwordEntity.domain}"
binding.tvUsername.text = "${getString(R.string.username_prefix)} ${passwordEntity.username}"
binding.tvPassword.text = "${getString(R.string.password_prefix)} ${passwordEntity.password}"
binding.tvNotes.text = "${getString(R.string.notes_prefix)} ${passwordEntity.notes}"
binding.tvUpdatedAt.text = "${getString(R.string.updatedat_prefix)} ${passwordEntity.updatedAt}"
binding.tvUpdatedAt.text = "${getString(R.string.updatedat_prefix)} ${lastUpdatedAt}"
}
} catch (e: PasswordNotFoundException) {
withContext(Dispatchers.Main) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import android.content.Context
import com.jeeldobariya.passcodes.database.MasterDatabase
import com.jeeldobariya.passcodes.database.Password
import com.jeeldobariya.passcodes.database.PasswordsDao
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import kotlinx.coroutines.flow.Flow

class InvalidInputException(message: String = "Input parameters cannot be blank.") : Exception(message)
Expand Down Expand Up @@ -34,7 +31,7 @@ class Controller(context: Context) {
throw InvalidInputException()
}

val currentTimestamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Date())
val currentTimestamp = DateTimeUtils.getCurrDateTime()
val newPassword = Password(
domain = domain,
username = username,
Expand Down Expand Up @@ -106,7 +103,7 @@ class Controller(context: Context) {
val existingPassword = passwordsDao.getPasswordById(id)
?: throw PasswordNotFoundException("Password with ID $id not found for update.")

val updatedTimestamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Date())
val updatedTimestamp = DateTimeUtils.getCurrDateTime()
val passwordToUpdate = existingPassword.copy(
domain = domain,
username = username,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.jeeldobariya.passcodes.utils

import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.concurrent.TimeUnit

class DateTimeUtils {
companion object {
/** Returns current date-time in `yyyy-MM-dd HH:mm:ss` format */
fun getCurrDateTime(): String {
return SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
.format(Date())
}

/** Returns current date in `yyyy-MM-dd` format */
fun getCurrDate(): String {
return SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
.format(Date())
}

/** Returns current time in `HH:mm:ss` format */
fun getCurrTime(): String {
return SimpleDateFormat("HH:mm:ss", Locale.getDefault())
.format(Date())
}

/** Parse a String into a Date with a given pattern */
fun parseDate(dateStr: String, pattern: String = "yyyy-MM-dd HH:mm:ss"): Date? {
return try {
SimpleDateFormat(pattern, Locale.getDefault()).parse(dateStr)
} catch (e: Exception) {
null
}
}

fun getRelativeDays(dateStr: String, pattern: String = "yyyy-MM-dd"): String {
return try {
val parsedDate = parseDate(dateStr, pattern) ?: return "Invalid date"

val now = System.currentTimeMillis()
val diff = parsedDate.time - now
val days = TimeUnit.MILLISECONDS.toDays(diff)

when {
days == 0L -> "today"
days > 0L -> "in $days day${if (days > 1) "s" else ""}"
else -> "${-days} day${if (days < -1) "s" else ""} ago"
}
} catch (e: Exception) {
"Invalid date format"
}
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_password_manager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".ui.PasswordManager"
tools:context=".ui.PasswordManagerActivity"
android:padding="4sp" >

<com.google.android.material.textview.MaterialTextView
Expand Down Expand Up @@ -37,7 +37,7 @@
android:text="@string/load_password_button_text"
android:textSize="14dp" />

<com.google.android.material.button.MaterialButton
<com.google.android.material.button.MaterialButton
android:enabled="false"
android:id="@+id/import_password_btn"
android:layout_width="match_parent"
Expand Down
38 changes: 34 additions & 4 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
android:layout_height="wrap_content"
android:text="@string/label_language_setting"
android:textColor="?attr/colorPrimary"
android:textSize="24sp"
android:textSize="16sp"
android:layout_gravity="left|center_vertical"
android:layout_margin="8dp" />

Expand All @@ -58,7 +58,7 @@
android:id="@+id/theme_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginTop="24dp"
app:cardCornerRadius="16dp">

<LinearLayout
Expand All @@ -73,7 +73,7 @@
android:layout_height="wrap_content"
android:text="@string/label_theme_setting"
android:textColor="?attr/colorPrimary"
android:textSize="24sp"
android:textSize="16sp"
android:layout_gravity="left|center_vertical"
android:layout_margin="8dp" />

Expand All @@ -82,10 +82,40 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toggle_theme_button_text"
android:textSize="14dp"
android:textSize="12sp"
android:layout_gravity="right|center_vertical" />

</LinearLayout>
</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
android:id="@+id/latest_features_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:padding="16dp">

<com.google.android.material.textview.MaterialTextView
android:textColor="?attr/colorPrimary"
android:text="@string/latest_feature"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp" />

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/switch_latest_features"
android:layout_width="wrap_content"
android:layout_height="match_parent"
tools:ignore="UseSwitchCompatOrMaterialXml"
android:layout_margin="8dp"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
11 changes: 4 additions & 7 deletions app/src/main/res/layout/activity_view_password.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,23 @@

<com.google.android.material.button.MaterialButton
android:id="@+id/copy_password_btn"
android:textColor="?attr/colorSecondary"
app:backgroundTint="?attr/colorSecondaryContainer"
style="@style/Widget.Material3.Button.TonalButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/copy_password_button_text"
android:textSize="14dp" />

<com.google.android.material.button.MaterialButton
android:id="@+id/update_password_btn"
android:textColor="?attr/colorSecondary"
app:backgroundTint="?attr/colorSecondaryContainer"
style="@style/Widget.Material3.Button.TonalButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/update_password_button_text"
android:textSize="14dp" />

<com.google.android.material.button.MaterialButton
<com.google.android.material.button.MaterialButton
android:id="@+id/delete_password_btn"
android:textColor="?attr/colorError"
app:backgroundTint="?attr/colorErrorContainer"
style="@style/Widget.Material3.Button.TonalButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/delete_password_button_text"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentNavigation">false</item>
<item name="android:windowLightNavigationBar" tools:targetApi="o_mr1">true</item>
<item name="android:windowLightStatusBar">true</item>
<item name="android:windowLightNavigationBar" tools:targetApi="o_mr1">false</item>
<item name="android:windowLightStatusBar">false</item>
</style>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<string name="label_language_setting">Language: </string>
<string name="label_contributor">Contributor: </string>
<string name="label_theme_setting">Themes: </string>
<string name="latest_feature" translatable="false">Preview Features: </string>
<string name="about_us_button_text">About Us</string>

<!-- Hyper Text Link Buttons Text -->
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,4 @@
<item name="android:windowLightNavigationBar" tools:targetApi="o_mr1">true</item>
<item name="android:windowLightStatusBar">true</item>
</style>

</resources>