Browse Source

LyricCopySheet增加了分享按钮

onecold 5 months ago
parent
commit
732b22d629

+ 24 - 0
entry/src/main/ets/common/util/Utility.ets

@@ -1097,6 +1097,30 @@ export class Utility {
 
   }
 
+  static async doShareText(content: string, context: common.UIAbilityContext, title?: string, description?: string) {
+    if (StrUtil.isEmpty(content)) {
+      return;
+    }
+
+    let shareData: systemShare.SharedData = new systemShare.SharedData({
+      utd: uniformTypeDescriptor.UniformDataType.TEXT,
+      content: content,
+      title: title,
+      description: description,
+      thumbnail: context.resourceManager.getMediaContentSync($r("app.media.icon_32"))
+    });
+
+    let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
+    controller.show(context, {
+      selectionMode: systemShare.SelectionMode.SINGLE,
+      previewMode: systemShare.SharePreviewMode.DETAIL,
+    }).then(() => {
+      console.info('ShareController show success.');
+    }).catch((error: BusinessError) => {
+      console.error(`ShareController show error. code: ${error.code}, message: ${error.message}`);
+    });
+  }
+
   static getNameList(localList:Array<VideoItem>){
     let list: Array<string> = [];
     for(let i=0;i<localList.length;i++){

+ 29 - 7
entry/src/main/ets/view/LocalMusic.ets

@@ -11652,10 +11652,19 @@ export struct LocalMusic {
   }
 
   private copySelectedLyricLines(): void {
-    if (ArrayUtil.isEmpty(this.lyricSelectedIndexes)) {
-      ToastUtil.showToast('请先选择歌词');
+    const selectedLines = this.getSelectedLyricLines();
+    if (ArrayUtil.isEmpty(selectedLines)) {
+      ToastUtil.showToast('暂无可复制歌词');
       return;
     }
+    Utility.copyText(selectedLines.join('\n'));
+    ToastUtil.showToast(`已复制${selectedLines.length}行歌词`);
+  }
+
+  private getSelectedLyricLines(): string[] {
+    if (ArrayUtil.isEmpty(this.lyricSelectedIndexes)) {
+      return [];
+    }
     const selectedIndexes = [...this.lyricSelectedIndexes].sort((a: number, b: number) => a - b);
     const selectedLines: string[] = [];
     for (let i = 0; i < selectedIndexes.length; i++) {
@@ -11664,12 +11673,17 @@ export struct LocalMusic {
         selectedLines.push(this.lyricCopyLines[index]);
       }
     }
+    return selectedLines;
+  }
+
+  private shareSelectedLyricLines(): void {
+    const selectedLines = this.getSelectedLyricLines();
     if (ArrayUtil.isEmpty(selectedLines)) {
-      ToastUtil.showToast('暂无可复制歌词');
+      ToastUtil.showToast('暂无可分享歌词');
       return;
     }
-    Utility.copyText(selectedLines.join('\n'));
-    ToastUtil.showToast(`已复制${selectedLines.length}行歌词`);
+    const shareTitle = `${this.currentSong?.name ?? '歌词'}歌词`;
+    Utility.doShareText(selectedLines.join('\n'), this.context, shareTitle, '分享歌词');
   }
 
   /**
@@ -12084,6 +12098,9 @@ export struct LocalMusic {
       },
       onCopySelectedLines: () => {
         this.copySelectedLyricLines();
+      },
+      onShareSelectedLines: () => {
+        this.shareSelectedLyricLines();
       }
     })
   }
@@ -13670,6 +13687,7 @@ export struct LocalMusic {
 
       Row() {
         this.pushLyricButton($r('app.media.cut_current'), 0, '本地歌词')
+        this.pushLyricButton($r('app.media.share2'), 2, '分享歌词')
         this.pushLyricButton($r('app.media.white_search'), 1, '获取歌词')
         // this.pushLyricButton($r('app.media.white_search'), 1, '繁体转简体')
       }
@@ -13981,13 +13999,17 @@ export struct LocalMusic {
             this.initLyric(lyricPath, true);
           }
           break;
+        case 2:
+          this.isLyricSetting = false
+          this.openLyricCopySheet()
+          break;
       }
     })
     .padding(5)
-    .width(110)
+    .layoutWeight(1)
     .backgroundColor(Color.Transparent)
     .height(40)
-    .margin({ left: 6, right: 6 })
+    .margin({ left: 4, right: 4 })
     .border({
       color: '#FFFFFF',
       radius: 20,

+ 24 - 2
entry/src/main/ets/view/LyricCopySheet.ets

@@ -14,6 +14,8 @@ export struct LyricCopySheet {
   }
   onCopySelectedLines = () => {
   }
+  onShareSelectedLines = () => {
+  }
 
   private isLyricLineSelected(index: number): boolean {
     return this.lyricSelectedIndexes.indexOf(index) >= 0;
@@ -85,7 +87,7 @@ export struct LyricCopySheet {
             width: 1,
             color: this.themeColor
           })
-          .margin({left:50,right:25})
+          .margin({ left: 12, right: 6 })
           .onClick(() => {
             this.onToggleSelectAll();
           })
@@ -97,13 +99,33 @@ export struct LyricCopySheet {
           .layoutWeight(1)
           .height(42)
           .fontSize(14)
-          .margin({left:25,right:50})
+          .margin({ left: 6, right: 6 })
           .enabled(this.lyricSelectedIndexes.length > 0)
           .opacity(this.lyricSelectedIndexes.length > 0 ? 1 : 0.55)
           .backgroundColor(this.themeColor)
           .onClick(() => {
             this.onCopySelectedLines();
           })
+
+        Button(`分享(${this.lyricSelectedIndexes.length})`, {
+          type: ButtonType.Capsule,
+          stateEffect: true
+        })
+          .layoutWeight(1)
+          .height(42)
+          .fontSize(14)
+          .margin({ left: 6, right: 12 })
+          .enabled(this.lyricSelectedIndexes.length > 0)
+          .opacity(this.lyricSelectedIndexes.length > 0 ? 1 : 0.55)
+          .fontColor(this.themeColor)
+          .backgroundColor(Color.Transparent)
+          .border({
+            width: 1,
+            color: this.themeColor
+          })
+          .onClick(() => {
+            this.onShareSelectedLines();
+          })
       }
       .padding({ left: 16, right: 16, top: 8, bottom: this.bottomSafeHeight + 16 })
       .justifyContent(FlexAlign.SpaceBetween)