Skip to content

Commit

Permalink
Add LocationIQ api client and change additionalData to contain a json…
Browse files Browse the repository at this point in the history
… object
  • Loading branch information
profiluefter committed Apr 22, 2021
1 parent 219d163 commit f51da02
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ class NoteEditorViewModel(note: Note) : ViewModel() {
get() {
val (day, month, year) = date.value!!.split(".").map { it.toInt() }
val (hour, minute) = time.value!!.split(":").map { it.toInt() }
return Note(localID, title.value!!, done.value!!, minute, hour, day, month, year, description.value!!)
return Note(
localID,
title.value!!,
done.value!!,
minute,
hour,
day,
month,
year,
description.value!!,
0.0,
0.0,
"",
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import javax.inject.Singleton

@Module
Expand All @@ -18,6 +20,14 @@ object GsonModule {
fun createGson(): Gson = GsonBuilder()
.setExclusionStrategies(ExposeExclusionStrategy())
.create()

@Provides
@Singleton
fun loggingHttpClient(): OkHttpClient = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
.build()
}

@Target(AnnotationTarget.FIELD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import me.profiluefter.profinote.data.entities.*
import me.profiluefter.profinote.data.local.NotesDatabase
import me.profiluefter.profinote.data.remote.Credentials
import me.profiluefter.profinote.data.remote.NotesAPI
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.time.LocalDateTime
import javax.inject.Inject
import javax.inject.Provider
Expand Down Expand Up @@ -268,12 +265,3 @@ class NotesRepository @Inject constructor(
local.listDao().nuke()
}
}

private fun <T> retrofitErrorCallback(onError: (Throwable) -> Unit): Callback<T> =
object : Callback<T> {
override fun onResponse(call: Call<T>, response: Response<T>) {}

override fun onFailure(call: Call<T>, t: Throwable) {
onError(t)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.profiluefter.profinote.data.entities

import android.util.Log
import com.google.gson.Gson
import java.io.Serializable
import java.time.LocalDateTime
import java.time.temporal.ChronoField.*
Expand All @@ -14,7 +16,10 @@ data class Note(
val day: Int,
val month: Int,
val year: Int,
val description: String
val description: String,
val latitude: Double,
val longitude: Double,
val address: String,
) : Serializable, Comparable<Note> {
constructor(calendar: Calendar = Calendar.getInstance()) : this(
0,
Expand All @@ -25,7 +30,10 @@ data class Note(
calendar.get(Calendar.DAY_OF_MONTH),
calendar.get(Calendar.MONTH) + 1,
calendar.get(Calendar.YEAR),
""
"",
0.0,
0.0,
"",
)

override fun compareTo(other: Note): Int {
Expand All @@ -42,12 +50,25 @@ data class Note(
description,
apiPattern.format(LocalDateTime.of(year, month, day, hour, minute)),
if (done) "DONE" else "TODO",
apiPattern.format(LocalDateTime.now())
Gson().toJson(
AdditionalDataContainer(
apiPattern.format(LocalDateTime.now()),
AdditionalDataContainer.LocationInformation(latitude, longitude, address)
)
),
)

companion object {
fun from(api: RawTodo): Note = api.run {
val due = apiPattern.parse(dueDate)
val dataContainer = try {
Gson().fromJson(additionalData, AdditionalDataContainer::class.java)
} catch (e: Exception) {
null
}

if (dataContainer?.locationInformation == null)
Log.w("Note", "Additional data from note id=$localID doesn't contain location information")

return Note(
localID,
Expand All @@ -66,7 +87,10 @@ data class Note(
due.get(DAY_OF_MONTH),
due.get(MONTH_OF_YEAR),
due.get(YEAR),
description
description,
dataContainer?.locationInformation?.latitude ?: 0.0,
dataContainer?.locationInformation?.longitude ?: 0.0,
dataContainer?.locationInformation?.address ?: "",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package me.profiluefter.profinote.data.entities
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import com.google.gson.Gson
import me.profiluefter.profinote.data.LocalOnly
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
Expand Down Expand Up @@ -112,6 +113,17 @@ data class RawTodo(
)
}

data class AdditionalDataContainer(
val lastChanged: String,
val locationInformation: LocationInformation?,
) {
data class LocationInformation(
val latitude: Double,
val longitude: Double,
val address: String,
)
}

val apiPattern: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

fun RawTodoList.serverEquals(other: RawTodoList): Boolean =
Expand All @@ -134,8 +146,14 @@ fun RawTodoList.changedDate(): LocalDateTime = parseAdditionalData(additionalDat
fun RawTodo.changedDate(): LocalDateTime = parseAdditionalData(additionalData)

fun parseAdditionalData(additionalData: String): LocalDateTime {
val lastChanged = try {
Gson().fromJson(additionalData, AdditionalDataContainer::class.java).lastChanged
} catch (e: Exception) {
additionalData
}

return try {
val accessor = apiPattern.parse(additionalData)
val accessor = apiPattern.parse(lastChanged)
val date = accessor.query(TemporalQueries.localDate())
val time = accessor.query(TemporalQueries.localTime())
date.atTime(time)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.profiluefter.profinote.data.entities

import com.google.gson.Gson
import java.time.LocalDateTime

data class TodoList(
Expand All @@ -12,7 +13,12 @@ data class TodoList(
remoteID,
"0",
name,
apiPattern.format(LocalDateTime.now())
Gson().toJson(
AdditionalDataContainer(
apiPattern.format(LocalDateTime.now()),
null
)
),
)

fun sorted(): TodoList = TodoList(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package me.profiluefter.profinote.data.geocoding

interface GeocodingService {
suspend fun reverse(latitude: Double, longitude: Double): String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package me.profiluefter.profinote.data.geocoding

import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
import retrofit2.http.GET
import retrofit2.http.Query
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton

class LocationIQGeocodingService @Inject constructor(
private val api: LocationIQAPI,
@Named("locationIQ")
private val key: String
) : GeocodingService {
override suspend fun reverse(latitude: Double, longitude: Double): String =
api.reverse(latitude, longitude, key).displayName
}

interface LocationIQAPI {
@GET("v1/reverse.php")
suspend fun reverse(
@Query("lat") latitude: Double,
@Query("lon") longitude: Double,
@Query("key") key: String,
@Query("format") format: String = "json"
): ReverseResponse
}

data class ReverseResponse(
@SerializedName("display_name")
val displayName: String
)

@Module
@InstallIn(SingletonComponent::class)
object LocationIQModule {
@Provides
@Singleton
fun locationIQAPIClient(gson: Gson, client: OkHttpClient): LocationIQAPI = Retrofit.Builder()
.baseUrl("https://eu1.locationiq.com/")
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
Expand All @@ -23,12 +22,4 @@ object NotesAPIModule {
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create()

@Provides
@Singleton
fun loggingHttpClient(): OkHttpClient = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
.build()
}

0 comments on commit f51da02

Please sign in to comment.