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
package com.example.todolist

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
4 changes: 2 additions & 2 deletions Android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.todo_list">
package="com.example.todolist">

<uses-permission android:name="android.permission.INTERNET"/>

Expand All @@ -12,7 +12,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.Todolist">
<activity
android:name=".MainActivity"
android:name="com.example.todolist.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
8 changes: 0 additions & 8 deletions Android/app/src/main/java/com/example/todo_list/Repository.kt

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.todo_list
package com.example.todolist

import android.os.Bundle
import android.view.MenuItem
Expand All @@ -7,35 +7,35 @@ 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.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.todolist.data.TasksRepository
import com.example.todolist.databinding.ActivityMainBinding
import com.example.todolist.history.HistoryAdapter
import com.example.todolist.tasks.data.Task
import com.google.android.material.navigation.NavigationView

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
private lateinit var binding: ActivityMainBinding
private lateinit var historyViewModel: HistoryViewModel
private lateinit var tasksViewModel: TasksViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

historyViewModel = ViewModelProvider(this, ViewModelFactory(HistoryRepository())).get(HistoryViewModel::class.java)
tasksViewModel = ViewModelProvider(this, ViewModelFactory(TasksRepository())).get(
TasksViewModel::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()
tasksViewModel.getHistories()
}

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

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

binding.todoTodoView.addTasks(tasksViewModel.getSomeTasks())
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
Expand Down
53 changes: 53 additions & 0 deletions Android/app/src/main/java/com/example/todolist/TasksViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.todolist

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.todolist.data.Repository
import com.example.todolist.history.data.HistoryCard
import com.example.todolist.tasks.data.Task
import kotlinx.coroutines.launch

class TasksViewModel(private val repository: Repository): ViewModel() {
private val _historyList = MutableLiveData<List<HistoryCard>>()
val historyList: LiveData<List<HistoryCard>> get() = _historyList

private val _checkLoading = MutableLiveData<Boolean>()
val checkLoading: LiveData<Boolean> get() = _checkLoading

fun getHistories() {
_checkLoading.value = true
viewModelScope.launch {
val response = repository.getHistories()
if (response.isSuccessful) {
_historyList.value = response.body()
_checkLoading.value = false
}
}
}

fun getSomeTasks(): List<Task> {
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"
)

return listOf(task1, task2)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.example.todo_list
package com.example.todolist

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

class ViewModelFactory(private val repository: Repository) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return if (modelClass.isAssignableFrom(HistoryViewModel::class.java)) {
HistoryViewModel(repository) as T
return if (modelClass.isAssignableFrom(TasksViewModel::class.java)) {
TasksViewModel(repository) as T
} else {
throw Exception("클래스가 존재하지 않습니다.")
}
Expand Down
19 changes: 19 additions & 0 deletions Android/app/src/main/java/com/example/todolist/data/Repository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.todolist.data

import com.example.todolist.history.data.HistoryCard
import com.example.todolist.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.todolist.data

import com.example.todolist.history.data.HistoryCard
import com.example.todolist.network.NetworkModule
import com.example.todolist.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
@@ -1,12 +1,12 @@
package com.example.todo_list.history
package com.example.todolist.history

import android.text.format.DateUtils
import android.widget.TextView
import androidx.core.text.HtmlCompat
import androidx.core.text.HtmlCompat.FROM_HTML_MODE_LEGACY
import androidx.databinding.BindingAdapter
import com.example.todo_list.R
import com.example.todo_list.history.data.HistoryCard
import com.example.todolist.R
import com.example.todolist.history.data.HistoryCard
import java.text.SimpleDateFormat
import java.util.*

Expand Down 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
Loading