Skip to content

Commit

Permalink
invoke the config data api
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-alfresco committed Sep 17, 2024
1 parent c5b117b commit e0f2b1a
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ 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<Entry> = emptyList(),
val isMultiSelection: Boolean = false,
val actions: List<Action> = emptyList(),
val topActions: List<Action> = emptyList(),
val fetch: Async<Entry> = 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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -31,6 +30,7 @@ class ContextualActionsViewModel(
var listener: EntryListener? = null

init {

if (!state.isMultiSelection) {
buildModelSingleSelection()
} else {
Expand All @@ -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 ->
Expand Down Expand Up @@ -120,8 +110,6 @@ class ContextualActionsViewModel(
else -> BrowseRepository()::fetchEntry.asFlow(entry.id)
}

private fun mobileConfigData(): Flow<MobileConfigDataEntry> = CommonRepository()::getMobileConfigData.asFlow()

fun execute(action: Action) =
action.execute(context, GlobalScope)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
41 changes: 41 additions & 0 deletions data/src/main/kotlin/com/alfresco/content/data/AppConfigUtils.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 <reified T> 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
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import kotlinx.parcelize.Parcelize
data class ContextualActionData(
val entries: List<Entry> = emptyList(),
val isMultiSelection: Boolean = false,
val mobileConfigData: MobileConfigDataEntry? = null,
) : Parcelable {
companion object {

fun withEntries(entries: List<Entry>, isMultiSelection: Boolean = false): ContextualActionData {
fun withEntries(entries: List<Entry>, isMultiSelection: Boolean = false, mobileConfigData: MobileConfigDataEntry? = null): ContextualActionData {
return ContextualActionData(
entries = entries,
isMultiSelection = isMultiSelection,
mobileConfigData = mobileConfigData
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -17,9 +20,10 @@ data class MobileConfigDataEntry(
}
}

@Parcelize
data class MobileFeatures(
val menus: List<AppMenu> = emptyList(),
) {
) : Parcelable {

companion object {
fun with(features: Features?): MobileFeatures {
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -371,6 +373,6 @@ abstract class ListFragment<VM : ListViewModel<S>, 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)
}
}

0 comments on commit e0f2b1a

Please sign in to comment.