Browse Source

feat: 直播音乐

tongmengxiao 5 months ago
parent
commit
9e6e94462e

+ 2 - 0
app/src/main/java/com/swago/app/SwagoApp.kt

@@ -20,6 +20,7 @@ import com.swago.baseswago.util.SwagoInfo
 import com.swago.baseswago.util.UserInfo
 import com.swago.lib_beauty.FuBeautyManager
 import com.swago.lib_beauty.IOperateCallback
+import com.swago.room.database.DBManager
 import com.tencent.qcloud.tim.uikit.TUIKit
 import com.tencent.qcloud.tim.uikit.TUIKitImpl
 import com.tencent.qcloud.tim.uikit.base.IMEventListener
@@ -39,6 +40,7 @@ class SwagoApp : Application() {
     override fun onCreate() {
         super.onCreate()
         AppContext.setContext(this)
+        DBManager.init(this)
         if (BuildConfig.DEBUG) {
             ARouter.openLog()
             ARouter.openDebug()

+ 1 - 0
baseswago/build.gradle

@@ -135,5 +135,6 @@ dependencies {
     api 'com.duolingo.open:rtl-viewpager:2.0.0'
     //lottie
     api 'com.airbnb.android:lottie:4.0.0'
+    api  'org.greenrobot:greendao:3.3.0' // 添加库
 
 }

+ 1 - 0
build.gradle

@@ -23,6 +23,7 @@ buildscript {
         classpath 'com.google.gms:google-services:4.3.10'
         classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1'
         classpath 'com.google.firebase:perf-plugin:1.4.1'
+        classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // 添加插件 更好支持GreenDao
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files
     }

+ 6 - 1
room/build.gradle

@@ -2,6 +2,7 @@ plugins {
     id 'com.android.library'
     id 'kotlin-android'
     id 'kotlin-kapt'
+    id 'org.greenrobot.greendao'
 }
 
 android {
@@ -40,7 +41,11 @@ android {
         viewBinding = true
     }
 }
-
+greendao {
+    schemaVersion 1
+    targetGenDir 'src/main/java'
+    daoPackage 'com.swago.room.database'
+}
 dependencies {
 
     implementation 'androidx.core:core-ktx:1.5.0'

BIN
room/src/main/assets/music.svga


+ 10 - 0
room/src/main/java/com/swago/room/database/DBHelper.kt

@@ -0,0 +1,10 @@
+package com.swago.room.database
+
+import com.swago.room.music.MusicModel
+
+object DBHelper {
+    private val daoSession = DBManager.getDaoSession()
+    fun getMusicModel() : MusicModelDao?{
+        return daoSession?.musicModelDao
+    }
+}

+ 18 - 0
room/src/main/java/com/swago/room/database/DBManager.kt

@@ -0,0 +1,18 @@
+package com.swago.room.database
+
+import android.content.Context
+
+object DBManager {
+    private var daoSession: DaoSession? = null
+
+    fun init(context: Context) {
+        val helper = DaoMaster.DevOpenHelper(context, "meliv_db")
+        val db = helper.writableDb
+        val daoMaster = DaoMaster(db)
+        daoSession = daoMaster.newSession()
+    }
+
+    fun getDaoSession(): DaoSession? {
+        return daoSession
+    }
+}

+ 96 - 0
room/src/main/java/com/swago/room/database/DaoMaster.java

@@ -0,0 +1,96 @@
+package com.swago.room.database;
+
+import android.content.Context;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteDatabase.CursorFactory;
+import android.util.Log;
+
+import org.greenrobot.greendao.AbstractDaoMaster;
+import org.greenrobot.greendao.database.StandardDatabase;
+import org.greenrobot.greendao.database.Database;
+import org.greenrobot.greendao.database.DatabaseOpenHelper;
+import org.greenrobot.greendao.identityscope.IdentityScopeType;
+
+
+// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
+/**
+ * Master of DAO (schema version 1): knows all DAOs.
+ */
+public class DaoMaster extends AbstractDaoMaster {
+    public static final int SCHEMA_VERSION = 1;
+
+    /** Creates underlying database table using DAOs. */
+    public static void createAllTables(Database db, boolean ifNotExists) {
+        MusicModelDao.createTable(db, ifNotExists);
+    }
+
+    /** Drops underlying database table using DAOs. */
+    public static void dropAllTables(Database db, boolean ifExists) {
+        MusicModelDao.dropTable(db, ifExists);
+    }
+
+    /**
+     * WARNING: Drops all table on Upgrade! Use only during development.
+     * Convenience method using a {@link DevOpenHelper}.
+     */
+    public static DaoSession newDevSession(Context context, String name) {
+        Database db = new DevOpenHelper(context, name).getWritableDb();
+        DaoMaster daoMaster = new DaoMaster(db);
+        return daoMaster.newSession();
+    }
+
+    public DaoMaster(SQLiteDatabase db) {
+        this(new StandardDatabase(db));
+    }
+
+    public DaoMaster(Database db) {
+        super(db, SCHEMA_VERSION);
+        registerDaoClass(MusicModelDao.class);
+    }
+
+    public DaoSession newSession() {
+        return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
+    }
+
+    public DaoSession newSession(IdentityScopeType type) {
+        return new DaoSession(db, type, daoConfigMap);
+    }
+
+    /**
+     * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} -
+     */
+    public static abstract class OpenHelper extends DatabaseOpenHelper {
+        public OpenHelper(Context context, String name) {
+            super(context, name, SCHEMA_VERSION);
+        }
+
+        public OpenHelper(Context context, String name, CursorFactory factory) {
+            super(context, name, factory, SCHEMA_VERSION);
+        }
+
+        @Override
+        public void onCreate(Database db) {
+            Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION);
+            createAllTables(db, false);
+        }
+    }
+
+    /** WARNING: Drops all table on Upgrade! Use only during development. */
+    public static class DevOpenHelper extends OpenHelper {
+        public DevOpenHelper(Context context, String name) {
+            super(context, name);
+        }
+
+        public DevOpenHelper(Context context, String name, CursorFactory factory) {
+            super(context, name, factory);
+        }
+
+        @Override
+        public void onUpgrade(Database db, int oldVersion, int newVersion) {
+            Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
+            dropAllTables(db, true);
+            onCreate(db);
+        }
+    }
+
+}

+ 48 - 0
room/src/main/java/com/swago/room/database/DaoSession.java

@@ -0,0 +1,48 @@
+package com.swago.room.database;
+
+import java.util.Map;
+
+import org.greenrobot.greendao.AbstractDao;
+import org.greenrobot.greendao.AbstractDaoSession;
+import org.greenrobot.greendao.database.Database;
+import org.greenrobot.greendao.identityscope.IdentityScopeType;
+import org.greenrobot.greendao.internal.DaoConfig;
+
+import com.swago.room.music.MusicModel;
+
+import com.swago.room.database.MusicModelDao;
+
+// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
+
+/**
+ * {@inheritDoc}
+ * 
+ * @see org.greenrobot.greendao.AbstractDaoSession
+ */
+public class DaoSession extends AbstractDaoSession {
+
+    private final DaoConfig musicModelDaoConfig;
+
+    private final MusicModelDao musicModelDao;
+
+    public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends AbstractDao<?, ?>>, DaoConfig>
+            daoConfigMap) {
+        super(db);
+
+        musicModelDaoConfig = daoConfigMap.get(MusicModelDao.class).clone();
+        musicModelDaoConfig.initIdentityScope(type);
+
+        musicModelDao = new MusicModelDao(musicModelDaoConfig, this);
+
+        registerDao(MusicModel.class, musicModelDao);
+    }
+    
+    public void clear() {
+        musicModelDaoConfig.clearIdentityScope();
+    }
+
+    public MusicModelDao getMusicModelDao() {
+        return musicModelDao;
+    }
+
+}

+ 153 - 0
room/src/main/java/com/swago/room/database/MusicModelDao.java

@@ -0,0 +1,153 @@
+package com.swago.room.database;
+
+import android.database.Cursor;
+import android.database.sqlite.SQLiteStatement;
+
+import org.greenrobot.greendao.AbstractDao;
+import org.greenrobot.greendao.Property;
+import org.greenrobot.greendao.internal.DaoConfig;
+import org.greenrobot.greendao.database.Database;
+import org.greenrobot.greendao.database.DatabaseStatement;
+
+import com.swago.room.music.MusicModel;
+
+// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
+/** 
+ * DAO for table "MUSIC_MODEL".
+*/
+public class MusicModelDao extends AbstractDao<MusicModel, Long> {
+
+    public static final String TABLENAME = "MUSIC_MODEL";
+
+    /**
+     * Properties of entity MusicModel.<br/>
+     * Can be used for QueryBuilder and for referencing column names.
+     */
+    public static class Properties {
+        public final static Property Id = new Property(0, long.class, "id", true, "_id");
+        public final static Property Title = new Property(1, String.class, "title", false, "TITLE");
+        public final static Property Data = new Property(2, String.class, "data", false, "DATA");
+        public final static Property Duration = new Property(3, long.class, "duration", false, "DURATION");
+        public final static Property IsSelect = new Property(4, boolean.class, "isSelect", false, "IS_SELECT");
+        public final static Property IsPlaying = new Property(5, boolean.class, "isPlaying", false, "IS_PLAYING");
+    }
+
+
+    public MusicModelDao(DaoConfig config) {
+        super(config);
+    }
+    
+    public MusicModelDao(DaoConfig config, DaoSession daoSession) {
+        super(config, daoSession);
+    }
+
+    /** Creates the underlying database table. */
+    public static void createTable(Database db, boolean ifNotExists) {
+        String constraint = ifNotExists? "IF NOT EXISTS ": "";
+        db.execSQL("CREATE TABLE " + constraint + "\"MUSIC_MODEL\" (" + //
+                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ," + // 0: id
+                "\"TITLE\" TEXT," + // 1: title
+                "\"DATA\" TEXT," + // 2: data
+                "\"DURATION\" INTEGER NOT NULL ," + // 3: duration
+                "\"IS_SELECT\" INTEGER NOT NULL ," + // 4: isSelect
+                "\"IS_PLAYING\" INTEGER NOT NULL );"); // 5: isPlaying
+    }
+
+    /** Drops the underlying database table. */
+    public static void dropTable(Database db, boolean ifExists) {
+        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"MUSIC_MODEL\"";
+        db.execSQL(sql);
+    }
+
+    @Override
+    protected final void bindValues(DatabaseStatement stmt, MusicModel entity) {
+        stmt.clearBindings();
+        stmt.bindLong(1, entity.getId());
+ 
+        String title = entity.getTitle();
+        if (title != null) {
+            stmt.bindString(2, title);
+        }
+ 
+        String data = entity.getData();
+        if (data != null) {
+            stmt.bindString(3, data);
+        }
+        stmt.bindLong(4, entity.getDuration());
+        stmt.bindLong(5, entity.getIsSelect() ? 1L: 0L);
+        stmt.bindLong(6, entity.getIsPlaying() ? 1L: 0L);
+    }
+
+    @Override
+    protected final void bindValues(SQLiteStatement stmt, MusicModel entity) {
+        stmt.clearBindings();
+        stmt.bindLong(1, entity.getId());
+ 
+        String title = entity.getTitle();
+        if (title != null) {
+            stmt.bindString(2, title);
+        }
+ 
+        String data = entity.getData();
+        if (data != null) {
+            stmt.bindString(3, data);
+        }
+        stmt.bindLong(4, entity.getDuration());
+        stmt.bindLong(5, entity.getIsSelect() ? 1L: 0L);
+        stmt.bindLong(6, entity.getIsPlaying() ? 1L: 0L);
+    }
+
+    @Override
+    public Long readKey(Cursor cursor, int offset) {
+        return cursor.getLong(offset + 0);
+    }    
+
+    @Override
+    public MusicModel readEntity(Cursor cursor, int offset) {
+        MusicModel entity = new MusicModel( //
+            cursor.getLong(offset + 0), // id
+            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // title
+            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // data
+            cursor.getLong(offset + 3), // duration
+            cursor.getShort(offset + 4) != 0, // isSelect
+            cursor.getShort(offset + 5) != 0 // isPlaying
+        );
+        return entity;
+    }
+     
+    @Override
+    public void readEntity(Cursor cursor, MusicModel entity, int offset) {
+        entity.setId(cursor.getLong(offset + 0));
+        entity.setTitle(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
+        entity.setData(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
+        entity.setDuration(cursor.getLong(offset + 3));
+        entity.setIsSelect(cursor.getShort(offset + 4) != 0);
+        entity.setIsPlaying(cursor.getShort(offset + 5) != 0);
+     }
+    
+    @Override
+    protected final Long updateKeyAfterInsert(MusicModel entity, long rowId) {
+        entity.setId(rowId);
+        return rowId;
+    }
+    
+    @Override
+    public Long getKey(MusicModel entity) {
+        if(entity != null) {
+            return entity.getId();
+        } else {
+            return null;
+        }
+    }
+
+    @Override
+    public boolean hasKey(MusicModel entity) {
+        throw new UnsupportedOperationException("Unsupported for entities with a non-null key");
+    }
+
+    @Override
+    protected final boolean isEntityUpdateable() {
+        return true;
+    }
+    
+}

+ 18 - 5
room/src/main/java/com/swago/room/music/LocalMusicActivity.kt

@@ -10,16 +10,17 @@ import com.swago.baseswago.activity.BaseXActivity
 import com.swago.baseswago.constant.ARouteConstant
 import com.swago.baseswago.util.AppContext
 import com.swago.room.R
+import com.swago.room.database.DBHelper
 import com.swago.room.databinding.ActivityLocalMusicBinding
+import com.zy.multistatepage.state.EmptyState
+import com.zy.multistatepage.state.ErrorState
+import com.zy.multistatepage.state.LoadingState
+import com.zy.multistatepage.state.SuccessState
 
 @Route(path = ARouteConstant.Room.musicList)
 class LocalMusicActivity : BaseXActivity<ActivityLocalMusicBinding>() {
     private val musicVm by viewModels<MusicVm>()
 
-    @Autowired
-    @JvmField
-    var data: ArrayList<MusicModel>? = null
-
     val adapter : LocalMusicAdapter by lazy {
         LocalMusicAdapter()
     }
@@ -32,6 +33,7 @@ class LocalMusicActivity : BaseXActivity<ActivityLocalMusicBinding>() {
         get() = LinearLayoutManager(this)
 
     override fun loadData() {
+        multiStateContainer.show<LoadingState>()
         musicVm.loadMediaItems()
     }
 
@@ -45,7 +47,16 @@ class LocalMusicActivity : BaseXActivity<ActivityLocalMusicBinding>() {
 
     override fun initLiveData() {
         musicVm.musicListData.observe(this){
-            adapter.setNewData(it)
+            if (it == null) {
+                multiStateContainer.show<ErrorState>()
+            } else {
+                if (it.isEmpty()) {
+                    multiStateContainer.show<EmptyState>()
+                } else {
+                    multiStateContainer.show<SuccessState>()
+                    adapter.setNewData(it)
+                }
+            }
         }
 
         binding.cbSelect.setOnCheckedChangeListener { _, isChecked ->
@@ -61,6 +72,8 @@ class LocalMusicActivity : BaseXActivity<ActivityLocalMusicBinding>() {
             if (selectMusicData.size == 0){
                 return@setOnClickListener
             }
+            DBHelper.getMusicModel()?.insertOrReplaceInTx(selectMusicData)
+            finish()
         }
 
         adapter.isSelectFun = {

+ 3 - 3
room/src/main/java/com/swago/room/music/LocalMusicAdapter.kt

@@ -12,7 +12,7 @@ class LocalMusicAdapter :  BaseQuickAdapter<MusicModel, BaseViewHolder>(R.layout
 
     fun setSelectAll(isSelect : Boolean){
         data.forEachIndexed { index, musicModel ->
-            data[index].isSelect = isSelect
+            data[index].setSelect(isSelect)
         }
         notifyDataSetChanged()
     }
@@ -21,9 +21,9 @@ class LocalMusicAdapter :  BaseQuickAdapter<MusicModel, BaseViewHolder>(R.layout
             itemView.findViewById<BoldTextView>(R.id.tvTitle).text = item.title
             itemView.findViewById<MediumTextView>(R.id.tvDuration).text = String.format("%02d:%02d", item.duration / 60000, (item.duration % 60000) / 1000)
             val checkBox = itemView.findViewById<CheckBox>(R.id.cbCheck)
-            checkBox.isChecked = item.isSelect
+            checkBox.isChecked = item.getIsSelect()
             checkBox.setOnCheckedChangeListener { _, isChecked ->
-                item.isSelect = isChecked
+                item.setSelect(isChecked)
                isSelectFun?.invoke(item , isChecked)
             }
         }

+ 137 - 0
room/src/main/java/com/swago/room/music/MusicModel.java

@@ -0,0 +1,137 @@
+package com.swago.room.music;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+
+import androidx.annotation.NonNull;
+
+import org.greenrobot.greendao.annotation.Entity;
+import org.greenrobot.greendao.annotation.Generated;
+import org.greenrobot.greendao.annotation.Id;
+
+@Entity
+public class MusicModel implements Parcelable {
+    @Id(autoincrement = true)
+    private long id;
+    private String title;
+    private String data;
+    private long duration;
+    private boolean isSelect;
+    private boolean isPlaying;
+
+    protected MusicModel(Parcel in) {
+        id = in.readLong();
+        title = in.readString();
+        data = in.readString();
+        duration = in.readLong();
+        isSelect = in.readByte() != 0;
+        isPlaying = in.readByte() != 0;
+    }
+
+    @Generated(hash = 1804109021)
+    public MusicModel(long id, String title, String data, long duration,
+            boolean isSelect, boolean isPlaying) {
+        this.id = id;
+        this.title = title;
+        this.data = data;
+        this.duration = duration;
+        this.isSelect = isSelect;
+        this.isPlaying = isPlaying;
+    }
+
+    @Generated(hash = 70061797)
+    public MusicModel() {
+    }
+
+
+    public static final Creator<MusicModel> CREATOR = new Creator<MusicModel>() {
+        @Override
+        public MusicModel createFromParcel(Parcel in) {
+            return new MusicModel(in);
+        }
+
+        @Override
+        public MusicModel[] newArray(int size) {
+            return new MusicModel[size];
+        }
+    };
+
+    public long getId() {
+        return id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getData() {
+        return data;
+    }
+
+    public void setData(String data) {
+        this.data = data;
+    }
+
+    public long getDuration() {
+        return duration;
+    }
+
+    public void setDuration(long duration) {
+        this.duration = duration;
+    }
+
+    public boolean isSelect() {
+        return isSelect;
+    }
+
+    public void setSelect(boolean select) {
+        isSelect = select;
+    }
+
+    public boolean isPlaying() {
+        return isPlaying;
+    }
+
+    public void setPlaying(boolean playing) {
+        isPlaying = playing;
+    }
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    @Override
+    public void writeToParcel(@NonNull Parcel dest, int flags) {
+        dest.writeLong(id);
+        dest.writeString(title);
+        dest.writeString(data);
+        dest.writeLong(duration);
+        dest.writeByte((byte) (isSelect ? 1 : 0));
+        dest.writeByte((byte) (isPlaying ? 1 : 0));
+    }
+
+    public boolean getIsSelect() {
+        return this.isSelect;
+    }
+
+    public void setIsSelect(boolean isSelect) {
+        this.isSelect = isSelect;
+    }
+
+    public boolean getIsPlaying() {
+        return this.isPlaying;
+    }
+
+    public void setIsPlaying(boolean isPlaying) {
+        this.isPlaying = isPlaying;
+    }
+}

+ 0 - 44
room/src/main/java/com/swago/room/music/MusicModel.kt

@@ -1,44 +0,0 @@
-package com.swago.room.music
-
-import android.os.Parcel
-import android.os.Parcelable
-
-data class MusicModel(
-    val id: Long = 0,
-    val title: String? = null,
-    val data: String? =null,
-    val duration: Long = 0,
-    var isSelect: Boolean = false
-) : Parcelable{
-    constructor(parcel: Parcel) : this(
-        parcel.readLong(),
-        parcel.readString(),
-        parcel.readString(),
-        parcel.readLong(),
-        parcel.readByte() != 0.toByte()
-    ) {
-    }
-
-    override fun writeToParcel(parcel: Parcel, flags: Int) {
-        parcel.writeLong(id)
-        parcel.writeString(title)
-        parcel.writeString(data)
-        parcel.writeLong(duration)
-        parcel.writeByte(if (isSelect) 1 else 0)
-    }
-
-    override fun describeContents(): Int {
-        return 0
-    }
-
-    companion object CREATOR : Parcelable.Creator<MusicModel> {
-        override fun createFromParcel(parcel: Parcel): MusicModel {
-            return MusicModel(parcel)
-        }
-
-        override fun newArray(size: Int): Array<MusicModel?> {
-            return arrayOfNulls(size)
-        }
-    }
-
-}

+ 1 - 15
room/src/main/java/com/swago/room/music/MusicVm.kt

@@ -48,25 +48,11 @@ class MusicVm(application: Application) : BaseViewModel(application) {
                         val data = it.getString(dataColumn)
                         val duration = it.getLong(durationColumn)
                         LogUtil.d("musicDialog $id $title $data $duration")
-                        audioList.add(MusicModel(id,title,data,duration,false))
+                        audioList.add(MusicModel(id,title,data,duration,false,false))
                     }
                 }
                 musicListData.postValue(audioList)
             }
         }
     }
-
-    fun toggleSelection(item: MusicModel){
-        val updateList = musicListData.value?.map {
-            if (it.id == item.id) it.copy(isSelect = !it.isSelect) else it
-        }
-        musicListData.value = updateList
-    }
-
-    fun selectAll(isSelect: Boolean){
-        val updatedList = musicListData.value?.map {
-            it.copy(isSelect = isSelect)
-        }
-        musicListData.value = updatedList
-    }
 }

+ 12 - 3
room/src/main/java/com/swago/room/music/MyMusicAdapter.kt

@@ -1,7 +1,9 @@
 package com.swago.room.music
 
+import android.view.View
 import com.chad.library.adapter.base.BaseQuickAdapter
 import com.chad.library.adapter.base.BaseViewHolder
+import com.opensource.svgaplayer.SVGAImageView
 import com.swago.baseswago.cusview.BoldTextView
 import com.swago.baseswago.cusview.MediumTextView
 import com.swago.room.R
@@ -10,10 +12,17 @@ class MyMusicAdapter :
     BaseQuickAdapter<MusicModel, BaseViewHolder>(R.layout.item_my_music, arrayListOf()) {
     override fun convert(helper: BaseViewHolder, item: MusicModel) {
         helper.apply {
-            itemView.findViewById<BoldTextView>(R.id.tvName).text = item.title
-            itemView.findViewById<MediumTextView>(R.id.tvDuration).text =
-                String.format("%02d:%02d", item.duration / 60000, (item.duration % 60000) / 1000)
+            setText(R.id.tvName , item.title)
+            setText(R.id.tvDuration, String.format("%02d:%02d", item.duration / 60000, (item.duration % 60000) / 1000))
             addOnClickListener(R.id.ivRecall)
+            val svgaImageView = itemView.findViewById<SVGAImageView>(R.id.svgaImageView)
+            if (item.getIsPlaying()){
+                svgaImageView.visibility = View.VISIBLE
+                svgaImageView.startAnimation()
+            } else {
+                svgaImageView.visibility = View.INVISIBLE
+                svgaImageView.stopAnimation()
+            }
         }
     }
 }

+ 84 - 71
room/src/main/java/com/swago/room/music/MyMusicDialog.kt

@@ -1,26 +1,32 @@
 package com.swago.room.music
 
-import android.content.Intent
 import android.os.Bundle
 import android.view.Gravity
 import android.view.View
-import androidx.appcompat.app.AppCompatActivity
 import androidx.fragment.app.viewModels
 import androidx.recyclerview.widget.LinearLayoutManager
 import androidx.recyclerview.widget.RecyclerView
 import com.alibaba.android.arouter.launcher.ARouter
-import com.chad.library.adapter.base.BaseQuickAdapter
 import com.swago.baseswago.agora.AgoraManager
 import com.swago.baseswago.constant.ARouteConstant
-import com.swago.baseswago.cusview.SwagoRecyclerView
-import com.swago.baseswago.dialog.BaseListDialogFragment
-import com.swago.baseswago.model.MomentModel
-import com.swago.baseswago.util.LogUtil
+import com.swago.baseswago.dialog.BaseXDFragment
+import com.swago.baseswago.dialog.ChoiceDialogFragment
+import com.swago.baseswago.util.AppContext
 import com.swago.room.R
+import com.swago.room.database.DBHelper
 import com.swago.room.databinding.DialogMyMusicBinding
 
-class MyMusicDialog : BaseListDialogFragment<DialogMyMusicBinding, MusicModel>() {
-    private val musicVm by viewModels<MusicVm>()
+class MyMusicDialog : BaseXDFragment<DialogMyMusicBinding>() {
+    val music_binding get() = binding
+    var isLocalCome = false
+    private val musicList by lazy {
+        ArrayList<MusicModel>()
+    }
+    private val adapter by lazy {
+        MyMusicAdapter()
+    }
+    val layoutManager: RecyclerView.LayoutManager
+        get() = LinearLayoutManager(activity)
 
     companion object {
         const val select_local_result = 10002
@@ -38,91 +44,98 @@ class MyMusicDialog : BaseListDialogFragment<DialogMyMusicBinding, MusicModel>()
         setDimAmount(0.5f)
     }
 
-
-    override val smartRecyclerView: SwagoRecyclerView<MusicModel> by lazy {
-        binding.swagoRv as SwagoRecyclerView<MusicModel>
-    }
-    override val adapter: BaseQuickAdapter<MusicModel, *> by lazy {
-        MyMusicAdapter()
-    }
-
-    override val layoutManager: RecyclerView.LayoutManager by lazy {
-        LinearLayoutManager(context)
-    }
-    override val loadData: (page: Int) -> Unit = {
-        musicVm.loadMediaItems()
-    }
-
-    override fun initViewData() {
-        binding.ivClose.setOnClickListener {
+    override fun initOther() {
+        music_binding.rvMusic.layoutManager = layoutManager
+        music_binding.rvMusic.adapter = adapter
+        music_binding.ivClose.setOnClickListener {
             dismissAllowingStateLoss()
         }
     }
 
     override fun initLiveData() {
-        musicVm.musicListData.observe(this) {
-            if (!it.isNullOrEmpty()){
-                binding.clAdd.visibility = View.VISIBLE
-                binding.clNoContent.visibility = View.GONE
-                smartRecyclerView.onFetchDataFinish(it, false)
-            } else {
-                binding.clAdd.visibility = View.GONE
-                binding.clNoContent.visibility = View.VISIBLE
-
-            }
-
+        val data = DBHelper.getMusicModel()?.loadAll()
+        if (!data.isNullOrEmpty()){
+            music_binding.clAdd.visibility = View.VISIBLE
+            music_binding.clNoContent.visibility = View.GONE
+            musicList.addAll(data)
+            adapter.setNewData(musicList)
+        } else {
+            music_binding.clAdd.visibility = View.GONE
+            music_binding.clNoContent.visibility = View.VISIBLE
         }
-        adapter.setOnItemClickListener { _, _, position ->
-            if (adapter.data.size > position){
-                AgoraManager.getRtcEngine()?.startAudioMixing(
-                    adapter.data[position].data,
-                    false,
-                    false,
-                    -1,
-                    0
-                )
-            }
-
-        }
-        binding.clAdd.setOnClickListener {
+        music_binding.clAdd.setOnClickListener {
+            isLocalCome = true
             ARouter.getInstance()
                 .build(ARouteConstant.Room.musicList)
-                .withParcelableArrayList("data",adapter.data as ArrayList<MusicModel>)
-                .navigation(activity, select_local_result)
+                .navigation()
         }
-        binding.clNoContent.setOnClickListener {
+        music_binding.clNoContent.setOnClickListener {
+            isLocalCome = true
             ARouter.getInstance()
                 .build(ARouteConstant.Room.musicList)
-                .withParcelableArrayList("data",adapter.data as ArrayList<MusicModel>)
-                .navigation(activity, select_local_result)
+                .navigation()
         }
 
         adapter.setOnItemChildClickListener { _, view, position ->
             when(view?.id){
                 R.id.ivRecall -> {
+                    ChoiceDialogFragment.newInstance(AppContext.getContext().getString(R.string.do_delete_room_admin),
+                        rightChoiceText = AppContext.getContext().getString(R.string.delete)).apply {
+                        this.sureFun = {
+                            DBHelper.getMusicModel()?.delete(adapter.data[position])
+                            musicList.remove(adapter.data[position])
+                            adapter.setNewData(musicList)
+                            if (adapter.data.isEmpty()){
+                                music_binding.clAdd.visibility = View.GONE
+                                music_binding.clNoContent.visibility = View.VISIBLE
+                            }
+                        }
+                    }.show(childFragmentManager,"ChoiceDialogFragment")
 
                 }
             }
-
         }
-    }
-
-    override fun loadMoreNeed(): Boolean {
-        return false
-    }
 
-    override fun refreshNeed(): Boolean {
-        return false
+        adapter.setOnItemClickListener { _, _, position ->
+            if (adapter.data[position].getIsPlaying()){
+                adapter.data[position].setIsPlaying(false)
+                adapter.notifyItemChanged(position)
+                AgoraManager.getRtcEngine()?.stopAudioMixing()
+                return@setOnItemClickListener
+            }
+            adapter.data.forEachIndexed { index, musicModel ->
+                if (musicModel.isPlaying()){
+                    adapter.data[index].setIsPlaying(false)
+                    adapter.notifyItemChanged(index)
+                    return@forEachIndexed
+                }
+            }
+            adapter.data[position].setIsPlaying(true)
+            AgoraManager.getRtcEngine()?.startAudioMixing(
+                adapter.data[position].data,
+                false,
+                false,
+                -1,
+                0
+            )
+            adapter.notifyItemChanged(position)
+        }
     }
 
-    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
-        super.onActivityResult(requestCode, resultCode, data)
-        if (resultCode == AppCompatActivity.RESULT_OK){
-            when(requestCode){
-                select_local_result -> {
-                   val  model = data?.getParcelableArrayExtra("data") as List<MusicModel>
-                    smartRecyclerView.onFetchDataFinish(model, false)
-                }
+    override fun onResume() {
+        super.onResume()
+        if (isLocalCome){
+            isLocalCome = false
+            val data = DBHelper.getMusicModel()?.loadAll()
+            if (!data.isNullOrEmpty()){
+                binding.clAdd.visibility = View.VISIBLE
+                binding.clNoContent.visibility = View.GONE
+                musicList.clear()
+                musicList.addAll(data)
+                adapter.setNewData(musicList)
+            } else {
+                binding.clAdd.visibility = View.GONE
+                binding.clNoContent.visibility = View.VISIBLE
             }
         }
     }

+ 63 - 35
room/src/main/res/layout/dialog_my_music.xml

@@ -15,7 +15,6 @@
         android:layout_height="500dp"
         android:background="@drawable/shape_white_top_20">
 
-
         <TextView
             android:id="@+id/tvTitle"
             android:textSize="17dp"
@@ -40,8 +39,8 @@
             android:layout_width="24dp"
             android:layout_height="24dp"/>
 
-        <com.swago.baseswago.cusview.SwagoRecyclerView
-            android:id="@+id/swagoRv"
+        <androidx.recyclerview.widget.RecyclerView
+            android:id="@+id/rvMusic"
             android:layout_marginTop="18dp"
             app:layout_constraintTop_toBottomOf="@+id/tvTitle"
             app:layout_constraintBottom_toTopOf="@+id/cl_add"
@@ -82,38 +81,67 @@
                 app:layout_constraintBottom_toBottomOf="parent"/>
         </androidx.constraintlayout.widget.ConstraintLayout>
 
-        <androidx.constraintlayout.widget.ConstraintLayout
-            android:id="@+id/clNoContent"
-            android:layout_width="140dp"
-            android:layout_height="36dp"
-            app:layout_constraintBottom_toBottomOf="parent"
-            app:layout_constraintStart_toStartOf="parent"
-            app:layout_constraintEnd_toEndOf="parent"
-            android:layout_marginBottom="80dp"
-            android:visibility="gone"
-            android:background="@drawable/shape_9b20fc_20">
-            <ImageView
-                android:layout_width="16dp"
-                android:layout_height="16dp"
-                app:layout_constraintTop_toTopOf="parent"
-                app:layout_constraintBottom_toBottomOf="parent"
-                android:background="@mipmap/ic_add_music"
-                android:layout_marginEnd="@dimen/dp_4"
-                app:layout_constraintRight_toLeftOf="@+id/tv_no_add"
-                />
-            <TextView
-                android:id="@+id/tv_no_add"
-                android:layout_width="wrap_content"
-                android:layout_height="36dp"
-                android:gravity="center"
-                android:text="Add Music"
-                android:textColor="#fff"
-                android:textSize="@dimen/sp_16"
-                android:layout_marginStart="@dimen/dp_10"
-                app:layout_constraintEnd_toEndOf="parent"
-                app:layout_constraintStart_toStartOf="parent"
-                app:layout_constraintBottom_toBottomOf="parent"/>
-        </androidx.constraintlayout.widget.ConstraintLayout>
+       <androidx.constraintlayout.widget.ConstraintLayout
+           android:id="@+id/clNoContent"
+           android:layout_width="wrap_content"
+           android:layout_height="wrap_content"
+           android:visibility="gone"
+           app:layout_constraintStart_toStartOf="parent"
+           app:layout_constraintEnd_toEndOf="parent"
+           app:layout_constraintTop_toTopOf="parent"
+           app:layout_constraintBottom_toBottomOf="parent">
+           <ImageView
+               android:id="@+id/iv_empty"
+               android:layout_width="wrap_content"
+               android:layout_height="wrap_content"
+               app:layout_constraintStart_toStartOf="parent"
+               app:layout_constraintEnd_toEndOf="parent"
+               app:layout_constraintTop_toTopOf="parent"
+               android:src="@mipmap/state_error_empty"/>
+           <TextView
+               android:id="@+id/tv_empty"
+               android:layout_width="wrap_content"
+               android:layout_height="wrap_content"
+               app:layout_constraintStart_toStartOf="parent"
+               app:layout_constraintEnd_toEndOf="parent"
+               app:layout_constraintTop_toBottomOf="@+id/iv_empty"
+               android:layout_marginTop="16dp"
+               android:text="@string/There_is_nothing_here"
+               android:textColor="#435568"
+               android:textSize="@dimen/sp_12"/>
+
+           <androidx.constraintlayout.widget.ConstraintLayout
+               android:layout_width="146dp"
+               android:layout_height="36dp"
+               app:layout_constraintStart_toStartOf="parent"
+               app:layout_constraintEnd_toEndOf="parent"
+               app:layout_constraintTop_toBottomOf="@+id/tv_empty"
+               android:layout_marginTop="27dp"
+               android:background="@drawable/shape_9b20fc_20">
+               <ImageView
+                   android:layout_width="16dp"
+                   android:layout_height="16dp"
+                   app:layout_constraintTop_toTopOf="parent"
+                   app:layout_constraintBottom_toBottomOf="parent"
+                   android:background="@mipmap/ic_add_music"
+                   android:layout_marginEnd="@dimen/dp_4"
+                   app:layout_constraintRight_toLeftOf="@+id/tv_no_add"
+                   />
+               <TextView
+                   android:id="@+id/tv_no_add"
+                   android:layout_width="wrap_content"
+                   android:layout_height="36dp"
+                   android:gravity="center"
+                   android:text="Add Music"
+                   android:textColor="#fff"
+                   android:textSize="@dimen/sp_16"
+                   android:layout_marginStart="@dimen/dp_10"
+                   app:layout_constraintEnd_toEndOf="parent"
+                   app:layout_constraintStart_toStartOf="parent"
+                   app:layout_constraintBottom_toBottomOf="parent"/>
+           </androidx.constraintlayout.widget.ConstraintLayout>
+
+       </androidx.constraintlayout.widget.ConstraintLayout>
 
     </androidx.constraintlayout.widget.ConstraintLayout>
 

+ 2 - 1
room/src/main/res/layout/item_my_music.xml

@@ -10,9 +10,10 @@
         android:id="@+id/svgaImageView"
         android:layout_height="18dp"
         android:layout_width="18dp"
-        app:source="cry.svga"
+        app:source="music.svga"
         app:autoPlay="true"
         app:loopCount="0"
+        android:visibility="invisible"
         android:background="#fff"
         android:layout_marginTop="-16dp"
         android:layout_marginStart="16dp"