Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feature/general_en…
Browse files Browse the repository at this point in the history
…hancements

# Conflicts:
#	app/src/main/java/com/red_velvet_cake/dailytodo/ui/team_todo_details/TeamTodoDetailsFragment.kt
  • Loading branch information
Mohammed-Alhams committed Apr 20, 2023
2 parents d6a1d4b + 9ed4d7c commit 2fd44f2
Show file tree
Hide file tree
Showing 36 changed files with 485 additions and 289 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.red_velvet_cake.dailytodo.data.local

class LocalDataImpl : LocalData {
class LocalDataImpl() : LocalData {
override fun getUserToken(): String? {
return SharedPrefs.token
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.red_velvet_cake.dailytodo.data.model

import com.google.gson.annotations.SerializedName

data class CreateTodoTeamResponseBody(
val id: String,
val title: String,
val description: String,
val assignee: String,
val status: Int,
val creationTime: String
@SerializedName("id") val id: String,
@SerializedName("title") val title: String,
@SerializedName("description") val description: String,
@SerializedName("assignee") val assignee: String,
@SerializedName("status") val status: Int,
@SerializedName("creationTime") val creationTime: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ interface AuthenticationService {
username: String,
password: String,
onSuccess: (response: LoginResponse) -> Unit,
onFailure: (errorMessage: String) -> Unit,
onFailure: (exception: Exception) -> Unit,
)

fun registerAccount(
username: String,
password: String,
teamId: String,
onSuccess: (response: RegisterAccountResponse) -> Unit,
onFailure: (errorMessage: String) -> Unit,
onFailure: (exception: Exception) -> Unit,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package com.red_velvet_cake.dailytodo.data.remote.auth
import com.google.gson.Gson
import com.red_velvet_cake.dailytodo.data.model.LoginResponse
import com.red_velvet_cake.dailytodo.data.model.RegisterAccountResponse
import com.red_velvet_cake.dailytodo.utils.Constants.HOST
import com.red_velvet_cake.dailytodo.data.remote.util.HttpMethod
import com.red_velvet_cake.dailytodo.data.remote.util.buildRequest
import com.red_velvet_cake.dailytodo.utils.Constants.HOST_NAME
import com.red_velvet_cake.dailytodo.utils.Constants.SCHEME
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Credentials
import okhttp3.FormBody
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
Expand All @@ -33,30 +34,34 @@ class AuthenticationServiceImpl : AuthenticationService {
username: String,
password: String,
onSuccess: (response: LoginResponse) -> Unit,
onFailure: (errorMessage: String) -> Unit
onFailure: (exception: Exception) -> Unit
) {
val url = HttpUrl.Builder()
.scheme(SCHEME)
.host(HOST)
.addPathSegment(PATH_LOGIN)
.host(HOST_NAME)
.addPathSegment(LOGIN_PATH)
.build()

val authHeaderValue = Credentials.basic(username, password)

val request = Request.Builder()
.url(url)
.header(HEADER_AUTHORIZATION, authHeaderValue)
.header(AUTHORIZATION_HEADER, authHeaderValue)
.build()

client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
onFailure(e.message.toString())
override fun onFailure(call: Call, ioException: IOException) {
onFailure(ioException)
}

override fun onResponse(call: Call, response: Response) {
response.body?.string()?.let { responseBody ->
val loginResponse = Gson().fromJson(responseBody, LoginResponse::class.java)
onSuccess(loginResponse)
try {
response.body?.string()?.let { responseBody ->
val loginResponse = Gson().fromJson(responseBody, LoginResponse::class.java)
onSuccess(loginResponse)
}
} catch (exception: Exception) {
onFailure(exception)
}
}
})
Expand All @@ -67,47 +72,44 @@ class AuthenticationServiceImpl : AuthenticationService {
password: String,
teamId: String,
onSuccess: (response: RegisterAccountResponse) -> Unit,
onFailure: (errorMessage: String) -> Unit,
onFailure: (exception: Exception) -> Unit,
) {
val formBody = FormBody.Builder()
.add(USERNAME, username)
.add(PASSWORD, password)
.add(TEAM_ID, teamId)
.build()

val url = HttpUrl
.Builder()
.scheme(SCHEME)
.host(HOST)
.addPathSegment(REGISTER_PATH)
.build()

val request = Request.Builder()
.url(url)
.post(formBody)
.build()

client.newCall(request)
val apiRequest = buildRequest(
HOST_NAME,
SIGNUP_PATH,
HttpMethod.POST,
USERNAME to username,
PASSWORD to password,
TEAM_ID to teamId
)

client.newCall(apiRequest)
.enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
onFailure(e.message.toString())
override fun onFailure(call: Call, ioException: IOException) {
onFailure(ioException)
}

override fun onResponse(call: Call, response: Response) {
val body = response.body?.string()
val result = gson.fromJson(body, RegisterAccountResponse::class.java)
onSuccess(result)
try {
response.body?.string().let {
val result = gson.fromJson(it, RegisterAccountResponse::class.java)
onSuccess(result)
}

} catch (exception: Exception) {
onFailure(exception)
}
}
})
}


companion object {
private const val REGISTER_PATH = "signup"
private const val USERNAME = "username"
private const val PASSWORD = "password"
private const val TEAM_ID = "teamId"
private const val PATH_LOGIN = "login"
private const val HEADER_AUTHORIZATION = "Authorization"
private const val SIGNUP_PATH = "signup"
private const val LOGIN_PATH = "login"
private const val AUTHORIZATION_HEADER = "Authorization"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.red_velvet_cake.dailytodo.data.remote.todo_service

import com.red_velvet_cake.dailytodo.data.local.LocalDataImpl
import com.red_velvet_cake.dailytodo.data.remote.util.CustomException
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
Expand All @@ -17,10 +18,16 @@ class AuthorizationInterceptor : Interceptor {
.addHeader(HEADER_AUTHORIZATION, authHeaderValue)
.build()

return chain.proceed(newRequest)
return chain.proceed(newRequest).also { response ->
if (response.code == UNAUTHORIZED_STATUS_CODE) {
throw CustomException.UnauthorizedUserException(response.message)
}
}
}


companion object {
private const val UNAUTHORIZED_STATUS_CODE = 401
private const val HEADER_AUTHORIZATION = "Authorization"
private const val BEARER = "Bearer"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,43 @@ import com.red_velvet_cake.dailytodo.data.model.CreateTodoPersonalResponse
import com.red_velvet_cake.dailytodo.data.model.CreateTodoTeamResponse
import com.red_velvet_cake.dailytodo.data.model.GetAllPersonalTodosResponse
import com.red_velvet_cake.dailytodo.data.model.GetAllTeamTodosResponse
import com.red_velvet_cake.dailytodo.data.model.LoginResponse
import com.red_velvet_cake.dailytodo.data.model.RegisterAccountResponse
import okio.IOException

interface TodoService {

fun createPersonalTodo(
title: String,
description: String,
onCreatePersonalTodoSuccess: (CreateTodoPersonalResponse) -> Unit,
onCreatePersonalTodoFailure: (errorMessage: String) -> Unit
onSuccess: (CreateTodoPersonalResponse) -> Unit,
onFailure: (exception: Exception) -> Unit
)

fun createTeamTodo(
title: String,
description: String,
assignee: String,
onCreateTeamTodoSuccess: (CreateTodoTeamResponse) -> Unit,
onCreateTeamTodoFailure: (errorMessage: String) -> Unit
onSuccess: (CreateTodoTeamResponse) -> Unit,
onFailure: (exception: Exception) -> Unit
)

fun updatePersonalTodoStatus(
todoId: String,
newTodoStatus: Int,
onUpdatePersonalTodoStatusFailure: (errorMessage: String) -> Unit
onFailure: (exception: Exception) -> Unit
)

fun updateTeamTodoStatus(
todoId: String,
newTodoStatus: Int,
onUpdateTeamTodoStatusFailure: (errorMessage: String) -> Unit
onFailure: (exception: Exception) -> Unit
)

fun getAllPersonalTodos(
onGetAllPersonalTodosSuccess: (getAllPersonalTodosResponse: GetAllPersonalTodosResponse) -> Unit,
onGetAllPersonalTodoFailure: (errorMessage: String) -> Unit
onSuccess: (getAllPersonalTodosResponse: GetAllPersonalTodosResponse) -> Unit,
onFailure: (exception: Exception) -> Unit
)

fun getAllTeamTodos(
onGetAllTeamTodosSuccess: (getAllTeamTodosResponse: GetAllTeamTodosResponse) -> Unit,
onGetAllTeamTodosFailure: (errorMessage: String) -> Unit,
onSuccess: (getAllTeamTodosResponse: GetAllTeamTodosResponse) -> Unit,
onFailure: (exception: Exception) -> Unit,
)
}
Loading

0 comments on commit 2fd44f2

Please sign in to comment.