Skip to content

Commit

Permalink
can read passport image from resource
Browse files Browse the repository at this point in the history
  • Loading branch information
jbsv committed Jan 22, 2024
1 parent d333161 commit 1853c31
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.epfl.dedis.hbt.data.document.Portrait
import com.epfl.dedis.hbt.service.document.DocumentService
import com.epfl.dedis.hbt.service.json.JsonService
import com.epfl.dedis.hbt.service.json.JsonType.USER_DATA
import retrofit2.awaitResponse
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -50,7 +51,7 @@ class UserDataSource @Inject constructor(
return username == usernamesKey || users.containsKey(username)
}

fun register(
suspend fun register(
username: String,
pincode: Int,
passport: String,
Expand All @@ -62,7 +63,7 @@ class UserDataSource @Inject constructor(
// create user
val user = User(username, pincode, passport, role)
val call = documentService.create(user, portrait, false)
val response = call.execute()
val response = call.awaitResponse()
if (response.errorBody() != null) {
return Result.Error(Exception("Failed to register : " + response.message()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class UserRepository @Inject constructor(private val dataSource: UserDataSource)
loggedInUser = null
}

fun register(
suspend fun register(
username: String,
pincode: String,
passport: String,
Expand Down
5 changes: 3 additions & 2 deletions android/app/src/main/java/com/epfl/dedis/hbt/di/HttpModule.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.epfl.dedis.hbt.di

import com.epfl.dedis.hbt.service.document.DocumentEndpoint
import com.epfl.dedis.hbt.service.document.DocumentService
import com.fasterxml.jackson.databind.ObjectMapper
import dagger.Module
Expand Down Expand Up @@ -30,8 +31,8 @@ object HttpModule {

@Provides
@Singleton
fun provideDocumentService(retrofit: Retrofit): DocumentService =
retrofit.create(DocumentService::class.java)
fun provideDocumentService(retrofit: Retrofit): DocumentEndpoint =
retrofit.create(DocumentEndpoint::class.java)

@Qualifier
@Retention(AnnotationRetention.BINARY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.epfl.dedis.hbt.service.document

import com.epfl.dedis.hbt.data.document.Document
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part

interface DocumentEndpoint {

@Multipart
@POST("document")
fun create(
@Part("name") name: String,
@Part("passport") passport: String,
@Part("role") role: Int,
@Part("image") image: RequestBody,
@Part("registered") registered: Boolean
): Call<Document>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,14 @@ import com.epfl.dedis.hbt.data.user.User
import okhttp3.MediaType
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
import javax.inject.Inject
import javax.inject.Singleton

interface DocumentService {

@Multipart
@POST("document")
fun create(
@Part("name") name: String,
@Part("passport") passport: String,
@Part("role") role: Int,
@Part("image") image: RequestBody,
@Part("registered") registered: Boolean
): Call<Document>
@Singleton
class DocumentService @Inject constructor(val endpoint: DocumentEndpoint) {

fun create(user: User, portrait: Portrait, registered: Boolean): Call<Document> =
create(
endpoint.create(
user.name,
user.passport,
user.role.ordinal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class PassportDataFragment : Fragment() {
)
)
}

}.root
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.coroutineScope
import com.epfl.dedis.hbt.R
import com.epfl.dedis.hbt.data.Result
import com.epfl.dedis.hbt.data.document.Portrait
import com.epfl.dedis.hbt.databinding.FragmentPassportNfcBinding
import com.epfl.dedis.hbt.service.passport.Passport
import com.epfl.dedis.hbt.service.passport.mrz.BACData
Expand All @@ -19,6 +20,7 @@ import com.epfl.dedis.hbt.ui.MainActivity
import com.epfl.dedis.hbt.ui.MainActivity.Companion.getSafeSerializable
import com.epfl.dedis.hbt.ui.NFCViewModel
import kotlinx.coroutines.launch
import java.io.FileNotFoundException

private const val USE_PERSONAL_DATA = true

Expand Down Expand Up @@ -83,7 +85,28 @@ class PassportNfcFragment : Fragment() {
}
}

return FragmentPassportNfcBinding.inflate(inflater, container, false).root
return FragmentPassportNfcBinding.inflate(inflater, container, false).apply {
skipNfc.setOnClickListener {
MainActivity.setCurrentFragment(
parentFragmentManager,
RegisterFragment.newInstance(
"10AZ000001",
"some checksum",
getMockPortrait()
)
)
}

}.root
}

fun getMockPortrait(): Portrait {
val stream = PassportNfcFragment::class.java.getResourceAsStream("/utopia.png")
?: throw FileNotFoundException()

stream.use {
return Portrait("image/png", it.readBytes())
}
}

private fun extractPersonalData(passport: Passport): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import com.epfl.dedis.hbt.data.document.Portrait
import com.epfl.dedis.hbt.data.user.Role
import com.epfl.dedis.hbt.data.user.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.runInterruptible
import javax.inject.Inject

@HiltViewModel
Expand All @@ -30,12 +35,14 @@ class RegisterViewModel @Inject constructor(private val userRepository: UserRepo
role: Role
) {
// can be launched in a separate asynchronous job
val result = userRepository.register(username, pincode, passport, role, portrait)

if (result is Success) {
_registerResult.value = RegisterResult(error = null)
} else {
_registerResult.value = RegisterResult(error = R.string.login_failed)
runBlocking {
userRepository.register(username, pincode, passport, role, portrait).collect {
if (it is Success) {
_registerResult.value = RegisterResult(success = it.data)
} else {
_registerResult.value = RegisterResult(error = R.string.login_failed)
}
}
}
}

Expand Down
24 changes: 13 additions & 11 deletions android/app/src/main/res/layout/fragment_passport_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,30 @@
android:id="@+id/validate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginVertical="@dimen/activity_vertical_margin"
android:layout_marginBottom="60dp"
android:text="@string/validate"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/manual_title"
app:layout_constraintVertical_bias="0.944" />
app:layout_constraintTop_toBottomOf="@+id/edit_expiration_date" />

<Button
android:id="@+id/automatic_scan"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="@dimen/activity_vertical_margin"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="@string/automatic_input"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@id/validate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_expiration_date" />

<EditText
android:id="@+id/edit_birth_date"
Expand All @@ -59,16 +61,15 @@
android:importantForAutofill="no"
android:inputType="date"
android:minHeight="48dp"
app:layout_constraintBottom_toTopOf="@+id/automatic_scan"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/split"
app:layout_constraintTop_toBottomOf="@+id/edit_birth_date" />

<EditText
android:id="@+id/passport_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_width="121dp"
android:layout_height="48dp"
android:autofillHints="@string/hint_passport_number"
android:hint="@string/hint_passport_number"
android:importantForAutofill="no"
Expand All @@ -78,18 +79,19 @@
app:layout_constraintBottom_toTopOf="@+id/edit_birth_date"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/pass_number"
app:layout_constraintTop_toBottomOf="@id/manual_title"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
tools:ignore="TextFields" />

<TextView
android:id="@+id/manual_title"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="@dimen/activity_vertical_margin"
android:layout_marginTop="16dp"
android:text="@string/manual_input"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand Down Expand Up @@ -119,11 +121,11 @@

<TextView
android:id="@+id/pass_number"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/passport_number"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/passport_number"
app:layout_constraintEnd_toStartOf="@+id/split"
app:layout_constraintTop_toTopOf="@+id/passport_number" />
Expand Down
10 changes: 10 additions & 0 deletions android/app/src/main/res/layout/fragment_passport_nfc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,14 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/progressBar"
app:layout_constraintVertical_bias="0.1" />

<Button
android:id="@+id/skip_nfc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/skip_nfc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<string name="expiration_date">Expiration Date :</string>
<string name="birth_date">Birth Date :</string>
<string name="validate">Validate</string>
<string name="skip_nfc">Skip Nfc</string>
<string name="manual_input">Manual Input</string>
<string name="automatic_input">Automatic Input</string>
<string name="expiration_hint">Expiration</string>
Expand Down
Binary file added android/app/src/main/resources/utopia.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import com.epfl.dedis.hbt.data.user.Role
import com.epfl.dedis.hbt.data.user.User
import com.epfl.dedis.hbt.data.user.UserDataSource
import com.epfl.dedis.hbt.di.JsonModule.provideObjectMapper
import com.epfl.dedis.hbt.service.document.DocumentEndpoint
import com.epfl.dedis.hbt.service.document.DocumentService
import com.epfl.dedis.hbt.service.document.DocumentServiceTest
import com.epfl.dedis.hbt.service.json.JsonService
import com.epfl.dedis.hbt.test.MockSharedPreferences
import kotlinx.coroutines.runBlocking
import org.hamcrest.CoreMatchers.instanceOf
import org.junit.Before
import org.junit.Test
Expand All @@ -29,7 +31,7 @@ import org.hamcrest.CoreMatchers.`is` as eq
class UserDataSourceTest {

private val jsonService = JsonService(provideObjectMapper())
private val docService = mock<DocumentService> {
private val docService = mock<DocumentEndpoint> {
on { create(any(), any(), any(), any(), any()) } doReturn mockCall(Document("ID"))
}

Expand Down Expand Up @@ -59,8 +61,10 @@ class UserDataSourceTest {
)
assertThat(dataSource.login(bob.name, bob.pincode), instanceOf(Result.Error::class.java))

dataSource.register(alice.name, alice.pincode, alice.passport, alice.role, mockPortrait)
dataSource.register(bob.name, bob.pincode, bob.passport, bob.role, mockPortrait)
runBlocking {
dataSource.register(alice.name, alice.pincode, alice.passport, alice.role, mockPortrait)
dataSource.register(bob.name, bob.pincode, bob.passport, bob.role, mockPortrait)
}

assertThat(dataSource.login(alice.name, alice.pincode), eq(Result.Success(alice)))
assertThat(dataSource.login(bob.name, bob.pincode), eq(Result.Success(bob)))
Expand All @@ -70,8 +74,10 @@ class UserDataSourceTest {
fun userDataSourceStoresUsers() {
val dataSource = UserDataSource(preferences, jsonService, docService)

dataSource.register(alice.name, alice.pincode, alice.passport, alice.role, mockPortrait)
dataSource.register(bob.name, bob.pincode, bob.passport, bob.role, mockPortrait)
runBlocking {
dataSource.register(alice.name, alice.pincode, alice.passport, alice.role, mockPortrait)
dataSource.register(bob.name, bob.pincode, bob.passport, bob.role, mockPortrait)
}

val dataSourceLoaded = UserDataSource(preferences, jsonService, docService)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DocumentServiceTest {
@Test
@Ignore("This is merely a PoC")
fun createSimpleDocumentSucceed() {
val service = HttpModule.provideDocumentService(retrofit)
val service = DocumentService(HttpModule.provideDocumentService(retrofit))

val user = User(
name = "Jon Smith",
Expand Down
Binary file added android/app/src/test/resources/utopia.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1853c31

Please sign in to comment.