Skip to content

Commit

Permalink
增加消息下拉加载更多,优化一些问题
Browse files Browse the repository at this point in the history
  • Loading branch information
caiyonglong committed Oct 1, 2018
1 parent 3791a61 commit fdd4097
Show file tree
Hide file tree
Showing 34 changed files with 332 additions and 245 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ android {
applicationId "com.cyl.musiclake"
minSdkVersion 21
targetSdkVersion 27
versionCode 19
versionName "4.1.6.beta"
versionCode 20
versionName "4.1.6"
multiDexEnabled true
android.compileOptions.sourceCompatibility 1.8
android.compileOptions.targetCompatibility 1.8
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@
android:name=".ui.music.search.SearchActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name"
android:screenOrientation="portrait" />
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustUnspecified|stateHidden" />
<activity
android:name=".ui.zone.EditActivity"
android:label="@string/app_name"
Expand Down Expand Up @@ -216,6 +217,9 @@
android:theme="@style/MainActivityTheme" />
<activity
android:name=".ui.music.importplaylist.ImportPlaylistActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name"
android:windowSoftInputMode="adjustUnspecified|stateHidden"
android:screenOrientation="portrait" />
<activity
android:name="com.tencent.bugly.beta.ui.BetaActivity"
Expand Down
51 changes: 28 additions & 23 deletions app/src/main/java/com/cyl/musiclake/api/MusicApiServiceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.cyl.musiclake.api

import com.cyl.musicapi.BaseApiImpl
import com.cyl.musicapi.bean.*
import com.cyl.musiclake.MusicApp
import com.cyl.musiclake.R
import com.cyl.musiclake.api.doupan.DoubanApiServiceImpl
import com.cyl.musiclake.bean.Artist
import com.cyl.musiclake.bean.Music
Expand Down Expand Up @@ -34,7 +36,7 @@ object MusicApiServiceImpl {
*/
fun searchMusic(key: String, type: SearchEngine.Filter, limit: Int, page: Int): Observable<MutableList<Music>> {
return create { result ->
BaseApiImpl.searchSong(key, limit, page) {
BaseApiImpl.searchSong(key, limit, page, success = {
val musicList = mutableListOf<Music>()
if (it.status) {
if (type == ANY || type == NETEASE)
Expand All @@ -61,9 +63,12 @@ object MusicApiServiceImpl {
result.onNext(musicList)
result.onComplete()
} else {
result.onError(Throwable("service error"))
result.onError(Throwable(MusicApp.getAppContext().getString(R.string.error_search)))
}
}
}, fail = {
result.onError(Throwable(it
?: MusicApp.getAppContext().getString(R.string.error_connection)))
})
}
}

Expand Down Expand Up @@ -315,27 +320,27 @@ object MusicApiServiceImpl {
MusicApi.getLocalLyricInfo(mLyricPath)
} else create { result ->
BaseApiImpl.getLyricInfo(vendor, mid) {
if (it.status) {
val lyricInfo = it.data.lyric
val lyric = StringBuilder()
lyricInfo.forEach {
lyric.append(it)
lyric.append("\n")
}
it.data.translate.forEach {
lyric.append(it)
lyric.append("\n")
}
//保存文件
val save = FileUtils.writeText(mLyricPath, lyric.toString())
LogUtil.e("保存网络歌词:$save")
Observable.fromArray(lyric)
result.onNext(lyric.toString())
result.onComplete()
} else {
result.onError(Throwable(""))
}
if (it.status) {
val lyricInfo = it.data.lyric
val lyric = StringBuilder()
lyricInfo.forEach {
lyric.append(it)
lyric.append("\n")
}
it.data.translate.forEach {
lyric.append(it)
lyric.append("\n")
}
//保存文件
val save = FileUtils.writeText(mLyricPath, lyric.toString())
LogUtil.e("保存网络歌词:$save")
Observable.fromArray(lyric)
result.onNext(lyric.toString())
result.onComplete()
} else {
result.onError(Throwable(""))
}
}
}
} catch (e: Throwable) {
e.printStackTrace()
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/cyl/musiclake/api/MusicUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ object MusicUtils {
}
}
val album = Album(music.albumId, music.album, music.coverUri)
if (music.type == Constants.BAIDU) music.isCp = false
return MusicInfo(music.mid, music.mid, music.title, artistsBeans, album, music.type, music.mid, music.isCp, music.isDl)
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/cyl/musiclake/base/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
Expand Down Expand Up @@ -122,6 +123,9 @@ private void initToolBar() {
}

if (mSwipeRefreshLayout != null) {
//设置刷新球颜色
mSwipeRefreshLayout.setColorSchemeColors(Color.parseColor("#05b962"), Color.parseColor("#F4B400"), Color.parseColor("#DB4437"));
mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.WHITE);
mSwipeRefreshLayout.setEnabled(false);
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/cyl/musiclake/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public class Constants {
*/
public static final String ABOUT_MUSIC_LAKE = "https://github.com/caiyonglong/MusicLake";
public static final String ABOUT_MUSIC_LAKE_ISSUES = "https://github.com/caiyonglong/MusicLake/issues/new";
public static final String ABOUT_MUSIC_LAKE_PC = "https://github.com/sunzongzheng/music/releases";
public static final String ABOUT_MUSIC_LAKE_URL = "https://github.com/caiyonglong/MusicLake/blob/develop/README.md";


Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/cyl/musiclake/data/db/DaoLitepal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ object DaoLitepal {
*/
fun getAllSearchInfo(title: String? = null): MutableList<SearchHistoryBean> {
return if (title == null) {
LitePal.findAll(SearchHistoryBean::class.java)
LitePal.order("id desc").find(SearchHistoryBean::class.java)
} else {
LitePal.where("title like ?", "%$title%").find(SearchHistoryBean::class.java)
LitePal.where("title like ?", "%$title%").order("id desc").find(SearchHistoryBean::class.java)
}
}

Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/cyl/musiclake/net/ApiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private static OkHttpClient getOkHttpClient() {
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(mRewriteCacheControlInterceptor)
.addInterceptor(mLoggingInterceptor)
// .addInterceptor(mLoggingInterceptor)
.build();
}
}
Expand Down Expand Up @@ -194,6 +194,7 @@ public void onError(Throwable e) {
}
} catch (IOException | JSONException e1) {
e1.printStackTrace();
result.error(MusicApp.getAppContext().getString(R.string.error_connection));
}
} else {
if (result != null) {
Expand All @@ -202,6 +203,8 @@ public void onError(Throwable e) {
} else {
result.error(e.getMessage());
}
} else {
result.error(MusicApp.getAppContext().getString(R.string.error_connection));
}
}

Expand Down
52 changes: 42 additions & 10 deletions app/src/main/java/com/cyl/musiclake/ui/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import com.cyl.musiclake.bean.UserInfoBean
import com.cyl.musiclake.common.Constants
import com.cyl.musiclake.socket.SocketListener
import com.cyl.musiclake.socket.SocketManager
import com.cyl.musiclake.utils.FormatUtil
import com.cyl.musiclake.utils.LogUtil
import com.cyl.musiclake.utils.ToastUtils
import com.cyl.musiclake.view.NoticeView
import kotlinx.android.synthetic.main.activity_chat.*
import kotlinx.android.synthetic.main.content_chat.*
Expand Down Expand Up @@ -60,7 +62,8 @@ class ChatActivity : BaseActivity<ChatPresenter>(), ChatContract.View {
}

override fun initData() {
mPresenter?.loadMessages()
showLoading()
mPresenter?.loadMessages(FormatUtil.getChatDateTime(System.currentTimeMillis()))
if (Intent.ACTION_SEND == intent.action && intent.type != null) {
if (Constants.TEXT_PLAIN == intent.type) {
dealTextMessage(intent)
Expand Down Expand Up @@ -122,14 +125,11 @@ class ChatActivity : BaseActivity<ChatPresenter>(), ChatContract.View {
* 更新用户状态(上下线)
*/
fun updateUserStatus(userInfo: UserInfoBean, isLeave: Boolean) {
userNoticeContainerView.removeAllViews()
val noticeView = NoticeView(this).apply {
this.userInfo = userInfo
this.isLeave = isLeave
}
val noticeView = NoticeView(this)
noticeView.setNewData(userInfo, isLeave)
noticeView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
userNoticeContainerView.addView(noticeView)
noticeView.postDelayed({ userNoticeContainerView.removeView(noticeView) }, 2000)
noticeView.postDelayed({ userNoticeContainerView.removeView(noticeView) }, 3000)
if (userNoticeContainerView.childCount > 5) {
userNoticeContainerView.removeViewAt(0)
}
Expand Down Expand Up @@ -161,8 +161,9 @@ class ChatActivity : BaseActivity<ChatPresenter>(), ChatContract.View {
}
})
addIv.setOnClickListener {
// updateUserStatus(MusicApp.socketManager.onlineUsers[0], true)
MaterialDialog.Builder(this)
.items("分享当前正在播放歌曲")
.items("分享正在播放歌曲")
.itemsCallback { _, _, _, _ ->
sendMusicMessage()
}
Expand All @@ -174,9 +175,33 @@ class ChatActivity : BaseActivity<ChatPresenter>(), ChatContract.View {
}
true
}
mSwipeRefreshLayout?.isEnabled = true
mAdapter?.isUpFetchEnable = true
mAdapter?.setUpFetchListener {
startUpFetch()
}
MusicApp.socketManager.addSocketListener(listener)
}

/**
* 下拉加载更多
*/
private fun startUpFetch() {
/**
* set fetching on when start network request.
*/
mAdapter?.isUpFetching = true
/**
* get data from internet.
*/
mSwipeRefreshLayout?.isRefreshing = true
messageRsv.postDelayed({
if (messages.size > 0) {
mPresenter?.loadMessages(messages[0].datetime)
}
}, 2000)
}


/**
* 发送消息(普通)
Expand Down Expand Up @@ -206,7 +231,7 @@ class ChatActivity : BaseActivity<ChatPresenter>(), ChatContract.View {
startActivity<ChatDetailActivity>()
} else if (item?.itemId == R.id.action_about) {
MaterialDialog.Builder(this)
.title(R.string.prompt)
.title(R.string.chat_about)
.content(R.string.about_music_lake)
.positiveText(R.string.sure)
.show()
Expand All @@ -228,10 +253,17 @@ class ChatActivity : BaseActivity<ChatPresenter>(), ChatContract.View {
* 显示历史消息
*/
override fun showHistortMessages(msgList: MutableList<MessageInfoBean>) {
LogUtil.e("showHistortMessages")
LogUtil.e("showHistortMessages =" + msgList.size)
messages.addAll(0, msgList)
mAdapter?.notifyDataSetChanged()
hideLoading()
messageRsv?.smoothScrollToPosition(msgList.size)
mAdapter?.isUpFetching = false
mSwipeRefreshLayout?.isRefreshing = false
if (msgList.size == 0) {
mSwipeRefreshLayout?.isEnabled = false
mAdapter?.isUpFetchEnable = false
}
}

override fun deleteSuccessful() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface ChatContract {

interface Presenter : BaseContract.BasePresenter<View> {
fun sendMusicMessage()
fun loadMessages(start: String? = null, end: String? = null)
fun loadMessages( end: String? = null)
fun loadLocalMessages()
fun deleteMessages()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ChatDetailActivity : BaseActivity<ChatPresenter>() {
private var mUserAdapter: OnlineUserListAdapter? = null

override fun setToolbarTitle(): String {
return getString(R.string.chat_about)
return getString(R.string.chat_detail)
}

override fun getLayoutResID(): Int {
Expand Down
Loading

0 comments on commit fdd4097

Please sign in to comment.