Skip to content
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

[Team-01][Android] Todo 화면 및 레포지토리 구현 #151

Open
wants to merge 11 commits into
base: team-01
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.example.todo_list

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
Expand Down
38 changes: 30 additions & 8 deletions Android/app/src/main/java/com/example/todo_list/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.todo_list.data.TasksRepository
import com.example.todo_list.databinding.ActivityMainBinding
import com.example.todo_list.history.HistoryAdapter
import com.example.todo_list.history.HistoryViewModel
import com.example.todo_list.history.data.HistoryRepository
import com.example.todo_list.tasks.data.Task
import com.google.android.material.navigation.NavigationView

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
Expand All @@ -22,19 +22,19 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

historyViewModel = ViewModelProvider(this, ViewModelFactory(HistoryRepository())).get(HistoryViewModel::class.java)
historyViewModel = ViewModelProvider(this, ViewModelFactory(TasksRepository())).get(HistoryViewModel::class.java)

val adapter = HistoryAdapter()
binding.recyclerviewHistory.adapter = adapter
binding.recyclerviewHistory.layoutManager = LinearLayoutManager(this)
val historyAdapter = HistoryAdapter()
binding.recyclerviewHistory.adapter = historyAdapter
binding.btnMenu.setOnClickListener {
binding.mainLayout.openDrawer(GravityCompat.END)
historyViewModel.getHistory()
historyViewModel.getHistories()
}

binding.btnClose.setOnClickListener { binding.mainLayout.closeDrawer(GravityCompat.END) }
binding.naviView.setNavigationItemSelectedListener(this)

historyViewModel.historyList.observe(this) { adapter.submitList(it) }
historyViewModel.historyList.observe(this) { historyAdapter.submitList(it) }
historyViewModel.checkLoading.observe(this) {
if (it) {
binding.spinner.visibility = View.VISIBLE
Expand All @@ -44,6 +44,28 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
binding.recyclerviewHistory.visibility = View.VISIBLE
}
}

val task1 = Task(
1,
"테스트하기1",
"콘텐츠테스트1",
"jung",
"doing",
"2022-04-06T15:30:00.000+09:00",
"2022-04-06T15:30:00.000+09:00"
)

val task2 = Task(
2,
"테스트하기2",
"콘텐츠테스트2",
"park",
"todo",
"2022-04-06T15:30:00.000+09:00",
"2022-04-06T15:30:00.000+09:00"
)

binding.todoTodoView.addTasks(listOf(task1, task2))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요런 기능은 별도의 함수로 묶으면 보기에 조금 더 편해서 나중에 좋은 점이 있습니다. 혹은 task 가 바뀌게 되더라도 별도의 함수로 묶어 놓으면 바꿔야 할 부분이 명확하게 보일거 같습니다. 분리된 메소드의 위치는 activity/viewmodel 두 군데가 있을텐데 어느 부분이 더 일관적일지 보셔서 추가하시면 좋을거같네요~

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ViewModel을 앱 전체에서 사용할 수 있도록 파일명 및 위치를 변경했습니다.
ViewModel에서 함수로 묶어 사용하는 것이 더 나을 것 같다고 판단해서 ViewModel에 구현했습니다.
감사합니다!

}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
Expand Down
8 changes: 0 additions & 8 deletions Android/app/src/main/java/com/example/todo_list/Repository.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.todo_list

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.example.todo_list.data.Repository
import com.example.todo_list.history.HistoryViewModel

