Skip to content

Commit

Permalink
Merge pull request #50 from RxKotlin/modifySaveEveryNoteButton
Browse files Browse the repository at this point in the history
add shareToEveryNoteButton
  • Loading branch information
TigerChain committed Jul 4, 2016
2 parents 5562ab8 + a4164d9 commit b0f5ce6
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 25 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ dependencies {
compile 'net.hockeyapp.android:HockeySDK:3.7.1'
// Apache commons
compile 'commons-io:commons-io:2.4'

compile 'com.android.support:design:23.4.0'

compile 'com.melnykov:floatingactionbutton:1.3.0'

}

buildscript {
Expand Down
101 changes: 101 additions & 0 deletions app/src/main/java/com/kotlinchina/smallpockets/utils/AppManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.kotlinchina.smallpockets.utils

import android.app.Activity
import android.content.Context

import java.util.Stack

@Suppress("UNUSED_VALUE")
/**
* Created by junjun.
*/
class AppManager private constructor() {

fun addActivity(activity: Activity?) {
if (activityStack == null) {
activityStack = Stack<Activity>()
}
activityStack?.add(activity)
}

fun currentActivity(): Activity? {
val activity = activityStack?.lastElement()
return activity as? Activity
}

fun finishActivity() {
val activity = activityStack?.lastElement()
Companion.finishActivity(activity)
}

fun finishActivity(cls: Class<*>) {
if(activityStack!=null){
activityStack?.forEach { activity: Activity ->
if (activity?.javaClass == cls) {
Companion.finishActivity(activity)
}
}
}

}

fun finishAllActivity() {
var i = 0
val size = activityStack?.size as Int
while (i < size) {
val tempActivityStack = activityStack as? Stack<Activity>
if(tempActivityStack!=null && tempActivityStack?.get(i)!=null){
Companion?.finishActivity(tempActivityStack?.get(i))
break
i++
}

}
activityStack?.clear()
}

fun AppExit(context: Context) {
try {
finishAllActivity()
android.os.Process.killProcess(android.os.Process.myPid())
System.exit(0)
} catch (e: Exception) {
}

}

companion object {
private var activityStack: Stack<Activity>? = null
private var instance: AppManager? = null
val appManager: AppManager?
get() {
if (instance == null) {
synchronized (AppManager::class.java) {
if (null == instance) {
instance = AppManager()
}
}
}
return instance as? AppManager
}

fun getActivity(cls: Class<*>): Activity? {
if (activityStack != null)
activityStack?.forEach { activity:Activity ->
if (activity.javaClass == cls) {
return activity
}
}
return null
}

fun finishActivity(activity: Activity?) {
val activity = activity
val canRemove = activity?.isFinishing as? Boolean
if (activity != null && canRemove!=null && !canRemove) {
activityStack?.remove(activity)
activity?.finish()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.kotlinchina.smallpockets.utils

import android.app.Activity
import android.os.Handler
import android.os.Looper
import android.view.KeyEvent
import android.widget.Toast
import com.kotlinchina.smallpockets.R

/**
* Created by junjun.
*/
class DoubClickExitHelper(private val mActivity: Activity) {

private var isOnKeyBacking: Boolean? = false
private var mHandler: Handler? = null
private var mBackToast: Toast? = null

init {
mHandler = Handler(Looper.getMainLooper())
}

fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode != KeyEvent.KEYCODE_BACK) {
return false
}
var isBack = isOnKeyBacking as? Boolean
if (isBack!=null && isBack) {
mHandler?.removeCallbacks(onBackTimeRunnable)
if (mBackToast != null) {
mBackToast?.cancel()
}
// 退出
AppManager.appManager?.AppExit(mActivity)
return true
} else {
isOnKeyBacking = true
if (mBackToast == null) {
mBackToast = Toast.makeText(mActivity, R.string.double_click_exit, 2000)
}
mBackToast?.show()
mHandler?.postDelayed(onBackTimeRunnable, 2000)
return true
}
}

private val onBackTimeRunnable = Runnable {
isOnKeyBacking = false
if (mBackToast != null) {
mBackToast?.cancel()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.kotlinchina.smallpockets.view.impl
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.widget.DrawerLayout
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.app.AlertDialog
import android.util.Log
import android.view.LayoutInflater
Expand All @@ -22,9 +23,10 @@ import com.kotlinchina.smallpockets.service.impl.RealmStore
import com.kotlinchina.smallpockets.service.impl.WebViewClientHttpService
import com.kotlinchina.smallpockets.view.Fragment.BaseWebViewFragment
import com.kotlinchina.smallpockets.view.ILinkListView
import com.melnykov.fab.FloatingActionButton
import java.util.*

class LinkListFragment() : Fragment(), ILinkListView {
class LinkListFragment() : Fragment(), ILinkListView ,SwipeRefreshLayout.OnRefreshListener {

var linkListPresenter: ILinkListPresenter? = null
val CLIPBOARD_TAG: String = "CLIPBOARD"
Expand All @@ -33,6 +35,9 @@ class LinkListFragment() : Fragment(), ILinkListView {
var adapter: ShowSiteListAdapter? = null
var drawerLayout: DrawerLayout? = null
var drawerContent: RelativeLayout? = null
var fab: FloatingActionButton? = null
var shShareWeeklyLinks:ShareWeeklyLinks? = null
var swipeLayout: SwipeRefreshLayout? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -64,9 +69,30 @@ class LinkListFragment() : Fragment(), ILinkListView {

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initView(view)
setSwipeLayout()
}

private fun setSwipeLayout() {
swipeLayout?.setOnRefreshListener(this)
swipeLayout?.setSize(SwipeRefreshLayout.LARGE)
swipeLayout?.setColorSchemeResources(android.R.color.holo_red_light,
android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_blue_bright)
}

private fun initView(view: View?) {
listView = view?.findViewById(R.id.link_list) as? ListView
drawerLayout = view?.findViewById(R.id.drawer_layout) as? DrawerLayout
drawerContent = view?.findViewById(R.id.drawer_content) as? RelativeLayout
fab = view?.findViewById(R.id.fab) as? FloatingActionButton
swipeLayout = view?.findViewById(R.id.swipeLayout) as? SwipeRefreshLayout
var curList = listView as? ListView
if(curList !=null){
fab?.attachToListView(curList)
}
fab?.setOnClickListener {
shShareWeeklyLinks?.shareLink()
}
}

override fun showDialog(link: String) {
Expand All @@ -91,6 +117,7 @@ class LinkListFragment() : Fragment(), ILinkListView {
data?.addAll(dataResult)
adapter = ShowSiteListAdapter(activity, R.layout.show_site_list_item, dataResult)
listView?.adapter = adapter
swipeLayout?.isRefreshing = false;
}

override fun showSaveScreenWithTitle(title: String, url: String) {
Expand All @@ -103,9 +130,7 @@ class LinkListFragment() : Fragment(), ILinkListView {
val title = data[SaveTagDialog.TITLE] as? String
val url = data[SaveTagDialog.URL] as? String
val tags = data[SaveTagDialog.TAGS] as? List<String>
if (title != null
&& url != null
&& tags != null) {
if (title != null && url != null && tags != null) {
linkListPresenter?.saveToDB(title, url, tags)
}
}
Expand All @@ -122,4 +147,15 @@ class LinkListFragment() : Fragment(), ILinkListView {
}
}

fun setShareWeeklyLinks(shShareWeeklyLinks:ShareWeeklyLinks){
this.shShareWeeklyLinks = shShareWeeklyLinks
}

interface ShareWeeklyLinks{
fun shareLink()
}

override fun onRefresh() {
linkListPresenter?.refreshList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.kotlinchina.smallpockets.view.impl
import android.os.Bundle

import android.support.v7.app.AppCompatActivity
import android.view.KeyEvent
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
Expand All @@ -14,12 +15,15 @@ import com.kotlinchina.smallpockets.presenter.impl.MainPresenter
import com.kotlinchina.smallpockets.service.impl.EvernoteShareService
import com.kotlinchina.smallpockets.service.impl.RealmStore
import com.kotlinchina.smallpockets.transform.impl.LinksToHTMLWithHTMLEngine
import com.kotlinchina.smallpockets.utils.AppManager
import com.kotlinchina.smallpockets.utils.DoubClickExitHelper
import com.kotlinchina.smallpockets.view.IMainView
import net.hockeyapp.android.CrashManager
import java.io.IOException
import java.util.*

class MainActivity : AppCompatActivity(), IMainView, EvernoteLoginFragment.ResultCallback {
class MainActivity : AppCompatActivity(), IMainView, EvernoteLoginFragment.ResultCallback, LinkListFragment.ShareWeeklyLinks {

companion object {
val SAVE_TAGS = "1000"
val EVERNOTE_SERVICE = EvernoteSession.EvernoteService.PRODUCTION
Expand All @@ -28,12 +32,19 @@ class MainActivity : AppCompatActivity(), IMainView, EvernoteLoginFragment.Resul
var mainPresenter: IMainPresenter? = null
var evernoteSession: EvernoteSession? = null
var linkListFragment: LinkListFragment? = null
var doubleClickExit: DoubClickExitHelper? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
setupFrament()
setupPresneter()
initMember()
}

private fun initMember() {
AppManager.appManager?.addActivity(this)
doubleClickExit = DoubClickExitHelper(this)
}

override fun onResume() {
Expand Down Expand Up @@ -76,7 +87,6 @@ class MainActivity : AppCompatActivity(), IMainView, EvernoteLoginFragment.Resul
val key = progerties.getProperty("consumer_key")
val secret = progerties.getProperty("consumer_secret")
inStream.close()

EvernoteSession.Builder(this)
.setEvernoteService(EVERNOTE_SERVICE)
.setSupportAppLinkedNotebooks(false)
Expand All @@ -98,12 +108,27 @@ class MainActivity : AppCompatActivity(), IMainView, EvernoteLoginFragment.Resul
linkListFragment = LinkListFragment()
fragmentTransaction.add(R.id.main_container, linkListFragment)
fragmentTransaction.commit()
(linkListFragment as? LinkListFragment)?.setShareWeeklyLinks(this)
}

override fun shareLink() {
if (checkEvernoteLogin()) {
mainPresenter?.shareWeeklyLinks(Date())
} else {
evernoteSession?.authenticate(this)
}
}

override fun onBackPressed() {
val backPressedEvent = linkListFragment?.backPressed() as? Boolean
if(backPressedEvent!=null && !backPressedEvent){
super.onBackPressed()
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if(keyCode == KeyEvent.KEYCODE_BACK){
val backPressedEvent = linkListFragment?.backPressed() as? Boolean
if(backPressedEvent!=null && !backPressedEvent){
val isExitApp = doubleClickExit?.onKeyDown(keyCode, event) as? Boolean
if(isExitApp!=null){
return isExitApp
}
}
}
return false ;
}
}
Loading

0 comments on commit b0f5ce6

Please sign in to comment.