Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Commit

Permalink
Cleaned up code, enhanced notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
bramhaag committed Jul 2, 2017
1 parent 8725b69 commit d49cada
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 62 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
signingConfigs {
Expand All @@ -17,7 +18,7 @@ android {
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "0.2.0"
versionName "0.2.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
signingConfig signingConfigs.config
}
Expand Down
101 changes: 58 additions & 43 deletions app/src/main/kotlin/me/bramhaag/owoandroid/activities/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package me.bramhaag.owoandroid.activities

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.preference.PreferenceManager
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.support.v7.app.AppCompatDelegate
import android.support.v7.app.AppCompatDelegate.MODE_NIGHT_NO
import android.support.v7.app.AppCompatDelegate.MODE_NIGHT_YES
import android.support.v7.widget.RecyclerView
import android.text.InputType
import android.view.Menu
import android.view.MenuItem
import com.afollestad.materialdialogs.MaterialDialog
import kotlinx.android.synthetic.main.activity_main.*
import me.bramhaag.owoandroid.R
import me.bramhaag.owoandroid.api.OwO
import me.bramhaag.owoandroid.data.files.FilesDbHelper
Expand All @@ -23,69 +27,45 @@ import me.bramhaag.owoandroid.listeners.UploadButtonListener
import me.bramhaag.owoandroid.managers.RecyclerViewManager
import me.bramhaag.owoandroid.services.QuickUploadService
import me.bramhaag.owoandroid.util.Consumer
import net.cachapa.expandablelayout.ExpandableLayout

class MainActivity : AppCompatActivity() {

companion object {
lateinit var owo: OwO
lateinit var mRecycleViewManager: RecyclerViewManager
}

val resultConsumerMap = HashMap<Int, Consumer<Intent?>>()

lateinit var filesDbHelper: FilesDbHelper
lateinit var urlDbHelper: UrlDbHelper

lateinit var mRecycleViewManager: RecyclerViewManager

override fun onCreate(savedInstanceState: Bundle?) {
AppCompatDelegate.setDefaultNightMode(if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("pref_dark_theme", false)) MODE_NIGHT_YES else MODE_NIGHT_NO)

AppCompatDelegate.setDefaultNightMode(
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("pref_dark_theme", false)) MODE_NIGHT_YES
else MODE_NIGHT_NO
)
super.onCreate(savedInstanceState)

filesDbHelper = FilesDbHelper(this)
urlDbHelper = UrlDbHelper(this)

val key = PreferenceManager.getDefaultSharedPreferences(this).getString("pref_key", null)
if(key == null || key.isBlank()) {
MaterialDialog.Builder(this)
.title(getString(R.string.dialog_welcome_title))
.content(getString(R.string.dialog_welcome_content))
.positiveText(getString(R.string.dialog_button_continue))
.inputType(InputType.TYPE_CLASS_TEXT)
.input(getString(R.string.dialog_welcome_input), null, false, { _, _ -> })
.checkBoxPrompt(getString(R.string.dialog_welcome_checkbox), true, { _, isChecked -> PreferenceManager.getDefaultSharedPreferences(this@MainActivity).edit().putBoolean("acra.disable", isChecked).apply() })
.onPositive { dialog, _ ->
val prefKey = dialog.inputEditText?.text.toString()
PreferenceManager.getDefaultSharedPreferences(this@MainActivity).edit().putString("pref_key", prefKey).apply()
owo = OwO(prefKey)
}
.cancelable(false)
.canceledOnTouchOutside(false)
.autoDismiss(true)
.show()
} else owo = OwO(key)

val data = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
if(intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM) != null) {
val mServiceIntent = Intent(this, QuickUploadService::class.java)
mServiceIntent.putExtra(Intent.EXTRA_STREAM, data)
this.startService(mServiceIntent)

finish()
return
} else {
setContentView(R.layout.activity_main)
}
val firstLaunch = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("first_launch", true)
if (firstLaunch) handleFirstLaunch()
else owo = OwO(PreferenceManager.getDefaultSharedPreferences(this).getString("pref_key", ""))

findViewById(R.id.upload_button).setOnClickListener(UploadButtonListener(this))
findViewById(R.id.shorten_button).setOnClickListener(ShortenButtonListener(this))
if (!firstLaunch && handleShare(intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM))) return
else setContentView(R.layout.activity_main)

