Commit 398eec8a authored by Niklas Fix's avatar Niklas Fix 🎓

boost fetching avatars

parent 51304081
......@@ -3,6 +3,7 @@ package de.akamu.tudarmstadt.data.user
import de.akamu.tudarmstadt.model.Avatar
import de.akamu.tudarmstadt.model.User
import java.util.*
import kotlin.collections.ArrayList
/**
* Defines general methods to change the user in the server
......@@ -46,14 +47,26 @@ interface UserDataSource {
}
/**
* Callback for loading the user's avatar
* Callback for loading all the avatars
*/
interface LoadAvatarListCallback {
interface LoadAllAvatarListCallback {
/** Called when the user avatar was successfully loaded */
/** Called when all the avatars were successfully loaded */
fun onLoadAvatarListSuccess(avatarList: ArrayList<Avatar>)
/** Called when loading all the avatars failed */
fun onLoadAvatarListFail(reason: String)
}
/**
* Callback for loading all the user's avatar
*/
interface LoadUnlockedAvatarListCallback {
/** Called when the unlocked avatars was successfully loaded */
fun onLoadAvatarListSuccess(avatarSortedMap: SortedMap<Avatar, ByteArray>)
/** Called when loading the user avatar failed */
/** Called when loading the unlocked avatars failed */
fun onLoadAvatarListFail(reason: String)
}
......@@ -65,7 +78,7 @@ interface UserDataSource {
fun loadUserAvatarFromServer(avatarUrl: String, callback: LoadAvatarCallback)
fun loadAllAvatarsFromServer(callback: LoadAvatarListCallback)
fun loadAllAvatarsFromServer(callback: LoadAllAvatarListCallback)
fun loadUnlockedAvatarsFromServer(callback: LoadAvatarListCallback)
fun loadUnlockedAvatarsFromServer(callback: LoadUnlockedAvatarListCallback)
}
\ No newline at end of file
......@@ -157,25 +157,18 @@ class UserDataSourceImpl : UserDataSource {
}
}
override fun loadAllAvatarsFromServer(callback: UserDataSource.LoadAvatarListCallback) {
override fun loadAllAvatarsFromServer(callback: UserDataSource.LoadAllAvatarListCallback) {
val getAllAvatarsTask = GetAllAvatarsTask(callback)
getAllAvatarsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
}
class GetAllAvatarsTask(var callback: UserDataSource.LoadAvatarListCallback) : AsyncTask<Void, Void, Boolean>() {
class GetAllAvatarsTask(var callback: UserDataSource.LoadAllAvatarListCallback) : AsyncTask<Void, Void, Boolean>() {
lateinit var avatarList: ArrayList<Avatar>
lateinit var avatarSortedMap: SortedMap<Avatar, ByteArray>
private var avatarHashMap: HashMap<Avatar, ByteArray> = HashMap()
private lateinit var errorMessage: String
override fun doInBackground(vararg p0: Void?): Boolean {
return try {
avatarList = ArrayList(V2APIAvatar.loadAllAvatarsFromServer())
for (avatar in avatarList) {
val res = V2APIAvatar.loadAvatarFromServer(avatar.image)
avatarHashMap[avatar] = res
}
avatarSortedMap = avatarHashMap.toSortedMap(compareBy { it.id })
true
} catch (e: Exception) {
errorMessage = e.message!!
......@@ -185,20 +178,20 @@ class UserDataSourceImpl : UserDataSource {
override fun onPostExecute(success: Boolean?) {
if (success!!) {
callback.onLoadAvatarListSuccess(avatarSortedMap)
callback.onLoadAvatarListSuccess(avatarList)
} else {
callback.onLoadAvatarListFail(errorMessage)
}
}
}
override fun loadUnlockedAvatarsFromServer(callback: UserDataSource.LoadAvatarListCallback) {
override fun loadUnlockedAvatarsFromServer(callback: UserDataSource.LoadUnlockedAvatarListCallback) {
val getUnlockedAvatarsTask = GetUnlockedAvatarsTask(callback)
getUnlockedAvatarsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
}
class GetUnlockedAvatarsTask(var callback: UserDataSource.LoadAvatarListCallback) : AsyncTask<Void, Void, Boolean>() {
class GetUnlockedAvatarsTask(var callback: UserDataSource.LoadUnlockedAvatarListCallback) : AsyncTask<Void, Void, Boolean>() {
lateinit var avatarList: ArrayList<Avatar>
lateinit var avatarSortedMap: SortedMap<Avatar, ByteArray>
private var avatarHashMap: HashMap<Avatar, ByteArray> = HashMap()
......
......@@ -7,6 +7,7 @@ import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import de.akamu.tudarmstadt.R
......@@ -93,7 +94,7 @@ class AvatarGridAdapter(
.into(avatarImg)
} else {
Glide.with(context)
.load(context.getDrawable(R.drawable.lockavatar))
.load(ContextCompat.getDrawable(context, R.drawable.lockavatar))
.into(avatarImg)
}
avatarUnlockLevel.text = String.format("LVL %d", mAvatar.level)
......
......@@ -6,6 +6,7 @@ import de.akamu.tudarmstadt.interactors.UserInteractor
import de.akamu.tudarmstadt.model.Avatar
import de.akamu.tudarmstadt.model.User
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
class ProfilePresenter(private var view: ProfileContract.View?, private val interactor: UserInteractor) : ProfileContract.Presenter {
......@@ -14,7 +15,7 @@ class ProfilePresenter(private var view: ProfileContract.View?, private val inte
var allAvatarsFetched = false
lateinit var unlockedAvatars : SortedMap<Avatar, ByteArray>
lateinit var allAvatars : SortedMap<Avatar, ByteArray>
lateinit var allAvatars : ArrayList<Avatar>
var avatars: HashMap<Avatar, ByteArray> = HashMap()
init {
......@@ -50,10 +51,10 @@ class ProfilePresenter(private var view: ProfileContract.View?, private val inte
}
override fun loadAllAvatarsFromServer() {
interactor.loadAllAvatarsFromServer(object : UserDataSource.LoadAvatarListCallback {
override fun onLoadAvatarListSuccess(avatarSortedMap: SortedMap<Avatar, ByteArray>) {
interactor.loadAllAvatarsFromServer(object : UserDataSource.LoadAllAvatarListCallback {
override fun onLoadAvatarListSuccess(avatarList: ArrayList<Avatar>) {
allAvatarsFetched = true
allAvatars = avatarSortedMap
allAvatars = avatarList
handleCallbacksReady()
}
......@@ -65,7 +66,7 @@ class ProfilePresenter(private var view: ProfileContract.View?, private val inte
}
override fun loadUnlockedAvatarsFromServer() {
interactor.loadUnlockedAvatarsFromServer(object : UserDataSource.LoadAvatarListCallback {
interactor.loadUnlockedAvatarsFromServer(object : UserDataSource.LoadUnlockedAvatarListCallback {
override fun onLoadAvatarListSuccess(avatarSortedMap: SortedMap<Avatar, ByteArray>) {
unlockedAvatarsFetched = true
unlockedAvatars = avatarSortedMap
......@@ -99,12 +100,14 @@ class ProfilePresenter(private var view: ProfileContract.View?, private val inte
}
}
fun prepareAvatars() {
for (av in allAvatars.keys) {
private fun prepareAvatars() {
for (av in allAvatars) {
if (unlockedAvatars.contains(av)) {
av.isUnlocked = true
avatars[av] = unlockedAvatars[av]!!
} else {
avatars[av] = byteArrayOf() // leave empty will be set later
}
avatars[av] = allAvatars[av]!!
}
}
......
......@@ -4,6 +4,7 @@ import de.akamu.tudarmstadt.data.user.UserDataSource
import de.akamu.tudarmstadt.model.Avatar
import de.akamu.tudarmstadt.model.User
import java.util.*
import kotlin.collections.ArrayList
class UserInteractor(private val dataSource: UserDataSource) : UserDataSource {
override fun updateUserInServer(user: User, callback: UserDataSource.UpdateUserCallback) {
......@@ -59,10 +60,10 @@ class UserInteractor(private val dataSource: UserDataSource) : UserDataSource {
})
}
override fun loadAllAvatarsFromServer(callback: UserDataSource.LoadAvatarListCallback) {
dataSource.loadAllAvatarsFromServer(object: UserDataSource.LoadAvatarListCallback{
override fun onLoadAvatarListSuccess(avatarSortedMap: SortedMap<Avatar, ByteArray>) {
callback.onLoadAvatarListSuccess(avatarSortedMap)
override fun loadAllAvatarsFromServer(callback: UserDataSource.LoadAllAvatarListCallback) {
dataSource.loadAllAvatarsFromServer(object: UserDataSource.LoadAllAvatarListCallback{
override fun onLoadAvatarListSuccess(avatarList: ArrayList<Avatar>) {
callback.onLoadAvatarListSuccess(avatarList)
}
override fun onLoadAvatarListFail(reason: String) {
......@@ -72,8 +73,8 @@ class UserInteractor(private val dataSource: UserDataSource) : UserDataSource {
})
}
override fun loadUnlockedAvatarsFromServer(callback: UserDataSource.LoadAvatarListCallback) {
dataSource.loadUnlockedAvatarsFromServer(object: UserDataSource.LoadAvatarListCallback{
override fun loadUnlockedAvatarsFromServer(callback: UserDataSource.LoadUnlockedAvatarListCallback) {
dataSource.loadUnlockedAvatarsFromServer(object: UserDataSource.LoadUnlockedAvatarListCallback {
override fun onLoadAvatarListSuccess(avatarSortedMap: SortedMap<Avatar, ByteArray>) {
callback.onLoadAvatarListSuccess(avatarSortedMap)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment