Skip to content

Commit

Permalink
Merge pull request #57 from GalievBulat/default-folder-setting
Browse files Browse the repository at this point in the history
created remember the last used folder option
  • Loading branch information
AmrDeveloper authored Apr 9, 2024
2 parents ae2c071 + c42142a commit 75dc82b
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ data class DataPackage (
@SerializedName("autoSaving")
val enableAutoSaving : Boolean?,

@SerializedName("defaultFolderMode")
val defaultFolderMode : Boolean?,

@SerializedName("theme")
val theme : Theme?
)
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ class JsonImportExportFileParser: ImportExportFileParser {
val links = linksResult.getOrDefault(listOf())
val showClickCounter = uiPreferences.isClickCounterEnabled()
val autoSaving = uiPreferences.isAutoSavingEnabled()
val defaultFolder = uiPreferences.isDefaultFolderEnabled()
val lastTheme = uiPreferences.getThemeType()
val dataPackage = DataPackage(folders, links, showClickCounter, autoSaving, lastTheme)
val dataPackage = DataPackage(folders, links, showClickCounter, autoSaving, defaultFolder, lastTheme)
return Result.success(Gson().toJson(dataPackage))
} else {
return Result.failure(Throwable());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package com.amrdeveloper.linkhub.ui.folder

import android.content.Context
import android.os.Bundle
import android.view.*
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
Expand All @@ -16,7 +21,6 @@ import com.amrdeveloper.linkhub.util.UiPreferences
import com.amrdeveloper.linkhub.util.showError
import com.amrdeveloper.linkhub.util.showSnackBar
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber
import javax.inject.Inject

@AndroidEntryPoint
Expand Down Expand Up @@ -143,6 +147,9 @@ class FolderFragment : Fragment() {
}

private fun deleteFolder() {
if(uiPreferences.isDefaultFolderEnabled() &&
uiPreferences.getDefaultFolderId() == currentFolder.id)
uiPreferences.deleteDefaultFolder()
folderViewModel.deleteFolder(currentFolder.id)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import androidx.fragment.app.viewModels
import com.amrdeveloper.linkhub.R
import com.amrdeveloper.linkhub.data.ImportExportFileType
import com.amrdeveloper.linkhub.databinding.FragmentImportExportBinding
import com.amrdeveloper.linkhub.util.UiPreferences
import com.amrdeveloper.linkhub.util.getFileName
import com.amrdeveloper.linkhub.util.getFileText
import com.amrdeveloper.linkhub.util.showSnackBar
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class ImportExportFragment : Fragment() {
Expand All @@ -32,6 +34,9 @@ class ImportExportFragment : Fragment() {

private val importExportViewModel by viewModels<ImportExportViewModel>()

@Inject
lateinit var uiPreferences: UiPreferences

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand All @@ -49,6 +54,8 @@ class ImportExportFragment : Fragment() {
launchFileTypePickerDialog(requireContext()) { fileType ->
importExportFileType = fileType
importDataFile(fileType)
if(uiPreferences.isDefaultFolderEnabled())
uiPreferences.deleteDefaultFolder()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class ImportExportViewModel @Inject constructor (
uiPreferences.setEnableAutoSave(
it.enableAutoSaving ?: lastAutoSavingEnabled
)
// Import use last folder mode
val defaultFolder = uiPreferences.isDefaultFolderEnabled()
uiPreferences.setEnableDefaultFolderEnabled(
it.defaultFolderMode ?: defaultFolder
)
// Import theme flag if it available
val lastThemeOption = uiPreferences.getThemeType()
uiPreferences.setThemeType(it.theme ?: lastThemeOption)
Expand Down
38 changes: 30 additions & 8 deletions app/src/main/java/com/amrdeveloper/linkhub/ui/link/LinkFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package com.amrdeveloper.linkhub.ui.link

import android.content.Context
import android.os.Bundle
import android.view.*
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.webkit.URLUtil
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
Expand All @@ -22,7 +27,6 @@ import com.amrdeveloper.linkhub.util.UiPreferences
import com.amrdeveloper.linkhub.util.showError
import com.amrdeveloper.linkhub.util.showSnackBar
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber
import java.text.DateFormat
import javax.inject.Inject

Expand Down Expand Up @@ -52,7 +56,7 @@ class LinkFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentLinkBinding.inflate(inflater, container, false)

handleDefaultFolder()
handleIntentSharedLink()
handleLinkArgument()
setupObservers()
Expand All @@ -63,6 +67,15 @@ class LinkFragment : Fragment() {
return binding.root
}

private fun handleDefaultFolder(){
if (uiPreferences.isDefaultFolderEnabled()){
val defFolderId = uiPreferences.getDefaultFolderId()
if (defFolderId!=-1){
linkViewModel.getFolderWithId(defFolderId)
}
}
}

private fun handleIntentSharedLink() {
val sharedLink = arguments?.getString("shared_link") ?: return

Expand Down Expand Up @@ -93,6 +106,13 @@ class LinkFragment : Fragment() {
}
}

private fun setActiveFolderToFolderList(folders: Iterable<Folder>, id: Int){
val folder = folders.find { it.id == currentLink.folderId }
folder?.let {
binding.folderNameMenu.setText(it.name, false)
}
}

private fun setupObservers() {
linkViewModel.currentFolderLiveData.observe(viewLifecycleOwner) {
binding.folderNameMenu.setText(it.name, false)
Expand All @@ -117,11 +137,10 @@ class LinkFragment : Fragment() {
findNavController().currentBackStackEntry?.savedStateHandle?.remove<String>(
CREATED_FOLDER_NAME_KEY
)
} else if (::currentLink.isInitialized) {
val folder = folders.find { it.id == currentLink.folderId }
folder?.let {
binding.folderNameMenu.setText(it.name, false)
}
} else if (::currentLink.isInitialized ) {
setActiveFolderToFolderList(folders, currentLink.folderId)
} else if (uiPreferences.isDefaultFolderEnabled() && uiPreferences.getDefaultFolderId()!=-1){
setActiveFolderToFolderList(folders, uiPreferences.getDefaultFolderId())
}
}

Expand Down Expand Up @@ -150,6 +169,9 @@ class LinkFragment : Fragment() {
FOLDER_NONE_ID
}
else -> {
if(uiPreferences.isDefaultFolderEnabled()) {
uiPreferences.setDefaultFolderId(folder.id)
}
folder.id
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class SettingFragment : Fragment() {

// Setup Auto saving
binding.autoSavingSwitch.isChecked = uiPreferences.isAutoSavingEnabled()

// Setup default folder
binding.defaultFolderSwitch.isChecked = uiPreferences.isDefaultFolderEnabled()
}

override fun onPause() {
Expand Down Expand Up @@ -119,6 +122,11 @@ class SettingFragment : Fragment() {
uiPreferences.setEnableAutoSave(isChecked)
}
}
binding.defaultFolderSwitch.setOnCheckedChangeListener { _, isChecked ->
if (isViewPassedResumedState) {
uiPreferences.setEnableDefaultFolderEnabled(isChecked)
}
}
}

override fun onDestroyView() {
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/com/amrdeveloper/linkhub/util/UiPreferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ private const val UI_PREFERENCE_NAME = "linkhub_settings"
private const val UI_THEME_KEY = "theme"
private const val UI_COUNTER_KEY = "counter"
private const val UI_AUTO_SAVE_KEY = "auto_save"
private const val UI_DEFAULT_FOLDER_KEY = "default_folder_mode"
private const val DEFAULT_FOLDER_NAME = "default_folder_name"
private const val PASSWORD_ENABLE_KEY = "password_enable"
private const val PASSWORD_TEXT_KEY = "password_text"

Expand All @@ -30,6 +32,24 @@ class UiPreferences(private val context: Context) {
editor.apply()
}

fun setEnableDefaultFolderEnabled(enable : Boolean) {
val editor = context.getSharedPreferences(UI_PREFERENCE_NAME, Context.MODE_PRIVATE).edit()
editor.putBoolean(UI_DEFAULT_FOLDER_KEY, enable)
editor.apply()
}

fun setDefaultFolderId(folderId : Int) {
val editor = context.getSharedPreferences(UI_PREFERENCE_NAME, Context.MODE_PRIVATE).edit()
editor.putInt(DEFAULT_FOLDER_NAME, folderId)
editor.apply()
}

fun deleteDefaultFolder() {
val editor = context.getSharedPreferences(UI_PREFERENCE_NAME, Context.MODE_PRIVATE).edit()
editor.putInt(DEFAULT_FOLDER_NAME, -1)
editor.apply()
}

fun getThemeType() : Theme {
val preferences = context.getSharedPreferences(UI_PREFERENCE_NAME, Context.MODE_PRIVATE)
val themeName = preferences.getString(UI_THEME_KEY, Theme.WHITE.name)
Expand Down Expand Up @@ -58,6 +78,16 @@ class UiPreferences(private val context: Context) {
return preferences.getBoolean(UI_AUTO_SAVE_KEY, true)
}

fun isDefaultFolderEnabled() : Boolean {
val preferences = context.getSharedPreferences(UI_PREFERENCE_NAME, Context.MODE_PRIVATE)
return preferences.getBoolean(UI_DEFAULT_FOLDER_KEY, false)
}

fun getDefaultFolderId() : Int {
val preferences = context.getSharedPreferences(UI_PREFERENCE_NAME, Context.MODE_PRIVATE)
return preferences.getInt(DEFAULT_FOLDER_NAME, -1)
}

fun isPasswordEnabled() : Boolean {
val preferences = context.getSharedPreferences(UI_PREFERENCE_NAME, Context.MODE_PRIVATE)
return preferences.getBoolean(PASSWORD_ENABLE_KEY, false)
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/res/layout/fragment_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@
android:layout_height="0.2dp"
android:background="@android:color/darker_gray" />

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/default_folder_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="@dimen/dimen10dp"
android:fontFamily="serif"
android:padding="@dimen/dimen10dp"
android:text="@string/default_folder"
android:textColor="@color/dark_sky"
android:textSize="@dimen/dimen20sp"
app:drawableStartCompat="@drawable/ic_folders"
app:thumbTint="@color/sky"
app:trackTint="@color/grey" />

<View
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:background="@android:color/darker_gray" />

<TextView
android:id="@+id/password_txt"
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<string name="import_export">أستيراد أو تصدير</string>
<string name="password">الرقم السري</string>
<string name="enable_password">تفعيل الرقم السري</string>
<string name="default_folder">تذكر المجلد الأخير</string>

<!--Actions-->
<string name="add_link">إضافه رابط</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<string name="import_export">Import und Export</string>
<string name="password">Passwort</string>
<string name="enable_password">Passwort aktivieren</string>
<string name="default_folder">Merken Sie sich den letzten Ordner</string>

<!--Actions-->
<string name="add_link">Link hinzufügen</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-vn/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="import_export">Nhập và xuất</string>
<string name="password">Mật khẩu</string>
<string name="enable_password">Bật mật khẩu</string>
<string name="default_folder">Nhớ thư mục cuối cùng</string>

<!--Actions-->
<string name="add_link">Thêm liên kết</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="import_export">导入和导出</string>
<string name="password">密码</string>
<string name="enable_password">启用密码</string>
<string name="default_folder">记住最后一个文件夹</string>

<!--操作-->
<string name="add_link">添加链接</string>
Expand Down
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 @@ -31,6 +31,7 @@
<string name="import_export_choose_file_type">Choose a file type</string>
<string name="password">Password</string>
<string name="enable_password">Enable password</string>
<string name="default_folder">Remember the last folder</string>

<!--Actions-->
<string name="add_link">Add Link</string>
Expand Down

0 comments on commit 75dc82b

Please sign in to comment.