val expandableLayoutListener = ExpandableLayoutListener(findViewById(R.id.expandable_layout_upload) as ExpandableLayout, findViewById(R.id.expandable_layout_shorten) as ExpandableLayout)
findViewById(R.id.button_expand_upload).setOnClickListener(expandableLayoutListener)
findViewById(R.id.button_expand_shorten).setOnClickListener(expandableLayoutListener)
upload_button.setOnClickListener(UploadButtonListener(this))
shorten_button.setOnClickListener(ShortenButtonListener(this))

val expandableLayoutListener = ExpandableLayoutListener(expandable_layout_upload, expandable_layout_shorten)
button_expand_upload.setOnClickListener(expandableLayoutListener)
button_expand_shorten.setOnClickListener(expandableLayoutListener)

mRecycleViewManager = RecyclerViewManager(findViewById(R.id.upload_recycler_view) as RecyclerView, findViewById(R.id.shorten_recycler_view) as RecyclerView, applicationContext)

mRecycleViewManager = RecyclerViewManager(upload_recycler_view, shorten_recycler_view, applicationContext)
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
Expand All @@ -106,4 +86,39 @@ class MainActivity : AppCompatActivity() {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
resultConsumerMap[requestCode]?.accept(data)
}

private fun handleFirstLaunch() {
MaterialDialog.Builder(this)
.title(getString(R.string.dialog_welcome_title))
.content(getString(R.string.dialog_welcome_content))
.positiveText(getString(R.string.dialog_button_continue))
.inputType(InputType.TYPE_CLASS_TEXT)
.input(getString(R.string.dialog_welcome_input), null, false, { _, _ -> })
.checkBoxPrompt(getString(R.string.dialog_welcome_checkbox), true, { _, isChecked -> PreferenceManager.getDefaultSharedPreferences(this@MainActivity).edit().putBoolean("acra.disable", isChecked).apply() })
.onPositive { dialog, _ ->
val prefKey = dialog.inputEditText?.text.toString()
PreferenceManager.getDefaultSharedPreferences(this@MainActivity).edit().putString("pref_key", prefKey).apply()
owo = OwO(prefKey)

PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("first_launch", false).apply()
}
.cancelable(false)
.canceledOnTouchOutside(false)
.autoDismiss(true)
.show()
}

