parent
8577dabf76
commit
e089cb7e7b
@ -1,10 +1,15 @@
|
||||
package fr.uca.iut.clfreville2.teaiswarm
|
||||
|
||||
import android.app.Application
|
||||
import fr.uca.iut.clfreville2.teaiswarm.db.TeaDatabase
|
||||
import fr.uca.iut.clfreville2.teaiswarm.network.GiteaService
|
||||
import fr.uca.iut.clfreville2.teaiswarm.network.RepositoryService
|
||||
|
||||
object TeaIsWarm {
|
||||
class TeaIsWarm : Application() {
|
||||
|
||||
val database: TeaDatabase by lazy {
|
||||
TeaDatabase.getInstance(this)
|
||||
}
|
||||
val service: RepositoryService by lazy {
|
||||
GiteaService()
|
||||
}
|
||||
|
@ -0,0 +1,71 @@
|
||||
package fr.uca.iut.clfreville2.teaiswarm.adapter
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.EditText
|
||||
import android.widget.TextView
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.asLiveData
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import fr.uca.iut.clfreville2.teaiswarm.R
|
||||
import fr.uca.iut.clfreville2.teaiswarm.db.CommentDao
|
||||
import fr.uca.iut.clfreville2.teaiswarm.db.CommentEntity
|
||||
import fr.uca.iut.clfreville2.teaiswarm.model.RepositoryIdentifier
|
||||
|
||||
class CommentListAdapter : ListAdapter<CommentEntity, CommentListAdapter.CommentViewHolder>(CommentComparator) {
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommentViewHolder {
|
||||
val view: View = LayoutInflater.from(parent.context)
|
||||
.inflate(R.layout.comment_view_item, parent, false)
|
||||
return CommentViewHolder(view)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: CommentViewHolder, position: Int) {
|
||||
val current = getItem(position)
|
||||
holder.bind(current.comment)
|
||||
}
|
||||
|
||||
class CommentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
private val commentText: EditText = itemView.findViewById(R.id.comment_edit_text)
|
||||
|
||||
fun bind(text: String?) {
|
||||
commentText.setText(text)
|
||||
}
|
||||
}
|
||||
|
||||
object CommentComparator : DiffUtil.ItemCallback<CommentEntity>() {
|
||||
override fun areItemsTheSame(oldItem: CommentEntity, newItem: CommentEntity): Boolean {
|
||||
return oldItem === newItem
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: CommentEntity, newItem: CommentEntity): Boolean {
|
||||
return oldItem.comment == newItem.comment
|
||||
}
|
||||
}
|
||||
|
||||
class CommentViewModel(
|
||||
repository: RepositoryIdentifier,
|
||||
commentDao: CommentDao
|
||||
) : ViewModel() {
|
||||
val allItems: LiveData<List<CommentEntity>> =
|
||||
commentDao.getForRepository(repository.owner, repository.name).asLiveData()
|
||||
}
|
||||
|
||||
class CommentViewModelFactory(
|
||||
private val repository: RepositoryIdentifier,
|
||||
private val commentDao: CommentDao
|
||||
) : ViewModelProvider.Factory {
|
||||
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
||||
if (modelClass.isAssignableFrom(CommentViewModel::class.java)) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return CommentViewModel(repository, commentDao) as T
|
||||
}
|
||||
throw IllegalArgumentException("Unknown ViewModel class")
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,11 @@
|
||||
package fr.uca.iut.clfreville2.teaiswarm
|
||||
package fr.uca.iut.clfreville2.teaiswarm.adapter
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.TextView
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import fr.uca.iut.clfreville2.teaiswarm.R
|
||||
import fr.uca.iut.clfreville2.teaiswarm.model.VersionedFile
|
||||
|
||||
class FileListAdapter(private val dataSet: List<VersionedFile>, private val onClick: (VersionedFile) -> Unit) :
|
@ -0,0 +1,20 @@
|
||||
package fr.uca.iut.clfreville2.teaiswarm.db
|
||||
|
||||
import androidx.room.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface CommentDao {
|
||||
|
||||
@Insert
|
||||
fun insert(comment: CommentEntity)
|
||||
|
||||
@Update
|
||||
fun update(comment: CommentEntity)
|
||||
|
||||
@Delete
|
||||
fun delete(comment: CommentEntity)
|
||||
|
||||
@Query("SELECT * FROM comment WHERE owner = :owner AND repository = :repository")
|
||||
fun getForRepository(owner: String, repository: String): Flow<List<CommentEntity>>
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package fr.uca.iut.clfreville2.teaiswarm.db
|
||||
|
||||
import androidx.room.Entity
|
||||
|
||||
@Entity(tableName = "comment", primaryKeys = ["id"])
|
||||
data class CommentEntity(val id: Int? = null, val owner: String, val repository: String, val comment: String)
|
@ -0,0 +1,26 @@
|
||||
package fr.uca.iut.clfreville2.teaiswarm.db
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
|
||||
@Database(entities = [CommentEntity::class], version = 1, exportSchema = false)
|
||||
abstract class TeaDatabase : RoomDatabase() {
|
||||
|
||||
abstract fun commentDao(): CommentDao
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var instance: TeaDatabase? = null
|
||||
|
||||
fun getInstance(ctx: Context): TeaDatabase =
|
||||
instance ?: synchronized(this) {
|
||||
Room.databaseBuilder(
|
||||
ctx.applicationContext,
|
||||
TeaDatabase::class.java,
|
||||
"tea.db"
|
||||
).build().also { instance = it }
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package fr.uca.iut.clfreville2.teaiswarm.fragment
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import fr.uca.iut.clfreville2.teaiswarm.R
|
||||
import fr.uca.iut.clfreville2.teaiswarm.REPOSITORY_NAME
|
||||
import fr.uca.iut.clfreville2.teaiswarm.REPOSITORY_OWNER
|
||||
import fr.uca.iut.clfreville2.teaiswarm.TeaIsWarm
|
||||
import fr.uca.iut.clfreville2.teaiswarm.adapter.CommentListAdapter
|
||||
import fr.uca.iut.clfreville2.teaiswarm.db.CommentEntity
|
||||
import fr.uca.iut.clfreville2.teaiswarm.model.RepositoryIdentifier
|
||||
|
||||
class CommentListFragment : Fragment() {
|
||||
|
||||
private lateinit var recyclerView: RecyclerView
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
val owner = arguments?.getString(REPOSITORY_OWNER)!!
|
||||
val repo = arguments?.getString(REPOSITORY_NAME)!!
|
||||
val repository = RepositoryIdentifier(owner, repo)
|
||||
val viewModel: CommentListAdapter.CommentViewModel by activityViewModels {
|
||||
CommentListAdapter.CommentViewModelFactory(
|
||||
repository,
|
||||
(activity?.application as TeaIsWarm).database.commentDao()
|
||||
)
|
||||
}
|
||||
|
||||
(activity?.application as TeaIsWarm).database.commentDao().insert(
|
||||
CommentEntity(
|
||||
null,
|
||||
owner,
|
||||
repo,
|
||||
"test"
|
||||
)
|
||||
)
|
||||
|
||||
recyclerView = view.findViewById(R.id.comment_list)
|
||||
|
||||
val adapter = CommentListAdapter()
|
||||
recyclerView.adapter = adapter
|
||||
viewModel.allItems.observe(this.viewLifecycleOwner) { items ->
|
||||
items.let {
|
||||
adapter.submitList(it)
|
||||
}
|
||||
}
|
||||
recyclerView.layoutManager = LinearLayoutManager(context)
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/comment_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
</LinearLayout>
|
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/comment_edit_text"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="150dp"
|
||||
android:inputType="text|textMultiLine" />
|
||||
|
||||
</LinearLayout>
|
Loading…
Reference in new issue