Skip to content

Create New Article added #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class ArticleData(
@Json(name = "body")
val body: String,
val body: String?=null,
@Json(name = "description")
val description: String,
val description: String?=null,
@Json(name = "tagList")
val tagList: List<String>,
val tagList: List<String>?=null,
@Json(name = "title")
val title: String
val title: String?=null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.realworld.api.models.requests

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import io.realworld.api.models.entities.Article
import io.realworld.api.models.entities.ArticleData


@JsonClass(generateAdapter = true)
data class UpsertArticleRequest(
@Json(name ="article")
val article : ArticleData
)
6 changes: 6 additions & 0 deletions api/src/main/java/io/realworld/api/services/ConduitAuthAPI.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.realworld.api.services

import io.realworld.api.models.requests.UpsertArticleRequest
import io.realworld.api.models.requests.UserUpdateRequest
import io.realworld.api.models.responses.ArticleResponse
import io.realworld.api.models.responses.ArticlesResponse
Expand Down Expand Up @@ -45,4 +46,9 @@ interface ConduitAuthAPI {
suspend fun unfavoriteArticle(
@Path("slug") slug: String
): Response<ArticleResponse>

@POST("articles")
suspend fun createArticle(
@Body article: UpsertArticleRequest
) :Response<ArticleResponse>
}
23 changes: 23 additions & 0 deletions app/src/main/java/io/realworld/android/data/ArticlesRepo.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
package io.realworld.android.data

import io.realworld.api.ConduitClient
import io.realworld.api.models.entities.Article
import io.realworld.api.models.entities.ArticleData
import io.realworld.api.models.requests.UpsertArticleRequest

object ArticlesRepo {
val api = ConduitClient.publicApi
val authApi = ConduitClient.authApi

suspend fun getGlobalFeed() = api.getArticles().body()?.articles
suspend fun getMyFeed() = authApi.getFeedArticles().body()?.articles

suspend fun createArticle(
title:String?,
description:String?,
body:String?,
tagList:List<String>?=null
) : Article? {
val response =authApi.createArticle(
UpsertArticleRequest(
ArticleData(
title=title,
description = description,
body = body,
tagList = tagList
)
)
)

return response.body()?.article
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import io.realworld.android.data.ArticlesRepo
import io.realworld.api.ConduitClient
import io.realworld.api.models.entities.Article
import kotlinx.coroutines.launch
Expand All @@ -20,4 +21,21 @@ class ArticleViewModel : ViewModel() {
response.body()?.article.let { _article.postValue(it) }

}


fun createArticle(
title:String?,
description:String?,
body:String?,
tagList:List<String>?=null
) =viewModelScope.launch {
val article = ArticlesRepo.createArticle(
title=title,
description = description,
body=body,
tagList = tagList
)
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.realworld.android.ui.article

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import io.realworld.android.databinding.FragmentCreateArticleBinding

class CreateArticleFragment: Fragment() {


private var _binding:FragmentCreateArticleBinding?= null
private lateinit var articleViewModel:ArticleViewModel

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding= FragmentCreateArticleBinding.inflate(layoutInflater,container,false)
articleViewModel= ViewModelProvider(this).get(ArticleViewModel::class.java)


return _binding?.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

_binding?.apply {
submitButton.setOnClickListener{
articleViewModel.createArticle(
title=articleTitleTv.text.toString().takeIf { it.isNotBlank() },
description = articleDesciptionTv.text.toString().takeIf { it.isNotBlank() },
body = articleBodyTv.text.toString().takeIf{it.isNotBlank()},
tagList = articleTagTv.text.toString().split("\\s".toRegex())
)
Toast.makeText(requireContext(),"Article Published",Toast.LENGTH_SHORT).show()
}
}
}

}
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/ic_create_article.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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">

<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"/>

</vector>
38 changes: 38 additions & 0 deletions app/src/main/res/layout/fragment_create_article.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:hint="Article Title"
android:id="@+id/articleTitleTv"
android:inputType="textUri"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:hint="What's this article about?"
android:id="@+id/articleDesciptionTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:hint="Write your article (in markdown)"
android:lines="5"
android:gravity="top"
android:id="@+id/articleBodyTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:hint="Enter tags"
android:id="@+id/articleTagTv"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button
android:layout_gravity="center"
android:text="Publish Article"
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
6 changes: 5 additions & 1 deletion app/src/main/res/menu/menu_main_user.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
android:id="@+id/nav_my_feed"
android:icon="@drawable/ic_my_feed"
android:title="@string/menu_my_feed" />
<item
android:id="@+id/nav_create_article"
android:icon="@drawable/ic_create_article"
android:title="@string/create_article" />

<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_menu_settings"
android:title="@string/menu_settings" />
</group>
</menu>
</menu>
5 changes: 5 additions & 0 deletions app/src/main/res/navigation/navigation_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@
android:id="@+id/nav_article"
android:name="io.realworld.android.ui.article.ArticleFragment"
tools:layout="@layout/fragment_article" />

<fragment
android:id="@+id/nav_create_article"
android:name="io.realworld.android.ui.article.CreateArticleFragment"
tools:layout="@layout/fragment_create_article" />
</navigation>
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
<string name="menu_my_feed">My Feed</string>
<string name="menu_auth">Login / Signup</string>
<string name="menu_settings">Settings</string>
<string name="create_article">New Article</string>


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

</resources>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ allprojects {

task clean(type: Delete) {
delete rootProject.buildDir
}
}