Skip to content
This repository has been archived by the owner on Sep 17, 2020. It is now read-only.

Commit

Permalink
ネット接続時にAppSyncにアップロード
Browse files Browse the repository at this point in the history
  • Loading branch information
okwrtdsh committed Oct 19, 2019
1 parent 25fea97 commit dfd3d9e
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .graphqlconfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ projects:
codeGenTarget: ''
generatedFileName: ''
docsFilePath: app/src/main/graphql/com/amazonaws/amplify/generated/graphql
extensions:
amplify:
version: 3
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

apply plugin: 'com.amazonaws.appsync'


android {
compileSdkVersion 28
defaultConfig {
Expand Down
98 changes: 97 additions & 1 deletion app/src/main/java/com/github/okwrtdsh/idobatter/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -16,11 +21,18 @@ import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.amazonaws.amplify.generated.graphql.CreateCoordinateMutation
import com.amazonaws.mobile.config.AWSConfiguration
import com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient
import com.apollographql.apollo.GraphQLCall
import com.apollographql.apollo.api.Response
import com.apollographql.apollo.exception.ApolloException
import com.github.okwrtdsh.idobatter.room.Message
import com.github.okwrtdsh.idobatter.room.MessageViewModel
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import kotlinx.android.synthetic.main.activity_main.*
import type.CreateCoordinateInput
import java.text.SimpleDateFormat
import java.util.*

Expand Down Expand Up @@ -63,9 +75,17 @@ class MainActivity : AppCompatActivity() {
private val newMessageActivityRequestCode = 1
private lateinit var messageViewModel: MessageViewModel
private lateinit var fusedLocationClient: FusedLocationProviderClient
private lateinit var cm: ConnectivityManager
private var isConnected = false
private lateinit var mAWSAppSyncClient: AWSAppSyncClient


companion object {
private const val REQUEST_CODE_PERMISSIONS = 1
// private const val TAG: String = "idobatter"
// private const val channelId = "com.github.okwrtdsh.idobatter"
// private const val channelDescription = "Description"
// private const val channelName = "idobatter"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -84,11 +104,31 @@ class MainActivity : AppCompatActivity() {
val intent = Intent(this@MainActivity, NewMessageActivity::class.java)
startActivityForResult(intent, newMessageActivityRequestCode)
}

// amplify
mAWSAppSyncClient = AWSAppSyncClient.builder()
.context(this)
.awsConfiguration(AWSConfiguration(this))
.build()

ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_NETWORK_STATE
),
REQUEST_CODE_PERMISSIONS
)

// watch net state
cm = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
NetworkRequest
.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
.build()
.run {
cm.registerNetworkCallback(this, mNetworkCallback)
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
Expand All @@ -108,4 +148,60 @@ class MainActivity : AppCompatActivity() {
Toast.LENGTH_LONG).show()
}
}

private val mNetworkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network?) {
updateConnectionStatus()
uploadCoordinateData()
}
override fun onLost(network: Network?) {
updateConnectionStatus()
uploadCoordinateData()
}
}

private fun updateConnectionStatus() {
val activeNetworks = cm.allNetworks.mapNotNull {
cm.getNetworkCapabilities(it)
}.filter {
it.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
it.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
}
isConnected = activeNetworks.isNotEmpty()
}

private fun uploadCoordinateData() {
if (isConnected) {
val mutation = CreateCoordinateMutation.builder()
messageViewModel.uploadable().map { message ->
Log.d("#########", message.content)
mutation.input(
CreateCoordinateInput.builder()
.uuid(message.uuid)
.lat(message.lat)
.lng(message.lng)
.time(message.created.toInt())
.build()
)
Log.d("#########2", message.content)
message.isUploaded = true
messageViewModel.update(message.uuid)
}
runMutate(mutation.build())
}

}
private fun runMutate(mutation: CreateCoordinateMutation) {
mAWSAppSyncClient.mutate(mutation).enqueue(mutationCallback)
}

private val mutationCallback = object : GraphQLCall.Callback<CreateCoordinateMutation.Data>() {
override fun onResponse(response: Response<CreateCoordinateMutation.Data>) {
Log.d("Results", "Added")
}

override fun onFailure(e: ApolloException) {
Log.e("Error", e.toString())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class Message(
val lat: Double,
val lng: Double,
val hops: Int,
@ColumnInfo(name = "is_fab") val isFab: Boolean,
@ColumnInfo(name = "is_fab") var isFab: Boolean,
@ColumnInfo(name = "is_auther") val isAuther: Boolean,
@ColumnInfo(name = "is_uploaded") val isUploaded: Boolean,
@ColumnInfo(name = "is_uploaded") var isUploaded: Boolean,
@ColumnInfo(name = "limit_dist") val limitDist: Int,
@ColumnInfo(name = "limit_time") val limitTime: Int,
@ColumnInfo(name = "limit_hops") val limitHops: Int
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.github.okwrtdsh.idobatter.room

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.*


@Dao
Expand Down Expand Up @@ -59,4 +56,11 @@ interface MessageDao {
LIMIT :limit
""")
fun enabled(current_time: Long, current_lat: Double, current_lng: Double, limit: Int): List<Message>

@Query("SELECT * FROM message_table WHERE is_uploaded = 0 ORDER BY created ASC")
fun uploadable(): List<Message>

@Query("update message_table set is_uploaded = 1 where uuid = :uuid")
fun update(uuid: String)

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ class MessageRepository(private val messageDao: MessageDao) {

val getAll: LiveData<List<Message>> = messageDao.getAll()

suspend fun insert(word: Message) {
messageDao.insert(word)
suspend fun insert(message: Message) {
messageDao.insert(message)
}

fun update(uuid: String) {
messageDao.update(uuid)
}

fun uploadable() = messageDao.uploadable()
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,10 @@ class MessageViewModel(application: Application) : AndroidViewModel(application)
repository.insert(message)
}

/*
@PrimaryKey val uuid: String,
val created: Long,
val content: String,
val lat: Double,
val lng: Double,
val hops: Int,
@ColumnInfo(name = "is_fab") val isFab: Boolean,
@ColumnInfo(name = "is_auther") val isAuther: Boolean,
@ColumnInfo(name = "is_uploaded") val isUploaded: Boolean,
@ColumnInfo(name = "limit_dist") val limitDist: Int,
@ColumnInfo(name = "limit_time") val limitTime: Int,
@ColumnInfo(name = "limit_hops") val limitHops: Int
*/
fun uploadable() = repository.uploadable()

fun update(uuid: String)= repository.update(uuid)


fun create(
content: String,
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/layout/activity_new_message.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

<Button
android:id="@+id/button_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ourOrange"
android:text="@string/button_save"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="@mipmap/ic_launcher_round"
android:textColor="@color/ourBlack" />

</LinearLayout>

0 comments on commit dfd3d9e

Please sign in to comment.