I am generating the documentation for my public API/functions. It works well. But now I want to include the samples (Example code) also in the documentation. I know we can achieve it using #sample, but somehow I am not able to do it.
/**
#sample com.example.samples.Samples.doStuffSample
*/
fun doStuff(path: String, deviceID: String, newName: String,): Operation {
// Do stuff here
}
This is my Sample class in which, I have the sample codes.
class Samples {
fun doStuffSample() {
Manager.doStuff("path", "deviceID", "newName", object : Delegate {
override fun onSuccess() {
// Success
}
override fun onError() {
// Error
}
})
}
}
This is build.gradle file.
dokkaJavadoc.configure {
dokkaSourceSets {
named("main") {
skipDeprecated.set(true)
skipEmptyPackages.set(true)
includeNonPublic.set(false)
reportUndocumented.set(true)
perPackageOption {
skipEmptyPackages.set(true)
includeNonPublic.set(false)
matchingRegex.set("//package name")
suppress.set(false)
matchingRegex.set("//package name")
suppress.set(true)
}
}
}
}
In generated documentation, I can only see "com.example.samples.Samples.doStuffSample" below the "Samples" header.
How can I achieve this? Any help is appreciated.
Related
I'm trying to implement a transform using ASM, in order to replace method like Log.d to MyLog.d. But registerTransform is not found, can anyone tell me what is wrong with my demo project? (https://github.com/thelou1s/android_kotlin_asm.git)
class SystracePlugin : Plugin<Project> {
override fun apply(project: Project) {
when {
project.plugins.hasPlugin("com.android.application") -> {
val extension = project.extensions.getByName ("android") /*as AppExtension*/
extension?.let {
it.registerTransform(TestTransFormO) //! method not found
println("SystracePluginTest.kt apply $it")
}
}
}
}
}
We can override function on Swift programming like below.
// ---------- BaseClass.swift -------------
public class BaseClass
{
public var method1:(Int) -> String { return doMethod1 }
public init() {}
}
// the extension could also be in a separate file
extension BaseClass
{
private func doMethod1(param:Int) -> String { return "BaseClass \(param)" }
}
// ---------- ClassA.swift ----------
public class A:BaseClass
{
override public var method1:(Int) -> String { return doMethod1 }
}
// this extension can be in a separate file but not in the same
// file as the BaseClass extension that defines its doMethod1 implementation
extension A
{
private func doMethod1(param:Int) -> String
{
return "A \(param) added to \(super.method1(param))"
}
}
// ---------- ClassB.swift ----------
public class B:A
{
override public var method1:(Int) -> String { return doMethod1 }
}
extension B
{
private func doMethod1(param:Int) -> String
{
return "B \(param) added to \(super.method1(param))"
}
}
But I try to call fun BaseClass.method1 in Kotlin like below;
class BaseClass {
fun method1(){
println("method1")
}
}
fun main() {
fun BaseClass.method1(){
println("method12")
}
BaseClass().method1()
}
It's print method1. If I add override, shows Modifier 'override' is not applicable to 'local function' error. Does anyone have a solution on this issue. I want to replace some features with the extension function without changing the original.
You can make your own extension function using the original one.
F.E.
Extension function:
fun String.toInt(){
this.toInt()
}
But you want to get the abs value
fun String.toPositiveInt(){
this.toInt().abs()
}
So I'm using the original toInt with a modification to get the number without the symbol.
I want to call several api and combine the response values of different object types.
val itemList : MutableList<BaseItem>
private fun fetchAllData() {
viewModelScope.launch{
val deferreds = listOf(
async { loadData1()},
async { loadData2()}
)
deferreds.awaitAll().forEach {
itemList.add(it)
}
}
}
I want to get a return by combining datatype1 and datatype2 into BaseItem.
Unable to return the callback data from the repository.
I think there's a way to do it using live data. What should I do?
fun loadData1(): ArrayList<DataType1> {
repository.getData1(param, callback) {
onSuccess(List<DataType1>) {
return
}
}
}
fun loadData2(): ArrayList<DataType2> {
repository.getData1(param, callback) {
onSuccess(List<DataType2>) {
return
}
}
}
I'll be waiting for your help.
Well, what I would do is I would switch repository functions to be suspend functions and write the code in synchronized way:
val itemsLiveData = MutableLiveData<BaseItem>()
private fun fetchAllData() = viewModelScope.launch{
try {
val itemList : MutableList<BaseItem>
itemsList.addAll(repository.loadData1(param))
itemsList.addAll(repository.loadData2(param))
itemsLiveData.postValue(itemsList)
} catch(e: Exception) {
// do something with exception
}
}
And if you want to call several Rest API for example, I would go with Retrofit which has built-in support for suspend functions since ver. 2.6.0
https://github.com/square/retrofit/blob/master/CHANGELOG.md#version-260-2019-06-05
I have a Dao class which returns List of Car objects as flow.
#Query("SELECT * FROM Car")
fun getAllCars(): Flow<List<Car>>
In my repository, I can use this Dao as follows
fun getAllCars(): Flow<List<Car>>
//Implementation
override fun getAllCars() = carDao.getAllCars()
I can observe this flow in view model and everything works and life was fine.
Now, after reading the post on Developer Android site about
A generic class that contains data and status about loading this data.
I got inspired, so I read one more post here which discuss about having Result class.
So, I have done some changes to repository and I am not able to solve them.
Error:
suspend fun getSomeData(): Flow<Result<List<Car>>> {
carDao.getAllCars().collect {
if (it.isNotEmpty()) {
return flowOf(Result.Success(it)) //<-- Here I am getting error from IDE
}
else {
val throwable = Throwable()
return flowOf(Result.Failure<List<Car>>(throwable)) //<-- Here I am getting error from IDE
}
}
}
The error is Return is not allowed here and Change to 'return#Collect'
What I want to achieve is:
// At view model side
viewmodelScope.launch {
repo.getSomeData().collect {
if (it == Result.Success) {
//Show data
}
else {
//Show empty screen
}
}
}
Is my approach of implementation of Result is wrong? I am not able to figure out what is wrong. Why I can't just return Flow from a flow
If you want to use Result, you should should return Result < YourClass>. It will look like that :
suspend fun getSomeData(): Result<Flow<List<Car>>> {
return carDao.getAllCars().collect {
if (it.isNotEmpty()) {
Result.Success(flowOf(it))
} else {
Result.Failure(Throwable()))
}
}
}
This is what your function should look like. Note there's no need for it to be a suspend fun.
fun getSomeData(): Flow<Result<List<Car>>> = flow {
carDao.getAllCars().collect {
if (it.isNotEmpty()) {
emit(Result.Success(it))
}
else {
emit(Result.Failure<List<Car>>(Throwable()))
}
}
}
But what it does is nothing more than adding a mapping step, which you can generalize.
fun <T> Flow<List<T>>.toResultFlow(): Flow<Result<List<T>>> = this.map {
if (it.isNotEmpty()) Result.Success(it)
else Result.Failure(Throwable())
}
I'm a newbie in Kotlin.
I'm building an app like Twitter.
I want to create custom class extends TwitterApiClient - to use more endpoints. Twitter's tutorial is here
Tutorial
Here's my code:
class TwitterApiList(session: TwitterSession) : TwitterApiClient(session) {
fun getHomeTimeline(): TwitterCustom {
return getService(TwitterCustom::class.java)
}
}
// TwitterCustom interface
public interface TwitterCustom {
#GET("/1.1/statuses/home_timeline.json")
fun home_timeline(#Query("count") count: Int?, #Query("since_id") since_id: Int?, #Query("max_id") max_id: Int?, cb: Callback<List<Tweet>>)
}
// And how I use it
val apiClient = TwitterApiList(TwitterCore.getInstance().sessionManager.activeSession)
apiClient.getHomeTimeline().home_timeline(null, null, null, object : Callback<List<Tweet>>() {
override fun success(result: Result<List<Tweet>>?) {
Log.d("result", result.toString())
}
override fun failure(exception: TwitterException?) {
Log.d("failed", exception?.message)
}
})
When I run app, it’s always crash with message “Service methods cannot return void.” at this line
apiClient.getHomeTimeline().home_timeline(null, null, null, object : Callback<List<Tweet>>()
Please help me to solve this problem.
Thank you all.
Your callback is calling methods that return Unit. I.e. success() has no return value, Log.d returns Unit, so the Unit is implied. Same goes for failure().
Look closer at the Callback class. If you have and IDE with "Intelli-sense" or the like, it will help you. Otherwise, look at the interface/class and determine how the methods are defined, specifically, look at the return type and either change your calls to match or change the Callback<T> methods to be Unit (assuming it is your interface). This link can help you figure out how to write your functions to return specific types.
Let's say you find something like this...
interface Callback<T> {
fun success(result: Result<T>) : T //notice the : T determines the type of the function
//... other members
}
Then your code might look like this...
apiClient.getHomeTimeline().home_timeline(null, null, null, object : Callback<List<Tweet>>() {
override fun success(result: Result<List<Tweet>>?) {
Log.d("result", result.toString())
return result.getMemberForT() // I made up the name, would need
// decl. for Result<T>
}
//...
}
But without the declarations for Callback and Result, I would only be guessing.