Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
feat: delete synchronized data
Browse files Browse the repository at this point in the history
Add an option in setting to delete (locally) the data already synchronized in the remote database (on the server).

related to #87
  • Loading branch information
rOmAiin062 committed Nov 16, 2018
1 parent a99fa72 commit 99d37bc
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 10 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ dependencies {

repositories {
mavenCentral()
maven { url 'https://maven.fabric.io/public' }

}

configurations {
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@

<meta-data
android:name="io.fabric.ApiKey"
android:value="${fabric_api_key}"/>
android:value="f70ae3b4e2335171d5d6b51368637af3bbfbbfb2"
/>

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="${google_map_api_key}" />
android:value="AIzaSyAnCO9njYPfNfdGKNEbmt_UJ3_lMDYTAb8" />

<meta-data
android:name="firebase_performance_logcat_enabled"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ interface SensorDao {
@Query("SELECT count(*) FROM Device WHERE isSync=0")
fun getSensorNotSyncCount(): Long

@Query("SELECT count(*) FROM Device WHERE isSync=1")
fun getSensorSyncCount(): Long

@Query("SELECT count(*) FROM Device")
fun getSensorCount(): Long

Expand All @@ -33,6 +36,9 @@ interface SensorDao {
@Query("SELECT * FROM Device WHERE id=:idDevice")
fun getSensorById(idDevice: Long): LiveData<Device>

@Query("DELETE FROM Device WHERE isSync=1")
fun deleteDataSync()

@Transaction
@Query("SELECT * FROM Device")
fun dumpSensor(): List<Device>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class SyncInfluxDBJob : Job(Params(PRIORITY)
doAsync {
dataNotSync.forEach {
it.isSync = 1
//Toasty.success(applicationContext, "isSync + 1", Toast.LENGTH_SHORT, true).show()
}
uiThread {
doAsync {
Expand Down
112 changes: 110 additions & 2 deletions app/src/main/java/science/apolline/view/activity/SettingsActivity.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package science.apolline.view.activity

import android.annotation.TargetApi
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.*
import android.preference.*
import android.support.annotation.RequiresApi
import android.text.TextUtils
import android.view.MenuItem
import android.widget.Toast
import science.apolline.R
import science.apolline.service.database.AppDatabase
import science.apolline.service.database.SensorDao
import java.lang.ref.WeakReference


/**
* A [PreferenceActivity] that presents a set of application settings. On
Expand Down Expand Up @@ -62,6 +69,7 @@ class SettingsActivity : AppCompatPreferenceActivity() {
|| ChartPreferenceFragment::class.java.name == fragmentName
|| DataSyncPreferenceFragment::class.java.name == fragmentName
|| NotificationPreferenceFragment::class.java.name == fragmentName
|| DataErasePreferenceFragment::class.java.name == fragmentName
}

/**
Expand Down Expand Up @@ -174,6 +182,106 @@ class SettingsActivity : AppCompatPreferenceActivity() {
}
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
class DataErasePreferenceFragment : PreferenceFragment(){

@RequiresApi(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val builder = AlertDialog.Builder(this.context)
var countSyncData = 0

if (this.context != null) {
//sensorModel = AppDatabase.getInstance(this.context).sensorDao()
//var res = sensorModel.getSensorSyncCount()
countSyncData = GetCountSyncDataAsyncTask(this).execute().get()
}

// Set the alert dialog title
builder.setTitle("Delete synchronized data")

// Display a message on alert dialog
builder.setMessage("Do you want to delete all the synchronized data ? (" +
countSyncData + ")")

// Set a positive button and its click listener on alert dialog
builder.setPositiveButton("YES"){dialog, which ->
// Do something when user press the positive button
DeleteDataSyncTask(this).execute()
Toast.makeText(this.context,"Synchonized data has been deleted",Toast.LENGTH_SHORT).show()
}

// Display a neutral button on alert dialog
builder.setNeutralButton("Cancel"){_,_ ->
Toast.makeText(this.context,"Synchronized data has been kept",Toast.LENGTH_SHORT).show()
}

// Finally, make the alert dialog using builder
val dialog: AlertDialog = builder.create()

// Display the alert dialog on app interface
dialog.show()
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == android.R.id.home) {
startActivity(Intent(activity, SettingsActivity::class.java))
return true
}
return super.onOptionsItemSelected(item)
}
}


private class GetCountSyncDataAsyncTask(activity: DataErasePreferenceFragment) : AsyncTask<Void, Void, Int>() {
//Prevent leak
private val weakActivity: WeakReference<Activity>
private var mActivity : DataErasePreferenceFragment

init{
weakActivity = WeakReference(activity.activity)
mActivity = activity
}

@RequiresApi(Build.VERSION_CODES.M)
protected override fun doInBackground(vararg params:Void):Int {
val sensorModel = AppDatabase.getInstance(mActivity.context).sensorDao()
return sensorModel.getSensorSyncCount().toInt()
}

protected override fun onPostExecute(countSyncData:Int) {
val activity = weakActivity.get() ?: return
Toast.makeText(activity, "Count Data Sync : " + countSyncData.toString(), Toast.LENGTH_LONG).show()
activity.onBackPressed()
}
}

private class DeleteDataSyncTask(activity: DataErasePreferenceFragment) : AsyncTask<Void, Void, Int>() {
//Prevent leak
private val weakActivity: WeakReference<Activity>
private var mActivity : DataErasePreferenceFragment

init{
weakActivity = WeakReference(activity.activity)
mActivity = activity
}

@RequiresApi(Build.VERSION_CODES.M)
protected override fun doInBackground(vararg params:Void):Int {
val sensorModel = AppDatabase.getInstance(mActivity.context).sensorDao()
sensorModel.deleteDataSync()
return 1
}

protected override fun onPostExecute(result: Int?) {
val activity = weakActivity.get() ?: return
activity.onBackPressed()
}
}


companion object {

/**
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/xml/pref_headers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
android:icon="@drawable/ic_sync_black_24dp"
android:title="@string/pref_header_data_sync" />




<header
android:fragment="science.apolline.view.activity.SettingsActivity$DataErasePreferenceFragment"
android:icon="@drawable/ic_sync_black_24dp"
android:title="Delete synchronized data" />

</preference-headers>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ buildscript {
sonarqube_url = hasProperty('SONARQUBE_URL') ? SONARQUBE_URL : System.getenv('SONARQUBE_URL')
}
repositories {
jcenter()
google()
maven { url 'https://maven.fabric.io/public' }
jcenter()
}
dependencies {

Expand All @@ -33,11 +33,11 @@ buildscript {

allprojects {
repositories {
jcenter()
google()
maven { url 'https://maven.google.com' }
maven { url "https://jitpack.io" }
maven { url 'https://maven.fabric.io/public' }
jcenter()
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Sun Apr 01 12:44:28 CEST 2018
#Fri Nov 16 09:49:20 CET 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down

0 comments on commit 99d37bc

Please sign in to comment.