private fun handleShare(data: Uri?): Boolean {
if(data == null) return false

ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1)
if(ContextCompat.checkSelfPermission(applicationContext, android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) return false

val mServiceIntent = Intent(this, QuickUploadService::class.java)
mServiceIntent.putExtra(Intent.EXTRA_STREAM, data)
this.startService(mServiceIntent)
finish()

return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import kotlinx.android.synthetic.main.shorten_history_item.view.*
import me.bramhaag.owoandroid.R
import me.bramhaag.owoandroid.components.ShortenHistoryItem
import me.bramhaag.owoandroid.listeners.ShortenHistoryItemListener
Expand Down Expand Up @@ -35,8 +36,8 @@ class ShortenUrlAdapter(var urls: List<ShortenHistoryItem>, var context: Context

init {
itemView.tag = this
originalUrl = itemView.findViewById(R.id.shorten_item_original_url) as TextView
description = itemView.findViewById(R.id.shorten_item_shortened_url) as TextView
originalUrl = itemView.shorten_item_original_url
description = itemView.shorten_item_shortened_url

itemView.setOnClickListener(ShortenHistoryItemListener(this))
itemView.setOnLongClickListener(ShortenHistoryItemListener(this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.upload_history_item.view.*
import me.bramhaag.owoandroid.R
import me.bramhaag.owoandroid.components.UploadHistoryItem
import me.bramhaag.owoandroid.listeners.UploadHistoryItemListener
Expand All @@ -30,7 +31,7 @@ class UploadFileAdapter(var files: List<UploadHistoryItem>, var context: Context
holder.url = url
Glide.with(context)
.load(url.toString())
.fitCenter()
.centerCrop()
.placeholder(R.drawable.not_found)
.into(holder.image)
}
Expand All @@ -44,9 +45,9 @@ class UploadFileAdapter(var files: List<UploadHistoryItem>, var context: Context

init {
itemView.tag = this
title = itemView.findViewById(R.id.upload_item_title) as TextView
description = itemView.findViewById(R.id.upload_item_description) as TextView
image = itemView.findViewById(R.id.upload_item_image) as ImageView
title = itemView.upload_item_title
description = itemView.upload_item_description
image = itemView.upload_item_image

itemView.setOnClickListener(UploadHistoryItemListener(this))
itemView.setOnLongClickListener(UploadHistoryItemListener(this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class ShortenButtonListener(val activity: MainActivity): View.OnClickListener {
resultDialog.setContent(shortenedUrl)
resultDialog.getActionButton(DialogAction.NEUTRAL).isEnabled = true

activity.mRecycleViewManager.addUrl(URL(url), URL(shortenedUrl), Date())
MainActivity.mRecycleViewManager.addUrl(URL(url), URL(shortenedUrl), Date())
}

override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.text.Html
import android.util.Log
import android.view.View
import com.afollestad.materialdialogs.MaterialDialog
import kotlinx.android.synthetic.main.activity_main.*
import me.bramhaag.owoandroid.R
import me.bramhaag.owoandroid.activities.MainActivity
import me.bramhaag.owoandroid.api.ProgressRequestBody
Expand Down Expand Up @@ -110,7 +111,7 @@ class UploadButtonListener(val activity: MainActivity): View.OnClickListener {
mUploadQueue.removeFirst()
val obj = JSONObject(response.body()?.string()).getJSONArray("files").getJSONObject(0)
val url = URL("${PreferenceManager.getDefaultSharedPreferences(activity).getString("pref_destination", "https://owo.whats-th.is")}/${obj.getString("url")}")
activity.mRecycleViewManager.addFile(obj.getString("name"), url, Date())
MainActivity.mRecycleViewManager.addFile(obj.getString("name"), url, Date())

if (mUploadQueue.isNotEmpty()) {
upload(mUploadQueue.first, index + 1, total)
Expand All @@ -119,9 +120,9 @@ class UploadButtonListener(val activity: MainActivity): View.OnClickListener {

dialog.dismiss()
if (total > 1) {
Snackbar.make(activity.findViewById(R.id.main_scroll_view), activity.getString(R.string.snackbar_upload_multi_content, total), Snackbar.LENGTH_LONG).show()
Snackbar.make(activity.main_scroll_view, activity.getString(R.string.snackbar_upload_multi_content, total), Snackbar.LENGTH_LONG).show()
} else {
Snackbar.make(activity.findViewById(R.id.main_scroll_view), activity.getString(R.string.snackbar_upload_single_content), Snackbar.LENGTH_LONG)
Snackbar.make(activity.main_scroll_view, activity.getString(R.string.snackbar_upload_single_content), Snackbar.LENGTH_LONG)
.setAction(R.string.dialog_open_file, { ContextCompat.startActivity(activity, Intent(Intent.ACTION_VIEW, Uri.parse(url.toString())), null) })
.show()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import retrofit2.Response
import java.io.ByteArrayOutputStream
import java.net.URL
import java.text.DecimalFormat
import java.util.*
import java.util.concurrent.atomic.AtomicInteger

class QuickUploadService : IntentService("QuickUploadService") {
Expand All @@ -51,12 +52,14 @@ class QuickUploadService : IntentService("QuickUploadService") {
val notification = NotificationCompat.Builder(applicationContext)
.setOngoing(true)
.setSmallIcon(R.drawable.upload)
.setContentTitle("Uploading...")
.setContentTitle(getString(R.string.notification_uploading))
.setProgress(100, 0, true)

val errorNotification = NotificationCompat.Builder(applicationContext)
.setContentTitle("Error!")
.setContentText("An error occurred while uploading your file!")
.setSmallIcon(R.drawable.owo_white)
.setColor(Color.RED)
.setContentTitle(getString(R.string.notification_error))
.setContentText(getString(R.string.notification_error_verbose))

notificationManager.notify(id, notification.build())

Expand Down Expand Up @@ -111,13 +114,15 @@ class QuickUploadService : IntentService("QuickUploadService") {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url.toString()))
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)

notificationManager.notify(id, NotificationCompat.Builder(applicationContext)
MainActivity.mRecycleViewManager.addFile(obj.getString("name"), url, Date())

notificationManager.notify(id,
NotificationCompat.Builder(applicationContext)
.setSmallIcon(R.drawable.owo_white)
.setContentTitle("Upload completed!")
.setContentTitle("Uploaded ${requestFile.name}")
.setColor(Color.rgb(40, 153, 217))
.setContentIntent(PendingIntent.getActivity(applicationContext, 0, browserIntent, 0))
.setAutoCancel(true)
.build()
.setAutoCancel(true).build()
)
}
})
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,7 @@
<string name="toast_uploading">Uploading…</string>
<string name="upload_history">Upload History</string>
<string name="shortened_urls_history">Shortened URLs History</string>
<string name="notification_uploading">Uploading…</string>
<string name="notification_error">Error!</string>
<string name="notification_error_verbose">An error occurred while uploading your file!</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.1.2-5'
ext.kotlin_version = '1.1.3'
ext.support_version = '25.3.1'

repositories {
Expand Down

0 comments on commit d49cada

Please sign in to comment.