From caa7a0870cbdabc2c6cebfc4e86f1dabb5f16ec5 Mon Sep 17 00:00:00 2001 From: Kaz Date: Wed, 3 Apr 2024 03:04:15 +0200 Subject: [PATCH 1/5] modified chirps display to fake storage containing only fetched chirps --- .../repository/SocialMediaRepository.kt | 37 ++++++++++++++++--- .../ui/lao/socialmedia/ChirpListFragment.kt | 11 ++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/fe2-android/app/src/main/java/com/github/dedis/popstellar/repository/SocialMediaRepository.kt b/fe2-android/app/src/main/java/com/github/dedis/popstellar/repository/SocialMediaRepository.kt index 158bda0a93..f4df627d20 100644 --- a/fe2-android/app/src/main/java/com/github/dedis/popstellar/repository/SocialMediaRepository.kt +++ b/fe2-android/app/src/main/java/com/github/dedis/popstellar/repository/SocialMediaRepository.kt @@ -25,6 +25,7 @@ import java.util.function.Consumer import javax.inject.Inject import javax.inject.Singleton import timber.log.Timber +import java.util.concurrent.atomic.AtomicInteger /** * This class is the repository of the social media feature @@ -102,6 +103,10 @@ constructor(appDatabase: AppDatabase, application: Application) { return getLaoChirps(laoId).reactionByChirpId[chirpId] ?: emptySet() } + fun queryMoreChirps(laoId: String) { + getLaoChirps(laoId).queryMoreChirps() + } + /** * @param laoId of the lao we want to observe the chirp list * @return an observable set of message ids whose correspond to the set of chirp published on the @@ -171,6 +176,9 @@ constructor(appDatabase: AppDatabase, application: Application) { private val chirps = ConcurrentHashMap() private val chirpSubjects = ConcurrentHashMap>() private val chirpsSubject: Subject> = BehaviorSubject.createDefault(emptySet()) + //TODO : used to fake page loading, to be removed when we will be able to request only wanted chirps. Maxime Teuber @Kaz-ookid - April 2024 + private var chirpsLoaded : AtomicInteger = AtomicInteger(0) + private var storageIsLoaded : Boolean = false // Reactions val reactionByChirpId = ConcurrentHashMap>() @@ -179,6 +187,7 @@ constructor(appDatabase: AppDatabase, application: Application) { init { loadStorage() + chirpsLoaded.set(CHIRPS_PAGE_SIZE) } fun add(chirp: Chirp) { @@ -197,8 +206,21 @@ constructor(appDatabase: AppDatabase, application: Application) { // Publish new values on subjects chirpSubjects[id] = BehaviorSubject.createDefault(chirp) chirpsSubject.toSerialized().onNext(HashSet(chirps.keys)) + + if (storageIsLoaded) { + //TODO : used to fake page loading, to be removed when we will be able to request only wanted chirps. Maxime Teuber @Kaz-ookid - April 2024 + // if the storage is loaded, we can increment the number of chirps loaded + chirpsLoaded.incrementAndGet() + } } + //TODO : used to fake page loading, to be removed when we will be able to request only wanted chirps. Maxime Teuber @Kaz-ookid - April 2024 + // Either here or directly in DB, we should query to servers tne CHIRPS_PAGE_SIZE next chirps, and not all of them. + fun queryMoreChirps() { + chirpsLoaded.addAndGet(CHIRPS_PAGE_SIZE) + chirpsSubject.onNext(chirps.keys.sortedByDescending { chirps[it]?.timestamp }.take(chirpsLoaded.get()).toSet()) + } + fun addReaction(reaction: Reaction): Boolean { // Check if the associated chirp is present val chirp = chirps[reaction.chirpId] ?: return false @@ -279,7 +301,11 @@ constructor(appDatabase: AppDatabase, application: Application) { } fun getChirpsSubject(): Observable> { - return chirpsSubject + //TODO : used to fake page loading, to be removed when we will be able to request only wanted chirps. Maxime Teuber @Kaz-ookid - April 2024 + // would normally return chirpsSubject + + // order chirpsSubject by timestamp then take the first chirpsLoaded + return chirpsSubject.map { chirps.keys.sortedByDescending { chirps[it]?.timestamp }.take(chirpsLoaded.get()).toSet() } } @Throws(UnknownChirpException::class) @@ -299,8 +325,7 @@ constructor(appDatabase: AppDatabase, application: Application) { */ private fun loadStorage() { repository.disposables.add( - repository.chirpDao - .getChirpsByLaoId(laoId) + repository.chirpDao.getChirpsByLaoId(laoId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( @@ -342,14 +367,16 @@ constructor(appDatabase: AppDatabase, application: Application) { chirp.id) })) }) - }, + storageIsLoaded = true + } , { err: Throwable -> Timber.tag(TAG).e(err, "No chirp found in the storage for lao %s", laoId) })) } } - companion object { + companion object { + private const val CHIRPS_PAGE_SIZE : Int = 10 private val TAG = SocialMediaRepository::class.java.simpleName } } diff --git a/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/socialmedia/ChirpListFragment.kt b/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/socialmedia/ChirpListFragment.kt index 2b1eccf91f..74bb196128 100644 --- a/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/socialmedia/ChirpListFragment.kt +++ b/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/socialmedia/ChirpListFragment.kt @@ -4,6 +4,7 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.Button import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import com.github.dedis.popstellar.R @@ -39,6 +40,7 @@ class ChirpListFragment : Fragment() { super.onViewCreated(view, savedInstanceState) setupSendButton() setupListViewAdapter() + addLoadMoreButtonToListView() } override fun onResume() { @@ -62,6 +64,15 @@ class ChirpListFragment : Fragment() { listView.adapter = mChirpListAdapter } + private fun addLoadMoreButtonToListView() { + val footerView = layoutInflater.inflate(R.layout.chirp_load_more_button, binding.chirpsList, false) + val buttonLoadMoreChirps = footerView.findViewById