# Android SDK

Installation

Appstent SDK on Android is currently released for the Jetpack Compose based presentation layer, under the name AppstentCompose. &#x20;

AppstentCompose can be imported using the following gradle reference:

```kts
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:

```kotlin
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:

```kotlin
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:

```kotlin
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:

```swift
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:

```kotlin
@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:

```kotlin
ModuleConfigs.customContentDataProvider = CustomContentDataProvider()
```

Here is a sample implementation of the `CustomComposable` method.

```kotlin
@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`.

```kotlin
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.

<pre class="language-kotlin"><code class="lang-kotlin">override fun visibility(ruleName: String, ruleValue: String): Boolean {

<strong>    when (ruleName) {
</strong>        "userState" -> return ruleValue == "loggedIn" &#x26;&#x26; userIsLoggedIn()
    }
    return true
}
</code></pre>

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:

```kotlin
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:

```kotlin
ModuleConfigs.customContentDataProvider = CustomContentDataProvider()
```

Here is a sample implementation of the `getStringFor(fieldName: String)` method.

```kotlin
override fun getStringFor(fieldName: String): String {

    return when (fieldName) {
        "landmarkName"  -> "Turtle Rock from Android"
        else            -> ""
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://appversation.gitbook.io/appstent/integrations/android-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
