Android SDK
Integration Help for iOS Platform
Installation
Appstent SDK on Android is currently released for the Jetpack Compose based presentation layer, under the name AppstentCompose.
AppstentCompose can be imported using the following gradle reference:
implementation("com.github.Appversation:AppstentCompose:v1.7.5")
Once the SDK package is added to the project AppstentCompose SDK classes can by referenced using the import statement as follows:
import com.appversation.appstentcompose.*
Usage Guide
Connecting the SDK with an account
Each Account is access controlled through an API key. When initializing the SDK, the API key can be set by using a static property apiKey
of the class ModuleConfigs, as below:
ModuleConfigs.apiKey = "<API Key>"
Load the View Document Content
Loading the View Content requires the complete path to the content document, by using the ViewContentRepository.getContent
method like below:
class AppstentViewModel (
private val appstentRepository: ViewContentRepository,
private val contentPath: String): ViewModel() {
private val _viewContent = MutableStateFlow(JSONObject())
val viewContent: StateFlow<JSONObject> get() = _viewContent
init {
ModuleConfigs.apiKey = "<API Key>"
ModuleConfigs.customContentDataProvider = CustomContentDataProvider()
getViewContent()
}
private fun getViewContent() {
viewModelScope.launch(Dispatchers.Default) {
val content = appstentRepository.getContent(contentPath)
_viewContent.emit(content)
}
}
}
Load the Content View
Once the view content is loaded, the AppstentView
which is a composable, can be loaded using the view content in the initializer.
Complete Sample Code to Initialize and Load a Content View
Here is a sample code to initialize and load content view:
import com.appversation.appstentcompose.AppstentView
import com.appversation.appstentcompose.ViewContentRepository
class MainActivity: ComponentActivity() {
private val viewModel: AppstentViewModel = AppstentViewModel((ViewContentRepository()), "text/textWithAttributes")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AppstentView(viewModel)
}
}
@Composable
fun AppstentView(viewModel: AppstentViewModel) {
val viewContent by viewModel.viewContent.collectAsState()
AppstentView(viewContent = viewContent)
}
}
In the sample code above a content document named text/textWithAttributes
is getting loaded as the main view of the App.
Advanced Usage
Custom Views
While Appstent can be used to build content based views, if you want to also manage the placement of the existing/custom application views with in a content view, you can provide a view provider to AppstentCompose SDK by implementing the interface CustomContentDataProvider
and implement the following method:
@Composable
fun CustomComposable(viewName: String)
AppstentCompose SDK will call this method to get a view from your App, with the custom view's name it finds in the content JSON.
To make sure that AppstentCompose SDK calls the CustomComposable
method though, you need to provide the object of the class/struct implementing the CustomContentDataProvider
interface to a static member of ModuleConfigs object like this:
ModuleConfigs.customContentDataProvider = CustomContentDataProvider()
Here is a sample implementation of the CustomComposable
method.
@Composable
override fun CustomComposable(viewName: String) {
when (viewName) {
"view1" -> Text(text = "This is a custom view 1")
else -> Text(text = "This is a default custom view")
}
}
Visibility Control
Appstent views' visibility can be controlled with the visibility rules. Some of these pre-defined rules can written as part of view content JSON, however, App developers can also provide their custom visibility rule by implementing the following method of CustomContentDataProvider
.
fun visibility(ruleName: String, ruleValue: String): Boolean
AppstentCompose SDK will call this method for each of the visibility rules defined for a view content. Here is a sample implementation of using custom visibility rules to control view's visibility.
override fun visibility(ruleName: String, ruleValue: String): Boolean {
when (ruleName) {
"userState" -> return ruleValue == "loggedIn" && userIsLoggedIn()
}
return true
}
In the above example, a custom visibility rule of userState
is being used to show content view only when the user authenticated state is detected to be Logged In.
Custom Data
When you want to control the layout of the views with Appstent CMS but need to render your App's data with in those content views, you can provide your data provider to AppstentCompose SDK to plug your data fields as part of content views, by implementing the interface CustomContentDataProvider
and implement the following method:
fun getStringFor(fieldName: String): String
AppstentCompose SDK will call this method to get the data value from your App, with the custom view's field name it finds in the content JSON.
o make sure that AppstentCompose SDK calls the CustomComposable
method though, you need to provide the object of the class/struct implementing the CustomContentDataProvider
interface to a static member of ModuleConfigs object like this:
ModuleConfigs.customContentDataProvider = CustomContentDataProvider()
Here is a sample implementation of the getStringFor(fieldName: String)
method.
override fun getStringFor(fieldName: String): String {
return when (fieldName) {
"landmarkName" -> "Turtle Rock from Android"
else -> ""
}
}
Last updated