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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations.xml

+10
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

+4-4
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
)
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

+6
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
}
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

+18
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
}
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+
}
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<LinearLayout
2+
xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:padding="10dp"
4+
android:orientation="vertical"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
<EditText
8+
android:hint="Article Title"
9+
android:id="@+id/articleTitleTv"
10+
android:inputType="textUri"
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"/>
13+
<EditText
14+
android:hint="What's this article about?"
15+
android:id="@+id/articleDesciptionTv"
16+
android:layout_width="match_parent"
17+
android:layout_height="wrap_content"/>
18+
<EditText
19+
android:hint="Write your article (in markdown)"
20+
android:lines="5"
21+
android:gravity="top"
22+
android:id="@+id/articleBodyTv"
23+
android:layout_width="match_parent"
24+
android:layout_height="wrap_content"/>
25+
<EditText
26+
android:hint="Enter tags"
27+
android:id="@+id/articleTagTv"
28+
android:inputType="textEmailAddress"
29+
android:layout_width="match_parent"
30+
android:layout_height="wrap_content"/>
31+
32+
<Button
33+
android:layout_gravity="center"
34+
android:text="Publish Article"
35+
android:id="@+id/submitButton"
36+
android:layout_width="wrap_content"
37+
android:layout_height="wrap_content"/>
38+
</LinearLayout>

app/src/main/res/menu/menu_main_user.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
android:id="@+id/nav_my_feed"
1313
android:icon="@drawable/ic_my_feed"
1414
android:title="@string/menu_my_feed" />
15+
<item
16+
android:id="@+id/nav_create_article"
17+
android:icon="@drawable/ic_create_article"
18+
android:title="@string/create_article" />
1519

1620
<item
1721
android:id="@+id/nav_settings"
1822
android:icon="@drawable/ic_menu_settings"
1923
android:title="@string/menu_settings" />
2024
</group>
21-
</menu>
25+
</menu>

app/src/main/res/navigation/navigation_main.xml

+5
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@
5151
android:id="@+id/nav_article"
5252
android:name="io.realworld.android.ui.article.ArticleFragment"
5353
tools:layout="@layout/fragment_article" />
54+
55+
<fragment
56+
android:id="@+id/nav_create_article"
57+
android:name="io.realworld.android.ui.article.CreateArticleFragment"
58+
tools:layout="@layout/fragment_create_article" />
5459
</navigation>

app/src/main/res/values/strings.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
<string name="menu_my_feed">My Feed</string>
1212
<string name="menu_auth">Login / Signup</string>
1313
<string name="menu_settings">Settings</string>
14+
<string name="create_article">New Article</string>
1415

1516

1617
<string name="arg_article_id">ARTICLE_ID</string>
1718

18-
</resources>
19+
</resources>

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ allprojects {
2828

2929
task clean(type: Delete) {
3030
delete rootProject.buildDir
31-
}
31+
}

0 commit comments

Comments
 (0)