Several changes #29

Closed
nilsbraden wants to merge 5 commits from nilsbraden/nextcloud-cookbook:master into master
  1. 4
      app/build.gradle
  2. 54
      app/src/main/java/de/micmun/android/nextcloudcookbook/data/RecipeRepository.kt
  3. 1
      app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipelist/RecipeListViewModel.kt
  4. 3
      app/src/main/java/de/micmun/android/nextcloudcookbook/util/JsonRecipeParser.kt
  5. 12
      build.gradle

4
app/build.gradle

@ -5,13 +5,13 @@ apply plugin: "androidx.navigation.safeargs.kotlin"
apply plugin: "kotlin-kapt"
android {
compileSdkVersion 29
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "de.micmun.android.nextcloudcookbook"
minSdkVersion 23
targetSdkVersion 29
targetSdkVersion 30
versionCode 140
versionName "1.4.0"

54
app/src/main/java/de/micmun/android/nextcloudcookbook/data/RecipeRepository.kt

@ -23,12 +23,20 @@ import java.util.stream.Collectors
*/
class RecipeRepository {
private val _recipeList = mutableListOf<Recipe>()
val recipeList: List<Recipe>
get() = _recipeList
var recipeList: List<Recipe>
@Synchronized get() = _recipeList
@Synchronized private set(value) {
_recipeList.clear()
_recipeList.addAll(value)
}
private val _recipeMap = mutableMapOf<Long, Recipe>()
private val _recipeCategories = mutableSetOf<String>()
val recipeCategories: Set<String>
get() = _recipeCategories
var recipeCategories: Set<String>
@Synchronized get() = _recipeCategories
@Synchronized private set(value) {
_recipeCategories.clear()
_recipeCategories.addAll(value)
}
companion object {
@Volatile
@ -51,11 +59,11 @@ class RecipeRepository {
fun getRecipeWithId(id: Long) = _recipeMap[id]
fun getCategoryTitle(id: Int): String {
return _recipeCategories.first { c -> c.hashCode() == id }
return recipeCategories.first { c -> c.hashCode() == id }
}
fun filterRecipesWithCategory(id: Int): List<Recipe> {
return _recipeList.filter { recipe ->
return recipeList.filter { recipe ->
if (recipe.recipeCategory == null) {
false
} else {
@ -70,7 +78,7 @@ class RecipeRepository {
}
fun filterRecipesUncategorized(): List<Recipe> {
return _recipeList.filter { recipe ->
return recipeList.filter { recipe ->
recipe.recipeCategory.isNullOrEmpty()
}
}
@ -85,13 +93,14 @@ class RecipeRepository {
val subdirs = dir.listFiles()
var id: Long = 1
if (subdirs != null && subdirs.isNotEmpty()) {
_recipeList.clear()
}
val tmpRecipeList = recipeList.toMutableList()
val tmpCategories = mutableSetOf<String>()
val tmpRecipeMap = mutableMapOf<Long, Recipe>()
if (subdirs != null && subdirs.isNotEmpty()) {
tmpRecipeList.clear()
}
subdirs?.forEach { sd ->
if (sd.exists() && sd.isDirectory) {
val jsonFiles = sd.listFiles()?.filter { f -> f.name.endsWith(".json") }
@ -111,9 +120,10 @@ class RecipeRepository {
recipe.recipeCategory = emptyArray()
recipe.thumbImage = if (thumbFile.exists()) Uri.fromFile(thumbFile) else null
recipe.imageUrl = if (fullFile.exists()) Uri.fromFile(fullFile).toString() else ""
recipe.imageUrl =
if (fullFile.exists()) Uri.fromFile(fullFile).toString() else ""
recipe.recipeId = id++
_recipeList.add(recipe)
tmpRecipeList.add(recipe)
tmpRecipeMap[recipe.recipeId] = recipe
val categories = recipe.recipeCategory
@ -129,14 +139,18 @@ class RecipeRepository {
}
}
}
if (tmpCategories.isNotEmpty())
_recipeCategories.clear()
_recipeCategories.addAll(tmpCategories.toSortedSet())
if (tmpRecipeMap.isNotEmpty())
_recipeMap.clear()
_recipeMap.putAll(tmpRecipeMap)
if (tmpCategories.isNotEmpty()) {
recipeCategories = tmpCategories.toSortedSet()
}
if (tmpRecipeMap.isNotEmpty()) {
synchronized(_recipeMap) {
_recipeMap.clear()
_recipeMap.putAll(tmpRecipeMap)
}
}
recipeList = tmpRecipeList
}
return _recipeList
return recipeList
}
/**

1
app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipelist/RecipeListViewModel.kt

@ -81,6 +81,7 @@ class RecipeListViewModel(application: Application) : AndroidViewModel(applicati
// categories in menu
val categories = getCategoriesFromRepo()
_categoryFilter.value?.let { it1 -> setCategoryFilter(it1) }
var order = 1
menu.removeGroup(R.id.menu_categories_group)

3
app/src/main/java/de/micmun/android/nextcloudcookbook/util/JsonRecipeParser.kt

@ -28,12 +28,13 @@ class JsonRecipeParser(private val jsonStr: String) {
val mapper = jacksonObjectMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
return try {
mapper.readValue(jsonStr)
} catch (e: JsonMappingException) {
Log.e("JsonRecipeParser", "Exception: ${e.localizedMessage}")
Log.e("JsonRecipeParser", "Exception: ${e.localizedMessage}", e)
null
}
}

12
build.gradle

@ -2,17 +2,17 @@
buildscript {
ext {
kotlin_version = '1.4.10'
version_navigation = '2.3.1'
kotlin_version = '1.4.31'
version_navigation = '2.3.4'
version_lifecycle_extensions = '2.2.0'
version_coroutine = '1.3.7'
version_coroutine = '1.3.8'
version_preference = '1.1.1'
version_jackson = '2.11.0'
version_recyclerview = '1.2.0-alpha06'
version_recyclerview = '1.2.0-rc01'
version_cardview = '1.0.0'
version_constraintlayout = '2.0.4'
version_appcompat = '1.2.0'
version_material = '1.3.0-alpha03'
version_material = '1.3.0'
version_viewpager2 = '1.1.0-alpha01'
}
@ -21,7 +21,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:4.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$version_navigation"
}

Loading…
Cancel
Save