fix challenge editing

This commit is contained in:
Phillip Thelen 2018-08-07 12:39:58 +02:00
parent 23435afcfc
commit 8fe091e830
7 changed files with 36 additions and 12 deletions

View file

@ -81,6 +81,11 @@
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin">
<LinearLayout
android:id="@+id/challenge_creation_views"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
style="@style/Subheader2"
@ -188,6 +193,7 @@
android:textColorHint="#61000000" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<TextView
style="@style/Subheader2"
android:layout_width="match_parent"

View file

@ -599,7 +599,7 @@
<string name="tasks">Tasks</string>
<string name="create_challenge">Create Challenge</string>
<string name="edit_challenge">Edit</string>
<string name="challenge_create_error_tavern_one_gem">You need at least 1 gem to create a challenge in Tavern.</string>
<string name="challenge_create_error_tavern_one_gem">You need at least 1 gem to create a public challenge.</string>
<string name="challenge_create_error_enough_gems">You don\'t have enough gems to create a challenge.</string>
<string name="identify_your_challenge_with_a_tag">Identify your challenge with a short name</string>
<string name="challenge_create_error_tag">You need a short name to create this Challenge.</string>

View file

@ -91,7 +91,9 @@ class ChallengeRepositoryImpl(localRepository: ChallengeLocalRepository, apiClie
var flowable: Flowable<*> = Flowable.just("")
updatedTaskList.forEach { task ->
updatedTaskList
.map { localRepository.getUnmanagedCopy(it) }
.forEach { task ->
flowable = flowable.flatMap { apiClient.updateTask(task.id ?: "", task) }
}
@ -105,6 +107,7 @@ class ChallengeRepositoryImpl(localRepository: ChallengeLocalRepository, apiClie
challenge.tasksOrder = getTaskOrders(fullTaskList)
return flowable.flatMap { apiClient.updateChallenge(challenge) }
.doOnNext { localRepository.save(challenge) }
}
override fun deleteChallenge(challengeId: String): Flowable<Void> {

View file

@ -64,6 +64,10 @@ open class Challenge : RealmObject() {
} else super.equals(other)
}
override fun hashCode(): Int {
return id?.hashCode() ?: 0
}
companion object {
const val TASK_ORDER_HABITS = "habits"

View file

@ -54,6 +54,7 @@ class ChallengeFormActivity : BaseActivity() {
private val challengeRemoveGemBtn: Button by bindView(R.id.challenge_remove_gem_btn)
private val createChallengeTaskList: RecyclerView by bindView(R.id.create_challenge_task_list)
private val gemIconView: ImageView by bindView(R.id.gem_icon)
private val challengeCreationViews: ViewGroup by bindView(R.id.challenge_creation_views)
@Inject
internal lateinit var challengeRepository: ChallengeRepository
@ -68,6 +69,7 @@ class ChallengeFormActivity : BaseActivity() {
private lateinit var locationAdapter: GroupArrayAdapter
private var challengeId: String? = null
private var groupID: String? = null
private var editMode: Boolean = false
private val addedTasks = HashMap<String, Task>()
@ -88,14 +90,18 @@ class ChallengeFormActivity : BaseActivity() {
val c = Challenge()
val locationPos = challengeLocationSpinner.selectedItemPosition
val locationGroup = locationAdapter.getItem(locationPos)
if (challengeId != null) {
c.id = challengeId
}
if (locationGroup != null) {
c.groupId = locationGroup.id
if (groupID != null) {
c.groupId = groupID
} else {
val locationGroup = locationAdapter.getItem(locationPos)
if (locationGroup != null) {
c.groupId = locationGroup.id
}
}
c.name = createChallengeTitle.text.toString()
c.description = createChallengeDescription.text.toString()
@ -318,15 +324,16 @@ class ChallengeFormActivity : BaseActivity() {
locationAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
socialRepository.getGroups("guild").subscribe(Consumer { groups ->
if (groups.first { it.id == "00000000-0000-4000-A000-000000000000" } == null) {
val mutableGroups = groups.toMutableList()
if (groups.firstOrNull { it.id == "00000000-0000-4000-A000-000000000000" } == null) {
val tavern = Group()
tavern.id = "00000000-0000-4000-A000-000000000000"
tavern.name = getString(R.string.public_challenge)
locationAdapter.add(tavern)
mutableGroups.add(0, tavern)
}
locationAdapter.clear()
locationAdapter.addAll(groups)
locationAdapter.addAll(mutableGroups)
}, RxErrorHandler.handleEmptyError())
challengeLocationSpinner.adapter = locationAdapter
@ -380,10 +387,13 @@ class ChallengeFormActivity : BaseActivity() {
private fun fillControlsByChallenge() {
challengeId.notNull {
challengeRepository.getChallenge(it).subscribe(Consumer { challenge ->
groupID = challenge.groupId
editMode = true
createChallengeTitle.setText(challenge.name)
createChallengeDescription.setText(challenge.description)
createChallengeTag.setText(challenge.shortName)
createChallengePrize.setText(challenge.prize.toString())
challengeCreationViews.visibility = View.GONE
for (i in 0 until locationAdapter.count) {
val group = locationAdapter.getItem(i)
@ -397,7 +407,7 @@ class ChallengeFormActivity : BaseActivity() {
}, RxErrorHandler.handleEmptyError())
challengeRepository.getChallengeTasks(it).subscribe(Consumer { tasks ->
tasks.forEach { task ->
addOrUpdateTaskInList(task)
addOrUpdateTaskInList(task, true)
}
}, RxErrorHandler.handleEmptyError())
}
@ -454,7 +464,7 @@ class ChallengeFormActivity : BaseActivity() {
)
}
private fun addOrUpdateTaskInList(task: Task) {
private fun addOrUpdateTaskInList(task: Task, isExistingTask: Boolean = false) {
if (!challengeTasks.replaceTask(task)) {
val taskAbove: Task? = when (task.type) {
Task.TYPE_HABIT -> addHabit
@ -465,7 +475,7 @@ class ChallengeFormActivity : BaseActivity() {
challengeTasks.addTaskUnder(task, taskAbove)
if (editMode) {
if (editMode && !isExistingTask) {
addedTasks[task.id ?: ""] = task
}
} else {

View file

@ -87,6 +87,7 @@ class ChallengeDetailFragment: BaseMainFragment() {
.map {
return@map (it.leaderId ?: "")
}
.filter { it.isNotEmpty() }
.flatMap { creatorID ->
return@flatMap socialRepository.getMember(creatorID)
}

View file

@ -24,7 +24,7 @@ class ChallengeDeserializer : JsonDeserializer<Challenge>, JsonSerializer<Challe
challenge.id = jsonObject.get("id").asString
challenge.name = jsonObject.get("name").asString
if (jsonObject.has("sortName")) {
if (jsonObject.has("shortName")) {
challenge.shortName = jsonObject.get("shortName").asString
}
challenge.description = jsonObject.get("description").asString