Skip to content

Commit

Permalink
Merge pull request #4 from 5hahryar/Development
Browse files Browse the repository at this point in the history
v1.2.2
  • Loading branch information
5hahryar authored Jan 6, 2021
2 parents 6b2f92b + 220222d commit c42c8fc
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 36 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId "com.sloupycom.shaper"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.2.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/com/sloupycom/shaper/database/LocalDao.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.sloupycom.shaper.database

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import androidx.room.*
import com.sloupycom.shaper.model.Task

@Dao
Expand All @@ -16,6 +13,9 @@ interface LocalDao {
@Update
suspend fun update(task: Task)

@Delete
suspend fun delete(task: Task)

@Query ("SELECT * FROM task_table")
fun getAllTasks(): LiveData<MutableList<Task>>?

Expand All @@ -28,8 +28,8 @@ interface LocalDao {
@Query ("DELETE FROM task_table WHERE next_due < :dateIndex AND state = 'DONE'")
suspend fun removeOldDoneTasks(dateIndex: String)

@Query("SELECT DISTINCT next_due FROM task_table")
fun getBusyDaysOfWeek(): LiveData<List<String>>?
@Query("SELECT DISTINCT next_due FROM task_table WHERE next_due <= :dayUntil")
fun getBusyDaysOfWeek(dayUntil: String): LiveData<List<String>>?

@Query ("SELECT * FROM task_table WHERE next_due <= :todayDateIndex")
suspend fun getReminderTasks(todayDateIndex: String): List<Task>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.sloupycom.shaper.model.adapter

import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

class SwipeToDeleteCallBack(private val adapter: TaskAdapter): ItemTouchHelper.SimpleCallback(
0,
ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
) {

override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return true
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
//Delete item on swipe
adapter.deleteItem(viewHolder.adapterPosition)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ class TaskAdapter(

private val mUtil: Util = Util()
private var listener: TaskStateListener? = null
private var recentDeletedItemPostition: Int? = null
private var recentDeletedItem: Task? = null

var data: List<Task> = mutableListOf()
var data: MutableList<Task> = mutableListOf()
set(value) {
field = value
notifyDataSetChanged()
Expand Down Expand Up @@ -94,7 +96,23 @@ class TaskAdapter(
this.listener = listener
}

fun deleteItem(position: Int) {
recentDeletedItem = data[position]
recentDeletedItemPostition = position
listener?.onTaskItemDeleted(recentDeletedItem!!)
data.drop(recentDeletedItemPostition!!)
notifyItemRemoved(recentDeletedItemPostition!!)
}

fun undoRemove() {
listener?.onTaskItemDeleteUndo(recentDeletedItem!!)
data.add(recentDeletedItemPostition!!, recentDeletedItem!!)
notifyItemInserted(recentDeletedItemPostition!!)
}

interface TaskStateListener {
fun onTaskStateChanged(task: Task)
fun onTaskItemDeleted(task: Task)
fun onTaskItemDeleteUndo(task: Task)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class ReminderBroadCast: BroadcastReceiver() {

job.join()
var notif = ""
if (data != null) {
if (data != null && data!!.isNotEmpty()) {
for (task in data!!) {
notif += " · ${task.title}"
}
createNotification(notif)
}
createNotification(notif)
Log.d(Constant.TAG, "ALARM: notification created with data size: ${data?.size}")
}
}
Expand Down
29 changes: 21 additions & 8 deletions app/src/main/java/com/sloupycom/shaper/utils/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ class Util @Inject constructor(){
)
}

/**
* Get date in a string, format: "ddMMyyyy"
*/
@SuppressLint("SimpleDateFormat")
fun getTodayDateIndexString(): String {
val dd = SimpleDateFormat("dd").format(mCalendar.time).toInt()
val MM = SimpleDateFormat("MM").format(mCalendar.time).toInt()
val yyyy = SimpleDateFormat("yyyy").format(mCalendar.time).toInt()
return "$yyyy$MM$dd"
}

/**
* Get date in a string by custom format
*/
Expand Down Expand Up @@ -129,9 +140,9 @@ class Util @Inject constructor(){

fun getDateIndex(date: Calendar): String {

val year = SimpleDateFormat("yyyy").format(date.time).toInt()
val month = SimpleDateFormat("MM").format(date.time).toInt()
val day = SimpleDateFormat("dd").format(date.time).toInt()
val year = SimpleDateFormat("yyyy").format(date.time)
val month = SimpleDateFormat("MM").format(date.time)
val day = SimpleDateFormat("dd").format(date.time)

return "$year$month$day"
}
Expand All @@ -145,14 +156,16 @@ class Util @Inject constructor(){
return weekIndex
}

fun getDayListFromDateIndex(list: List<String>?): List<Int> {
var days = mutableListOf<Int>()
if (list != null) {
fun getBusyWeekDaysFromDateIndex(list: List<String>?): List<Int> {
val days = mutableListOf<Int>()
val todayIndex = getTodayDateIndexString()
if (list != null && list.isNotEmpty()) {
days.add(todayIndex.substring(6).toInt())
for (date in list) {
days.add(date.substring(5, 8).toInt())
if (date.substring(0, 6) == todayIndex.substring(0, 6))
days.add(date.substring(6).toInt())
}
}

return days
}

Expand Down
30 changes: 27 additions & 3 deletions app/src/main/java/com/sloupycom/shaper/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import com.shahryar.daybar.DayBar
import com.shahryar.daybar.DayBarChip
import com.sloupycom.shaper.R
import com.sloupycom.shaper.databinding.ActivityMainBinding
import com.sloupycom.shaper.model.Task
import com.sloupycom.shaper.model.adapter.SwipeToDeleteCallBack
import com.sloupycom.shaper.model.adapter.TaskAdapter
import com.sloupycom.shaper.utils.Util
import com.sloupycom.shaper.viewmodel.MainActivityViewModel
Expand All @@ -36,20 +39,31 @@ class MainActivity : AppCompatActivity(), DayBar.OnDayChangedListener {
setupRecyclerView()

mBinding?.viewModel?.busyDays?.observe(this, {
dayBar.setIndicationByDay(Util().getDayListFromDateIndex(it))
dayBar.setIndicationByDay(Util().getBusyWeekDaysFromDateIndex(it))
//TODO: issue -> Indication is not removed when busyDay is removed
})

}

private fun setupRecyclerView() {

// adapter.setHasStableIds(true)
recyclerView_todayDue.adapter = adapter

//Setup on item swipe listener
ItemTouchHelper(SwipeToDeleteCallBack(adapter)).attachToRecyclerView(recyclerView_todayDue)

adapter.setOnTaskStateListener(object : TaskAdapter.TaskStateListener {
override fun onTaskStateChanged(task: Task) {
mBinding?.viewModel?.onTaskStateChanged(task)
}

override fun onTaskItemDeleted(task: Task) {
mBinding?.viewModel?.deleteTaskItem(task)
showItemDeletedSnackBar()
}

override fun onTaskItemDeleteUndo(task: Task) {
mBinding?.viewModel?.undoDeleteTaskItem(task)
}
})

//Observe liveData in order to update recyclerView
Expand Down Expand Up @@ -92,6 +106,16 @@ class MainActivity : AppCompatActivity(), DayBar.OnDayChangedListener {
}
}

private fun showItemDeletedSnackBar() {
Snackbar.make(floatingActionButton, getText(R.string.delete_task), Snackbar.LENGTH_SHORT)
.setAnchorView(floatingActionButton)
.setBackgroundTint(getColor(R.color.colorSecondary))
.setTextColor(getColor(R.color.colorOnSecondary))
.setAction(getString(R.string.undo)) { adapter.undoRemove() }
.setActionTextColor(getColor(R.color.colorOnSecondary))
.show()
}

/**
* Called when selected day from DayBar changes
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AddTaskViewModel(private val activity: Activity): AndroidViewModel(activit
private val mCalendar: Calendar = Calendar.getInstance()
private val mUtil = Util()

private var mDateIndex: List<Int>? = null
private var mDateIndex: List<String>? = null
private var onTaskAddedListener: OnTaskAddedListener? = null
val textDate: ObservableField<String> = ObservableField("")
var textTitle: String? = null
Expand All @@ -43,9 +43,9 @@ class AddTaskViewModel(private val activity: Activity): AndroidViewModel(activit
val creationDate = mUtil.getDate("EEE MMM dd yyyy", mCalendar.time)
if (mDateIndex == null) {
mDateIndex = listOf(
SimpleDateFormat("dd").format(mCalendar.time).toInt(),
SimpleDateFormat("MM").format(mCalendar.time).toInt(),
SimpleDateFormat("yyyy").format(mCalendar.time).toInt()
SimpleDateFormat("dd").format(mCalendar.time),
SimpleDateFormat("MM").format(mCalendar.time),
SimpleDateFormat("yyyy").format(mCalendar.time)
)
}
val nextDue = "${mDateIndex!![2]}${mDateIndex!![1]}${mDateIndex!![0]}"
Expand Down Expand Up @@ -76,9 +76,9 @@ class AddTaskViewModel(private val activity: Activity): AndroidViewModel(activit
date.set(year, month, dayOfMonth)
textDate.set(SimpleDateFormat("EEEE, MMM dd").format(date.time))
this.mDateIndex = listOf(
SimpleDateFormat("dd").format(date.time).toInt(),
SimpleDateFormat("MM").format(date.time).toInt(),
SimpleDateFormat("yyyy").format(date.time).toInt())
SimpleDateFormat("dd").format(date.time),
SimpleDateFormat("MM").format(date.time),
SimpleDateFormat("yyyy").format(date.time))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package com.sloupycom.shaper.viewmodel
import android.app.Application
import androidx.databinding.ObservableField
import androidx.lifecycle.*
import com.google.android.material.snackbar.Snackbar
import com.sloupycom.shaper.database.Local
import com.sloupycom.shaper.model.Task
import com.sloupycom.shaper.utils.Util
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.util.*


Expand All @@ -28,7 +30,7 @@ class MainActivityViewModel(application: Application) : AndroidViewModel(applica
val liveDataMerger: MediatorLiveData<MutableList<Task>> = MediatorLiveData<MutableList<Task>>()

var textDate: ObservableField<String> = ObservableField(mUtil.getDate("EEEE, MMM dd"))
var busyDays: LiveData<List<String>>? = mLocal.localDao.getBusyDaysOfWeek()
var busyDays: LiveData<List<String>>? = mLocal.localDao.getBusyDaysOfWeek(weekIndex[6])
private lateinit var lastLiveData: LiveData<*>

init {
Expand Down Expand Up @@ -60,4 +62,16 @@ class MainActivityViewModel(application: Application) : AndroidViewModel(applica
}

}

fun deleteTaskItem(task: Task) {
viewModelScope.launch {
mLocal.localDao.delete(task)
}
}

fun undoDeleteTaskItem(task: Task) {
viewModelScope.launch {
mLocal.localDao.insert(task)
}
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/layout/dialog_support.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@
android:gravity="center"
android:text="@string/support_repo"/>

<TextView
android:id="@+id/textView_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextOverLine"
android:alpha="0.6"
android:layout_marginTop="20dp"
android:gravity="bottom"
android:text="v1.2.2"/>

</LinearLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@
<string name="on_task_added">New task added successfully.</string>
<string name="separator">Separator</string>
<string name="question_mark">Question mark</string>
<string name="delete_task">Task removed successfully.</string>
<string name="undo">Undo</string>
</resources>
6 changes: 0 additions & 6 deletions app/src/test/java/com/sloupycom/shaper/utils/UtilTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,5 @@ class UtilTest {
assertEquals(expect, actual)
}

@Test
fun util_getDayListFromDateIndex() {
val actual = Util().getDayListFromDateIndex(listOf("20201021", "20201001", "20201010", "20201013"))
val expect = mutableListOf("21", "1", "10", "13")

assertEquals(expect, actual)
}
}

0 comments on commit c42c8fc

Please sign in to comment.