release 0.1 #30
@ -0,0 +1,27 @@
|
||||
package it.unisannio.ding.ids.wedroid.app.data.dao;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Delete;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.OnConflictStrategy;
|
||||
import androidx.room.Query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
//import io.reactivex.Completable;
|
||||
import it.unisannio.ding.ids.wedroid.app.data.entity.WList;
|
||||
|
||||
@Dao
|
||||
public interface WListDao {
|
||||
|
||||
@Query("SELECT * from wlist_table ORDER BY title ASC")
|
||||
LiveData<List<WList>> getAllWList();
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
/**Completable**/ void insert(WList wList);
|
||||
|
||||
@Delete
|
||||
void delete(WList wList);
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package it.unisannio.ding.ids.wedroid.app.data.database;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.room.Database;
|
||||
import androidx.room.Room;
|
||||
import androidx.room.RoomDatabase;
|
||||
|
||||
import it.unisannio.ding.ids.wedroid.app.data.dao.WListDao;
|
||||
import it.unisannio.ding.ids.wedroid.app.data.entity.WList;
|
||||
|
||||
@Database(entities = WList.class, version = 1, exportSchema = false)
|
||||
public abstract class WListDatabase extends RoomDatabase {
|
||||
private static volatile WListDatabase INSTANCE;
|
||||
public abstract WListDao wListDao();
|
||||
|
||||
public static WListDatabase getDatabase(Context context) {
|
||||
if (INSTANCE != null)
|
||||
return INSTANCE;
|
||||
synchronized (WListDatabase.class) {
|
||||
INSTANCE = Room.databaseBuilder(
|
||||
context.getApplicationContext(),
|
||||
WListDatabase.class,
|
||||
"wlist_database"
|
||||
).build();
|
||||
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package it.unisannio.ding.ids.wedroid.app.data.entity
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "wlist_table")
|
||||
data class WList(
|
||||
@PrimaryKey @ColumnInfo(name = "id") val id: String,
|
||||
@ColumnInfo(name = "title") val title: String = ""
|
||||
)
|
||||
|
||||
fun it.unisannio.ding.ids.wedroid.wrapper.entity.WList.convert(): WList {
|
||||
return WList(this.id, this.title)
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package it.unisannio.ding.ids.wedroid.app.data.repository
|
||||
|
||||
import android.util.Log
|
||||
import it.unisannio.ding.ids.wedroid.app.data.dao.WListDao
|
||||
import it.unisannio.ding.ids.wedroid.app.data.entity.WList
|
||||
import it.unisannio.ding.ids.wedroid.app.data.entity.convert
|
||||
import it.unisannio.ding.ids.wedroid.app.util.PreferenceReader
|
||||
import it.unisannio.ding.ids.wedroid.wrapper.api.ListService
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
|
||||
class WListRepository(
|
||||
private val dao: WListDao,
|
||||
private val service: ListService,
|
||||
private val reader: PreferenceReader
|
||||
) {
|
||||
val allWLists by lazy{
|
||||
dao.getAllWList()
|
||||
}
|
||||
|
||||
init {
|
||||
synchronize()
|
||||
}
|
||||
|
||||
fun synchronize() {
|
||||
service.getAllList(reader.boardId)
|
||||
.enqueue(object :
|
||||
Callback<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.WList>> {
|
||||
override fun onFailure(
|
||||
call: Call<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.WList>>,
|
||||
t: Throwable
|
||||
) = logNetworkError(t.message)
|
||||
|
||||
override fun onResponse(
|
||||
call: Call<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.WList>>,
|
||||
response: Response<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.WList>>
|
||||
) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
synchronizeCallback(response)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private suspend fun synchronizeCallback(
|
||||
response: Response<MutableList<it.unisannio.ding.ids.wedroid.wrapper.entity.WList>>)
|
||||
{
|
||||
if (!response.isSuccessful) {
|
||||
logNetworkError("${response.code()} ${response.message()}")
|
||||
return
|
||||
}
|
||||
|
||||
val wLists = (response.body() ?: return)
|
||||
.map { it.convert() }
|
||||
|
||||
addNewWListToDb(wLists)
|
||||
removeOldWListsFromDb(wLists)
|
||||
}
|
||||
|
||||
private suspend fun addNewWListToDb(wLists: Collection<WList>) {
|
||||
wLists.forEach {
|
||||
dao.insert(it)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun removeOldWListsFromDb(wLists: Collection<WList>) {
|
||||
allWLists.value?.minus(wLists)
|
||||
?.forEach {
|
||||
dao.delete(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun logNetworkError(message: String?) {
|
||||
Log.e("RETROFIT", message)
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package it.unisannio.ding.ids.wedroid.app.util;
|
||||
|
||||
public interface PreferenceReader {
|
||||
String getBaseUrl();
|
||||
String getUserId();
|
||||
String getToken();
|
||||
String getBoardId();
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package it.unisannio.ding.ids.wedroid.app.util;
|
||||
|
||||
public interface PreferenceWriter {
|
||||
void setBaseUrl(String baseUrl);
|
||||
void setUserId(String userId);
|
||||
void setToken(String token);
|
||||
void setBoardId(String boardId);
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package it.unisannio.ding.ids.wedroid.app.util
|
||||
|
||||
import it.unisannio.ding.ids.wedroid.wrapper.api.*
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
|
||||
class ServicesFactory(
|
||||
reader: PreferenceReader
|
||||
) {
|
||||
private val retrofit: Retrofit
|
||||
|
||||
init {
|
||||
val httpClient = OkHttpClient.Builder()
|
||||
.addInterceptor {
|
||||
val request = it.request().newBuilder()
|
||||
.addHeader("Authorization", "Bearer ${reader.token}")
|
||||
.addHeader("Accept", "application/json")
|
||||
.addHeader("Content-type", "application/json")
|
||||
.build()
|
||||
|
||||
it.proceed(request)
|
||||
}.build()
|
||||
|
||||
retrofit = Retrofit.Builder()
|
||||
.baseUrl(reader.baseUrl)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.client(httpClient)
|
||||
.build()
|
||||
}
|
||||
|
||||
val boardService by lazy {
|
||||
retrofit.create(BoardService::class.java)
|
||||
}
|
||||
|
||||
val cardCommentService by lazy {
|
||||
retrofit.create(CardCommentService::class.java)
|
||||
}
|
||||
|
||||
val cardService by lazy {
|
||||
retrofit.create(CardService::class.java)
|
||||
}
|
||||
|
||||
val checklistService by lazy {
|
||||
retrofit.create(ChecklistService::class.java)
|
||||
}
|
||||
|
||||
val listService by lazy {
|
||||
retrofit.create(ListService::class.java)
|
||||
}
|
||||
|
||||
val swimlanesService by lazy {
|
||||
retrofit.create(SwimlanesService::class.java)
|
||||
}
|
||||
|
||||
val userService by lazy {
|
||||
retrofit.create(UserService::class.java)
|
||||
}
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var INSTANCE: ServicesFactory? = null
|
||||
|
||||
fun getInstance(reader: PreferenceReader): ServicesFactory {
|
||||
val tempInstance = INSTANCE
|
||||
|
||||
if (tempInstance != null)
|
||||
return tempInstance
|
||||
|
||||
synchronized(this) {
|
||||
val instance = ServicesFactory(reader)
|
||||
INSTANCE = instance
|
||||
return instance
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,44 @@
|
||||
package it.unisannio.ding.ids.wedroid.app.util
|
||||
|
||||
import android.content.Context
|
||||
|
||||
class SharedPreferenceHelper(context : Context) : PreferenceReader, PreferenceWriter {
|
||||
private val sp = context.getSharedPreferences("userinfo", Context.MODE_PRIVATE)
|
||||
|
||||
override fun getBaseUrl(): String? {
|
||||
return sp.getString("url", "")
|
||||
}
|
||||
|
||||
override fun getUserId(): String? {
|
||||
return sp.getString("id", "")
|
||||
}
|
||||
|
||||
override fun getToken(): String? {
|
||||
return sp.getString("token", "")
|
||||
}
|
||||
|
||||
override fun setBaseUrl(baseUrl: String?) {
|
||||
val editor = sp.edit()
|
||||
editor.putString("url", baseUrl).apply()
|
||||
|
||||
}
|
||||
|
||||
override fun setUserId(userId: String?) {
|
||||
val editor = sp.edit()
|
||||
editor.putString("id", userId).apply()
|
||||
}
|
||||
|
||||
override fun setToken(token: String?) {
|
||||
val editor = sp.edit()
|
||||
editor.putString("token", token).apply()
|
||||
}
|
||||
|
||||
override fun getBoardId(): String? {
|
||||
return sp.getString("boardId", "")
|
||||
}
|
||||
|
||||
override fun setBoardId(token: String?) {
|
||||
val editor = sp.edit()
|
||||
editor.putString("boardId", token).apply()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user