Add showing proper fragment in notifications (#331)

This commit is contained in:
Kacper Ziubryniewicz 2019-04-29 19:56:23 +02:00 committed by Rafał Borcz
parent f84040109c
commit dbbc8069b1
11 changed files with 103 additions and 56 deletions

View File

@ -15,7 +15,8 @@ import io.github.wulkanowy.data.repositories.grade.GradeRepository
import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository
import io.github.wulkanowy.services.sync.channels.NewEntriesChannel
import io.github.wulkanowy.ui.modules.main.MainActivity
import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU_INDEX
import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU
import io.github.wulkanowy.ui.modules.main.MainView
import io.github.wulkanowy.utils.getCompatColor
import io.reactivex.Completable
import javax.inject.Inject
@ -48,7 +49,7 @@ class GradeWork @Inject constructor(
.setColor(context.getCompatColor(R.color.colorPrimary))
.setContentIntent(
PendingIntent.getActivity(context, 0,
MainActivity.getStartIntent(context).putExtra(EXTRA_START_MENU_INDEX, 0), FLAG_UPDATE_CURRENT))
MainActivity.getStartIntent(context).putExtra(EXTRA_START_MENU, MainView.MenuView.GRADE), FLAG_UPDATE_CURRENT))
.setStyle(NotificationCompat.InboxStyle().run {
setSummaryText(context.resources.getQuantityString(R.plurals.grade_number_item, grades.size, grades.size))
grades.forEach { addLine("${it.subject}: ${it.entry}") }

View File

@ -15,7 +15,8 @@ import io.github.wulkanowy.data.repositories.luckynumber.LuckyNumberRepository
import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository
import io.github.wulkanowy.services.sync.channels.NewEntriesChannel
import io.github.wulkanowy.ui.modules.main.MainActivity
import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU_INDEX
import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU
import io.github.wulkanowy.ui.modules.main.MainView
import io.github.wulkanowy.utils.getCompatColor
import io.reactivex.Completable
import javax.inject.Inject
@ -47,8 +48,9 @@ class LuckyNumberWork @Inject constructor(
.setPriority(PRIORITY_HIGH)
.setColor(context.getCompatColor(R.color.colorPrimary))
.setContentIntent(
PendingIntent.getActivity(context, 0,
MainActivity.getStartIntent(context).putExtra(EXTRA_START_MENU_INDEX, 4), FLAG_UPDATE_CURRENT))
PendingIntent.getActivity(context, MainView.MenuView.MESSAGE.id,
MainActivity.getStartIntent(context).putExtra(EXTRA_START_MENU, MainView.MenuView.LUCKY_NUMBER)
, FLAG_UPDATE_CURRENT))
.build())
}
}

View File