class ViewModelFactory(private val repository: Repository) : ViewModelProvider.Factory {
Expand Down
19 changes: 19 additions & 0 deletions Android/app/src/main/java/com/example/todo_list/data/Repository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.todo_list.data
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

패키지명이 todo_list 인가요? 특별한 이유가 없다면 소문자로 작성되는것이 컨벤션이라 맞추는게 좋을거 같습니다!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

변경 완료했습니다. 감사합니다!


import com.example.todo_list.history.data.HistoryCard
import com.example.todo_list.tasks.data.Task
import retrofit2.Response

interface Repository {
suspend fun getHistories(): Response<List<HistoryCard>>

suspend fun getAllTasks(): Response<List<Task>>

suspend fun getTask(id: Int): Response<Task>

suspend fun createTask(title: String, contents: String, user: String, status: String): Response<Task>

suspend fun deleteTask(id: Int)

suspend fun updateTask(id: Int, param: HashMap<String, String>): Response<Task>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.example.todo_list.data

import com.example.todo_list.history.data.HistoryCard
import com.example.todo_list.network.NetworkModule
import com.example.todo_list.tasks.data.Task
import retrofit2.Response

class TasksRepository : Repository {
private val network = NetworkModule.service

override suspend fun getHistories(): Response<List<HistoryCard>> {
return network.getHistories("histories")
}

override suspend fun getAllTasks(): Response<List<Task>> {
return network.getAllTasks()
}

override suspend fun getTask(id: Int): Response<Task> {
return network.getTask(id)
}

override suspend fun createTask(
title: String,
contents: String,
user: String,
status: String
): Response<Task> {
return network.createTask(
title,
contents,
user,
status
)
}

override suspend fun deleteTask(id: Int) {
network.deleteTask(id)
}

override suspend fun updateTask(id: Int, param: HashMap<String, String>): Response<Task> {
return network.updateTask(
id,
param
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ fun setBody(view: TextView, body: HistoryCard) {
HtmlCompat.fromHtml(
view.context.getString(
R.string.history_move_string,
body.todo.contents,
convertStatus(body.from_status),
convertStatus(body.to_status),
body.todoTitle,
convertStatus(body.fromStatus),
convertStatus(body.toStatus),
convertAction(body.action)
),
FROM_HTML_MODE_LEGACY
Expand All @@ -50,8 +50,8 @@ fun setBody(view: TextView, body: HistoryCard) {
HtmlCompat.fromHtml(
view.context.getString(
R.string.history_default_string,
convertStatus(body.todo.status),
body.todo.contents,
convertStatus(body.toStatus),
body.todoTitle,
convertAction(body.action)
),
FROM_HTML_MODE_LEGACY
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.todo_list.Repository
import com.example.todo_list.data.Repository
import com.example.todo_list.history.data.HistoryCard
import kotlinx.coroutines.launch

Expand All @@ -15,10 +15,10 @@ class HistoryViewModel(private val repository: Repository): ViewModel() {
private val _checkLoading = MutableLiveData<Boolean>()
val checkLoading: LiveData<Boolean> get() = _checkLoading

fun getHistory() {
fun getHistories() {
_checkLoading.value = true
viewModelScope.launch {
val response = repository.getHistory()
val response = repository.getHistories()
if (response.isSuccessful) {
_historyList.value = response.body()
_checkLoading.value = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
package com.example.todo_list.history.data

import com.google.gson.annotations.SerializedName

data class HistoryCard(
val id: Int,
val todo: Todo,

val action: String,
@SerializedName("from status")
val from_status: String,
@SerializedName("to status")
val to_status: String,
val createdDateTime: String
)

data class Todo(
val id: Int,
val title: String,
val contents: String,
val todoId: Int,
val todoTitle: String,
val user: String,
val status: String,
val createdDateTime: String,
val updatedDateTime: String
val action: String,
val fromStatus: String,
val toStatus: String,
val createdAt: String
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.todo_list.network

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class NetworkModule {
companion object RetrofitApiObject {
private const val BASE_URL = "https://f278a12c-c825-466b-aa01-65337bbdf28a.mock.pstmn.io/"

private val retrofit =
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
val service: TodoService = retrofit.create(TodoService::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.todo_list.network

import com.example.todo_list.history.data.HistoryCard
import com.example.todo_list.tasks.data.Task
import retrofit2.Response
import retrofit2.http.*

interface TodoService {

@GET("api/{histories}")
suspend fun getHistories(
@Path("histories") variable: String
): Response<List<HistoryCard>>

@GET("api/todos")
suspend fun getAllTasks(): Response<List<Task>>

@GET("api/todos/{id}")
suspend fun getTask(
@Path("id") id: Int
): Response<Task>

@FormUrlEncoded
@POST("api/todos")
suspend fun createTask(
@Field("title") title: String,
@Field("contents") contents: String,
@Field("user") user: String,
@Field("status") status: String,
): Response<Task>

@DELETE("api/todos/{id}")
suspend fun deleteTask(
@Path("id") id: Int
): Response<Unit>

@FormUrlEncoded
@PATCH("api/todos/{id}")
suspend fun updateTask(
@Path("id") id: Int,
@FieldMap param: HashMap<String, String>
): Response<Task>
}
Loading