Hide tabs without content in shared items view

Signed-off-by: Tim Krüger <t@timkrueger.me>
This commit is contained in:
Tim Krüger 2022-04-29 13:41:54 +02:00
parent b22ed7399d
commit 721797c078
No known key found for this signature in database
GPG key ID: FECE3A7222C52A4E
9 changed files with 299 additions and 51 deletions

View file

@ -15,6 +15,7 @@ import com.nextcloud.talk.adapters.SharedItemsAdapter
import com.nextcloud.talk.adapters.SharedItemsListAdapter
import com.nextcloud.talk.databinding.ActivitySharedItemsBinding
import com.nextcloud.talk.models.database.UserEntity
import com.nextcloud.talk.repositories.SharedItemType
import com.nextcloud.talk.utils.DisplayUtils
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_CONVERSATION_NAME
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_TOKEN
@ -25,12 +26,12 @@ class SharedItemsActivity : AppCompatActivity() {
private lateinit var binding: ActivitySharedItemsBinding
private lateinit var viewModel: SharedItemsViewModel
private lateinit var currentTab: String
private lateinit var currentTab: SharedItemType
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
currentTab = TAB_MEDIA
currentTab = SharedItemType.MEDIA
val roomToken = intent.getStringExtra(KEY_ROOM_TOKEN)!!
val conversationName = intent.getStringExtra(KEY_CONVERSATION_NAME)
@ -54,17 +55,19 @@ class SharedItemsActivity : AppCompatActivity() {
supportActionBar?.title = conversationName
supportActionBar?.setDisplayHomeAsUpEnabled(true)
initTabs()
viewModel = ViewModelProvider(
this,
SharedItemsViewModel.Factory(userEntity, roomToken, currentTab)
).get(SharedItemsViewModel::class.java)
viewModel.sharedItemType.observe(this) {
initTabs(it)
}
viewModel.sharedItems.observe(this) {
Log.d(TAG, "Items received: $it")
if (currentTab == TAB_MEDIA) {
if (currentTab == SharedItemType.MEDIA) {
val adapter = SharedItemsAdapter()
adapter.items = it.items
adapter.authHeader = it.authHeader
@ -94,50 +97,65 @@ class SharedItemsActivity : AppCompatActivity() {
})
}
fun updateItems(type: String) {
fun updateItems(type: SharedItemType) {
currentTab = type
viewModel.loadItems(type)
}
private fun initTabs() {
val tabMedia: TabLayout.Tab = binding.sharedItemsTabs.newTab()
tabMedia.tag = TAB_MEDIA
tabMedia.setText(R.string.shared_items_media)
binding.sharedItemsTabs.addTab(tabMedia)
private fun initTabs(sharedItemTypes: Set<SharedItemType>) {
val tabFile: TabLayout.Tab = binding.sharedItemsTabs.newTab()
tabFile.tag = TAB_FILE
tabFile.setText(R.string.shared_items_file)
binding.sharedItemsTabs.addTab(tabFile)
if(sharedItemTypes.contains(SharedItemType.MEDIA)) {
val tabMedia: TabLayout.Tab = binding.sharedItemsTabs.newTab()
tabMedia.tag = SharedItemType.MEDIA
tabMedia.setText(R.string.shared_items_media)
binding.sharedItemsTabs.addTab(tabMedia)
}
val tabAudio: TabLayout.Tab = binding.sharedItemsTabs.newTab()
tabAudio.tag = TAB_AUDIO
tabAudio.setText(R.string.shared_items_audio)
binding.sharedItemsTabs.addTab(tabAudio)
if(sharedItemTypes.contains(SharedItemType.FILE)) {
val tabFile: TabLayout.Tab = binding.sharedItemsTabs.newTab()
tabFile.tag = SharedItemType.FILE
tabFile.setText(R.string.shared_items_file)
binding.sharedItemsTabs.addTab(tabFile)
}
val tabVoice: TabLayout.Tab = binding.sharedItemsTabs.newTab()
tabVoice.tag = TAB_VOICE
tabVoice.setText(R.string.shared_items_voice)
binding.sharedItemsTabs.addTab(tabVoice)
if(sharedItemTypes.contains(SharedItemType.AUDIO)) {
val tabAudio: TabLayout.Tab = binding.sharedItemsTabs.newTab()
tabAudio.tag = SharedItemType.AUDIO
tabAudio.setText(R.string.shared_items_audio)
binding.sharedItemsTabs.addTab(tabAudio)
}
// val tabLocation: TabLayout.Tab = binding.sharedItemsTabs.newTab()
// tabLocation.tag = TAB_LOCATION
// tabLocation.text = "location"
// binding.sharedItemsTabs.addTab(tabLocation)
if(sharedItemTypes.contains(SharedItemType.VOICE)) {
val tabVoice: TabLayout.Tab = binding.sharedItemsTabs.newTab()
tabVoice.tag = SharedItemType.VOICE
tabVoice.setText(R.string.shared_items_voice)
binding.sharedItemsTabs.addTab(tabVoice)
}
// val tabDeckCard: TabLayout.Tab = binding.sharedItemsTabs.newTab()
// tabDeckCard.tag = TAB_DECKCARD
// tabDeckCard.text = "deckcard"
// binding.sharedItemsTabs.addTab(tabDeckCard)
// if(sharedItemTypes.contains(SharedItemType.LOCATION)) {
// val tabLocation: TabLayout.Tab = binding.sharedItemsTabs.newTab()
// tabLocation.tag = SharedItemType.LOCATION
// tabLocation.text = "location"
// binding.sharedItemsTabs.addTab(tabLocation)
// }
// val tabOther: TabLayout.Tab = binding.sharedItemsTabs.newTab()
// tabOther.tag = TAB_OTHER
// tabOther.setText(R.string.shared_items_other)
// binding.sharedItemsTabs.addTab(tabOther)
// if(sharedItemTypes.contains(SharedItemType.DECKCARD)) {
// val tabDeckCard: TabLayout.Tab = binding.sharedItemsTabs.newTab()
// tabDeckCard.tag = SharedItemType.DECKCARD
// tabDeckCard.text = "deckcard"
// binding.sharedItemsTabs.addTab(tabDeckCard)
// }
// if(sharedItemTypes.contains(SharedItemType.OTHER)) {
// val tabOther: TabLayout.Tab = binding.sharedItemsTabs.newTab()
// tabOther.tag = SharedItemType.OTHER
// tabOther.setText(R.string.shared_items_other)
// binding.sharedItemsTabs.addTab(tabOther)
// }
binding.sharedItemsTabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
updateItems(tab.tag as String)
updateItems(tab.tag as SharedItemType)
}
override fun onTabUnselected(tab: TabLayout.Tab) = Unit
@ -157,13 +175,6 @@ class SharedItemsActivity : AppCompatActivity() {
companion object {
private val TAG = SharedItemsActivity::class.simpleName
const val TAB_AUDIO = "audio"
const val TAB_FILE = "file"
const val TAB_MEDIA = "media"
const val TAB_VOICE = "voice"
const val TAB_LOCATION = "location"
const val TAB_DECKCARD = "deckcard"
const val TAB_OTHER = "other"
const val SPAN_COUNT: Int = 4
}
}

View file

@ -28,6 +28,7 @@ import com.nextcloud.talk.models.json.capabilities.CapabilitiesOverall;
import com.nextcloud.talk.models.json.chat.ChatOverall;
import com.nextcloud.talk.models.json.chat.ChatOverallSingleMessage;
import com.nextcloud.talk.models.json.chat.ChatShareOverall;
import com.nextcloud.talk.models.json.chat.ChatShareOverviewOverall;
import com.nextcloud.talk.models.json.conversations.RoomOverall;
import com.nextcloud.talk.models.json.conversations.RoomsOverall;
import com.nextcloud.talk.models.json.generic.GenericOverall;
@ -345,6 +346,12 @@ public interface NcApi {
@Nullable @Query("lastKnownMessageId") Integer lastKnownMessageId,
@Nullable @Query("limit") Integer limit);
@GET
Observable<Response<ChatShareOverviewOverall>> getSharedItemsOverview(@Header("Authorization") String authorization,
@Url String url,
@Nullable @Query("limit") Integer limit);
@GET
Observable<MentionOverall> getMentionAutocompleteSuggestions(@Header("Authorization") String authorization,
@Url String url, @Query("search") String query,

View file

@ -0,0 +1,78 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.models.json.chat;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import org.parceler.Parcel;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import androidx.annotation.NonNull;
@Parcel
@JsonObject
public class ChatShareOverviewOCS {
@JsonField(name = "data")
public HashMap<String, List<Object>> data;
public HashMap<String, List<Object>> getData() {
return this.data;
}
public void setData(HashMap<String, List<Object>> data) {
this.data = data;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
}
if (!(o instanceof ChatShareOverviewOCS)) {
return false;
}
final ChatShareOverviewOCS other = (ChatShareOverviewOCS) o;
if (!other.canEqual(this)) {
return false;
}
final Object this$data = this.getData();
final Object other$data = other.getData();
return Objects.equals(this$data, other$data);
}
protected boolean canEqual(final Object other) {
return other instanceof ChatShareOverviewOCS;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $data = this.getData();
return result * PRIME + ($data == null ? 43 : $data.hashCode());
}
public String toString() {
return "ChatShareOverviewOCS(data=" + this.getData() + ")";
}
}

View file

@ -0,0 +1,75 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.models.json.chat;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import org.parceler.Parcel;
import java.util.Objects;
@Parcel
@JsonObject
public class ChatShareOverviewOverall {
@JsonField(name = "ocs")
public ChatShareOverviewOCS ocs;
public ChatShareOverviewOCS getOcs() {
return this.ocs;
}
public void setOcs(ChatShareOverviewOCS ocs) {
this.ocs = ocs;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
}
if (!(o instanceof ChatShareOverviewOverall)) {
return false;
}
final ChatShareOverviewOverall other = (ChatShareOverviewOverall) o;
if (!other.canEqual(this)) {
return false;
}
final Object this$ocs = this.getOcs();
final Object other$ocs = other.getOcs();
return Objects.equals(this$ocs, other$ocs);
}
protected boolean canEqual(final Object other) {
return other instanceof ChatShareOverviewOverall;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $ocs = this.getOcs();
return result * PRIME + ($ocs == null ? 43 : $ocs.hashCode());
}
public String toString() {
return "ChatShareOverviewOverall(ocs=" + this.getOcs() + ")";
}
}

View file

@ -0,0 +1,18 @@
package com.nextcloud.talk.repositories
import java.util.Locale
enum class SharedItemType {
AUDIO,
FILE,
MEDIA,
VOICE,
LOCATION,
DECKCARD,
OTHER;
companion object {
fun typeFor(name: String) = valueOf(name.uppercase(Locale.ROOT))
}
}

View file

@ -7,9 +7,11 @@ import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.application.NextcloudTalkApplication.Companion.sharedApplication
import com.nextcloud.talk.models.database.UserEntity
import com.nextcloud.talk.models.json.chat.ChatShareOverall
import com.nextcloud.talk.models.json.chat.ChatShareOverviewOverall
import com.nextcloud.talk.utils.ApiUtils
import io.reactivex.Observable
import retrofit2.Response
import java.util.Locale
import javax.inject.Inject
@AutoInjector(NextcloudTalkApplication::class)
@ -24,22 +26,32 @@ class SharedItemsRepository {
sharedApplication!!.componentApplication.inject(this)
}
fun media(type: String): Observable<Response<ChatShareOverall>>? {
fun media(type: SharedItemType): Observable<Response<ChatShareOverall>>? {
return media(type, null)
}
fun media(type: String, lastKnownMessageId: Int?): Observable<Response<ChatShareOverall>>? {
fun media(type: SharedItemType, lastKnownMessageId: Int?): Observable<Response<ChatShareOverall>>? {
val credentials = ApiUtils.getCredentials(parameters!!.userName, parameters!!.userToken)
return ncApi.getSharedItems(
credentials,
ApiUtils.getUrlForChatSharedItems(1, parameters!!.baseUrl, parameters!!.roomToken),
type,
type.toString().lowercase(Locale.ROOT),
lastKnownMessageId,
BATCH_SIZE
)
}
fun availableTypes(): Observable<Response<ChatShareOverviewOverall>>? {
val credentials = ApiUtils.getCredentials(parameters!!.userName, parameters!!.userToken)
return ncApi.getSharedItemsOverview(
credentials,
ApiUtils.getUrlForChatSharedItemsOverview(1, parameters!!.baseUrl, parameters!!.roomToken),
1
)
}
fun authHeader(): Map<String, String> {
return mapOf(Pair("Authorization", ApiUtils.getCredentials(parameters!!.userName, parameters!!.userToken)))
}

View file

@ -1,7 +1,7 @@
package com.nextcloud.talk.repositories
class SharedMediaItems(
val type: String,
val type: SharedItemType,
val items: MutableList<SharedItem>,
var lastSeenId: Int?,
var moreItemsExisting: Boolean,

View file

@ -265,6 +265,10 @@ public class ApiUtils {
return getUrlForChat(version, baseUrl, token) + "/share";
}
public static String getUrlForChatSharedItemsOverview(int version, String baseUrl, String token) {
return getUrlForChatSharedItems(version, baseUrl, token) + "/overview";
}
public static String getUrlForSignaling(int version, String baseUrl) {
return getUrlForApi(version, baseUrl) + "/signaling";
}

View file

@ -7,7 +7,9 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.nextcloud.talk.models.database.UserEntity
import com.nextcloud.talk.models.json.chat.ChatShareOverall
import com.nextcloud.talk.models.json.chat.ChatShareOverviewOverall
import com.nextcloud.talk.repositories.SharedItem
import com.nextcloud.talk.repositories.SharedItemType
import com.nextcloud.talk.repositories.SharedItemsRepository
import com.nextcloud.talk.repositories.SharedMediaItems
import io.reactivex.Observer
@ -16,15 +18,24 @@ import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import retrofit2.Response
class SharedItemsViewModel(private val repository: SharedItemsRepository, private val initialType: String) :
class SharedItemsViewModel(private val repository: SharedItemsRepository, private val initialType: SharedItemType) :
ViewModel() {
private val _sharedItemType: MutableLiveData<Set<SharedItemType>> by lazy {
MutableLiveData<Set<SharedItemType>>().also {
availableTypes()
}
}
private val _sharedItems: MutableLiveData<SharedMediaItems> by lazy {
MutableLiveData<SharedMediaItems>().also {
loadItems(initialType)
}
}
val sharedItemType: LiveData<Set<SharedItemType>>
get() = _sharedItemType
val sharedItems: LiveData<SharedMediaItems>
get() = _sharedItems
@ -38,13 +49,13 @@ class SharedItemsViewModel(private val repository: SharedItemsRepository, privat
}
}
fun loadItems(type: String) {
fun loadItems(type: SharedItemType) {
repository.media(type)?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(observer(type, true))
}
private fun observer(type: String, initModel: Boolean): Observer<Response<ChatShareOverall>> {
private fun observer(type: SharedItemType, initModel: Boolean): Observer<Response<ChatShareOverall>> {
return object : Observer<Response<ChatShareOverall>> {
var chatLastGiven: Int? = null
@ -119,7 +130,39 @@ class SharedItemsViewModel(private val repository: SharedItemsRepository, privat
}
}
class Factory(val userEntity: UserEntity, val roomToken: String, private val initialType: String) :
private fun availableTypes() {
repository.availableTypes()?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(object : Observer<Response<ChatShareOverviewOverall>> {
val types = mutableSetOf<SharedItemType>()
override fun onSubscribe(d: Disposable) = Unit
override fun onNext(response: Response<ChatShareOverviewOverall>) {
val typeMap = response.body()!!.ocs!!.data
for (it in typeMap) {
if (it.value.size > 0) {
try {
types += SharedItemType.typeFor(it.key)
} catch (e: IllegalArgumentException) {
Log.w(TAG, "Server responds an unknown shared item type: ${it.key}")
}
}
}
}
override fun onError(e: Throwable) {
Log.d(TAG, "An error occurred: $e")
}
override fun onComplete() {
this@SharedItemsViewModel._sharedItemType.value = types
}
})
}
class Factory(val userEntity: UserEntity, val roomToken: String, private val initialType: SharedItemType) :
ViewModelProvider
.Factory {