@ -16,7 +16,8 @@ import io.github.wulkanowy.data.repositories.message.MessageRepository
import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository
import io.github.wulkanowy.services.sync.channels.NewEntriesChannel
import io.github.wulkanowy.ui.modules.main.MainActivity
import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU_INDEX
import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU
import io.github.wulkanowy.ui.modules.main.MainView
import io.github.wulkanowy.utils.getCompatColor
import io.reactivex.Completable
import javax.inject.Inject
@ -48,8 +49,8 @@ class MessageWork @Inject constructor(
.setPriority(PRIORITY_HIGH)
.setColor(context.getCompatColor(R.color.colorPrimary))
.setContentIntent(
PendingIntent.getActivity(context, 0, MainActivity.getStartIntent(context)
.putExtra(EXTRA_START_MENU_INDEX, 4), FLAG_UPDATE_CURRENT)
PendingIntent.getActivity(context, MainView.MenuView.MESSAGE.id, MainActivity.getStartIntent(context)
.putExtra(EXTRA_START_MENU, MainView.MenuView.MESSAGE), FLAG_UPDATE_CURRENT)
)
.setStyle(NotificationCompat.InboxStyle().run {
setSummaryText(context.resources.getQuantityString(R.plurals.message_number_item, messages.size, messages.size))

View File

@ -15,7 +15,8 @@ import io.github.wulkanowy.data.repositories.note.NoteRepository
import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository
import io.github.wulkanowy.services.sync.channels.NewEntriesChannel
import io.github.wulkanowy.ui.modules.main.MainActivity
import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU_INDEX
import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU
import io.github.wulkanowy.ui.modules.main.MainView
import io.github.wulkanowy.utils.getCompatColor
import io.reactivex.Completable
import javax.inject.Inject
@ -47,8 +48,9 @@ class NoteWork @Inject constructor(
.setPriority(PRIORITY_HIGH)
.setColor(context.getCompatColor(R.color.colorPrimary))
.setContentIntent(
PendingIntent.getActivity(context, 0,
MainActivity.getStartIntent(context).putExtra(EXTRA_START_MENU_INDEX, 4), FLAG_UPDATE_CURRENT))
PendingIntent.getActivity(context, MainView.MenuView.NOTE.id,
MainActivity.getStartIntent(context).putExtra(EXTRA_START_MENU, MainView.MenuView.NOTE)
, FLAG_UPDATE_CURRENT))
.setStyle(NotificationCompat.InboxStyle().run {
setSummaryText(context.resources.getQuantityString(R.plurals.note_number_item, notes.size, notes.size))
notes.forEach { addLine("${it.teacher}: ${it.category}") }

View File

@ -6,7 +6,7 @@ import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.repositories.student.StudentRepository
import io.github.wulkanowy.ui.base.BasePresenter
import io.github.wulkanowy.ui.base.ErrorHandler
import io.github.wulkanowy.ui.modules.luckynumberwidget.LuckyNumberWidgetProvider.Companion.createWidgetKey
import io.github.wulkanowy.ui.modules.luckynumberwidget.LuckyNumberWidgetProvider.Companion.getStudentWidgetKey
import io.github.wulkanowy.utils.SchedulersProvider
import javax.inject.Inject
@ -34,7 +34,7 @@ class LuckyNumberWidgetConfigurePresenter @Inject constructor(
private fun loadData() {
disposable.add(studentRepository.getSavedStudents(false)
.map { it to appWidgetId?.let { id -> sharedPref.getLong(createWidgetKey(id), 0) } }
.map { it to appWidgetId?.let { id -> sharedPref.getLong(getStudentWidgetKey(id), 0) } }
.map { (students, currentStudentId) ->
students.map { student -> LuckyNumberWidgetConfigureItem(student, student.id == currentStudentId) }
}
@ -51,7 +51,7 @@ class LuckyNumberWidgetConfigurePresenter @Inject constructor(
private fun registerStudent(student: Student) {
appWidgetId?.also {
sharedPref.putLong(createWidgetKey(it), student.id)
sharedPref.putLong(getStudentWidgetKey(it), student.id)
view?.apply {
updateLuckyNumberWidget(it)
setSuccessResult(it)

View File

@ -26,7 +26,8 @@ import io.github.wulkanowy.data.repositories.luckynumber.LuckyNumberRepository
import io.github.wulkanowy.data.repositories.semester.SemesterRepository
import io.github.wulkanowy.data.repositories.student.StudentRepository
import io.github.wulkanowy.ui.modules.main.MainActivity
import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU_INDEX
import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU
import io.github.wulkanowy.ui.modules.main.MainView
import io.github.wulkanowy.utils.SchedulersProvider
import io.reactivex.Maybe
import timber.log.Timber
@ -53,7 +54,7 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() {
lateinit var sharedPref: SharedPrefHelper
companion object {
fun createWidgetKey(appWidgetId: Int) = "lucky_number_widget_$appWidgetId"
fun getStudentWidgetKey(appWidgetId: Int) = "lucky_number_widget_student_$appWidgetId"
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@ -70,11 +71,11 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() {
intent.getIntArrayExtra(EXTRA_APPWIDGET_IDS).forEach { appWidgetId ->
RemoteViews(context.packageName, R.layout.widget_luckynumber).apply {
setTextViewText(R.id.luckyNumberWidgetNumber,
getLuckyNumber(sharedPref.getLong(createWidgetKey(appWidgetId), 0), appWidgetId)?.luckyNumber?.toString() ?: "#"
getLuckyNumber(sharedPref.getLong(getStudentWidgetKey(appWidgetId), 0), appWidgetId)?.luckyNumber?.toString() ?: "#"
)
setOnClickPendingIntent(R.id.luckyNumberWidgetContainer,
PendingIntent.getActivity(context, 2, MainActivity.getStartIntent(context).apply {
putExtra(EXTRA_START_MENU_INDEX, 4)
PendingIntent.getActivity(context, MainView.MenuView.LUCKY_NUMBER.id, MainActivity.getStartIntent(context).apply {
putExtra(EXTRA_START_MENU, MainView.MenuView.LUCKY_NUMBER)
}, PendingIntent.FLAG_UPDATE_CURRENT))
}.also {
setStyles(it, intent)
@ -85,7 +86,7 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() {
private fun onDelete(intent: Intent) {
intent.getIntExtra(EXTRA_APPWIDGET_ID, 0).let {
if (it != 0) sharedPref.delete(createWidgetKey(it))
if (it != 0) sharedPref.delete(getStudentWidgetKey(it))
}
}
@ -97,12 +98,14 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() {
.flatMap { students ->
students.singleOrNull { student -> student.id == studentId }
.let { student ->
if (student != null) {
Maybe.just(student)
} else {
studentRepository.getCurrentStudent(false)
.toMaybe()
.doOnSuccess { sharedPref.putLong(createWidgetKey(appWidgetId), it.id) }
when {
student != null -> Maybe.just(student)
studentId != 0L -> {
studentRepository.getCurrentStudent(false)
.toMaybe()
.doOnSuccess { sharedPref.putLong(getStudentWidgetKey(appWidgetId), it.id) }
}
else -> null
}
}
}
@ -132,12 +135,12 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() {
val maxWidth = options?.getInt(OPTION_APPWIDGET_MAX_WIDTH) ?: 150
val maxHeight = options?.getInt(OPTION_APPWIDGET_MAX_HEIGHT) ?: 40
Timber.d("New luckynumber widget measurement: %dx%d", maxWidth, maxHeight)
Timber.d("New lucky number widget measurement: %dx%d", maxWidth, maxHeight)
when {
// 1x1
maxWidth < 150 && maxHeight < 110 -> {
Timber.d("Luckynumber widget size: 1x1")
Timber.d("Lucky number widget size: 1x1")
views.run {
setViewVisibility(R.id.luckyNumberWidgetImageTop, GONE)
setViewVisibility(R.id.luckyNumberWidgetImageLeft, GONE)
@ -147,7 +150,7 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() {
}
// 1x2
maxWidth < 150 && maxHeight > 110 -> {
Timber.d("Luckynumber widget size: 1x2")
Timber.d("Lucky number widget size: 1x2")
views.run {
setViewVisibility(R.id.luckyNumberWidgetImageTop, VISIBLE)
setViewVisibility(R.id.luckyNumberWidgetImageLeft, GONE)
@ -157,7 +160,7 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() {
}
// 2x1
maxWidth >= 150 && maxHeight <= 110 -> {
Timber.d("Luckynumber widget size: 2x1")
Timber.d("Lucky number widget size: 2x1")
views.run {
setViewVisibility(R.id.luckyNumberWidgetImageTop, GONE)
setViewVisibility(R.id.luckyNumberWidgetImageLeft, VISIBLE)
@ -167,7 +170,7 @@ class LuckyNumberWidgetProvider : BroadcastReceiver() {
}
// 2x2 and bigger
else -> {
Timber.d("Luckynumber widget size: 2x2 and bigger")
Timber.d("Lucky number widget size: 2x2 and bigger")
views.run {
setViewVisibility(R.id.luckyNumberWidgetImageTop, GONE)
setViewVisibility(R.id.luckyNumberWidgetImageLeft, GONE)

View File

@ -22,8 +22,12 @@ import io.github.wulkanowy.ui.modules.account.AccountDialog
import io.github.wulkanowy.ui.modules.attendance.AttendanceFragment
import io.github.wulkanowy.ui.modules.exam.ExamFragment
import io.github.wulkanowy.ui.modules.grade.GradeFragment
import io.github.wulkanowy.ui.modules.homework.HomeworkFragment
import io.github.wulkanowy.ui.modules.login.LoginActivity
import io.github.wulkanowy.ui.modules.luckynumber.LuckyNumberFragment
import io.github.wulkanowy.ui.modules.message.MessageFragment
import io.github.wulkanowy.ui.modules.more.MoreFragment
import io.github.wulkanowy.ui.modules.note.NoteFragment
import io.github.wulkanowy.ui.modules.timetable.TimetableFragment
import io.github.wulkanowy.utils.getThemeAttrColor
import io.github.wulkanowy.utils.safelyPopFragment
@ -40,7 +44,7 @@ class MainActivity : BaseActivity(), MainView {
lateinit var navController: FragNavController
companion object {
const val EXTRA_START_MENU_INDEX = "extraStartMenuIndex"
const val EXTRA_START_MENU = "extraStartMenu"
fun getStartIntent(context: Context) = Intent(context, MainActivity::class.java)
}
@ -56,14 +60,27 @@ class MainActivity : BaseActivity(), MainView {
override var startMenuIndex = 0
override var startMenuMoreIndex = -1
private val moreMenuFragments = listOf<Fragment>(
MessageFragment.newInstance(),
HomeworkFragment.newInstance(),
NoteFragment.newInstance(),
LuckyNumberFragment.newInstance()
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(mainToolbar)
messageContainer = mainFragmentContainer
presenter.onAttachView(this, intent.getIntExtra(EXTRA_START_MENU_INDEX, -1))
navController.initialize(startMenuIndex, savedInstanceState)
presenter.onAttachView(this, intent.getSerializableExtra(EXTRA_START_MENU) as? MainView.MenuView)
navController.run {
initialize(startMenuIndex, savedInstanceState)
pushFragment(moreMenuFragments.getOrNull(startMenuMoreIndex))
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {

View File

@ -22,22 +22,19 @@ class MainPresenter @Inject constructor(
private val analytics: FirebaseAnalyticsHelper
) : BasePresenter<MainView>(errorHandler) {
fun onAttachView(view: MainView, initMenuIndex: Int) {
fun onAttachView(view: MainView, initMenu: MainView.MenuView?) {
super.onAttachView(view)
view.run {
startMenuIndex = if (initMenuIndex != -1) initMenuIndex else prefRepository.startMenuIndex
view.apply {
getProperViewIndexes(initMenu).let { (main, more) ->
startMenuIndex = main
startMenuMoreIndex = more
}
initView()
Timber.i("Main view was initialized with $startMenuIndex menu index")
Timber.i("Main view was initialized with $startMenuIndex menu index and $startMenuMoreIndex more index")
}
syncManager.startSyncWorker()
analytics.logEvent(APP_OPEN, DESTINATION to when (initMenuIndex) {
1 -> "Grades"
3 -> "Timetable"
4 -> "More"
else -> "User action"
})
analytics.logEvent(APP_OPEN, DESTINATION to initMenu?.name)
}
fun onViewChange() {
@ -104,4 +101,12 @@ class MainPresenter @Inject constructor(
errorHandler.dispatch(it)
}))
}
private fun getProperViewIndexes(initMenu: MainView.MenuView?): Pair<Int, Int> {
return when {
initMenu?.id in 0..3 -> initMenu!!.id to -1
(initMenu?.id ?: 0) > 3 -> 4 to initMenu!!.id - 4
else -> prefRepository.startMenuIndex to -1
}
}
}

View File

@ -6,6 +6,8 @@ interface MainView : BaseView {
var startMenuIndex: Int
var startMenuMoreIndex: Int
val isRootView: Boolean
val currentViewTitle: String?
@ -37,4 +39,15 @@ interface MainView : BaseView {
val titleStringId: Int
}
enum class MenuView(val id: Int) {
GRADE(0),
ATTENDANCE(1),
EXAM(2),
TIMETABLE(3),
MESSAGE(4),
HOMEWORK(5),
NOTE(6),
LUCKY_NUMBER(7),
}
}

View File

@ -20,7 +20,8 @@ import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.repositories.student.StudentRepository
import io.github.wulkanowy.services.widgets.TimetableWidgetService
import io.github.wulkanowy.ui.modules.main.MainActivity
import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU_INDEX
import io.github.wulkanowy.ui.modules.main.MainActivity.Companion.EXTRA_START_MENU
import io.github.wulkanowy.ui.modules.main.MainView
import io.github.wulkanowy.utils.FirebaseAnalyticsHelper
import io.github.wulkanowy.utils.SchedulersProvider
import io.github.wulkanowy.utils.nextOrSameSchoolDay
@ -131,7 +132,7 @@ class TimetableWidgetProvider : BroadcastReceiver() {
}, FLAG_UPDATE_CURRENT))
setPendingIntentTemplate(R.id.timetableWidgetList,
PendingIntent.getActivity(context, 1, MainActivity.getStartIntent(context).apply {
putExtra(EXTRA_START_MENU_INDEX, 3)
putExtra(EXTRA_START_MENU, MainView.MenuView.TIMETABLE)
}, FLAG_UPDATE_CURRENT))
}.also {
sharedPref.putLong(getDateWidgetKey(appWidgetId), date.toEpochDay(), true)
@ -151,20 +152,22 @@ class TimetableWidgetProvider : BroadcastReceiver() {
}, FLAG_UPDATE_CURRENT)
}
private fun getStudent(id: Long, appWidgetId: Int): Student? {
private fun getStudent(studentId: Long, appWidgetId: Int): Student? {
return try {
studentRepository.isStudentSaved()
.filter { true }
.flatMap { studentRepository.getSavedStudents(false).toMaybe() }
.flatMap { students ->
students.singleOrNull { student -> student.id == id }
students.singleOrNull { student -> student.id == studentId }
.let { student ->
if (student != null) {
Maybe.just(student)
} else {
studentRepository.getCurrentStudent(false)
.toMaybe()
.doOnSuccess { sharedPref.putLong(getStudentWidgetKey(appWidgetId), it.id) }
when {
student != null -> Maybe.just(student)
studentId != 0L -> {
studentRepository.getCurrentStudent(false)
.toMaybe()
.doOnSuccess { sharedPref.putLong(getStudentWidgetKey(appWidgetId), it.id) }
}
else -> null
}
}
}

View File

@ -41,7 +41,7 @@ class MainPresenterTest {
clearInvocations(mainView)
presenter = MainPresenter(errorHandler, studentRepository, prefRepository, syncManager, TestSchedulersProvider(), analytics)
presenter.onAttachView(mainView, -1)
presenter.onAttachView(mainView, null)
}
@Test