-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from MuindiStephen/68-respective-gaps
68 respective gaps
- Loading branch information
Showing
36 changed files
with
1,216 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/src/main/java/com/steve_md/smartmkulima/adapter/others/LocalFarmCycleAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.steve_md.smartmkulima.adapter.others | ||
|
||
import android.annotation.SuppressLint | ||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.steve_md.smartmkulima.databinding.CropCycleTaskRowBinding | ||
import com.steve_md.smartmkulima.model.LocalFarmCycle | ||
|
||
/** | ||
* Attach data to locally created Farm cycles to Room DB | ||
*/ | ||
class LocalFarmCycleAdapter(private val onClickListener: OnClickListener) : | ||
ListAdapter<LocalFarmCycle, LocalFarmCycleAdapter.MyViewHolder>(TaskDiffUtil) { | ||
|
||
object TaskDiffUtil : DiffUtil.ItemCallback<LocalFarmCycle>() { | ||
override fun areItemsTheSame(oldItem: LocalFarmCycle, newItem: LocalFarmCycle): Boolean { | ||
return oldItem == newItem | ||
} | ||
|
||
override fun areContentsTheSame(oldItem: LocalFarmCycle, newItem: LocalFarmCycle): Boolean { | ||
return oldItem.cropName == newItem.cropName | ||
} | ||
} | ||
|
||
inner class MyViewHolder(private var binding: CropCycleTaskRowBinding) : | ||
RecyclerView.ViewHolder(binding.root) { | ||
|
||
@SuppressLint("SetTextI18n") | ||
fun bind(cycle: LocalFarmCycle?) { | ||
binding.farmID.text = cycle?.farmName | ||
binding.cycleData.text = cycle?.cropName | ||
binding.dateForCycle.text = cycle?.startDate | ||
binding.textView85.text = "Crop cycle" | ||
} | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { | ||
return MyViewHolder( | ||
CropCycleTaskRowBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
) | ||
} | ||
|
||
override fun onBindViewHolder(holder: MyViewHolder, position: Int) { | ||
val cycle = getItem(position) | ||
holder.bind(cycle) | ||
|
||
holder.itemView.setOnClickListener { | ||
onClickListener.onClick(cycle = cycle) | ||
} | ||
} | ||
|
||
class OnClickListener(val clickListener: (cycle: LocalFarmCycle) -> Unit) { | ||
fun onClick(cycle: LocalFarmCycle) = clickListener(cycle) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
app/src/main/java/com/steve_md/smartmkulima/adapter/others/LocalFarmCycleTasksAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.steve_md.smartmkulima.adapter.others | ||
|
||
import android.annotation.SuppressLint | ||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.steve_md.smartmkulima.databinding.DetailCycleRowBinding | ||
import com.steve_md.smartmkulima.model.LocalTasks | ||
|
||
/** | ||
* Able to view actual tasks for cycle - respective tasks | ||
*/ | ||
class LocalFarmCycleTasksAdapter : RecyclerView.Adapter<LocalFarmCycleTasksAdapter.TaskViewHolder>() { | ||
|
||
private var tasks: List<LocalTasks> = ArrayList() | ||
|
||
inner class TaskViewHolder(private val binding: DetailCycleRowBinding) : | ||
RecyclerView.ViewHolder(binding.root) { | ||
|
||
fun bind(task: LocalTasks) { | ||
binding.textView75.text = task.taskName | ||
binding.textView76.text = task.startDate | ||
binding.textView77.text = task.endDate | ||
} | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskViewHolder { | ||
val binding = | ||
DetailCycleRowBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
return TaskViewHolder(binding) | ||
} | ||
|
||
override fun onBindViewHolder(holder: TaskViewHolder, position: Int) { | ||
val task = tasks[position] | ||
holder.bind(task) | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
return tasks.size | ||
} | ||
|
||
@SuppressLint("NotifyDataSetChanged") | ||
fun submitList(taskList: List<LocalTasks>) { | ||
tasks = taskList | ||
notifyDataSetChanged() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app/src/main/java/com/steve_md/smartmkulima/data/repositories/FarmCycleRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.steve_md.smartmkulima.data.repositories | ||
|
||
import androidx.lifecycle.LiveData | ||
import com.steve_md.smartmkulima.data.room.AppDatabase | ||
import com.steve_md.smartmkulima.data.room.GAPDao | ||
import com.steve_md.smartmkulima.data.room.LocalFarmCycleDao | ||
import com.steve_md.smartmkulima.model.Cycle | ||
import com.steve_md.smartmkulima.model.GAP | ||
import com.steve_md.smartmkulima.model.LocalFarmCycle | ||
import com.steve_md.smartmkulima.utils.apiRequestByResource | ||
import com.steve_md.smartmkulima.utils.safeCall | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Farm cycles repository | ||
* Manual creating of repositories | ||
*/ | ||
class FarmCycleRepository @Inject constructor( | ||
database: AppDatabase | ||
) { | ||
private val gapDao: GAPDao = database.gapDao() | ||
|
||
private val localCycleDao: LocalFarmCycleDao = database.localFarmCycleDao() | ||
|
||
suspend fun insertCycle(cycle: LocalFarmCycle) = apiRequestByResource { | ||
localCycleDao.insertLocalFarmCycle(cycle) | ||
} | ||
|
||
val allCycles : Flow<List<Cycle>> = gapDao.getAllCycle() | ||
|
||
|
||
|
||
|
||
fun getAllCycles(): LiveData<List<LocalFarmCycle>> { | ||
return localCycleDao.getAllFarmCycle() | ||
} | ||
|
||
suspend fun updateTaskStatus(status: String) { | ||
localCycleDao.updateTaskStatus(status) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/steve_md/smartmkulima/data/room/GAPDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.steve_md.smartmkulima.data.room | ||
|
||
import androidx.room.* | ||
import com.steve_md.smartmkulima.model.Cycle | ||
import com.steve_md.smartmkulima.model.GAP | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
@Dao | ||
interface GAPDao { | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insertGAP(cycle: Cycle) | ||
|
||
@Query("SELECT * FROM cycle") | ||
fun getAllCycle(): Flow<List<Cycle>> | ||
|
||
@Delete | ||
suspend fun deleteGAP(cycle: Cycle) | ||
} |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/com/steve_md/smartmkulima/data/room/LocalFarmCycleDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.steve_md.smartmkulima.data.room | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.Query | ||
import androidx.room.Update | ||
import com.steve_md.smartmkulima.model.LocalFarmCycle | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
|
||
@Dao | ||
interface LocalFarmCycleDao { | ||
|
||
@Insert | ||
suspend fun insertLocalFarmCycle(localFarmCycle: LocalFarmCycle) | ||
|
||
@Update | ||
suspend fun updateLocalFarmCycle(localFarmCycle: LocalFarmCycle) | ||
|
||
@Query("SELECT * FROM localcycle WHERE cropName = :cropName") | ||
suspend fun getFarmCycleBYName(cropName: String): LocalFarmCycle? | ||
|
||
@Query("SELECT * FROM localcycle") | ||
fun getAllFarmCycle(): LiveData<List<LocalFarmCycle>> | ||
|
||
@Query("UPDATE localcycle SET status = :status") | ||
suspend fun updateTaskStatus(status: String) | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/steve_md/smartmkulima/data/room/converters/GAPConverters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.steve_md.smartmkulima.data.room.converters | ||
|
||
import androidx.room.TypeConverter | ||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
import com.steve_md.smartmkulima.model.Cycle | ||
import com.steve_md.smartmkulima.model.GAPtask | ||
import com.steve_md.smartmkulima.model.Tasks | ||
|
||
class Converters { | ||
@TypeConverter | ||
fun fromTasksList(tasks: List<Tasks>?): String? { | ||
return Gson().toJson(tasks) | ||
} | ||
|
||
@TypeConverter | ||
fun toTasksList(tasksString: String?): List<Tasks>? { | ||
val listType = object : TypeToken<List<Tasks>>() {}.type | ||
return Gson().fromJson(tasksString, listType) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/steve_md/smartmkulima/data/room/converters/LocalFarmCycleConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.steve_md.smartmkulima.data.room.converters | ||
|
||
import androidx.room.TypeConverter | ||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
import com.steve_md.smartmkulima.model.LocalTasks | ||
|
||
class LocalFarmCycleConverter { | ||
@TypeConverter | ||
fun fromLocalTasksList(value: List<LocalTasks>?): String { | ||
val gson = Gson() | ||
val type = object : TypeToken<List<LocalTasks>>() {}.type | ||
return gson.toJson(value, type) | ||
} | ||
|
||
@TypeConverter | ||
fun toLocalTasksList(value: String): List<LocalTasks>? { | ||
val gson = Gson() | ||
val type = object : TypeToken<List<LocalTasks>>() {}.type | ||
return gson.fromJson(value, type) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.