From e0f2b1aab5470b76fdc099c30039f14ff7bb39f1 Mon Sep 17 00:00:00 2001 From: Amanpal Singh <87360222+aman-alfresco@users.noreply.github.com> Date: Tue, 17 Sep 2024 16:32:28 +0530 Subject: [PATCH] invoke the config data api --- .../content/actions/ContextualActionsState.kt | 5 ++- .../actions/ContextualActionsViewModel.kt | 20 ++------- .../content/app/activity/LoginActivity.kt | 2 + .../alfresco/content/data/AppConfigUtils.kt | 41 +++++++++++++++++++ .../alfresco/content/data/CommonRepository.kt | 13 +++++- .../content/data/ContextualActionData.kt | 4 +- .../content/data/MobileConfigDataEntry.kt | 11 +++-- .../alfresco/content/listview/ListFragment.kt | 4 +- 8 files changed, 77 insertions(+), 23 deletions(-) diff --git a/actions/src/main/kotlin/com/alfresco/content/actions/ContextualActionsState.kt b/actions/src/main/kotlin/com/alfresco/content/actions/ContextualActionsState.kt index 7ed3ecedf..88547db82 100644 --- a/actions/src/main/kotlin/com/alfresco/content/actions/ContextualActionsState.kt +++ b/actions/src/main/kotlin/com/alfresco/content/actions/ContextualActionsState.kt @@ -5,6 +5,7 @@ import com.airbnb.mvrx.MavericksState import com.airbnb.mvrx.Uninitialized import com.alfresco.content.data.ContextualActionData import com.alfresco.content.data.Entry +import com.alfresco.content.data.MobileConfigDataEntry data class ContextualActionsState( val entries: List = emptyList(), @@ -12,6 +13,8 @@ data class ContextualActionsState( val actions: List = emptyList(), val topActions: List = emptyList(), val fetch: Async = Uninitialized, + val mobileConfigDataEntry: MobileConfigDataEntry? = null, ) : MavericksState { - constructor(target: ContextualActionData) : this(entries = target.entries, isMultiSelection = target.isMultiSelection) + constructor(target: ContextualActionData) : this(entries = target.entries, isMultiSelection = target.isMultiSelection, + mobileConfigDataEntry = target.mobileConfigData) } diff --git a/actions/src/main/kotlin/com/alfresco/content/actions/ContextualActionsViewModel.kt b/actions/src/main/kotlin/com/alfresco/content/actions/ContextualActionsViewModel.kt index b31a300ed..ce548f8ce 100644 --- a/actions/src/main/kotlin/com/alfresco/content/actions/ContextualActionsViewModel.kt +++ b/actions/src/main/kotlin/com/alfresco/content/actions/ContextualActionsViewModel.kt @@ -8,18 +8,17 @@ import com.airbnb.mvrx.Success import com.airbnb.mvrx.ViewModelContext import com.alfresco.content.common.EntryListener import com.alfresco.content.data.BrowseRepository -import com.alfresco.content.data.CommonRepository +import com.alfresco.content.data.CommonRepository.Companion.KEY_FEATURES_MOBILE import com.alfresco.content.data.Entry import com.alfresco.content.data.FavoritesRepository -import com.alfresco.content.data.MobileConfigDataEntry import com.alfresco.content.data.SearchRepository import com.alfresco.content.data.SearchRepository.Companion.SERVER_VERSION_NUMBER import com.alfresco.content.data.Settings +import com.alfresco.content.data.getJsonFromSharedPrefs import com.alfresco.coroutines.asFlow import com.alfresco.events.on import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.launch class ContextualActionsViewModel( @@ -31,6 +30,7 @@ class ContextualActionsViewModel( var listener: EntryListener? = null init { + if (!state.isMultiSelection) { buildModelSingleSelection() } else { @@ -52,19 +52,9 @@ class ContextualActionsViewModel( if (state.entries.isNotEmpty()) { state.entries.first().let { entry -> + if (entry.isPartial && !entry.hasOfflineStatus) { viewModelScope.launch { - mobileConfigData().execute { - when (it) { - is Success -> - copy() - - is Fail -> - copy() - - else -> copy() - } - } fetchEntry(entry).execute { when (it) { is Success -> @@ -120,8 +110,6 @@ class ContextualActionsViewModel( else -> BrowseRepository()::fetchEntry.asFlow(entry.id) } - private fun mobileConfigData(): Flow = CommonRepository()::getMobileConfigData.asFlow() - fun execute(action: Action) = action.execute(context, GlobalScope) diff --git a/app/src/main/java/com/alfresco/content/app/activity/LoginActivity.kt b/app/src/main/java/com/alfresco/content/app/activity/LoginActivity.kt index 114cc72a3..0de8363e4 100644 --- a/app/src/main/java/com/alfresco/content/app/activity/LoginActivity.kt +++ b/app/src/main/java/com/alfresco/content/app/activity/LoginActivity.kt @@ -10,6 +10,7 @@ import com.alfresco.content.app.R import com.alfresco.content.data.APIEvent import com.alfresco.content.data.AnalyticsManager import com.alfresco.content.data.BrowseRepository +import com.alfresco.content.data.CommonRepository import com.alfresco.content.data.OfflineRepository import com.alfresco.content.data.PeopleRepository import com.alfresco.content.data.SearchRepository @@ -28,6 +29,7 @@ class LoginActivity : com.alfresco.auth.activity.LoginActivity() { val session = Session(context, account) val person = PeopleRepository(session).me() val myFiles = BrowseRepository(session).myFilesNodeId() + CommonRepository(session).getMobileConfigData() processAccountInformation(person, myFiles, credentials, authConfig, endpoint) AnalyticsManager(session).apiTracker(APIEvent.Login, true) if (isExtension) { diff --git a/data/src/main/kotlin/com/alfresco/content/data/AppConfigUtils.kt b/data/src/main/kotlin/com/alfresco/content/data/AppConfigUtils.kt index 20b1fb037..e8e6f0953 100644 --- a/data/src/main/kotlin/com/alfresco/content/data/AppConfigUtils.kt +++ b/data/src/main/kotlin/com/alfresco/content/data/AppConfigUtils.kt @@ -1,7 +1,11 @@ package com.alfresco.content.data import android.content.Context +import android.content.SharedPreferences +import android.os.Build import android.os.Environment +import androidx.annotation.RequiresApi +import androidx.preference.PreferenceManager import com.google.common.reflect.TypeToken import com.google.gson.Gson import com.google.gson.GsonBuilder @@ -135,6 +139,7 @@ val gson: Gson = GsonBuilder() out.value(value.toString()) } + @RequiresApi(Build.VERSION_CODES.O) override fun read(inType: JsonReader): ZonedDateTime? { return ZonedDateTime.parse(inType.nextString(), formatter) } @@ -143,7 +148,43 @@ val gson: Gson = GsonBuilder() .enableComplexMapKeySerialization() .create() +@RequiresApi(Build.VERSION_CODES.O) private val formatter = DateTimeFormatterBuilder() .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME) .optionalStart().appendOffset("+HHMM", "Z").optionalEnd() .toFormatter() + + + +// Function to store a JSON object +fun saveJsonToSharedPrefs(context: Context, key: String, obj: Any) { + val sharedPreferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + val editor = sharedPreferences.edit() + val gson = Gson() + + // Convert object to JSON + val jsonString = gson.toJson(obj) + + // Save JSON string in SharedPreferences + editor.putString(key, jsonString) + editor.apply() +} + + +// Function to retrieve a JSON object +inline fun getJsonFromSharedPrefs(context: Context, key: String): T? { + val sharedPreferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + val gson = Gson() + + // Retrieve JSON string + val jsonString = sharedPreferences.getString(key, null) + + // Convert JSON string back to object + return if (jsonString != null) { + gson.fromJson(jsonString, T::class.java) + } else { + null + } +} + + diff --git a/data/src/main/kotlin/com/alfresco/content/data/CommonRepository.kt b/data/src/main/kotlin/com/alfresco/content/data/CommonRepository.kt index 209b0328c..8f3c63fd5 100644 --- a/data/src/main/kotlin/com/alfresco/content/data/CommonRepository.kt +++ b/data/src/main/kotlin/com/alfresco/content/data/CommonRepository.kt @@ -9,6 +9,8 @@ import com.alfresco.events.EventBus import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import retrofit2.http.Url +import java.net.URL class CommonRepository(otherSession: Session? = null) { @@ -32,5 +34,14 @@ class CommonRepository(otherSession: Session? = null) { session.createService(MobileConfigApi::class.java) } - suspend fun getMobileConfigData() = MobileConfigDataEntry.with(service.getMobileConfig("https://run.mocky.io/v3/7b75235d-381a-4cdb-9eed-2eec246f38f4")) + suspend fun getMobileConfigData() { + + val data = MobileConfigDataEntry.with(service.getMobileConfig("https://${URL(session.account.serverUrl).host}/app-config.json")) + + saveJsonToSharedPrefs(context, KEY_FEATURES_MOBILE, data) + } + + companion object { + const val KEY_FEATURES_MOBILE = "features_mobile" + } } diff --git a/data/src/main/kotlin/com/alfresco/content/data/ContextualActionData.kt b/data/src/main/kotlin/com/alfresco/content/data/ContextualActionData.kt index ce9518048..06797b00b 100644 --- a/data/src/main/kotlin/com/alfresco/content/data/ContextualActionData.kt +++ b/data/src/main/kotlin/com/alfresco/content/data/ContextualActionData.kt @@ -7,13 +7,15 @@ import kotlinx.parcelize.Parcelize data class ContextualActionData( val entries: List = emptyList(), val isMultiSelection: Boolean = false, + val mobileConfigData: MobileConfigDataEntry? = null, ) : Parcelable { companion object { - fun withEntries(entries: List, isMultiSelection: Boolean = false): ContextualActionData { + fun withEntries(entries: List, isMultiSelection: Boolean = false, mobileConfigData: MobileConfigDataEntry? = null): ContextualActionData { return ContextualActionData( entries = entries, isMultiSelection = isMultiSelection, + mobileConfigData = mobileConfigData ) } } diff --git a/data/src/main/kotlin/com/alfresco/content/data/MobileConfigDataEntry.kt b/data/src/main/kotlin/com/alfresco/content/data/MobileConfigDataEntry.kt index 58bc3cc31..832077582 100644 --- a/data/src/main/kotlin/com/alfresco/content/data/MobileConfigDataEntry.kt +++ b/data/src/main/kotlin/com/alfresco/content/data/MobileConfigDataEntry.kt @@ -1,12 +1,15 @@ package com.alfresco.content.data +import android.os.Parcelable import com.alfresco.content.models.DynamicMenu import com.alfresco.content.models.Features import com.alfresco.content.models.MobileConfigData +import kotlinx.parcelize.Parcelize +@Parcelize data class MobileConfigDataEntry( val featuresMobile: MobileFeatures? = null, -) { +) : Parcelable { companion object { fun with(configData: MobileConfigData): MobileConfigDataEntry { @@ -17,9 +20,10 @@ data class MobileConfigDataEntry( } } +@Parcelize data class MobileFeatures( val menus: List = emptyList(), -) { +) : Parcelable { companion object { fun with(features: Features?): MobileFeatures { @@ -34,11 +38,12 @@ data class MobileFeatures( } } +@Parcelize data class AppMenu( val id: String, val name: String, val enabled: Boolean, -) { +) : Parcelable { companion object { fun with(menuData: DynamicMenu): AppMenu { return AppMenu( diff --git a/listview/src/main/kotlin/com/alfresco/content/listview/ListFragment.kt b/listview/src/main/kotlin/com/alfresco/content/listview/ListFragment.kt index 72d93bed8..de320f9ab 100644 --- a/listview/src/main/kotlin/com/alfresco/content/listview/ListFragment.kt +++ b/listview/src/main/kotlin/com/alfresco/content/listview/ListFragment.kt @@ -33,11 +33,13 @@ import com.alfresco.content.actions.ActionStartProcess import com.alfresco.content.actions.ActionUpdateFileFolder import com.alfresco.content.actions.ContextualActionsSheet import com.alfresco.content.common.EntryListener +import com.alfresco.content.data.CommonRepository.Companion.KEY_FEATURES_MOBILE import com.alfresco.content.data.ContextualActionData import com.alfresco.content.data.Entry import com.alfresco.content.data.MultiSelection import com.alfresco.content.data.MultiSelectionData import com.alfresco.content.data.ResponsePaging +import com.alfresco.content.data.getJsonFromSharedPrefs import com.alfresco.content.listview.ListViewModel.Companion.MULTI_SELECTION_LIMIT import com.alfresco.content.simpleController import com.alfresco.events.on @@ -371,6 +373,6 @@ abstract class ListFragment, S : ListViewState>(layoutID: open fun onItemLongClicked(entry: Entry) {} open fun onItemMoreClicked(entry: Entry) { - ContextualActionsSheet.with(ContextualActionData.withEntries(listOf(entry))).show(childFragmentManager, null) + ContextualActionsSheet.with(ContextualActionData.withEntries(listOf(entry), mobileConfigData = getJsonFromSharedPrefs(requireContext(), KEY_FEATURES_MOBILE))).show(childFragmentManager, null) } }