Skip to content

Commit 0433cbd

Browse files
authored
Complete profile feature implemented
Implemented #33: Complete profile feature implemented
2 parents 53b082b + 14cfb04 commit 0433cbd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+903
-188
lines changed

apikey.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
API_KEY="replace this with your api key"

app/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ plugins {
66
id 'dagger.hilt.android.plugin'
77
}
88

9+
Properties properties = new Properties()
10+
properties.load(project.rootProject.file('apikey.properties').newDataInputStream())
11+
def apiKey = properties.getProperty('API_KEY')
12+
913
android {
1014
compileSdkVersion 29
1115
buildToolsVersion "30.0.2"
@@ -17,6 +21,8 @@ android {
1721
versionCode 1
1822
versionName "1.0"
1923

24+
buildConfigField "String", "API_KEY", apiKey
25+
2026
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2127
}
2228
buildFeatures {
@@ -51,6 +57,8 @@ dependencies {
5157
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
5258
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
5359

60+
testImplementation "androidx.arch.core:core-testing:2.1.0"
61+
5462
implementation "androidx.room:room-runtime:$versions.room"
5563
kapt "androidx.room:room-compiler:$versions.room"
5664

@@ -98,4 +106,6 @@ dependencies {
98106

99107
implementation 'com.github.florent37:inline-activity-result-kotlin:1.0.4'
100108
implementation 'androidx.documentfile:documentfile:1.0.1'
109+
110+
testImplementation "com.google.truth:truth:$versions.truth"
101111
}

app/src/main/java/com/github/code/gambit/backgroundtask/FileUploadWorker.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import retrofit2.Retrofit
2525
import retrofit2.converter.gson.GsonConverterFactory
2626
import timber.log.Timber
2727
import java.io.File
28+
import kotlin.math.pow
2829

2930
class FileUploadWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx, params) {
3031

@@ -39,15 +40,17 @@ class FileUploadWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker
3940
val filePath: String = inputData.getString(AppConstant.Worker.FILE_URI_KEY)!!
4041
val fileName: String = inputData.getString(AppConstant.Worker.FILE_NAME_KEY)!!
4142
val fileSize: Int = inputData.getInt(AppConstant.Worker.FILE_SIZE_KEY, -1)
42-
makeStatusNotification(1, "New file", "Uploading file $fileName of size $fileSize mb", true)
43+
val uid = System.currentTimeMillis().toInt()
44+
val size = String.format("%.2f", fileSize.div(10.0.pow(6.0)))
45+
makeStatusNotification(uid, "New file", "Uploading file $fileName of size $size MB", true)
4346
val file = File(filePath)
4447
val res = InfuraIPFS().add.file(file, fileName, fileName).Hash
4548
// val res = "test-hash-to-avoid-unnecessary-server-call"
4649
Timber.tag("out").i(res)
4750
val fileService = getFileService()
4851
val task = fileService.uploadFile(FileNetworkEntity("", "", res, fileName, fileSize, fileName.split(".")[1]))
4952
val data = workDataOf(AppConstant.Worker.FILE_OUTPUT_KEY to task.toString())
50-
makeStatusNotification(1, "File Uploaded", task.toString(), false)
53+
makeStatusNotification(uid, "File Uploaded", task.toString(), false)
5154
return Result.success(data)
5255
}
5356

app/src/main/java/com/github/code/gambit/data/entity/network/UserNetworkEntity.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ class UserNetworkEntity(
1313
@Expose
1414
var sk: String,
1515

16-
@SerializedName("name")
17-
@Expose
18-
var name: String,
19-
2016
@SerializedName("type")
2117
@Expose
2218
var type: String,
@@ -25,7 +21,4 @@ class UserNetworkEntity(
2521
@Expose
2622
var storageUsed: Int,
2723

28-
@SerializedName("thumbnail")
29-
@Expose
30-
var thumbnail: String
3124
)

app/src/main/java/com/github/code/gambit/data/mapper/aws/UserAttributeMapper.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ constructor() : EntityMapper<List<AuthUserAttribute>, User> {
2121
AuthUserAttributeKey.email() -> {
2222
user.email = it.value
2323
}
24-
AuthUserAttributeKey.custom("custom:profile_image") -> {
25-
user.thumbnail = it.value
26-
}
2724
}
2825
}
2926
return user
@@ -32,8 +29,7 @@ constructor() : EntityMapper<List<AuthUserAttribute>, User> {
3229
override fun mapToEntity(domainModel: User): List<AuthUserAttribute> {
3330
return listOf(
3431
AuthUserAttribute(AuthUserAttributeKey.email(), domainModel.email),
35-
AuthUserAttribute(AuthUserAttributeKey.name(), domainModel.name),
36-
AuthUserAttribute(AuthUserAttributeKey.custom("custom:profile_image"), domainModel.thumbnail)
32+
AuthUserAttribute(AuthUserAttributeKey.name(), domainModel.name)
3733
)
3834
}
3935

app/src/main/java/com/github/code/gambit/data/mapper/network/UserNetworkMapper.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ constructor() : EntityMapper<UserNetworkEntity, User> {
1111

1212
override fun mapFromEntity(entity: UserNetworkEntity): User {
1313
return User(
14-
name = entity.name,
15-
email = entity.pk.split("#")[2],
14+
id = entity.pk.split("#")[1],
15+
"",
16+
"",
1617
type = entity.type,
17-
thumbnail = entity.thumbnail,
1818
storageUsed = entity.storageUsed
1919
)
2020
}
2121

2222
override fun mapToEntity(domainModel: User): UserNetworkEntity {
2323
return UserNetworkEntity(
24-
pk = "USER#${domainModel.email}",
24+
pk = "USER#${domainModel.id}",
2525
sk = "METADATA",
26-
name = domainModel.name,
2726
type = domainModel.type,
28-
storageUsed = domainModel.storageUsed,
29-
thumbnail = domainModel.thumbnail
27+
storageUsed = domainModel.storageUsed
3028
)
3129
}
3230

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
package com.github.code.gambit.data.model
22

3+
import java.lang.IndexOutOfBoundsException
4+
35
data class User(
6+
var id: String,
47
var name: String,
58
var email: String,
69
var type: String,
7-
var thumbnail: String,
810
var storageUsed: Int
9-
)
11+
) {
12+
13+
companion object {
14+
fun fromString(data: String): User {
15+
val arr = data.split("\n")
16+
if (arr.size < 5) {
17+
throw IndexOutOfBoundsException("Invalid data string")
18+
}
19+
return User(arr[0], arr[1], arr[2], arr[3], arr[4].toInt())
20+
}
21+
22+
fun merge(user1: User, user2: User): User {
23+
return User("", "", "", "", 0).let {
24+
for (i in 1..5) {
25+
when (i) {
26+
1 -> it.id = if (user1.id != "") user1.id else user2.id
27+
2 -> it.name = if (user1.name != "") user1.name else user2.name
28+
3 -> it.email = if (user1.email != "") user1.email else user2.email
29+
4 -> it.type = if (user1.type != "") user1.type else user2.type
30+
5 -> it.storageUsed = if (user1.storageUsed != 0) user1.storageUsed else user2.storageUsed
31+
}
32+
}
33+
it
34+
}
35+
}
36+
}
37+
38+
override fun toString(): String {
39+
return "$id\n$name\n$email\n$type\n$storageUsed"
40+
}
41+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.code.gambit.data.remote
2+
3+
import com.github.code.gambit.BuildConfig
4+
import okhttp3.Interceptor
5+
import okhttp3.Response
6+
7+
class ApiGatewayInterceptor : Interceptor {
8+
override fun intercept(chain: Interceptor.Chain): Response {
9+
val request = chain.request().newBuilder()
10+
.addHeader("x-api-key", BuildConfig.API_KEY)
11+
.build()
12+
return chain.proceed(request)
13+
}
14+
}

app/src/main/java/com/github/code/gambit/data/remote/services/ApiService.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,23 @@ import retrofit2.http.GET
1212
import retrofit2.http.POST
1313
import retrofit2.http.PUT
1414
import retrofit2.http.Path
15-
import javax.inject.Named
1615

1716
interface ApiService {
1817

1918
@GET("user/{userId}")
20-
suspend fun getUser(@Path("userId") @Named("UID") userId: String): Response<UserNetworkEntity>
19+
suspend fun getUser(@Path("userId") userId: String): Response<UserNetworkEntity>
2120

2221
@PUT("user/{userId}")
23-
suspend fun updateUser(@Path("userId") @Named(AppConstant.Named.USER_ID) userId: String, @Body userNetworkEntity: UserNetworkEntity): Response<UserNetworkEntity>
22+
suspend fun updateUser(@Path("userId") userId: String, @Body userNetworkEntity: UserNetworkEntity): Response<UserNetworkEntity>
2423

2524
@DELETE("user/{userId}")
26-
suspend fun deleteUser(@Path("userId") @Named(AppConstant.Named.USER_ID) userId: String): Response<UserNetworkEntity>
25+
suspend fun deleteUser(@Path("userId") userId: String): Response<UserNetworkEntity>
2726

2827
@GET("user/{userId}/file")
29-
suspend fun getFiles(@Path("userId") @Named(AppConstant.Named.USER_ID) userId: String): ListResponse<FileNetworkEntity>
28+
suspend fun getFiles(@Path("userId") userId: String): ListResponse<FileNetworkEntity>
3029

3130
@POST("user/{userId}/file")
32-
suspend fun uploadFiles(@Path("userId") @Named(AppConstant.Named.USER_ID) userId: String, @Body fileNetworkEntity: FileNetworkEntity): Response<FileNetworkEntity>
31+
suspend fun uploadFiles(@Path("userId") userId: String, @Body fileNetworkEntity: FileNetworkEntity): Response<FileNetworkEntity>
3332

3433
@DELETE("user/{userId}/file/{fileId}")
3534
suspend fun deleteFile(@Path(AppConstant.API_PATH.USER_ID) userId: String, @Path(AppConstant.API_PATH.FILE_ID) fileId: String): Response<FileNetworkEntity>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.github.code.gambit.data.remote.services
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.net.ConnectivityManager
6+
import com.github.code.gambit.utility.NoInternetException
7+
import dagger.hilt.android.qualifiers.ApplicationContext
8+
import okhttp3.Interceptor
9+
import okhttp3.Response
10+
import timber.log.Timber
11+
import java.io.IOException
12+
import java.net.HttpURLConnection
13+
import java.net.URL
14+
15+
class NetworkInterceptor
16+
constructor(@ApplicationContext context: Context) : Interceptor {
17+
18+
private val applicationContext = context.applicationContext
19+
20+
override fun intercept(chain: Interceptor.Chain): Response {
21+
if (!hasActiveInternetConnection()) {
22+
throw NoInternetException("Not connected to internet")
23+
}
24+
return chain.proceed(chain.request())
25+
}
26+
27+
@SuppressLint("ServiceCast")
28+
private fun isConnected(): Boolean {
29+
val connectivityManager = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
30+
val network_info = connectivityManager.activeNetwork
31+
return network_info != null
32+
}
33+
34+
fun hasActiveInternetConnection(): Boolean {
35+
if (isConnected()) {
36+
try {
37+
val urlc: HttpURLConnection =
38+
URL("https://clients3.google.com/generate_204").openConnection() as HttpURLConnection
39+
urlc.setRequestProperty("User-Agent", "Android")
40+
urlc.setRequestProperty("Connection", "close")
41+
urlc.connectTimeout = 1500
42+
urlc.connect()
43+
return (urlc.responseCode == 204 && urlc.contentLength == 0)
44+
} catch (e: IOException) {
45+
Timber.e("Error checking internet connection")
46+
}
47+
} else {
48+
Timber.e("No network available!")
49+
}
50+
return false
51+
}
52+
}

0 commit comments

Comments
 (0)