Skip to content

Commit

Permalink
feat(settings): add "Add bookmark automatically" option
Browse files Browse the repository at this point in the history
  • Loading branch information
DesarrolloAntonio committed Aug 1, 2024
1 parent 84a28fc commit 9f9a361
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 101 deletions.
11 changes: 0 additions & 11 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ interface SettingsPreferenceDataSource {
suspend fun getIsLegacyApi(): Boolean
suspend fun getCompactView(): Boolean
suspend fun setCompactView(isCompactView: Boolean)
suspend fun getAutoAddBookmark(): Boolean
suspend fun setAutoAddBookmark(isAutoAddBookmark: Boolean)
suspend fun getCategoriesVisible(): Boolean
suspend fun setCategoriesVisible(isCategoriesVisible: Boolean)
suspend fun setSelectedCategories(categories: List<String>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SettingsPreferencesDataSourceImpl(

val THEME_MODE_KEY = stringPreferencesKey("theme_mode")
val COMPACT_VIEW = booleanPreferencesKey("compact_view")
val AUTO_ADD_BOOKMARK = booleanPreferencesKey("auto_add_bookmark")
val CATEGORIES_VISIBLE = booleanPreferencesKey("categories_visible")
val SELECTED_CATEGORIES_KEY = stringPreferencesKey("selected_categories")
val USE_DYNAMIC_COLORS = booleanPreferencesKey("use_dynamic_colors")
Expand Down Expand Up @@ -271,4 +272,13 @@ class SettingsPreferencesDataSourceImpl(
}
}

override suspend fun setAutoAddBookmark(isAutoAddBookmark: Boolean) {
dataStore.edit { preferences ->
preferences[AUTO_ADD_BOOKMARK] = isAutoAddBookmark
}
}
override suspend fun getAutoAddBookmark(): Boolean = runBlocking {
dataStore.data.firstOrNull()?.get(AUTO_ADD_BOOKMARK) ?: false
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.mutablePreferencesOf
import androidx.datastore.preferences.core.preferencesOf
import com.desarrollodroide.data.RememberUserPreferences
import com.desarrollodroide.data.UserPreferences
Expand All @@ -23,6 +24,7 @@ import kotlinx.coroutines.flow.first
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertNull
import org.mockito.kotlin.any
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.verify

@ExperimentalCoroutinesApi
Expand All @@ -37,6 +39,7 @@ class SettingsPreferencesDataSourceImplTest {
private val COMPACT_VIEW_KEY = booleanPreferencesKey("compact_view")
private val CATEGORIES_VISIBLE_KEY = booleanPreferencesKey("categories_visible")
private val SELECTED_CATEGORIES_KEY = stringPreferencesKey("selected_categories")
private val AUTO_ADD_BOOKMARK_KEY = booleanPreferencesKey("auto_add_bookmark")

@BeforeEach
fun setUp() {
Expand All @@ -60,7 +63,7 @@ class SettingsPreferencesDataSourceImplTest {
fun `setThemeMode updates theme mode to DARK`() = runTest {
val themeMode = ThemeMode.DARK
settingsPreferencesDataSourceImpl.setTheme(themeMode)
verify(preferencesDataStore).edit(any())
verifyPreferenceEdit(preferencesDataStore, THEME_MODE_KEY, themeMode.name)
}

@Test
Expand Down Expand Up @@ -91,7 +94,7 @@ class SettingsPreferencesDataSourceImplTest {
fun `setCompactView updates compact view to false`() = runTest {
val compactView = false
settingsPreferencesDataSourceImpl.setCompactView(compactView)
verify(preferencesDataStore).edit(any())
verifyPreferenceEdit(preferencesDataStore, COMPACT_VIEW_KEY, compactView)
}

@Test
Expand All @@ -106,7 +109,7 @@ class SettingsPreferencesDataSourceImplTest {
fun `setCategoriesVisible updates categories visible to false`() = runTest {
val categoriesVisible = false
settingsPreferencesDataSourceImpl.setCategoriesVisible(categoriesVisible)
verify(preferencesDataStore).edit(any())
verifyPreferenceEdit(preferencesDataStore, CATEGORIES_VISIBLE_KEY, categoriesVisible)
}

@Test
Expand Down Expand Up @@ -194,7 +197,7 @@ class SettingsPreferencesDataSourceImplTest {
fun `setSelectedCategories updates selected categories correctly`() = runTest {
val selectedCategories = listOf("Sports", "Technology")
settingsPreferencesDataSourceImpl.setSelectedCategories(selectedCategories)
verify(preferencesDataStore).edit(any())
verifyPreferenceEdit(preferencesDataStore, SELECTED_CATEGORIES_KEY, selectedCategories.joinToString(","))
}

@Test
Expand Down Expand Up @@ -324,4 +327,39 @@ class SettingsPreferencesDataSourceImplTest {
assertNull(actualTag)
}

@Test
fun `getAutoAddBookmark retrieves the correct value`() = runTest {
val expectedValue = true
whenever(preferencesDataStore.data).thenReturn(flowOf(preferencesOf(AUTO_ADD_BOOKMARK_KEY to expectedValue)))
val actualValue = settingsPreferencesDataSourceImpl.getAutoAddBookmark()
assertEquals(expectedValue, actualValue)
}

@Test
fun `getAutoAddBookmark returns false when preference is not set`() = runTest {
whenever(preferencesDataStore.data).thenReturn(flowOf(preferencesOf()))
val actualValue = settingsPreferencesDataSourceImpl.getAutoAddBookmark()
assertEquals(false, actualValue)
}

@Test
fun `setAutoAddBookmark updates auto add bookmark preference correctly`() = runTest {
val newValue = true
settingsPreferencesDataSourceImpl.setAutoAddBookmark(newValue)
verifyPreferenceEdit(preferencesDataStore, AUTO_ADD_BOOKMARK_KEY, newValue)
}

}

private suspend fun <T> verifyPreferenceEdit(
preferencesDataStore: DataStore<Preferences>,
key: Preferences.Key<T>,
expectedValue: T
) {
val argumentCaptor = argumentCaptor<suspend (Preferences) -> Preferences>()
verify(preferencesDataStore).updateData(argumentCaptor.capture())

val preferences = mutablePreferencesOf()
val updatedPreferences = argumentCaptor.firstValue(preferences)
assertEquals(expectedValue, updatedPreferences[key])
}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ android.nonTransitiveRClass=true
compileSdkVersion=34
minSdkVersion=21
targetSdkVersion=34
versionCode=44
versionName=1.34
versionCode=45
versionName=1.35

android.defaults.buildfeatures.buildconfig=true
android.nonFinalResIds=false
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.lifecycle.lifecycleScope
import com.desarrollodroide.pagekeeper.ui.theme.ShioriTheme
import com.desarrollodroide.model.Bookmark
import com.desarrollodroide.pagekeeper.MainActivity
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.viewModel

class BookmarkEditorActivity : ComponentActivity() {
Expand All @@ -31,38 +33,52 @@ class BookmarkEditorActivity : ComponentActivity() {
}
}
}
setContent {
ShioriTheme {
if (bookmarkViewModel.userHasSession()) {
Surface(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.inverseOnSurface)
) {
BookmarkEditorScreen(
title = "Add",
bookmarkEditorType = BookmarkEditorType.ADD,
bookmark = Bookmark(
url = sharedUrl,
tags = emptyList(),
public = if (bookmarkViewModel.getMakeArchivePublic()) 1 else 0,
createArchive = bookmarkViewModel.getCreateArchive(),
createEbook = bookmarkViewModel.getCreateEbook()
),
onBackClick = {
Toast.makeText(this@BookmarkEditorActivity, "Bookmark saved", Toast.LENGTH_LONG).show()
finish()
},
updateBookmark = { },
startMainActivity = { startMainActivity() }
)
}
} else {
NotSessionScreen(
onClickLogin = {
startMainActivity()
lifecycleScope.launch {
bookmarkViewModel.toastMessage.collect { message ->
message?.let {
Toast.makeText(this@BookmarkEditorActivity, it, Toast.LENGTH_LONG).show()
finish()
}
}
}
lifecycleScope.launch {
if (bookmarkViewModel.getAutoAddBookmark()) {
bookmarkViewModel.autoAddBookmark(sharedUrl)
} else {
setContent {
ShioriTheme {
if (bookmarkViewModel.userHasSession()) {
Surface(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.inverseOnSurface)
) {
BookmarkEditorScreen(
title = "Add",
bookmarkEditorType = BookmarkEditorType.ADD,
bookmark = Bookmark(
url = sharedUrl,
tags = emptyList(),
public = if (bookmarkViewModel.getMakeArchivePublic()) 1 else 0,
createArchive = bookmarkViewModel.getCreateArchive(),
createEbook = bookmarkViewModel.getCreateEbook()
),
onBack = { finish() },
updateBookmark = { finish() },
showToast = { message ->
Toast.makeText(this@BookmarkEditorActivity, message, Toast.LENGTH_LONG).show()
},
startMainActivity = { startMainActivity() }
)
}
} else {
NotSessionScreen(
onClickLogin = {
startMainActivity()
}
)
}
)
}
}
}
}
Expand All @@ -83,3 +99,4 @@ class BookmarkEditorActivity : ComponentActivity() {




Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ fun BookmarkEditorScreen(
title: String,
bookmarkEditorType: BookmarkEditorType,
bookmark: Bookmark,
onBackClick: () -> Unit,
onBack: () -> Unit,
updateBookmark: (Bookmark) -> Unit,
showToast: (String) -> Unit = {},
startMainActivity: () -> Unit = {}
) {
) {
val bookmarkViewModel = get<BookmarkViewModel>()
val newTag = remember { mutableStateOf("") }
val availableTags = bookmarkViewModel.availableTags.collectAsState()
val bookmarkUiState = bookmarkViewModel.bookmarkUiState.collectAsState().value

BackHandler {
onBackClick()
onBack()
}
if (bookmarkUiState.isLoading) {
Log.v("BookmarkEditorScreen", "isLoading")
Expand Down Expand Up @@ -85,7 +86,7 @@ fun BookmarkEditorScreen(
}
}
},
onBackClick = onBackClick,
onBackClick = onBack,
createArchive = createArchive,
makeArchivePublic = makeArchivePublic,
createEbook = createEbook,
Expand All @@ -94,7 +95,7 @@ fun BookmarkEditorScreen(

if (bookmarkUiState.data != null) {
updateBookmark(bookmarkUiState.data)
onBackClick()
showToast("Bookmark saved")
}
}

Expand Down
Loading

0 comments on commit 9f9a361

Please sign in to comment.