Last active
February 19, 2019 19:28
-
-
Save nxsyed/c31eea8baa2f3106d388d3dbedb9f9db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity(), CardStackListener { | |
private val cardStackView by lazy { findViewById<CardStackView>(R.id.card_stack_view) } | |
private val manager by lazy { CardStackLayoutManager(this, this) } | |
private val adapter by lazy { CardStackAdapter(createSpots("Welcome to Dating Swipe!", "0")) } | |
private val MY_PERMISSIONS_REQUESTACCESS_COARSE_LOCATION = 1 | |
private lateinit var fusedLocationClient: FusedLocationProviderClient | |
private val pnConfiguration = PNConfiguration() | |
init { | |
pnConfiguration.publishKey = "YOUR-PUB-KEY" | |
pnConfiguration.subscribeKey = "YOUR-SUB-KEY" | |
} | |
private val pubNub = PubNub(pnConfiguration) | |
private val userLocation = mutableListOf<Double>(0.0,0.0) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val androidID = Settings.Secure.getString(this.contentResolver, Settings.Secure.ANDROID_ID) | |
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this) | |
if (checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) | |
!= PackageManager.PERMISSION_GRANTED) { | |
if (ActivityCompat.shouldShowRequestPermissionRationale(this, | |
android.Manifest.permission.ACCESS_COARSE_LOCATION)) { | |
} else { | |
ActivityCompat.requestPermissions(this, | |
arrayOf(android.Manifest.permission.ACCESS_COARSE_LOCATION), | |
MY_PERMISSIONS_REQUESTACCESS_COARSE_LOCATION) | |
} | |
} else { | |
fusedLocationClient.lastLocation | |
.addOnSuccessListener { location: Location? -> | |
if (location != null) { | |
userLocation[0] = location.latitude | |
userLocation[1] = location.longitude | |
Log.d("Location", location.latitude.toString()) | |
} else { | |
Log.d("Location", location?.latitude.toString()) | |
} | |
} | |
} | |
var subscribeCallback: SubscribeCallback = object : SubscribeCallback() { | |
override fun status(pubnub: PubNub, status: PNStatus) { | |
} | |
override fun message(pubnub: PubNub, message: PNMessageResult) { | |
Log.d("PubNub", message.message.toString()) | |
var person = message.message.asJsonObject | |
runOnUiThread { paginate(person.get("ID").toString(), person.get("distance").toString()) } | |
} | |
override fun presence(pubnub: PubNub, presence: PNPresenceEventResult) { | |
} | |
} | |
pubNub.run { | |
addListener(subscribeCallback) | |
subscribe() | |
.channels(Arrays.asList(androidID)) | |
.execute() | |
} | |
setupCardStackView() | |
setupButton() | |
} | |
override fun onStart() { | |
super.onStart() | |
val androidID = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID) | |
pubNub.run { | |
publish() | |
.message(""" | |
{ | |
"location": { | |
"lat":${userLocation[0]}, | |
"long":${userLocation[1]} | |
}, | |
"id": "$androidID" | |
} | |
""".trimIndent()) | |
.channel("Users") | |
.async(object : PNCallback<PNPublishResult>() { | |
override fun onResponse(result: PNPublishResult, status: PNStatus) { | |
if (!status.isError) { | |
println("Message was published") | |
} else { | |
println("Could not publish") | |
} | |
} | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment