1
0
mirror of https://github.com/etesync/android synced 2025-01-11 00:01:12 +00:00

Change journal: make it possible to revert an item to a past state.

Fixes #83
This commit is contained in:
Tom Hacohen 2019-07-30 20:26:14 +01:00
parent f84ef25d6a
commit 1cfdc59b17
4 changed files with 88 additions and 7 deletions

View File

@ -4,10 +4,13 @@ import android.accounts.Account
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.provider.CalendarContract
import android.provider.ContactsContract
import android.text.format.DateFormat
import android.text.format.DateUtils
import android.view.*
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
@ -15,6 +18,7 @@ import androidx.viewpager.widget.ViewPager
import at.bitfire.ical4android.Event
import at.bitfire.ical4android.InvalidCalendarException
import at.bitfire.ical4android.Task
import at.bitfire.ical4android.TaskProvider
import at.bitfire.vcard4android.Contact
import com.etesync.syncadapter.App
import com.etesync.syncadapter.Constants
@ -22,6 +26,7 @@ import com.etesync.syncadapter.R
import com.etesync.syncadapter.model.CollectionInfo
import com.etesync.syncadapter.model.JournalEntity
import com.etesync.syncadapter.model.SyncEntry
import com.etesync.syncadapter.resource.*
import com.etesync.syncadapter.ui.journalviewer.ListEntriesFragment.Companion.setJournalEntryView
import com.etesync.syncadapter.utils.EventEmailInvitation
import com.google.android.material.tabs.TabLayout
@ -79,9 +84,8 @@ class JournalItemActivity : BaseActivity(), Refreshable {
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
if (emailInvitationEvent != null) {
menuInflater.inflate(R.menu.activity_journal_item, menu)
}
menuInflater.inflate(R.menu.activity_journal_item, menu)
menu.setGroupVisible(R.id.journal_item_menu_event_invite, emailInvitationEvent != null)
return true
}
@ -96,6 +100,62 @@ class JournalItemActivity : BaseActivity(), Refreshable {
startActivity(intent)
}
fun restoreItem(item: MenuItem) {
// FIXME: This code makes the assumption that providers are all available. May not be true for tasks, and potentially others too.
when (info.type) {
CollectionInfo.Type.CALENDAR -> {
val provider = contentResolver.acquireContentProviderClient(CalendarContract.CONTENT_URI)!!
val localCalendar = LocalCalendar.findByName(account, provider, LocalCalendar.Factory, info.uid!!)!!
val event = Event.fromReader(StringReader(syncEntry.content))[0]
var localEvent = localCalendar.findByUid(event.uid!!)
if (localEvent != null) {
localEvent.updateAsDirty(event)
} else {
localEvent = LocalEvent(localCalendar, event, event.uid, null)
localEvent.addAsDirty()
}
}
CollectionInfo.Type.TASKS -> {
val provider = TaskProvider.acquire(this, TaskProvider.ProviderName.OpenTasks)!!
val localTaskList = LocalTaskList.findByName(account, provider, LocalTaskList.Factory, info.uid!!)!!
val task = Task.fromReader(StringReader(syncEntry.content))[0]
var localTask = localTaskList.findByUid(task.uid!!)
if (localTask != null) {
localTask.updateAsDirty(task)
} else {
localTask = LocalTask(localTaskList, task, task.uid, null)
localTask.addAsDirty()
}
}
CollectionInfo.Type.ADDRESS_BOOK -> {
val provider = contentResolver.acquireContentProviderClient(ContactsContract.RawContacts.CONTENT_URI)!!
val localAddressBook = LocalAddressBook.findByUid(this, provider, account, info.uid!!)!!
val contact = Contact.fromReader(StringReader(syncEntry.content), null)[0]
if (contact.group) {
// FIXME: not currently supported
} else {
var localContact = localAddressBook.findByUid(contact.uid!!) as LocalContact?
if (localContact != null) {
localContact.updateAsDirty(contact)
} else {
localContact = LocalContact(localAddressBook, contact, contact.uid, null)
localContact.createAsDirty()
}
}
}
}
val dialog = AlertDialog.Builder(this)
.setTitle(R.string.journal_item_restore_action)
.setIcon(R.drawable.ic_restore_black)
.setMessage(R.string.journal_item_restore_dialog_body)
.setPositiveButton(android.R.string.ok) { dialog, which ->
// dismiss
}
.create()
dialog.show()
}
private class TabsAdapter(fm: FragmentManager, private val context: Context, private val info: CollectionInfo, private val syncEntry: SyncEntry) : FragmentPagerAdapter(fm) {
override fun getCount(): Int {

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M13,3c-4.97,0 -9,4.03 -9,9L1,12l3.89,3.89 0.07,0.14L9,12L6,12c0,-3.87 3.13,-7 7,-7s7,3.13 7,7 -3.13,7 -7,7c-1.93,0 -3.68,-0.79 -4.94,-2.06l-1.42,1.42C8.27,19.99 10.51,21 13,21c4.97,0 9,-4.03 9,-9s-4.03,-9 -9,-9zM12,8v5l4.28,2.54 0.72,-1.21 -3.5,-2.08L13.5,8L12,8z"/>
</vector>

View File

@ -10,8 +10,17 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="@string/calendar_attendees_send_email_action"
android:icon="@drawable/ic_email_black"
android:onClick="sendEventInvite"
app:showAsAction="always" />
<group android:id="@+id/journal_item_menu_event_invite">
<item android:title="@string/calendar_attendees_send_email_action"
android:icon="@drawable/ic_email_black"
android:onClick="sendEventInvite"
app:showAsAction="always" />
</group>
<group android:id="@+id/journal_item_menu_restore">
<item android:title="@string/journal_item_restore_action"
android:icon="@drawable/ic_restore_black"
android:onClick="restoreItem"
app:showAsAction="never" />
</group>
</menu>

View File

@ -176,6 +176,9 @@
<string name="journal_item_relation">Relation</string>
<string name="journal_item_member">Member</string>
<string name="journal_item_member_count">Member Count</string>
<string name="journal_item_restore_action">Restore state</string>
<string name="journal_item_restore_dialog_body">Successfully restored the entry to previous state.</string>
<!-- PermissionsActivity -->
<string name="permissions_title">EteSync permissions</string>