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

add some abstraction layer #23

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
android:name=".components.ApplicationController"
android:theme="@style/AppTheme">

<activity android:name=".features.home.MainActivity"
<activity android:name=".features.MainActivity"
android:theme="@style/AppTheme.NoActionBar.Splash"
android:launchMode="singleInstance"
android:windowSoftInputMode="adjustPan">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.esbati.keivan.persiancalendar.pojos.CalendarDay
import com.esbati.keivan.persiancalendar.pojos.UserEvent
import com.esbati.keivan.persiancalendar.pojos.GoogleEvent
import com.esbati.keivan.persiancalendar.R
import com.esbati.keivan.persiancalendar.repository.Repository
import com.esbati.keivan.persiancalendar.utils.AndroidUtilities
import com.esbati.keivan.persiancalendar.utils.Constants
import java.util.*

/**
* Created by asus on 11/25/2016.
Expand All @@ -37,7 +34,7 @@ class CalendarBottomSheet @JvmOverloads constructor(context: Context, attrs: Att
private var mShouldUpdateBottomSheet: Boolean = false
private var mShouldExpandBottomSheet: Boolean = false
private lateinit var mSelectedDay: CalendarDay
private var mSelectedEvent: UserEvent? = null
private var mSelectedEvent: GoogleEvent? = null

//Views
private var mBottomSheet: NestedScrollView
Expand Down Expand Up @@ -95,22 +92,22 @@ class CalendarBottomSheet @JvmOverloads constructor(context: Context, attrs: Att
proceedToSetupBottomSheet(Mode.SHEET_MODE_DATE)
}

fun showEvent(event: UserEvent) {
fun showEvent(event: GoogleEvent) {
mShouldExpandBottomSheet = true
mSelectedEvent = event

proceedToSetupBottomSheet(Mode.SHEET_MODE_VIEW_EVENT)
}

fun editEvent(event: UserEvent?, isEditable: Boolean) {
fun editEvent(event: GoogleEvent?, isEditable: Boolean) {
mShouldExpandBottomSheet = true
mSelectedEvent = event

proceedToSetupBottomSheet(if (isEditable) Mode.SHEET_MODE_EDIT_EVENT else CalendarBottomSheet.Mode.SHEET_MODE_VIEW_EVENT)
}

@SuppressLint("SetTextI18n")
private fun setDateSheet(day: CalendarDay, onEventClick: (UserEvent) -> Unit) {
private fun setDateSheet(day: CalendarDay, onEventClick: (GoogleEvent) -> Unit) {
//Set Date
mPersianDate.text = day.formattedDate
mGregorianDate.text = day.formattedDateSecondary
Expand All @@ -127,7 +124,7 @@ class CalendarBottomSheet @JvmOverloads constructor(context: Context, attrs: Att
else
eventTitle.setText(R.string.event_no_title)

eventView.setOnClickListener { onEventClick(event)}
// eventView.setOnClickListener { onEventClick(event)}
mBottomSheetContainer.addView(eventView)
}

Expand All @@ -143,14 +140,14 @@ class CalendarBottomSheet @JvmOverloads constructor(context: Context, attrs: Att
val eventView = LayoutInflater.from(context).inflate(R.layout.cell_bottom_sheet_day, mBottomSheetContainer, false)
val eventTitle = eventView.findViewById(R.id.event_title) as TextView

eventTitle.text = calendarEvent.mTitle
eventTitle.text = calendarEvent.title
eventView.setBackgroundResource(if (calendarEvent.isHoliday) R.drawable.bg_calendar_holiday else R.drawable.bg_calendar_today)
mBottomSheetContainer.addView(eventView)
}
}
}

private fun setShowEventSheet(event: UserEvent, onDeleteEvent: () -> Unit) {
private fun setShowEventSheet(event: GoogleEvent, onDeleteEvent: () -> Unit) {
//Set Bottom Sheet
mBottomSheetContainer.removeAllViews()

Expand Down Expand Up @@ -192,7 +189,7 @@ class CalendarBottomSheet @JvmOverloads constructor(context: Context, attrs: Att
mBottomSheetContainer.addView(eventDescription)
}

private fun setEditEventSheet(event: UserEvent) {
private fun setEditEventSheet(event: GoogleEvent) {
mBottomSheetContainer.removeAllViews()

val eventSheet = LayoutInflater.from(context).inflate(R.layout.cell_event_sheet, mBottomSheetContainer, false)
Expand Down Expand Up @@ -264,17 +261,17 @@ class CalendarBottomSheet @JvmOverloads constructor(context: Context, attrs: Att
}

CalendarBottomSheet.Mode.SHEET_MODE_EDIT_EVENT -> {
val tempEvent = mSelectedEvent?.copy() ?: Repository.createEventFor(mSelectedDay)
setEditEventSheet(tempEvent)

eventActionBtn.setImageResource(R.drawable.ic_check_white_24dp)
eventActionBtn.setOnClickListener { view ->
AndroidUtilities.hideSoftKeyboard(view)
onEventListener?.onEventEdited(tempEvent.copy(
title = mEventTitle.text.toString(),
description = mEventDesc.text.toString()
))
}
// val tempEvent = mSelectedEvent?.copy() ?: Repository.createEventFor(mSelectedDay)
// setEditEventSheet(tempEvent)
//
// eventActionBtn.setImageResource(R.drawable.ic_check_white_24dp)
// eventActionBtn.setOnClickListener { view ->
// AndroidUtilities.hideSoftKeyboard(view)
// onEventListener?.onEventEdited(tempEvent.copy(
// title = mEventTitle.text.toString(),
// description = mEventDesc.text.toString()
// ))
// }
}
}

Expand All @@ -295,7 +292,7 @@ class CalendarBottomSheet @JvmOverloads constructor(context: Context, attrs: Att
}

interface OnEventListener{
fun onEventDeleted(deletedEvent: UserEvent)
fun onEventEdited(editedEvent: UserEvent)
fun onEventDeleted(deletedEvent: GoogleEvent)
fun onEventEdited(editedEvent: GoogleEvent)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.esbati.keivan.persiancalendar.features.home
package com.esbati.keivan.persiancalendar.features

import android.Manifest
import android.app.AlertDialog
Expand All @@ -12,6 +12,7 @@ import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.view.View
import com.esbati.keivan.persiancalendar.R
import com.esbati.keivan.persiancalendar.features.home.HomeFragment
import com.esbati.keivan.persiancalendar.utils.AndroidUtilities
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ import com.esbati.keivan.persiancalendar.components.SoundManager
import com.esbati.keivan.persiancalendar.features.home.HomeFragment
import com.esbati.keivan.persiancalendar.pojos.CalendarDay
import com.esbati.keivan.persiancalendar.R
import com.esbati.keivan.persiancalendar.repository.Repository
import com.esbati.keivan.persiancalendar.refactoring.CalendarManagerFactory
import com.esbati.keivan.persiancalendar.refactoring.bases.CalendarManager

class CalendarFragment: Fragment() {

private val mYear by lazy { arguments!!.get(EXTRA_YEAR) as Int }
private val mMonth by lazy { arguments!!.get(EXTRA_MONTH) as Int }

private val calendarManager: CalendarManager = CalendarManagerFactory.create()

private lateinit var mRecyclerView: RecyclerView
private val mAdapter by lazy {
CalendarAdapter(mYear, mMonth, Repository.prepareDays(mYear, mMonth)).apply {
CalendarAdapter(mYear, mMonth, calendarManager.provideMonth(mYear, mMonth)).apply {
onCalendarClickListener = object: CalendarAdapter.OnCalendarClickListener {
override fun onCalendarClick(day: CalendarDay) {
SoundManager.playSound(day.mDay)
Expand Down Expand Up @@ -53,6 +56,6 @@ class CalendarFragment: Fragment() {
}

fun refreshCalendar() {
mAdapter.refresh(Repository.prepareDays(mYear, mMonth))
mAdapter.refresh(calendarManager.provideMonth(mYear, mMonth))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.content.ContextCompat
import android.support.v7.widget.Toolbar
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -24,22 +23,25 @@ import com.esbati.keivan.persiancalendar.components.ApplicationController
import com.esbati.keivan.persiancalendar.components.views.CalendarBottomSheet
import com.esbati.keivan.persiancalendar.components.views.CalendarPager
import com.esbati.keivan.persiancalendar.features.calendarPage.CalendarFragment
import com.esbati.keivan.persiancalendar.features.notification.NotificationUpdateService
import com.esbati.keivan.persiancalendar.features.settings.SettingFragment
import com.esbati.keivan.persiancalendar.pojos.CalendarDay
import com.esbati.keivan.persiancalendar.pojos.UserEvent
import com.esbati.keivan.persiancalendar.pojos.GoogleEvent
import com.esbati.keivan.persiancalendar.R
import com.esbati.keivan.persiancalendar.repository.Repository
import com.esbati.keivan.persiancalendar.refactoring.CalendarManagerFactory
import com.esbati.keivan.persiancalendar.refactoring.bases.CalendarManager
import com.esbati.keivan.persiancalendar.utils.AndroidUtilities
import com.esbati.keivan.persiancalendar.utils.Constants
import ir.smartlab.persindatepicker.util.PersianCalendar

class HomeFragment : Fragment() {

private var mDisplayedMonth: Int = 0
private var mDisplayedYear: Int = 0
private lateinit var mSelectedDay: CalendarDay


private val calendarManager: CalendarManager = CalendarManagerFactory.create()


//Toolbar
private var mToolbarMargin: Int = 0
private lateinit var mCoordinatorLayout: CoordinatorLayout
Expand Down Expand Up @@ -68,7 +70,7 @@ class HomeFragment : Fragment() {
setupBottomSheet(this)

//Setup Initial Day
mSelectedDay = Repository.getToday().also {
mSelectedDay = calendarManager.provideToday().also {
mDisplayedYear = it.mYear
mDisplayedMonth = it.mMonth
}
Expand Down Expand Up @@ -201,51 +203,51 @@ class HomeFragment : Fragment() {
mBottomSheet = view.findViewById(R.id.bottom_sheet) as CalendarBottomSheet
mBottomSheet.eventActionBtn = mEventActionBtn
mBottomSheet.onEventListener = object: CalendarBottomSheet.OnEventListener {
override fun onEventDeleted(deletedEvent: UserEvent) {
Repository.deleteEvent(deletedEvent).also {
//Refresh UI and show Date if Event Successfully added
if (it == 1) {
refreshFragment(deletedEvent.year, deletedEvent.month)

mSelectedDay.mEvents.remove(deletedEvent)
showDate(mSelectedDay, true)

//Update Notification
NotificationUpdateService.enqueueUpdate(context!!)
} else {
Toast.makeText(context, "Problem in deleting event!", Toast.LENGTH_SHORT).show()
}
}
override fun onEventDeleted(deletedEvent: GoogleEvent) {
// GoogleEventDataSource.deleteEvent(deletedEvent.id).also {
// //Refresh UI and show Date if Event Successfully added
// if (it == 1) {
// refreshFragment(deletedEvent.year, deletedEvent.month)
//
// mSelectedDay.mEvents.remove(deletedEvent)
// showDate(mSelectedDay, true)
//
// //Update Notification
// NotificationUpdateService.enqueueUpdate(context!!)
// } else {
// Toast.makeText(context, "Problem in deleting event!", Toast.LENGTH_SHORT).show()
// }
// }
}

override fun onEventEdited(editedEvent: UserEvent) {
override fun onEventEdited(editedEvent: GoogleEvent) {
if(TextUtils.isEmpty(editedEvent.title) && TextUtils.isEmpty(editedEvent.description)){
Toast.makeText(context, R.string.event_error_no_content, Toast.LENGTH_SHORT).show()
return
}

if(ContextCompat.checkSelfPermission(ApplicationController.getContext(), Manifest.permission.WRITE_CALENDAR)
== PackageManager.PERMISSION_GRANTED)
Repository.saveEvent(editedEvent).also {
//Refresh UI and show Date if Event Successfully added
if (it == 1) {
refreshFragment(editedEvent.year, editedEvent.month)

mSelectedDay.mEvents.clear()
mSelectedDay.mEvents.addAll(Repository.getEvents(
mSelectedDay.mYear
, mSelectedDay.mMonth
, mSelectedDay.mDay
))
showDate(mSelectedDay, true)

//Update Notification
NotificationUpdateService.enqueueUpdate(context!!)
} else {
Toast.makeText(context, "Problem in saving event!", Toast.LENGTH_SHORT).show()
Log.d("Calendar", getString(R.string.event_error_no_calendar))
}
}
// GoogleEventDataSource.saveEvent(editedEvent).also {
// //Refresh UI and show Date if Event Successfully added
// if (it == 1) {
// refreshFragment(editedEvent.year, editedEvent.month)
//
// mSelectedDay.mEvents.clear()
// mSelectedDay.mEvents.addAll(Repository.getEvents(
// mSelectedDay.mYear
// , mSelectedDay.mMonth
// , mSelectedDay.mDay
// ))
// showDate(mSelectedDay, true)
//
// //Update Notification
// NotificationUpdateService.enqueueUpdate(context!!)
// } else {
// Toast.makeText(context, "Problem in saving event!", Toast.LENGTH_SHORT).show()
// Log.d("Calendar", getString(R.string.event_error_no_calendar))
// }
// }
else
Toast.makeText(context, R.string.event_error_write_permission, Toast.LENGTH_SHORT).show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import android.support.v4.app.NotificationCompat
import android.text.TextUtils
import com.esbati.keivan.persiancalendar.BuildConfig

import com.esbati.keivan.persiancalendar.features.home.MainActivity
import com.esbati.keivan.persiancalendar.features.MainActivity
import com.esbati.keivan.persiancalendar.pojos.CalendarDay
import com.esbati.keivan.persiancalendar.R
import com.esbati.keivan.persiancalendar.refactoring.events.UserEvent
import com.esbati.keivan.persiancalendar.repository.PreferencesHelper
import com.esbati.keivan.persiancalendar.utils.ColorHelper
import com.esbati.keivan.persiancalendar.utils.LanguageHelper
Expand Down Expand Up @@ -116,8 +117,10 @@ object NotificationHelper {
inboxStyle.addLine(
if (!TextUtils.isEmpty(event.title))
event.title
else
else if(event is UserEvent)
event.description
else
""
)

mBuilder.setStyle(inboxStyle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import android.content.Intent
import android.content.IntentFilter
import android.support.v4.content.ContextCompat
import android.util.Log
import com.esbati.keivan.persiancalendar.repository.Repository
import com.esbati.keivan.persiancalendar.refactoring.CalendarManagerFactory
import com.esbati.keivan.persiancalendar.refactoring.bases.CalendarManager
import com.esbati.keivan.persiancalendar.utils.AndroidUtilities

/**
Expand All @@ -22,14 +23,17 @@ class NotificationService : Service() {
addAction(Intent.ACTION_TIMEZONE_CHANGED)
}

private val calendarManager: CalendarManager = CalendarManagerFactory.create()


override fun onBind(paramIntent: Intent) = null

override fun onCreate() {
super.onCreate()
Log.d(javaClass.simpleName, "Created")

//Promote service to foreground using sticky notification
val today = Repository.getToday()
val today = calendarManager.provideToday()
val notification = NotificationHelper.createStickyNotification(this, today)
startForeground(NotificationHelper.STICKY_NOTIFICATION_ID, notification)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ import android.content.Context
import android.content.Intent
import android.support.v4.app.JobIntentService
import android.util.Log
import com.esbati.keivan.persiancalendar.refactoring.CalendarManagerFactory
import com.esbati.keivan.persiancalendar.refactoring.bases.CalendarManager
import com.esbati.keivan.persiancalendar.repository.PreferencesHelper
import com.esbati.keivan.persiancalendar.repository.Repository

/**
* Created by Keivan Esbati on 4/10/2017.
*/

class NotificationUpdateService : JobIntentService() {

private val calendarManager: CalendarManager = CalendarManagerFactory.create()



override fun onHandleWork(intent: Intent) {
Log.d(javaClass.simpleName, "Updating notification")

//If notification is active update it, else cancel ongoing notification
if (PreferencesHelper.isOptionActive(PreferencesHelper.KEY_NOTIFICATION_SHOW, true)) {
//Show Sticky Notification
val today = Repository.getToday()
val today = calendarManager.provideToday()
NotificationHelper.showStickyNotification(this, today)
} else {
NotificationHelper.cancelNotification(this)
Expand Down