Skip to content

Commit

Permalink
Remove unnecessary mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
kamerok committed Sep 1, 2024
1 parent 217e585 commit 01274b9
Showing 1 changed file with 26 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import easydone.service.trello.api.TrelloApi
import easydone.service.trello.api.model.Card
import easydone.service.trello.api.model.NestedBoard
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import java.time.LocalDate
import java.time.format.DateTimeFormatter
Expand All @@ -25,8 +23,6 @@ class TrelloRemoteDataSource(

private val authInfoHolder: AuthInfoHolder = AuthInfoHolder(prefs)

private val syncMutex = Mutex(false)

override suspend fun isConnected(): Boolean =
authInfoHolder.getToken() != null && authInfoHolder.getBoardId() != null

Expand All @@ -53,7 +49,7 @@ class TrelloRemoteDataSource(
val type = when (card.idList) {
authInfoHolder.getInboxListId() -> Task.Type.Inbox
authInfoHolder.getWaitingListId() -> Task.Type.Waiting(
//todo: handle waiting items with no data
//todo: handle waiting items with no date
requireNotNull(card.due)
.let { LocalDate.parse(it, DateTimeFormatter.ISO_DATE_TIME) }
)
Expand All @@ -72,41 +68,36 @@ class TrelloRemoteDataSource(

override suspend fun updateTask(delta: TaskDelta) {
withContext(Dispatchers.IO) {
syncMutex.withLock {
val serverId: String = requireNotNull(
idMappings.getString(delta.taskId)
) { "Try to update task with $delta but server id was not found" }
api.editCard(
serverId,
apiKey,
authInfoHolder.getToken()!!,
name = delta.title,
desc = delta.description,
closed = delta.isDone,
due = delta.getDueDate(),
listId = delta.type?.let { getListId(it) },
idLabels = delta.convertMarkersToLabels()
)

}
val serverId: String = requireNotNull(
idMappings.getString(delta.taskId)
) { "Try to update task with $delta but server id was not found" }
api.editCard(
serverId,
apiKey,
authInfoHolder.getToken()!!,
name = delta.title,
desc = delta.description,
closed = delta.isDone,
due = delta.getDueDate(),
listId = delta.type?.let { getListId(it) },
idLabels = delta.convertMarkersToLabels()
)
}
}

override suspend fun createTask(delta: TaskDelta) {
withContext(Dispatchers.IO) {
syncMutex.withLock {
val card = api.postCard(
listId = getListId(delta.type!!),
name = delta.title!!,
desc = delta.description,
due = delta.getDueDate(),
apiKey = apiKey,
token = authInfoHolder.getToken()!!,
idLabels = delta.convertMarkersToLabels()
)
idMappings.putString(delta.taskId, card.id)
idMappings.putString(card.id, delta.taskId)
}
val card = api.postCard(
listId = getListId(delta.type!!),
name = delta.title!!,
desc = delta.description,
due = delta.getDueDate(),
apiKey = apiKey,
token = authInfoHolder.getToken()!!,
idLabels = delta.convertMarkersToLabels()
)
idMappings.putString(delta.taskId, card.id)
idMappings.putString(card.id, delta.taskId)
}
}

Expand Down

0 comments on commit 01274b9

Please sign in to comment.