Skip to content

Commit 95f9e93

Browse files
Merge pull request #1 from ksog66/master
Create New Article added
2 parents 01ca616 + b658a81 commit 95f9e93

File tree

15 files changed

+179
-9
lines changed

15 files changed

+179
-9
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/src/main/java/io/realworld/api/models/entities/ArticleData.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import com.squareup.moshi.JsonClass
77
@JsonClass(generateAdapter = true)
88
data class ArticleData(
99
@Json(name = "body")
10-
val body: String,
10+
val body: String?=null,
1111
@Json(name = "description")
12-
val description: String,
12+
val description: String?=null,
1313
@Json(name = "tagList")
14-
val tagList: List<String>,
14+
val tagList: List<String>?=null,
1515
@Json(name = "title")
16-
val title: String
16+
val title: String?=null
1717
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.realworld.api.models.requests
2+
3+
import com.squareup.moshi.Json
4+
import com.squareup.moshi.JsonClass
5+
import io.realworld.api.models.entities.Article
6+
import io.realworld.api.models.entities.ArticleData
7+
8+
9+
@JsonClass(generateAdapter = true)
10+
data class UpsertArticleRequest(
11+
@Json(name ="article")
12+
val article : ArticleData
13+
)

api/src/main/java/io/realworld/api/services/ConduitAuthAPI.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.realworld.api.services
22

3+
import io.realworld.api.models.requests.UpsertArticleRequest
34
import io.realworld.api.models.requests.UserUpdateRequest
45
import io.realworld.api.models.responses.ArticleResponse
56
import io.realworld.api.models.responses.ArticlesResponse
@@ -45,4 +46,9 @@ interface ConduitAuthAPI {
4546
suspend fun unfavoriteArticle(
4647
@Path("slug") slug: String
4748
): Response<ArticleResponse>
49+
50+
@POST("articles")
51+
suspend fun createArticle(
52+
@Body article: UpsertArticleRequest
53+
) :Response<ArticleResponse>
4854
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
11
package io.realworld.android.data
22

33
import io.realworld.api.ConduitClient
4+
import io.realworld.api.models.entities.Article
5+
import io.realworld.api.models.entities.ArticleData
6+
import io.realworld.api.models.requests.UpsertArticleRequest
47

58
object ArticlesRepo {
69
val api = ConduitClient.publicApi
710
val authApi = ConduitClient.authApi
811

912
suspend fun getGlobalFeed() = api.getArticles().body()?.articles
1013
suspend fun getMyFeed() = authApi.getFeedArticles().body()?.articles
14+
15+
suspend fun createArticle(
16+
title:String?,
17+
description:String?,
18+
body:String?,
19+
tagList:List<String>?=null
20+
) : Article? {
21+
val response =authApi.createArticle(
22+
UpsertArticleRequest(
23+
ArticleData(
24+
title=title,
25+
description = description,
26+
body = body,
27+
tagList = tagList
28+
)
29+
)
30+
)
31+
32+
return response.body()?.article
33+
}
1134
}

app/src/main/java/io/realworld/android/ui/article/ArticleViewModel.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData
44
import androidx.lifecycle.MutableLiveData
55
import androidx.lifecycle.ViewModel
66
import androidx.lifecycle.viewModelScope
7+
import io.realworld.android.data.ArticlesRepo
78
import io.realworld.api.ConduitClient
89
import io.realworld.api.models.entities.Article
910
import kotlinx.coroutines.launch
@@ -20,4 +21,21 @@ class ArticleViewModel : ViewModel() {
2021
response.body()?.article.let { _article.postValue(it) }
2122

2223
}
24+
25+
26+
fun createArticle(
27+
title:String?,
28+
description:String?,
29+
body:String?,
30+
tagList:List<String>?=null
31+
) =viewModelScope.launch {
32+
val article = ArticlesRepo.createArticle(
33+
title=title,
34+
description = description,
35+
body=body,
36+
tagList = tagList
37+
)
38+
}
39+
40+
2341
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.realworld.android.ui.article
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import android.widget.Toast
8+
import androidx.fragment.app.Fragment
9+
import androidx.lifecycle.ViewModelProvider
10+
import io.realworld.android.databinding.FragmentCreateArticleBinding
11+
12+
class CreateArticleFragment: Fragment() {
13+
14+
15+
private var _binding:FragmentCreateArticleBinding?= null
16+
private lateinit var articleViewModel:ArticleViewModel
17+
18+
override fun onCreateView(
19+
inflater: LayoutInflater,
20+
container: ViewGroup?,
21+
savedInstanceState: Bundle?
22+
): View? {
23+
_binding= FragmentCreateArticleBinding.inflate(layoutInflater,container,false)
24+
articleViewModel= ViewModelProvider(this).get(ArticleViewModel::class.java)
25+
26+
27+
return _binding?.root
28+
}
29+
30+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
31+
super.onViewCreated(view, savedInstanceState)
32+
33+
_binding?.apply {
34+
submitButton.setOnClickListener{
35+
articleViewModel.createArticle(
36+
title=articleTitleTv.text.toString().takeIf { it.isNotBlank() },
37+
description = articleDesciptionTv.text.toString().takeIf { it.isNotBlank() },
38+
body = articleBodyTv.text.toString().takeIf{it.isNotBlank()},
39+
tagList = articleTagTv.text.toString().split("\\s".toRegex())
40+
)
41+
Toast.makeText(requireContext(),"Article Published",Toast.LENGTH_SHORT).show()
42+
}
43+
}
44+
}
45+
46+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp">
3+
4+
<path android:fillColor="@android:color/white" android:pathData="M14,10L2,10v2h12v-2zM14,6L2,6v2h12L14,6zM18,14v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zM2,16h8v-2L2,14v2z"/>
5+
6+
</vector>

0 commit comments

Comments
 (0)