Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created remember the last used folder option #57

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 Auto saving
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, Fix this comment to be // 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/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 used folder as default one</string>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to find translations for other langs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should I use google translate to translate this line to all the other languages?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should I use google translate to translate this line to all the other languages?

For now yes, and can we make it shorter than Remember the last used folder as default one, hmmm maybe Enable Remember Folder or Remember last folder what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what's right, "Remember the last folder" in my opinion would be fine

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great let's go with it


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